~ubuntu-branches/ubuntu/wily/acl2/wily

« back to all changes in this revision

Viewing changes to books/workshops/2003/toma-borrione/support/bv-op-defuns.lisp

  • Committer: Package Import Robot
  • Author(s): Camm Maguire
  • Date: 2015-01-16 10:35:45 UTC
  • mfrom: (3.3.26 sid)
  • Revision ID: package-import@ubuntu.com-20150116103545-prehe9thgo79o8w8
Tags: 7.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;------------------------------------------
 
2
;
 
3
; Author:  Diana Toma
 
4
; TIMA-VDS, Grenoble, France 
 
5
; March 2003
 
6
; ACL2 formalization of bit-vectors as lists
 
7
; Definitions of bit-vectors operations
 
8
;------------------------------------------
 
9
 
 
10
 
 
11
(in-package "ACL2")
 
12
 
 
13
 
 
14
(include-book "misc")
 
15
 
 
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)
 
19
 
 
20
;----def bit
 
21
 
 
22
(defun bitp (b)
 
23
    (or (equal 0 b)
 
24
        (equal 1 b)))
 
25
 
 
26
            
 
27
;--- bit operations
 
28
 
 
29
; or 
 
30
(defun b-or (x y)
 
31
   (if (and (bitp x) (bitp y))
 
32
       (if (or (equal x 1) (equal y 1))
 
33
            1
 
34
            0)
 
35
       nil))
 
36
 
 
37
; and   
 
38
(defun b-and (x y)
 
39
   (if (and (bitp x) (bitp y))
 
40
       (if (and (equal x 1) (equal y 1))
 
41
            1
 
42
            0)
 
43
       nil))
 
44
 
 
45
;not
 
46
(defun b-not (x)
 
47
   (if (bitp x)
 
48
       (if (equal x 0) 
 
49
           1
 
50
           0)
 
51
       nil)) 
 
52
 
 
53
;xor
 
54
(defun  b-xor (x y)
 
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)))
 
58
            0
 
59
            1)
 
60
       nil))
 
61
 
 
62
;----- def of a bit-vector
 
63
 
 
64
(defun bvp (m)
 
65
  (if (true-listp m)
 
66
      (if (endp m)
 
67
          t
 
68
          (and (bitp (car m))
 
69
               (bvp (cdr m))))
 
70
       nil))
 
71
 
 
72
;------ word of len i
 
73
 
 
74
(defun wordp (w i)
 
75
  (and (bvp w) (integerp i) (<= 0 i)
 
76
       (equal (len w) i)))
 
77
 
 
78
 
 
79
;------ vector of words each one with len i
 
80
 
 
81
(defun wvp (m i)
 
82
   (if  (and (true-listp m) (integerp i) (<= 0 i))
 
83
       (if (endp m)
 
84
            t
 
85
           (and (wordp (car m) i) (wvp (cdr m) i)))
 
86
       nil))
 
87
 
 
88
 
 
89
 
 
90
 
 
91
 
 
92
; transforms a bit-vector into the positive integer corresponding at the little-endian interpretation 
 
93
; we treat only the unsigned case
 
94
 
 
95
(defun bv-int-little-endian (v)
 
96
   (if   (bvp v)
 
97
       (if (endp v)
 
98
           0
 
99
           (+  (car v) (* 2 ( bv-int-little-endian (cdr v)))))
 
100
      nil))
 
101
 
 
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
 
104
; below.
 
105
(set-verify-guards-eagerness 1)
 
106
 
 
107
; transforms v into the positive integer corresponding at the big-endian interpretation
 
108
 
 
109
(defun bv-int-big-endian (v)
 
110
   (bv-int-little-endian ( reverse v)))
 
111
 
 
112
; transforms a positive integer into the bit-vector corresponding to the little-endian interpretation
 
113
; we treat only the unsigned case
 
114
 
 
115
(defun int-bv-little-endian(i)
 
116
  (if (and (integerp i)
 
117
            (<= 0 i)) 
 
118
       (if  (equal (floor i 2) 0) 
 
119
            (list (mod i 2))
 
120
            (cons (mod i 2) (int-bv-little-endian (floor i 2)))) 
 
121
    nil))
 
122
 
 
123
                         
 
124
;  transforms i into the bit-vector corresponding at the big-endian interpretation of i
 
125
 
 
126
(defun int-bv-big-endian (i)
 
127
  (reverse (int-bv-little-endian i)))
 
128
 
 
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)
 
130
 
 
131
(defun bv-to-n (v n)
 
132
  (if (and (bvp v)
 
133
           (integerp n)
 
134
           (<= 0 n))
 
135
      (if  (>= n (len v))
 
136
           (append (make-list  (- n (len v)) :initial-element 0) v)
 
137
           (nthcdr (- (len v) n) v))
 
138
       nil))
 
139
 
 
140
 
 
141
;and between two bit-vectors with the same length
 
142
 
 
143
(defun bv-a (x y)
 
144
   (if (and (bvp x) (bvp y)
 
145
            (equal (len x) (len y)))
 
146
       (if (endp x) nil
 
147
           (cons (b-and (car x) (car y))
 
148
                 (bv-a (cdr x) (cdr y))))
 
149
       nil))
 
150
 
 
151
 
 
152
 
 
153
;and between two bit-vectors with arbitrary length
 
154
 
 
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))))
 
160
      nil))
 
