forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.lisp
57 lines (49 loc) · 1.66 KB
/
bits.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(coalton-library/utils::defstdlib-package #:coalton-library/bits
(:shadow
#:and
#:or
#:xor
#:not)
(:use
#:coalton)
(:import-from
#:coalton-library/classes
#:Num)
(:export
#:Bits
#:and
#:or
#:xor
#:not
#:shift
#:dpb
#:ldb))
(in-package #:coalton-library/bits)
(named-readtables:in-readtable coalton:coalton)
#+coalton-release
(cl:declaim #.coalton-impl/settings:*coalton-optimize-library*)
(coalton-toplevel
(define-class (Num :int => Bits :int)
"Operations on the bits of twos-complement integers"
(and "The bitwise logical `and` of two integers"
(:int -> :int -> :int))
(or "The bitwise logical `or` of two integers"
(:int -> :int -> :int))
(xor "The bitwise logical exclusive `or` of two integers"
(:int -> :int -> :int))
(not "The bitwise logical `not` of two integers"
(:int -> :int))
(shift "The arithmetic left-shift of an integer by an integer number of bits"
(Integer -> :int -> :int)))
(declare dpb (Bits :a => :a -> UFix -> UFix -> :a -> :a))
(define (dpb newbyte size position bitstring)
"Deposits a byte `newbyte` of size `size` into a bitstring `bitstring` at a position `position`."
(lisp :a (newbyte bitstring size position)
(cl:dpb newbyte (cl:byte size position) bitstring)))
(declare ldb (Bits :a => UFix -> UFix -> :a -> :a))
(define (ldb size position bitstring)
"Deposits a byte of size `size` into a bitstring at a position `position`."
(lisp :a (bitstring size position)
(cl:ldb (cl:byte size position) bitstring))))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/BITS")