File: powOfTwo.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (344 lines) | stat: -rw-r--r-- 12,682 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
; X86ISA Library

; Note: The license below is based on the template at:
; http://opensource.org/licenses/BSD-3-Clause

; Copyright (C) 2015, Regents of the University of Texas
; All rights reserved.

; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are
; met:

; o Redistributions of source code must retain the above copyright
;   notice, this list of conditions and the following disclaimer.

; o Redistributions in binary form must reproduce the above copyright
;   notice, this list of conditions and the following disclaimer in the
;   documentation and/or other materials provided with the distribution.

; o Neither the name of the copyright holders nor the names of its
;   contributors may be used to endorse or promote products derived
;   from this software without specific prior written permission.

; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
; HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

; Original Author(s):
; Shilpi Goel         <shigoel@cs.utexas.edu>

(in-package "X86ISA")

(include-book "app-view/user-level-memory-utils" :dir :proof-utils :ttags :all)

(local (include-book "centaur/gl/gl" :dir :system))
(local (include-book "centaur/bitops/ihs-extensions" :dir :system))
(local (include-book "centaur/bitops/signed-byte-p" :dir :system))

;; ======================================================================

;; bool isPowerOfTwo? (long int v) {
;;   bool f;
;;   f = v && !(v & (v - 1));
;;   return f;
;; }


;; GCC O2 Optimization
;; 0000000100000ec0 <_powerOfTwo>:
;;    100000ec0:	55                      push   %rbp
;;    100000ec1:	48 89 e5                mov    %rsp,%rbp
;;    100000ec4:	48 85 ff                test   %rdi,%rdi
;;    100000ec7:	74 0c                   je     100000ed5 <_powerOfTwo+0x15>
;;    100000ec9:	48 8d 47 ff             lea    -0x1(%rdi),%rax
;;    100000ecd:	48 85 c7                test   %rax,%rdi
;;    100000ed0:	0f 94 c0                sete   %al
;;    100000ed3:	eb 02                   jmp    100000ed7 <_powerOfTwo+0x17>
;;    100000ed5:	31 c0                   xor    %eax,%eax
;;    100000ed7:	5d                      pop    %rbp
;;    100000ed8:	c3                      retq
;;    100000ed9:	0f 1f 80 00 00 00 00    nopl   0x0(%rax)

(defconst *program*
  '(
    #x55                               ;; push   %rbp
    #x48 #x89 #xe5                     ;; mov    %rsp,%rbp
    #x48 #x85 #xff                     ;; test   %rdi,%rdi
    #x74 #x0c                          ;; je     100000ed5 <_powerOfTwo+0x15>
    #x48 #x8d #x47 #xff                ;; lea    -0x1(%rdi),%rax
    #x48 #x85 #xc7                     ;; test   %rax,%rdi
    #x0f #x94 #xc0                     ;; sete   %al
    #xeb #x02                          ;; jmp    100000ed7 <_powerOfTwo+0x17>
    #x31 #xc0                          ;; xor    %eax,%eax
    #x5d                               ;; pop    %rbp
    #xc3                               ;; retq
    ))

(defconst *l* (len *program*))

(encapsulate
  ()
  (local (include-book "arithmetic-5/top" :dir :system))

  (defun power-of-2-p (x)
    (if (zp x)
        nil
      (if (equal x 1)
          t
        (if (zp (mod x 2))
            (power-of-2-p (floor x 2))
          nil))))

  (defun compute-log-2 (x count)
    (declare (xargs :measure (log-2-measure x)
                    :guard (natp count)))
    ;; Find the number n such that x approx.== 2^n.
    (if (natp x)
        (if (<= x 1)
            count
          (compute-log-2 (* 1/2 x) (1+ count)))
      count))

  (defthm integerp-compute-log-2
    (implies (integerp count)
             (integerp (compute-log-2 x count)))
    :rule-classes :type-prescription)

  (defthm natp-compute-log-2
    (implies (natp count)
             (natp (compute-log-2 x count)))
    :rule-classes :type-prescription)

  (local
   (defthm compute-log-2-and-expt
     (implies
      ;; natp would lead to a stronger rule, but I want a weak one here.
      (posp count)
      (equal (expt 2 (compute-log-2 x count))
             (expt 2 (+ 1 (compute-log-2 x (1- count))))))))

  (defthm if-power-of-2-p-returns-t-then-x-is-indeed-a-power-of-2
    ;; If (power-of-2-p x) is t, then there exists a natural number n
    ;; such that x is equal to (expt 2 n).
    (implies (and (power-of-2-p x)
                  (natp x))
             (equal (expt 2 (compute-log-2 x 0)) x)))

  (defthm power-of-2-p-returns-t-for-x=2^n
    ;; Every number x == 2^n, where n is a natural number,
    ;; satisfies power-of-2-p.
    (implies (and (natp n)
                  (equal x (expt 2 n)))
             (power-of-2-p x))
    :hints (("Goal" :in-theory (e/d* (expt) ())))))

