1
;------------------------------------------
4
; TIMA-VDS, Grenoble, France
6
; ACL2 formalization of bit-vectors as lists
7
; Definitions of bit-vectors operations
8
;------------------------------------------
16
; Added by Matt K. in v2-9 to eliminate stack overflows in GCL in, at least,
17
; the proofs of last64-padding-1-256=length and last128-padding-512=length.
18
(set-verify-guards-eagerness 2)
31
(if (and (bitp x) (bitp y))
32
(if (or (equal x 1) (equal y 1))
39
(if (and (bitp x) (bitp y))
40
(if (and (equal x 1) (equal y 1))
55
(if (and (bitp x) (bitp y))
56
(if (or (and (equal x 1) (equal y 1))
57
(and (equal x 0) (equal y 0)))
62
;----- def of a bit-vector
75
(and (bvp w) (integerp i) (<= 0 i)
79
;------ vector of words each one with len i
82
(if (and (true-listp m) (integerp i) (<= 0 i))
85
(and (wordp (car m) i) (wvp (cdr m) i)))
92
; transforms a bit-vector into the positive integer corresponding at the little-endian interpretation
93
; we treat only the unsigned case
95
(defun bv-int-little-endian (v)
99
(+ (car v) (* 2 ( bv-int-little-endian (cdr v)))))
102
; Added by Matt K. to balance the earlier call of set-verify-guards-eagerness,
103
; since guard verification fails for the function bv-int-big-endian just
105
(set-verify-guards-eagerness 1)
107
; transforms v into the positive integer corresponding at the big-endian interpretation
109
(defun bv-int-big-endian (v)
110
(bv-int-little-endian ( reverse v)))
112
; transforms a positive integer into the bit-vector corresponding to the little-endian interpretation
113
; we treat only the unsigned case
115
(defun int-bv-little-endian(i)
116
(if (and (integerp i)
118
(if (equal (floor i 2) 0)
120
(cons (mod i 2) (int-bv-little-endian (floor i 2))))
124
; transforms i into the bit-vector corresponding at the big-endian interpretation of i
126
(defun int-bv-big-endian (i)
127
(reverse (int-bv-little-endian i)))
129
; transforms a bit-vector v into a bit-vector of len n, if n is bigger then v's length. if not, returns the last n bits of v (v is considered in big-endian representation)
136
(append (make-list (- n (len v)) :initial-element 0) v)
137
(nthcdr (- (len v) n) v))
141
;and between two bit-vectors with the same length
144
(if (and (bvp x) (bvp y)
145
(equal (len x) (len y)))
147
(cons (b-and (car x) (car y))
148
(bv-a (cdr x) (cdr y))))
153
;and between two bit-vectors with arbitrary length
155
(defun binary-bv-and (x y)
156
(if (and (bvp x) (bvp y))
157
(if (<= (len x) (len y))
158
(bv-a (bv-to-n x (len y)) y)
159
(bv-a x (bv-to-n y (len x))))
163
(defun bv-and-macro (lst)
165
(if (consp (cdr lst))
166
(list 'binary-bv-and (car lst)
167
(bv-and-macro (cdr lst)))
171
(defmacro bv-and (&rest args)
176
;or between two bit-vectors with the same length
179
(if (and (bvp x) (bvp y)
180
(equal (len x) (len y)))
182
(cons (b-or (car x) (car y))
183
(bv-o (cdr x) (cdr y))))
186
;or between two bit-vectors with arbitrary length
188
(defun binary-bv-or (x y)
189
(if (and (bvp x) (bvp y))
190
(if (<= (len x) (len y))
191
(bv-o (bv-to-n x (len y)) y)
192
(bv-o x (bv-to-n y (len x))))
195
(defun bv-or-macro (lst)
197
(if (consp (cdr lst))
198
(list 'binary-bv-or (car lst)
199
(bv-or-macro (cdr lst)))
203
(defmacro bv-or (&rest args)
208
;xor between two bit-vectors with the same length
211
(if (and (bvp x) (bvp y)
212
(equal (len x) (len y)))
214
(cons (b-xor (car x) (car y))
215
(bv-xo (cdr x) (cdr y))))
220
;xor between two bit-vectors with arbitrary length
222
(defun binary-bv-xor (x y)
223
(if (and (bvp x) (bvp y))
224
(if (<= (len x) (len y))
225
(bv-xo (bv-to-n x (len y)) y)
226
(bv-xo x (bv-to-n y (len x))))
229
(defun bv-xor-macro (lst)
231
(if (consp (cdr lst))
232
(list 'binary-bv-xor (car lst)
233
(bv-xor-macro (cdr lst)))
237
(defmacro bv-xor (&rest args)
241
; not of a bit-vector x
247
(cons (b-not (car x)) (bv-not (cdr x))))
252
; addition modulo (2 pow i) of two bit-vectors x and y
254
(defun binary-plus (i x y )
255
(if (and (bvp x) (<= 0 (bv-int-big-endian x))
256
(<= (bv-int-big-endian x) (expt 2 i))
257
(bvp y) (<= 0 (bv-int-big-endian y))
258
(<= (bv-int-big-endian y) (expt 2 i))
259
(integerp i) (<= 0 i))
260
(bv-to-n (int-bv-big-endian (mod (+ (bv-int-big-endian x)
261
(bv-int-big-endian y)) (expt 2 i))) i)
265
(defun plus-macro (i lst )
266
(if (and (consp lst) (integerp i) (<= 0 i))
267
(if (consp (cdr lst))
268
(list 'binary-plus i (car lst)
269
(plus-macro i (cdr lst) ))
273
(defmacro plus (i &rest args )
274
(plus-macro i args ))
276
;auxiliary shift operations
279
(if (and (wordp x w )
281
(<= 0 n) (integerp w)
286
(t (append (nthcdr n x) (make-list n :initial-element 0) )))
289
;ACL2 !>(<< '(1 1 1 1) 2 4)
301
(t (append (make-list n :initial-element 0) (firstn (- (len x) n) x) ) ))
304
;ACL2 !>(>> '(1 1 1 1) 2 4)
307
;right shift of x with n elements on w bits
318
;rotate right (circular right shift) of x with n elements on w bits
326
(bv-or (>> x n w) (<< x (- w n) w)) nil))
328
;rotate left (circular left shift) of x with n elements on w bits
336
(bv-or (<< x n w) (>> x (- w n) w)) nil))