161
 
 
162
 
 
163
(defun bv-and-macro (lst)
 
164
  (if (consp lst)
 
165
      (if (consp (cdr lst))
 
166
          (list 'binary-bv-and (car lst)
 
167
                (bv-and-macro (cdr lst)))
 
168
          (car lst))
 
169
      nil))
 
170
 
 
171
(defmacro bv-and (&rest args)
 
172
   (bv-and-macro args))      
 
173
 
 
174
 
 
175
 
 
176
;or between two bit-vectors with the same length 
 
177
 
 
178
(defun bv-o (x y)
 
179
   (if (and (bvp x) (bvp y)
 
180
            (equal (len x) (len y)))
 
181
       (if (endp x) nil
 
182
           (cons (b-or (car x) (car y))
 
183
                 (bv-o (cdr x) (cdr y))))
 
184
       nil))
 
185
 
 
186
;or between two bit-vectors with arbitrary length
 
187
 
 
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))))
 
193
      nil))
 
194
 
 
195
(defun bv-or-macro (lst)
 
196
  (if (consp lst)
 
197
      (if (consp (cdr lst))
 
198
          (list 'binary-bv-or (car lst)
 
199
                (bv-or-macro (cdr lst)))
 
200
          (car lst))
 
201
      nil))
 
202
 
 
203
(defmacro bv-or (&rest args)
 
204
   (bv-or-macro args)) 
 
205
 
 
206
 
 
207
 
 
208
;xor between two bit-vectors with the same length
 
209
 
 
210
(defun bv-xo (x y)
 
211
   (if (and (bvp x) (bvp y)
 
212
            (equal (len x) (len y)))
 
213
       (if (endp x) nil
 
214
           (cons (b-xor (car x) (car y))
 
215
                 (bv-xo (cdr x) (cdr y))))
 
216
       nil))
 
217
 
 
218
 
 
219
 
 
220
;xor between two bit-vectors with arbitrary length
 
221
 
 
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))))
 
227
      nil))
 
228
 
 
229
(defun bv-xor-macro (lst)
 
230
  (if (consp lst)
 
231
      (if (consp (cdr lst))
 
232
          (list 'binary-bv-xor (car lst)
 
233
                (bv-xor-macro (cdr lst)))
 
234
          (car lst))
 
235
      nil))
 
236
 
 
237
(defmacro bv-xor (&rest args)
 
238
   (bv-xor-macro args)) 
 
239
 
 
240
 
 
241
; not of a bit-vector x
 
242
 
 
243
(defun bv-not (x)   
 
244
  (if (bvp x)
 
245
      (if (endp x)
 
246
           nil
 
247
          (cons (b-not (car x)) (bv-not (cdr x))))
 
248
      nil)) 
 
249
 
 
250
 
 
251
 
 
252
; addition modulo  (2 pow i) of two bit-vectors x and y
 
253
 
 
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)
 
262
      nil))
 
263
 
 
264
 
 
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) ))
 
270
          (car lst))
 
271
      nil))
 
272
 
 
273
(defmacro plus (i &rest args )
 
274
   (plus-macro  i args )) 
 
275
 
 
276
;auxiliary shift operations
 
277
 
 
278
(defun << (x n w)
 
279
   (if (and (wordp x w )
 
280
           (integerp n)
 
281
           (<= 0 n) (integerp w)
 
282
           (<= 0 w)
 
283
           (<= n w))
 
284
       (cond ((zp n) x)
 
285
               ((endp x ) nil)
 
286
               (t  (append  (nthcdr n x) (make-list  n :initial-element 0) )))
 
287
       nil))
 
288
 
 
289
;ACL2 !>(<< '(1 1 1 1) 2 4)
 
290
;(1 1 0 0)
 
291
 
 
292
 
 
293
(defun >> (  x n w)
 
294
     (if (and (wordp x w)
 
295
           (integerp n)
 
296
           (<= 0 n)(integerp w)
 
297
           (<= 0 w)
 
298
           (<= n w))
 
299
         (cond ((zp n) x)
 
300
               ((endp x ) nil)
 
301
               (t  (append (make-list  n :initial-element 0) (firstn (- (len x) n)  x) ) ))
 
302
          nil))
 
303
 
 
304
;ACL2 !>(>> '(1 1 1 1) 2 4)
 
305
;(0 0 1 1)
 
306
 
 
307
;right shift of x with n elements on w bits
 
308
 
 
309
(defun shr (n x w)
 
310
     (if (and (wordp x w)
 
311
           (integerp n)
 
312
           (<= 0 n)(integerp w)
 
313
           (<= 0 w)
 
314
           (<= n w))
 
315
        (>>  x n w) nil))
 
316
 
 
317
 
 
318
;rotate right (circular right shift) of x with n elements on w bits
 
319
 
 
320
(defun rotr (n x w)
 
321
     (if (and (wordp x w)
 
322
           (integerp n)
 
323
           (<= 0 n)(integerp w)
 
324
           (<= 0 w)
 
325
           (<= n w))
 
326
        (bv-or (>>  x n w) (<< x (- w n) w)) nil))
 
327
 
 
328
;rotate left (circular left shift) of x with n elements on w bits
 
329
 
 
330
(defun rotl (n x w)
 
331
     (if (and (wordp x w)
 
332
           (integerp n)
 
333
           (<= 0 n)(integerp w)
 
334
           (<= 0 w)
 
335
           (<= n w))
 
336
        (bv-or (<<  x n w) (>> x (- w n) w)) nil))