File: saturate.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 (300 lines) | stat: -rw-r--r-- 11,161 bytes parent folder | download | duplicates (5)
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
; Centaur Bitops Library
; Copyright (C) 2010-2015 Centaur Technology
;
; Contact:
;   Centaur Technology Formal Verification Group
;   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
;   http://www.centtech.com/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original authors: Jared Davis <jared@centtech.com>
;                   Sol Swords <sswords@centtech.com>

(in-package "BITOPS")
(include-book "std/util/define" :dir :system)
(include-book "std/basic/defs" :dir :system)
(include-book "ihs/basic-definitions" :dir :system)
(include-book "std/basic/arith-equivs" :dir :system)
(local (include-book "ihsext-basics"))
(local (include-book "ash-bounds"))
(local (include-book "arithmetic/top-with-meta" :dir :system))

(defsection bitops/saturate
  :parents (bitops)
  :short "Definitions of signed and unsigned saturation operations.")

(local (xdoc::set-default-parents bitops/saturate))

(defsection unsigned-saturate
  :short "@(call unsigned-saturate) coerces the integer @('x') into an
@('n')-bit unsigned integer by unsigned saturation."

  :long "<p>By unsigned saturation, we mean:</p>
<ul>
<li>If @('x') is too small (less than 0) it becomes 0.</li>
<li>If @('x') is too big (@('2^n') or more) it becomes @('2^n - 1').</li>
<li>Otherwise @('x') is unchanged.</li>
</ul>

<p>@('unsigned-saturate') is actually a macro.  Generally it expands into a
call of @('unsigned-saturate-fn').  But, in the common cases where @('n') is
explicitly 8, 16, 32, or 64, it instead expands into a call of an optimized,
inlined function.</p>

@(def unsigned-saturate)"

  (defmacro unsigned-saturate (n x)
    (cond ((eql n 8)   `(unsigned-saturate8 ,x))
          ((eql n 16)  `(unsigned-saturate16 ,x))
          ((eql n 32)  `(unsigned-saturate32 ,x))
          ((eql n 64)  `(unsigned-saturate64 ,x))
          (t           `(unsigned-saturate-fn ,n ,x)))))

(define unsigned-saturate-fn ((n posp)
                              (x integerp))
  :parents (unsigned-saturate)
  :returns (saturated natp :rule-classes :type-prescription)
  :short "Logical definition of @(see unsigned-saturate), and also its
executable implementation in the general case."
  (b* ((n   (lnfix n))
       (x   (lifix x))
       (2^n (ash 1 n))
       (max (+ -1 2^n))
       ((when (>= x max)) max)
       ((when (<= x 0)) 0))
    x)
  ///
  (defthm unsigned-byte-p-of-unsigned-saturate-fn
    (implies (natp n)
             (unsigned-byte-p n (unsigned-saturate-fn n x)))
    :hints(("Goal" :in-theory (enable expt-2-is-ash))))

  (defcong nat-equiv equal (unsigned-saturate-fn n x) 1)
  (defcong int-equiv equal (unsigned-saturate-fn n x) 2)

  (add-macro-alias unsigned-saturate unsigned-saturate-fn))

(local (in-theory (enable unsigned-saturate-fn)))

(define unsigned-saturate8 ((x integerp))
  :parents (unsigned-saturate)
  :short "Optimized implementation of 8-bit unsigned saturation."
  :inline t
  :enabled t
  (mbe :logic (unsigned-saturate-fn 8 x)
       :exec
       (cond ((>= x #ux_FF) #ux_FF)
             ((<= x 0)      0)
             (t             x))))

(define unsigned-saturate16 ((x integerp))
  :parents (unsigned-saturate)
  :short "Optimized implementation of 16-bit unsigned saturation."
  :inline t
  :enabled t
  (mbe :logic (unsigned-saturate-fn 16 x)
       :exec
       (cond ((>= x #ux_FFFF) #ux_FFFF)
             ((<= x 0)        0)
             (t               x))))

(define unsigned-saturate32 ((x integerp))
  :parents (unsigned-saturate)
  :short "Optimized implementation of 32-bit unsigned saturation."
  :inline t
  :enabled t
  (mbe :logic (unsigned-saturate-fn 32 x)
       :exec
       (cond ((>= x #ux_FFFF_FFFF) #ux_FFFF_FFFF)
             ((<= x 0)             0)
             (t                    x))))

(define unsigned-saturate64 ((x integerp))
  :parents (unsigned-saturate)
  :short "Optimized implementation of 64-bit unsigned saturation."
  :inline t
  :enabled t
  (mbe :logic (unsigned-saturate-fn 64 x)
       :exec
       (cond ((>= x #ux_FFFF_FFFF_FFFF_FFFF) #ux_FFFF_FFFF_FFFF_FFFF)
             ((<= x 0)                       0)
             (t                              x))))



(defsection signed-saturate
  :parents (bitops/saturate)
  :short "@(call signed-saturate) coerces the integer @('x') into an @('n')-bit
signed integer by signed saturation, then returns the result as an @('n')-bit
<b>unsigned</b> number."

  :long "<p>Normally signed saturation to @('n') bits is understood as:</p>

<ul>
<li>If @('x') is too small (less than @('-2^{n-1}')) it becomes @('-2^{n-1}').</li>
<li>If @('x') is too big (@('2^{n-1}') or more) it becomes @('2^{n-1} - 1').</li>
<li>Otherwise @('x') is unchanged.</li>
</ul>

<p>This is <b>almost</b> what we compute.  The twist is: after saturating as
above, we mask the above with @('2^{n-1}') to obtain an unsigned, @('n')-bit
result.</p>

<p>@('signed-saturate') is actually a macro.  Generally it expands into a call
of @('signed-saturate-fn').  But, in the common cases where @('n') is
explicitly 8, 16, 32, or 64, it instead expands into a call of an optimized,
inlined function.</p>

@(def signed-saturate)"

  (defmacro signed-saturate (n x)
    (cond ((eql n 8)   `(signed-saturate8 ,x))
          ((eql n 16)  `(signed-saturate16 ,x))
          ((eql n 32)  `(signed-saturate32 ,x))
          ((eql n 64)  `(signed-saturate64 ,x))
          (t           `(signed-saturate-fn ,n ,x)))))

