2018-01-24 18:08:18 +03:00
|
|
|
(system-include "carp_int.h")
|
2018-01-24 17:53:18 +03:00
|
|
|
|
2021-07-05 15:48:35 +03:00
|
|
|
(doc Int "is the default integral data type.")
|
2017-06-26 12:15:03 +03:00
|
|
|
(defmodule Int
|
2017-10-17 10:42:57 +03:00
|
|
|
(register + (λ [Int Int] Int))
|
|
|
|
(register - (λ [Int Int] Int))
|
|
|
|
(register * (λ [Int Int] Int))
|
|
|
|
(register / (λ [Int Int] Int))
|
|
|
|
(register < (λ [Int Int] Bool))
|
|
|
|
(register > (λ [Int Int] Bool))
|
|
|
|
(register = (λ [Int Int] Bool))
|
2020-05-09 19:59:47 +03:00
|
|
|
(register copy (λ [&Int] Int))
|
|
|
|
(register inc (λ [Int] Int))
|
|
|
|
(register dec (λ [Int] Int))
|
2018-05-07 22:57:15 +03:00
|
|
|
(register neg (λ [Int] Int))
|
2017-10-17 10:42:57 +03:00
|
|
|
(register mod (λ [Int Int] Int))
|
2020-05-09 19:59:47 +03:00
|
|
|
|
|
|
|
(doc to-int "acts as the identity function to implement the interface.")
|
|
|
|
(sig to-int (Fn [Int] Int))
|
|
|
|
(defn to-int [a] a)
|
|
|
|
|
|
|
|
(doc from-int "acts as the identity function to implement the interface.")
|
|
|
|
(sig from-int (Fn [Int] Int))
|
|
|
|
(defn from-int [a] a)
|
|
|
|
|
|
|
|
(implements + Int.+)
|
|
|
|
(implements - Int.-)
|
|
|
|
(implements * Int.*)
|
|
|
|
(implements / Int./)
|
|
|
|
(implements < Int.<)
|
|
|
|
(implements > Int.>)
|
|
|
|
(implements = Int.=)
|
|
|
|
(implements copy Int.copy)
|
|
|
|
(implements inc Int.inc)
|
|
|
|
(implements dec Int.dec)
|
|
|
|
(implements neg Int.neg)
|
|
|
|
(implements mod Int.mod)
|
|
|
|
(implements to-int Int.to-int)
|
|
|
|
(implements from-int Int.from-int)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Int
|
|
|
|
(register MAX Int "CARP_INT_MAX")
|
|
|
|
(register MIN Int "CARP_INT_MIN")
|
2017-11-29 15:10:35 +03:00
|
|
|
(register bit-shift-left (λ [Int Int] Int))
|
|
|
|
(register bit-shift-right (λ [Int Int] Int))
|
|
|
|
(register bit-and (λ [Int Int] Int))
|
|
|
|
(register bit-or (λ [Int Int] Int))
|
|
|
|
(register bit-xor (λ [Int Int] Int))
|
|
|
|
(register bit-not (λ [Int] Int))
|
2020-07-08 22:11:13 +03:00
|
|
|
(implements bit-shift-left Int.bit-shift-left)
|
|
|
|
(implements bit-shift-right Int.bit-shift-right)
|
|
|
|
(implements bit-and Int.bit-and)
|
|
|
|
(implements bit-or Int.bit-or)
|
|
|
|
(implements bit-xor Int.bit-xor)
|
|
|
|
(implements bit-not Int.bit-not)
|
2017-11-17 14:26:01 +03:00
|
|
|
|
2018-03-27 10:43:39 +03:00
|
|
|
(doc abs "The absolute value (removes the negative sign) of an Int.")
|
2017-11-17 14:26:01 +03:00
|
|
|
(register abs (λ [Int] Int))
|
2020-05-09 19:59:47 +03:00
|
|
|
(implements abs Int.abs)
|
2017-11-17 14:26:01 +03:00
|
|
|
|
2017-11-29 17:28:21 +03:00
|
|
|
(defn even? [a] (= (mod a 2) 0))
|
|
|
|
(defn odd? [a] (not (even? a)))
|
2017-12-04 09:21:35 +03:00
|
|
|
|
|
|
|
(defn zero []
|
|
|
|
0)
|
2020-05-09 19:59:47 +03:00
|
|
|
(implements zero Int.zero)
|
2017-12-04 09:21:35 +03:00
|
|
|
|
|
|
|
(defn add-ref [x y]
|
2018-01-28 07:56:03 +03:00
|
|
|
(Int.+ @x @y))
|
2018-01-23 16:34:06 +03:00
|
|
|
|
2018-06-01 17:40:14 +03:00
|
|
|
;; Move to generic math module?
|
2018-01-23 16:34:06 +03:00
|
|
|
(defn clamp [min, max, val]
|
|
|
|
(if (> val max)
|
|
|
|
max
|
|
|
|
(if (< val min)
|
|
|
|
min
|
|
|
|
val)))
|
|
|
|
|
2018-05-12 16:58:49 +03:00
|
|
|
(doc pow "Raise x to the power of y.")
|
2018-01-23 16:34:06 +03:00
|
|
|
(defn pow [x y]
|
|
|
|
(let-do [r 1]
|
|
|
|
(while (/= y 0)
|
|
|
|
(do
|
|
|
|
(when (/= (bit-and y 1) 0)
|
2018-02-02 09:19:10 +03:00
|
|
|
(set! r (* r x)))
|
|
|
|
(set! y (/ y 2))
|
|
|
|
(set! x (* x x))))
|
2018-01-23 16:34:06 +03:00
|
|
|
r))
|
2018-12-01 12:05:11 +03:00
|
|
|
|
|
|
|
(doc positive-mod "Like mod but always returns a positive answer.")
|
|
|
|
(defn positive-mod [k n]
|
|
|
|
(let [r (Int.mod k n)]
|
|
|
|
(if (> 0 r)
|
|
|
|
(+ r n)
|
|
|
|
r)))
|
2019-03-26 20:08:17 +03:00
|
|
|
|
2020-05-09 19:59:47 +03:00
|
|
|
(implements add-ref Int.add-ref)
|
|
|
|
(implements pow Int.pow)
|
2018-01-15 18:27:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule IntRef
|
|
|
|
(defn = [a b]
|
|
|
|
(Int.= @a @b))
|
2020-05-10 20:32:22 +03:00
|
|
|
(implements = IntRef.=)
|
2018-01-15 18:27:05 +03:00
|
|
|
|
|
|
|
(defn < [a b]
|
|
|
|
(Int.< @a @b))
|
2020-05-10 20:32:22 +03:00
|
|
|
(implements < IntRef.<)
|
2017-12-04 09:21:35 +03:00
|
|
|
|
2018-01-15 18:27:05 +03:00
|
|
|
(defn > [a b]
|
|
|
|
(Int.> @a @b))
|
2020-05-10 20:32:22 +03:00
|
|
|
(implements > IntRef.>)
|
2017-11-29 17:28:21 +03:00
|
|
|
)
|