3
(local (include-book "../support/support/setbitn"))
4
(local (include-book "../support/support/guards"))
9
(declare (xargs :guard t))
14
(declare (xargs :guard (integerp k)))
20
(declare (xargs :guard (real/rationalp x)))
24
(declare (xargs :guard (and (natp x)
27
(mbe :logic (if (or (not (integerp i))
30
(fl (/ (mod x (expt 2 (1+ i))) (expt 2 j))))
33
(logand (ash x (- j)) (1- (ash 1 (1+ (- i j))))))))
36
(declare (xargs :guard (and (natp x)
38
(mbe :logic (bits x n n)
39
:exec (if (evenp (ash x (- n))) 0 1)))
41
(defund binary-cat (x m y n)
42
(declare (xargs :guard (and (natp x)
45
(integerp n) (< 0 n))))
46
(if (and (natp m) (natp n))
47
(+ (* (expt 2 n) (bits x (1- m) 0))
52
;;an auxiliary function that does not appear in translate-rtl output.
53
(declare (xargs :guard t))
54
(if (and (acl2-numberp x) (acl2-numberp y))
58
;;X is a list of alternating data values and sizes. CAT-SIZE returns the
59
;;formal sum of the sizes. X must contain at least 1 data/size pair, but we do
60
;;not need to specify this in the guard, and leaving it out of that guard
61
;;simplifies the guard proof.
64
;;an auxiliary function that does not appear in translate-rtl output.
65
(declare (xargs :guard (and (true-listp x) (evenp (length x)))))
69
(cat-size (cddr x)))))
71
(defmacro cat (&rest x)
72
(declare (xargs :guard (and x (true-listp x) (evenp (length x)))))
73
(cond ((endp (cddr x))
74
`(bits ,(car x) ,(formal-+ -1 (cadr x)) 0))
81
,(cat-size (cddr x))))))
83
;Allows things like (in-theory (disable cat)) to refer to binary-cat.
84
(add-macro-alias cat binary-cat)
86
(defund setbits (x w i j y)
87
(declare (xargs :guard (and (natp x)
95
(mbe :logic (cat (bits x (1- w) (1+ i))
97
(cat (bits y (+ i (- j)) 0)
102
:exec (cond ((int= j 0)
103
(cond ((int= (1+ i) w)
104
(bits y (+ i (- j)) 0))
106
(cat (bits x (1- w) (1+ i))
108
(bits y (+ i (- j)) 0)
111
(cat (bits y (+ i (- j)) 0)
116
(cat (bits x (1- w) (1+ i))
118
(cat (bits y (+ i (- j)) 0)
128
(defund setbitn (x w n y)
129
(declare (xargs :guard (and (natp x)
137
(defthm setbitn-nonnegative-integer-type
138
(and (integerp (setbitn x w n y))
139
(<= 0 (setbitn x w n y)))
140
:rule-classes (:type-prescription))
142
;this rule is no better than setbits-nonnegative-integer-type and might be worse:
143
(in-theory (disable (:type-prescription setbitn)))
146
(natp (setbitn x w n y)))
148
;add setbitn-bvecp-simple?
150
(defthm setbitn-bvecp
151
(implies (and (<= w k)
152
(case-split (integerp k)))
153
(bvecp (setbitn x w n y) k)))
155
(defthm setbitn-rewrite
156
(implies (syntaxp (quotep n))
157
(equal (setbitn x w n y)
158
(setbits x w n n y))))
162
(implies (and (case-split (bvecp y 1))
166
(case-split (<= 0 k))
167
(case-split (integerp w))
168
(case-split (integerp n))
170
(case-split (integerp k))
172
(equal (bitn (setbitn x w n y) k)
177
(defthm setbitn-setbitn
178
(implies (and (case-split (<= 0 n))
180
(case-split (integerp w))
181
(case-split (integerp n))
183
(equal (setbitn (setbitn x w n y) w n y2)
184
(setbitn x w n y2))))
186
(defthm setbitn-does-nothing
187
(implies (and (case-split (<= 0 n))
189
(case-split (integerp w))
190
(case-split (integerp n))
192
(equal (setbitn x w n (bitn x n))
193
(bits x (+ -1 w) 0))))