bv-ref-tests.rkt (11107B)
1 #lang s-exp "../../rosette/bv.rkt" 2 (require syntax/parse/define 3 (only-in racket/base for-syntax) (for-syntax racket/base)) 4 (require "../rackunit-typechecking.rkt") 5 6 ; The 25 Hacker's Delight benchmarks from the following paper: 7 ; Sumit Gulwani, Susmit Jha, Ashish Tiwari, and Ramarathnam Venkatesan. 8 ; 2011. Synthesis of loop-free programs. In Proceedings of the 32nd ACM 9 ; SIGPLAN Conference on Programming Language Design and Implementation (PLDI '11). 10 ; 11 ; The first 20 benchmarks are also used in the SyGuS competition: http://www.sygus.org 12 13 (current-bvpred (bitvector 32)) 14 15 (check-type (thunk (bv 1)) : (C→ BV)) 16 17 ; Constants. 18 (define bv1 (thunk (bv 1))) 19 (define bv2 (thunk (bv 2))) 20 (define bvsz (thunk (bv (sub1 (bitvector-size (current-bvpred)))))) 21 22 (check-type bv1 : (C→ BV)) 23 (check-type (bv1) : BV -> (bv 1)) 24 (check-type ((bitvector 4) (bv1)) : Bool -> #f) 25 (check-type ((bitvector 32) (bv1)) : Bool -> #t) 26 (check-type bv2 : (C→ BV)) 27 (check-type bvsz : (C→ BV)) 28 29 (check-type (bvsub (bv 1) (bv1)) : BV -> (bv 0)) 30 (check-type ((bitvector 32) (bvsub (bv 1) (bv1))) : Bool -> #t) 31 32 ; Mask off the rightmost 1-bit. 33 (define (p1 [x : BV] -> BV) 34 (let* ([o1 (bvsub x (bv 1))] 35 [o2 (bvand x o1)]) 36 o2)) 37 38 (check-type (p1 (bv 1)) : BV -> (bv 0)) 39 (check-type ((bitvector 32) (p1 (bv 1))) : Bool -> #t) 40 41 ; Test whether an unsigned integer is of the form 2^n-1. 42 (define (p2 [x : BV] -> BV) 43 (let* ([o1 (bvadd x (bv 1))] 44 [o2 (bvand x o1)]) 45 o2)) 46 47 ; Isolate the right most 1-bit. 48 (define (p3 [x : BV] -> BV) 49 (let* ([o1 (bvneg x)] 50 [o2 (bvand x o1)]) 51 o2)) 52 53 ; Form a mask that identifies the rightmost 1-bit and trailing 0s. 54 (define (p4 [x : BV] -> BV) 55 (let* ([o1 (bvsub x (bv 1))] 56 [o2 (bvxor x o1)]) 57 o2)) 58 59 ; Right propagate rightmost 1-bit. 60 (define (p5 [x : BV] -> BV) 61 (let* ([o1 (bvsub x (bv 1))] 62 [o2 (bvor x o1)]) 63 o2)) 64 65 ; Turn on the right-most 0-bit in a word. 66 (define (p6 [x : BV] -> BV) 67 (let* ([o1 (bvadd x (bv 1))] 68 [o2 (bvor x o1)]) 69 o2)) 70 71 ; Isolate the right most 0 bit of a given bitvector. 72 (define (p7 [x : BV] -> BV) 73 (let* ([o1 (bvnot x)] 74 [o2 (bvadd x (bv 1))] 75 [o3 (bvand o1 o2)]) 76 o3)) 77 78 ; Form a mask that identifies the trailing 0s. 79 (define (p8 [x : BV] -> BV) 80 (let* ([o1 (bvsub x (bv 1))] 81 [o2 (bvnot x)] 82 [o3 (bvand o1 o2)]) 83 o3)) 84 85 ; Absolute value function. 86 (define (p9 [x : BV] -> BV) 87 (let* ([o1 (bvashr x (bv 31))] 88 [o2 (bvxor x o1)] 89 [o3 (bvsub o2 o1)]) 90 o3)) 91 92 ; Test if nlz(x) == nlz(y) where nlz is number of leading zeroes. 93 (define (p10 [x : BV] [y : BV] -> BV) 94 (let* ([o1 (bvand x y)] 95 [o2 (bvxor x y)] 96 [o3 (bvule o2 o1)]) 97 o3)) 98 99 ; Test if nlz(x) < nlz(y) where nlz is number of leading zeroes. 100 (define (p11 [x : BV] [y : BV] -> BV) 101 (let* ([o1 (bvnot y)] 102 [o2 (bvand x o1)] 103 [o3 (bvugt o2 y)]) 104 o3)) 105 106 ; Test if nlz(x) <= nlz(y) where nlz is number of leading zeroes. 107 (define (p12 [x : BV] [y : BV] -> BV) 108 (let* ([o1 (bvnot x)] 109 [o2 (bvand y o1)] 110 [o3 (bvule o2 x)]) 111 o3)) 112 113 ; Sign function. 114 (define (p13 [x : BV] -> BV) 115 (let* ([o1 (bvneg x)] 116 [o2 (bvlshr o1 (bv 31))] 117 [o3 (bvashr x (bv 31))] 118 [o4 (bvor o2 o3)]) 119 o4)) 120 121 ; Floor of average of two integers without over-flowing. 122 (define (p14 [x : BV] [y : BV] -> BV) 123 (let* ([o1 (bvand x y)] 124 [o2 (bvxor x y)] 125 [o3 (bvlshr o2 (bv 1))] 126 [o4 (bvadd o1 o3)]) 127 o4)) 128 129 ; Ceil of average of two integers without over-flowing. 130 (define (p15 [x : BV] [y : BV] -> BV) 131 (let* ([o1 (bvor x y)] 132 [o2 (bvxor x y)] 133 [o3 (bvlshr o2 (bv 1))] 134 [o4 (bvsub o1 o3)]) 135 o4)) 136 137 ; The max function. 138 (define (p16 [x : BV] [y : BV] -> BV) 139 (if (equal? (bv 1) (bvsge x y)) x y)) 140 141 ; Turn-off the rightmost contiguous string of 1 bits. 142 (define (p17 [x : BV] -> BV) 143 (let* ([o1 (bvsub x (bv 1))] 144 [o2 (bvor x o1)] 145 [o3 (bvadd o2 (bv 1))] 146 [o4 (bvand o3 x)]) 147 o4)) 148 149 ; Test whether an unsigned integer is of the form 2^n. 150 (define (p18 [x : BV] -> BV) 151 (let* ([o1 (bvsub x (bv 1))] 152 [o2 (bvand o1 x)] 153 [o3 (bvredor x)] 154 [o4 (bvredor o2)] 155 [o5 (bvnot o4)] 156 [o6 (bvand o5 o3)]) 157 o6)) 158 159 ; Exchanging 2 fields A and B of the same register 160 ; x where m is mask which identifies field B and k 161 ; is number of bits from end of A to start of B. 162 (define (p19 [x : BV] [m : BV] [k : BV] -> BV) 163 (let* ([o1 (bvlshr x k)] 164 [o2 (bvxor x o1)] 165 [o3 (bvand o2 m)] 166 [o4 (bvshl o3 k)] 167 [o5 (bvxor o4 o3)] 168 [o6 (bvxor o5 x)]) 169 o6)) 170 171 ; Next higher unsigned number with the same number of 1 bits. 172 (define (p20 [x : BV] -> BV) 173 (let* ([o1 (bvneg x)] 174 [o2 (bvand x o1)] 175 [o3 (bvadd x o2)] 176 [o4 (bvxor x o2)] 177 [o5 (bvlshr o4 (bv 2))] 178 [o6 (bvudiv o5 o2)] 179 [o7 (bvor o6 o3)]) 180 o7)) 181 182 ; Cycling through 3 values a, b, c. 183 (define (p21 [x : BV] [a : BV] [b : BV] [c : BV] -> BV) 184 (let* ([o1 (bveq x c)] 185 [o2 (bvneg o1)] 186 [o3 (bvxor a c)] 187 [o4 (bveq x a)] 188 [o5 (bvneg o4)] 189 [o6 (bvxor b c)] 190 [o7 (bvand o2 o3)] 191 [o8 (bvand o5 o6)] 192 [o9 (bvxor o7 o8)] 193 [o10 (bvxor o9 c)]) 194 o10)) 195 196 ; Compute parity. 197 (define (p22 [x : BV] -> BV) 198 (let* ([o1 (bvlshr x (bv 1))] 199 [o2 (bvxor o1 x)] 200 [o3 (bvlshr o2 (bv 2))] 201 [o4 (bvxor o2 o3)] 202 [o5 (bvand o4 (bv #x11111111))] 203 [o6 (bvmul o5 (bv #x11111111))] 204 [o7 (bvlshr o6 (bv 28))] 205 [o8 (bvand o7 (bv 1))]) 206 o8)) 207 208 ; Counting number of bits. 209 (define (p23 [x : BV] -> BV) 210 (let* ([o1 (bvlshr x (bv 1))] 211 [o2 (bvand o1 (bv #x55555555))] 212 [o3 (bvsub x o2)] 213 [o4 (bvand o3 (bv #x33333333))] 214 [o5 (bvlshr o3 (bv 2))] 215 [o6 (bvand o5 (bv #x33333333))] 216 [o7 (bvadd o4 o6)] 217 [o8 (bvlshr o7 (bv 4))] 218 [o9 (bvadd o8 o7)] 219 [o10 (bvand o9 (bv #x0f0f0f0f))]) 220 o10)) 221 222 ; Round up to the next higher power of 2. 223 (define (p24 [x : BV] -> BV) 224 (let* ([o1 (bvsub x (bv 1))] 225 [o2 (bvlshr o1 (bv 1))] 226 [o3 (bvor o1 o2)] 227 [o4 (bvlshr o3 (bv 2))] 228 [o5 (bvor o3 o4)] 229 [o6 (bvlshr o5 (bv 4))] 230 [o7 (bvor o5 o6)] 231 [o8 (bvlshr o7 (bv 8))] 232 [o9 (bvor o7 o8)] 233 [o10 (bvlshr o9 (bv 16))] 234 [o11 (bvor o9 o10)] 235 [o12 (bvadd o11 (bv 1))]) 236 o12)) 237 238 ; Compute higher order half of product of x and y. 239 (define (p25 [x : BV] [y : BV] -> BV) 240 (let* ([o1 (bvand x (bv #xffff))] 241 [o2 (bvlshr x (bv 16))] 242 [o3 (bvand y (bv #xffff))] 243 [o4 (bvlshr y (bv 16))] 244 [o5 (bvmul o1 o3)] 245 [o6 (bvmul o2 o3)] 246 [o7 (bvmul o1 o4)] 247 [o8 (bvmul o2 o4)] 248 [o9 (bvlshr o5 (bv 16))] 249 [o10 (bvadd o6 o9)] 250 [o11 (bvand o10 (bv #xffff))] 251 [o12 (bvlshr o10 (bv 16))] 252 [o13 (bvadd o7 o11)] 253 [o14 (bvlshr o13 (bv 16))] 254 [o15 (bvadd o14 o12)] 255 [o16 (bvadd o15 o8)]) 256 o16)) 257 258 (define-simple-macro (check-equal/rand/bv f) 259 #:with out (syntax/loc this-syntax 260 (check-equal/rand f #:process (λ ([x : CInt]) (bv x)))) 261 out) 262 263 ;; Mask off the rightmost 1-bit. < 1 sec. 264 (define-fragment (p1* x) 265 #:implements p1 266 #:library (bvlib [{bv1 bvand bvsub} 1])) 267 268 (check-equal/rand/bv p1) 269 270 ; Test whether an unsigned integer is of the form 2^n-1. < 1 sec. 271 (define-fragment (p2* x) 272 #:implements p2 273 #:library (bvlib [{bv1 bvand bvadd} 1])) 274 275 (check-equal/rand/bv p2) 276 277 ; Isolate the right most 1-bit. < 1 sec. 278 (define-fragment (p3* x) 279 #:implements p3 280 #:library (bvlib [{bvand bvneg} 1])) 281 282 (check-equal/rand/bv p3) 283 284 ;; Form a mask that identifies the rightmost 1-bit and trailing 0s. < 1 sec. 285 (define-fragment (p4* x) 286 #:implements p4 287 #:library (bvlib [{bv1 bvsub bvxor} 1])) 288 289 (check-equal/rand/bv p4) 290 291 ; Right propagate rightmost 1-bit. < 1 sec. 292 (define-fragment (p5* x) 293 #:implements p5 294 #:library (bvlib [{bv1 bvsub bvor} 1])) 295 296 (check-equal/rand/bv p5) 297 298 ; Turn on the right-most 0-bit in a word. < 1 sec. 299 (define-fragment (p6* x) 300 #:implements p6 301 #:library (bvlib [{bv1 bvor bvadd} 1])) 302 303 (check-equal/rand/bv p6) 304 305 ; Isolate the right most 0 bit of a given bitvector. < 1 sec. 306 (define-fragment (p7* x) 307 #:implements p7 308 #:library (bvlib [{bvand bvadd bvnot bv1} 1])) 309 310 (check-equal/rand/bv p7) 311 312 ; Form a mask that identifies the trailing 0s. < 1 sec. 313 (define-fragment (p8* x) 314 #:implements p8 315 #:library (bvlib [{bv1 bvsub bvand bvnot} 1])) 316 317 (check-equal/rand/bv p8) 318 319 ; Absolute value function. ~ 1 sec. 320 (define-fragment (p9* x) 321 #:implements p9 322 #:library (bvlib [{bvsz bvsub bvxor bvashr} 1])) 323 324 (check-equal/rand/bv p9) 325 326 ; Test if nlz(x) == nlz(y) where nlz is number of leading zeroes. < 1 sec. 327 (define-fragment (p10* x y) 328 #:implements p10 329 #:library (bvlib [{bvand bvxor bvule} 1])) 330 331 (check-equal/rand/bv p10) 332 333 ; Test if nlz(x) < nlz(y) where nlz is number of leading zeroes. < 1 sec. 334 (define-fragment (p11* x y) 335 #:implements p11 336 #:library (bvlib [{bvnot bvand bvugt} 1])) 337 338 (check-equal/rand/bv p11) 339 340 ; Test if nlz(x) <= nlz(y) where nlz is number of leading zeroes. < 1 sec. 341 (define-fragment (p12* x y) 342 #:implements p12 343 #:library (bvlib [{bvand bvnot bvule} 1])) 344 345 (check-equal/rand/bv p12) 346 347 ; Sign function. ~ 1.4 sec. 348 (define-fragment (p13* x) 349 #:implements p13 350 #:library (bvlib [{bvsz bvneg bvlshr bvashr bvor} 1]) 351 #:minbv 32) 352 353 (check-equal/rand/bv p13) 354 355 ; Floor of average of two integers without over-flowing. ~ 2.5 sec. 356 (define-fragment (p14* x y) 357 #:implements p14 358 #:library (bvlib [{bv1 bvand bvlshr bvxor bvadd} 1])) 359 360 (check-equal/rand/bv p14) 361 362 ; Ceil of average of two integers without over-flowing. ~ 1 sec. 363 (define-fragment (p15* x y) 364 #:implements p15 365 #:library (bvlib [{bv1 bvor bvlshr bvxor bvsub} 1])) 366 367 (check-equal/rand/bv p15) 368 369 ; Compute max of two integers. Bug in the PLDI'11 paper: bvuge should be bvge. 370 ; ~ 1.3 sec. 371 (define-fragment (p16* x y) 372 #:implements p16 373 #:library (bvlib [{bvneg bvsge bvand} 1] [{bvxor} 2])) 374 375 (check-equal/rand/bv p16) 376 377 ; Turn-off the rightmost contiguous string of 1 bits. ~ 1.8 sec. 378 (define-fragment (p17* x) 379 #:implements p17 380 #:library (bvlib [{bv1 bvand bvor bvadd bvsub} 1])) 381 382 (check-equal/rand/bv p17) 383 384 ; Test whether an unsigned integer is of the form 2^n. ~ 1.6 sec. 385 (define-fragment (p18* x) 386 #:implements p18 387 #:library (bvlib [{bvand bvredor} 2] [{bvsub bvnot bv1} 1])) 388 389 (check-equal/rand/bv p18) 390 391 ; x where m is mask which identifies field B and k 392 ; is number of bits from end of A to start of B. ~ 3.5 sec. 393 (define-fragment (p19* x m k) 394 #:implements p19 395 #:library (bvlib [{bvlshr bvshl bvand} 1] [{bvxor} 3])) 396 397 (check-equal/rand/bv p19) 398 399 ; Next higher unsigned number with the same number of 1 bits. ~ 600 sec. 400 (define-fragment (p20* x) 401 #:implements p20 402 #:library (bvlib [{bv2 bvneg bvand bvadd bvxor bvlshr bvudiv bvor} 1])) 403 404 (check-equal/rand/bv p20)