(local
 (def-gl-thm program-effects-helper-1
   :hyp (and (unsigned-byte-p 64 x)
             (power-of-2-p x))
   :concl (equal (logand x (+ -1 x)) 0)
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

(local
 (def-gl-thm program-effects-helper-2
   :hyp (and (unsigned-byte-p 64 x)
             (not (equal x 0))
             (not (power-of-2-p x)))
   :concl (equal (equal (logand x (+ -1 x)) 0) nil)
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

(local
 (def-gl-thm program-effects-helper-3
   :hyp (and (signed-byte-p 64 x)
             (power-of-2-p (loghead 64 x)))
   :concl (equal (logand (loghead 64 x)
                         (loghead 64 (+ -1 x)))
                 0)
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

(local
 (def-gl-thm program-effects-helper-4
   :hyp (and (signed-byte-p 64 x)
             (< 0 (loghead 64 x))
             (not (power-of-2-p (loghead 64 x))))
   :concl (not (equal (logand (loghead 64 x)
                              (loghead 64 (+ -1 x)))
                      0))
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

(local
 (def-gl-thm power-of-2-p-result-helper-1
   :hyp (unsigned-byte-p 64 x)
   :concl (equal (loghead 8 (logior 1 (logext 64 (bitops::logsquash 8 (+ -1 x)))))
                 1)
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

(local
 (def-gl-thm power-of-2-p-result-helper-2
   :hyp (signed-byte-p 64 x)
   :concl (signed-byte-p 64 (logior 1 (bitops::logsquash 8 (logext 64 (+ -1 x)))))
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

(local
 (def-gl-thm power-of-2-p-result-helper-3
   :hyp (signed-byte-p 64 x)
   :concl (signed-byte-p 64 (bitops::logsquash 8 (logext 64 (+ -1 x))))
   :g-bindings
   `((x    (:g-number ,(increasing-list 0 1 65))))))

;; ======================================================================

(defun-nx preconditions (x86)
  (and
   ;; The x86 state is well-formed.
   (x86p x86)
   ;; Alignment checking is turned off.
   (not (alignment-checking-enabled-p x86))
   ;; The model is operating in 64-bit mode.
   (64-bit-modep x86)
   ;; The model is operating in the application-level view.
   (app-view x86)
   ;; The program is located at linear addresses ranging from (rip
   ;; x86) to (+ -1 (len *program*) (rip x86)).
   (program-at (rip x86) *program* x86)
   ;; The addresses where the program is located are canonical.
   (canonical-address-p (rip x86))
   (canonical-address-p (+ (len *program*) (rip x86)))
   ;; The initial state is error-free.
   (equal (ms x86) nil)
   (equal (fault x86) nil)
   ;; Stack addresses are canonical.
   (canonical-address-p (+ -8 (xr :rgf *rsp* x86)))
   (canonical-address-p (xr :rgf *rsp* x86))
   (canonical-address-p (+ 8 (xr :rgf *rsp* x86)))
   ;; Return address is canonical.
   (canonical-address-p
    (logext 64 (mv-nth 1 (rb 8 (xr :rgf *rsp* x86) :r x86))))
   ;; Program and stack are disjoint.
   (separate
    :x *l* (xr :rip nil x86)
    :w  8  (+ -8 (xr :rgf *rsp* x86)))
   (unsigned-byte-p 64 (rr64 *rdi* x86))))

(defun is-power-of-2-clock () 10)

(defun is-not-power-of-2-clock (x)
  (if (zp x) 7 10))

(defthm power-of-2-p-result
  (implies (and (preconditions x86)
                (power-of-2-p (rr64 *rdi* x86)))
           (equal (loghead 8 (xr :rgf *rax* (x86-run (is-power-of-2-clock) x86)))
                  1))
  :hints (("Goal"
           :do-not-induct t
           :in-theory (e/d* (instruction-decoding-and-spec-rules
                             rflag-RoWs-enables
                             x86-operation-mode

                             jcc/cmovcc/setcc-spec
                             gpr-and-spec-8
                             gpr-xor-spec-4

                             one-byte-opcode-execute
                             !rgfi-size
                             x86-operand-to-reg/mem
                             wr08
                             wr32
                             wr64
                             rr32
                             rr64
                             rime-size
                             rme-size
                             wime-size
                             wme-size
                             rml32
                             rml64
                             wml32
                             wml64
                             rr32
                             x86-operand-from-modr/m-and-sib-bytes
                             check-instruction-length
                             riml-size
                             riml32
                             n32-to-i32
                             n64-to-i64
                             riml08
                             two-byte-opcode-decode-and-execute
                             x86-effective-addr-when-64-bit-modep
                             x86-effective-addr-32/64
                             ;; Flags
                             write-user-rflags
                             zf-spec)
                            (canonical-address-p
                             signed-byte-p)))))

(defthm not-power-of-2-p-result
  (implies (and (preconditions x86)
                (not (power-of-2-p (rr64 *rdi* x86))))
           (equal (loghead
                   8
                   (xr :rgf *rax*
                       (x86-run (is-not-power-of-2-clock (rr64 *rdi* x86)) x86)))
                  0))
  :hints (("Goal"
           :do-not-induct t
           :cases (equal (rgfi *rdi* x86) 0)
           :in-theory (e/d* (instruction-decoding-and-spec-rules
                             rflag-RoWs-enables
                             x86-operation-mode

                             jcc/cmovcc/setcc-spec
                             gpr-and-spec-8
                             gpr-xor-spec-4

                             one-byte-opcode-execute
                             !rgfi-size
                             x86-operand-to-reg/mem
                             wr08
                             wr32
                             wr64
                             rr32
                             rr64
                             rime-size
                             rme-size
                             wime-size
                             wme-size
                             rml32
                             rml64
                             wml32
                             wml64
                             rr32
                             x86-operand-from-modr/m-and-sib-bytes
                             check-instruction-length
                             riml-size
                             riml32
                             n32-to-i32
                             n64-to-i64
                             riml08
                             two-byte-opcode-decode-and-execute
                             x86-effective-addr-when-64-bit-modep
                             x86-effective-addr-32/64
                             ;; Flags
                             write-user-rflags
                             zf-spec)
                            (canonical-address-p
                             not
                             signed-byte-p)))))

;; ======================================================================