(define signed-saturate-fn ((n posp)
                            (x integerp))
  :parents (signed-saturate)
  :returns (saturated natp :rule-classes :type-prescription)
  :short "Logical definition of @(see signed-saturate), and also its executable
implementation in the general case."
  (b* ((n       (lnfix n))
       (x       (lifix x))
       ((when (mbe :logic (zp n)
                   :exec nil))
        0)
       (2^{n-1} (ash 1 (1- n)))
       (max     (+ -1 2^{n-1}))
       ((when (>= x max))
        max)
       (min     (- 2^{n-1}))
       ((when (<= x min))
        2^{n-1})
       (mask    (+ -1 (ash 1 n))))
    (logand mask x))
  ///
  (local (defthm crock
           (implies (and (integerp n)
                         (< 0 n))
                    (< (ash 1 (+ -1 n)) (ash 1 n)))
           :rule-classes :linear))

  (local (include-book "ihs-extensions")) ;; bozo
  (defthm unsigned-byte-p-of-signed-saturate-fn
    (implies (natp n)
             (unsigned-byte-p n (signed-saturate-fn n x)))
    :hints(("Goal" :in-theory (e/d (expt-2-is-ash)
                                   (ash-1-removal)))))

  (defcong nat-equiv equal (signed-saturate-fn n x) 1)
  (defcong int-equiv equal (signed-saturate-fn n x) 2)

  (add-macro-alias signed-saturate signed-saturate-fn))

(local (in-theory (enable signed-saturate-fn)))

