2019-10-24 12:23:38 +03:00
|
|
|
(system-include "carp_byte.h")
|
|
|
|
|
|
|
|
(defmodule Byte
|
2020-05-09 19:59:47 +03:00
|
|
|
(register = (λ [Byte Byte] Bool))
|
|
|
|
(register copy (λ [&Byte] Byte))
|
2019-10-24 12:23:38 +03:00
|
|
|
(register + (λ [Byte Byte] Byte))
|
|
|
|
(register - (λ [Byte Byte] Byte))
|
|
|
|
(register * (λ [Byte Byte] Byte))
|
|
|
|
(register / (λ [Byte Byte] Byte))
|
|
|
|
(register < (λ [Byte Byte] Bool))
|
|
|
|
(register > (λ [Byte Byte] Bool))
|
|
|
|
(register mod (λ [Byte Byte] Byte))
|
2020-05-09 19:59:47 +03:00
|
|
|
(register inc (λ [Byte] Byte))
|
|
|
|
(register dec (λ [Byte] Byte))
|
|
|
|
(register to-int (λ [Byte] Int))
|
|
|
|
(register from-int (λ [Int] Byte))
|
|
|
|
(defn zero [] 0b)
|
|
|
|
|
|
|
|
(implements = Byte.=)
|
|
|
|
(implements copy Byte.copy)
|
|
|
|
(implements + Byte.+)
|
|
|
|
(implements - Byte.-)
|
|
|
|
(implements * Byte.*)
|
|
|
|
(implements / Byte./)
|
|
|
|
(implements < Byte.<)
|
|
|
|
(implements > Byte.>)
|
|
|
|
(implements mod Byte.mod)
|
|
|
|
(implements inc Byte.inc)
|
|
|
|
(implements dec Byte.dec)
|
|
|
|
(implements zero Byte.zero)
|
|
|
|
(implements to-int Byte.to-int)
|
|
|
|
(implements from-int Byte.from-int)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Byte
|
2019-10-24 12:23:38 +03:00
|
|
|
(register bit-shift-left (λ [Byte Byte] Byte))
|
|
|
|
(register bit-shift-right (λ [Byte Byte] Byte))
|
|
|
|
(register bit-and (λ [Byte Byte] Byte))
|
|
|
|
(register bit-or (λ [Byte Byte] Byte))
|
|
|
|
(register bit-xor (λ [Byte Byte] Byte))
|
|
|
|
(register bit-not (λ [Byte] Byte))
|
2020-07-08 22:11:13 +03:00
|
|
|
(implements bit-shift-left Byte.bit-shift-left)
|
|
|
|
(implements bit-shift-right Byte.bit-shift-right)
|
|
|
|
(implements bit-and Byte.bit-and)
|
|
|
|
(implements bit-or Byte.bit-or)
|
|
|
|
(implements bit-xor Byte.bit-xor)
|
|
|
|
(implements bit-not Byte.bit-not)
|
2019-10-24 12:23:38 +03:00
|
|
|
|
|
|
|
(defn even? [a] (= (mod a 2b) 0b))
|
|
|
|
(defn odd? [a] (not (even? a)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn add-ref [x y]
|
|
|
|
(Byte.+ @x @y))
|
|
|
|
|
|
|
|
;; Move to generic math module?
|
|
|
|
(defn clamp [min, max, val]
|
|
|
|
(if (> val max)
|
|
|
|
max
|
|
|
|
(if (< val min)
|
|
|
|
min
|
|
|
|
val)))
|
|
|
|
|
|
|
|
(doc pow "Raise x to the power of y.")
|
|
|
|
(defn pow [x y]
|
|
|
|
(let-do [r 1b]
|
|
|
|
(while (/= y 0b)
|
|
|
|
(do
|
|
|
|
(when (/= (bit-and y 1b) 0b)
|
|
|
|
(set! r (* r x)))
|
|
|
|
(set! y (/ y 2b))
|
|
|
|
(set! x (* x x))))
|
|
|
|
r))
|
2020-05-09 19:59:47 +03:00
|
|
|
|
|
|
|
(implements pow Byte.pow)
|
|
|
|
(implements add-ref Byte.add-ref)
|
2019-10-24 12:23:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule ByteRef
|
|
|
|
(defn = [a b]
|
|
|
|
(Byte.= @a @b))
|
|
|
|
|
|
|
|
(defn < [a b]
|
|
|
|
(Byte.< @a @b))
|
|
|
|
|
|
|
|
(defn > [a b]
|
|
|
|
(Byte.> @a @b))
|
2020-05-09 19:59:47 +03:00
|
|
|
|
|
|
|
(implements = ByteRef.=)
|
|
|
|
(implements < ByteRef.<)
|
|
|
|
(implements > ByteRef.>)
|
2019-10-24 12:23:38 +03:00
|
|
|
)
|