(define signed-saturate8 ((x integerp))
  :parents (signed-saturate)
  :short "Optimized implementation of 8-bit signed saturation."
  :inline t
  :enabled t
  (mbe :logic (signed-saturate-fn 8 x)
       :exec
       (cond ((>= x #ux_+7F) #ux_7F)
             ((<= x #ux_-80) #ux_80)
             (t
              (the (unsigned-byte 8)
                (logand (the (signed-byte 8) x) #ux_FF))))))

(define signed-saturate16 ((x integerp))
  :parents (signed-saturate)
  :short "Optimized implementation of 16-bit signed saturation."
  :inline t
  :enabled t
  (mbe :logic (signed-saturate-fn 16 x)
       :exec
       (cond ((>= x #ux_+7FFF) #ux_7FFF)
             ((<= x #ux_-8000) #ux_8000)
             (t (the (unsigned-byte 16)
                  (logand (the (signed-byte 16) x) #ux_FFFF))))))

(define signed-saturate32 ((x integerp))
  :parents (signed-saturate)
  :short "Optimized implementation of 32-bit signed saturation."
  :inline t
  :enabled t
  (mbe :logic (signed-saturate-fn 32 x)
       :exec
       (cond ((>= x #ux_+7FFF_FFFF) #ux_7FFF_FFFF)
             ((<= x #ux_-8000_0000) #ux_8000_0000)
             (t (the (unsigned-byte 32)
                  (logand (the (signed-byte 32) x) #ux_FFFF_FFFF))))))

(define signed-saturate64 ((x integerp))
  :parents (signed-saturate)
  :short "Optimized implementation of 64-bit signed saturation."
  :inline t
  :enabled t
  (mbe :logic (signed-saturate-fn 64 x)
       :exec
       (cond ((>= x #ux_+7FFF_FFFF_FFFF_FFFF) #ux_7FFF_FFFF_FFFF_FFFF)
             ((<= x #ux_-8000_0000_0000_0000) #ux_8000_0000_0000_0000)
             (t (the (unsigned-byte 64)
                  (logand (the (signed-byte 64) x) #ux_FFFF_FFFF_FFFF_FFFF))))))




#||

Basic timing tests...

;; UNOPTIMIZED: this is somewhat unfair because these don't get inlined...

(time (loop for i fixnum from 1 to 1000000000 do (signed-saturate 4 i)))    ;; 13.052 sec
(time (loop for i fixnum from 1 to 1000000000 do (signed-saturate 15 i)))   ;; 12.391 sec
(time (loop for i fixnum from 1 to 1000000000 do (signed-saturate 31 i)))   ;; 23.527 sec
(time (loop for i fixnum from 1 to 1000000000 do (unsigned-saturate 4 i)))  ;; 11.540 sec
(time (loop for i fixnum from 1 to 1000000000 do (unsigned-saturate 15 i))) ;; 11.542 sec
(time (loop for i fixnum from 1 to 1000000000 do (unsigned-saturate 31 i))) ;; 11.807 sec

;; OPTIMIZED:

(time (loop for i fixnum from 1 to 1000000000 do (signed-saturate 8 i)))    ;; 1.338 sec
(time (loop for i fixnum from 1 to 1000000000 do (signed-saturate 16 i)))   ;; 1.338 sec
(time (loop for i fixnum from 1 to 1000000000 do (signed-saturate 32 i)))   ;; 2.006 sec
(time (loop for i fixnum from 1 to 1000000000 do (unsigned-saturate 8 i)))  ;; 1.338 sec
(time (loop for i fixnum from 1 to 1000000000 do (unsigned-saturate 16 i))) ;; 1.338 sec
(time (loop for i fixnum from 1 to 1000000000 do (unsigned-saturate 32 i))) ;; 1.338 sec


;; 100x fewer tests due to bignums:

(time (loop for i fixnum from 1 to 10000000 do (signed-saturate 63 i)))  ;; 4.218 sec, 1.44 GB
(time (loop for i fixnum from 1 to 10000000 do (signed-saturate 64 i)))  ;; 0.356 sec, 0 bytes

(time (loop for i fixnum from 1 to 10000000 do (unsigned-saturate 63 i)))  ;; 1.264 sec, 640 MB
(time (loop for i fixnum from 1 to 10000000 do (unsigned-saturate 64 i)))  ;; 0.177 sec, 0 bytes

||#