1 /*
2  * This file is part of the Yices SMT Solver.
3  * Copyright (C) 2017 SRI International.
4  *
5  * Yices is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Yices is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Yices.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /*
20  * Bit-vector constants = finite-precision integers.
21  * Constants are represented as fixed-size arrays of 32bit integers
22  *
23  * These functions are intended to be used for small arrays (128-256 bits at most).
24  * For larger sizes, GMP integers should be faster.
25  */
26 
27 #include <stdio.h>
28 #include <assert.h>
29 #include <ctype.h>
30 
31 #include "terms/bv_constants.h"
32 #include "utils/bit_tricks.h"
33 #include "utils/hash_functions.h"
34 #include "utils/memalloc.h"
35 #include "utils/object_stores.h"
36 
37 
38 
39 /*****************
40  *  ALLOCATION   *
41  ****************/
42 
43 /*
44  * Allocator = array of object_stores
45  * - store[0] is unused
46  * - store[i] is for vectors of size 2i and (2i - 1)
47  *   (the size is measured in number of words)
48  *
49  * We must ensure that all bitvectors allocated in store[i]
50  * have size <= MAX_OBJ_SIZE (i.e., 512 bytes). So we want
51  *  2 * i * sizeof(uint32_t) <= MAX_OBJ_SIZE.
52  *
53  * Bitvectors os size larger than that are allocated via
54  * direct calls to malloc/free.
55  */
56 typedef struct {
57   object_store_t *store;
58   uint32_t nstores;
59 } bvconst_allocator_t;
60 
61 
62 /*
63  * i < MAX_NUM_STORE ==> 2 i * sizeof(uint32_t) <= MAX_OBJ_SIZE
64  */
65 #define MAX_NUM_STORES ((uint32_t) (1 + (MAX_OBJ_SIZE/(2 * sizeof(uint32_t)))))
66 
67 
68 /*
69  * Block size for each object store = number of bitvectors
70  * in each block of the store.
71  */
72 #define BVCONST_BANK_SIZE 128
73 
74 
75 /*
76  * Global allocator
77  */
78 static bvconst_allocator_t allocator;
79 
80 
81 /*
82  * Initialize s
83  */
init_allocator(bvconst_allocator_t * s)84 static void init_allocator(bvconst_allocator_t *s) {
85   s->nstores = 0;
86   s->store = NULL;
87 }
88 
89 /*
90  * Extend allocator to include at least n+1 stores
91  */
resize_allocator(bvconst_allocator_t * s,uint32_t n)92 static void resize_allocator(bvconst_allocator_t *s, uint32_t n) {
93   uint32_t new_size, i;
94 
95   assert(s->nstores <= n && n < MAX_NUM_STORES);
96 
97   // new_size = min(max(s->nstores * 1.5, n+1), MAX_NUM_STORES)
98   new_size = s->nstores + 1;
99   new_size += new_size >> 1;
100   if (new_size <= n) {
101     new_size = n + 1;
102   } else if (new_size > MAX_NUM_STORES) {
103     new_size = MAX_NUM_STORES;
104   }
105 
106   assert(new_size <= MAX_NUM_STORES && new_size >= n+1);
107 
108   s->store = (object_store_t *) safe_realloc(s->store, new_size * sizeof(object_store_t));
109   for (i=s->nstores; i<new_size; i++) {
110     init_objstore(s->store + i, (2 * i) * sizeof(uint32_t), BVCONST_BANK_SIZE);
111   }
112   s->nstores = new_size;
113 }
114 
115 
116 /*
117  * Allocate a  new vector of size k
118  */
alloc_vector(bvconst_allocator_t * s,uint32_t k)119 static uint32_t *alloc_vector(bvconst_allocator_t *s, uint32_t k) {
120   object_store_t *m;
121   uint32_t *p;
122   uint32_t i;
123 
124   assert(0 < k && k <= (UINT32_MAX/sizeof(uint32_t)));
125 
126   i = ((k + 1) >> 1); // store index for size k
127   if (i < MAX_NUM_STORES) {
128     if (i >= s->nstores) {
129       resize_allocator(s, i);
130     }
131     m = s->store + i;
132     assert(m->objsize >= k * sizeof(uint32_t));
133     p = (uint32_t *) objstore_alloc(m);
134   } else {
135     p = (uint32_t *) safe_malloc(k * sizeof(uint32_t));
136   }
137 
138   return p;
139 }
140 
141 
142 /*
143  * Free vector bv of size k
144  */
free_vector(bvconst_allocator_t * s,uint32_t * bv,uint32_t k)145 static void free_vector(bvconst_allocator_t *s, uint32_t *bv, uint32_t k) {
146   uint32_t i;
147 
148   // store index = ceil(k/2)
149   i = ((k + 1) >> 1);
150   if (i < MAX_NUM_STORES) {
151     assert(0 < i && i < s->nstores);
152     objstore_free(s->store + i, (void *) bv);
153   } else {
154     safe_free(bv);
155   }
156 }
157 
158 
159 /*
160  * Delete s: free all stores
161  */
delete_allocator(bvconst_allocator_t * s)162 static void delete_allocator(bvconst_allocator_t *s) {
163   uint32_t i;
164 
165   for (i=0; i<s->nstores; i++) {
166     delete_objstore(s->store + i);
167   }
168   safe_free(s->store);
169   s->store = NULL;
170   s->nstores = 0;
171 }
172 
173 
174 /*
175  * Initialization: prepare global store
176  */
init_bvconstants(void)177 void init_bvconstants(void) {
178   init_allocator(&allocator);
179 }
180 
181 /*
182  * Cleanup: free the store
183  */
cleanup_bvconstants(void)184 void cleanup_bvconstants(void) {
185   delete_allocator(&allocator);
186 }
187 
188 /*
189  * Allocate a vector of k words.
190  */
bvconst_alloc(uint32_t k)191 uint32_t *bvconst_alloc(uint32_t k) {
192   return alloc_vector(&allocator, k);
193 }
194 
195 /*
196  * Free vector bv of size k.
197  */
bvconst_free(uint32_t * bv,uint32_t k)198 void bvconst_free(uint32_t *bv, uint32_t k) {
199   free_vector(&allocator, bv, k);
200 }
201 
202 
203 
204 
205 
206 /****************************
207  *  BVCONSTANT STRUCTURES   *
208  ***************************/
209 
210 /*
211  * Initialization: bitsize is set to 0
212  */
init_bvconstant(bvconstant_t * b)213 void init_bvconstant(bvconstant_t *b) {
214   b->bitsize = 0;
215   b->width = 0;
216   b->arraysize = 0;
217   b->data = NULL;
218 }
219 
220 /*
221  * Delete: free allocated memory
222  */
delete_bvconstant(bvconstant_t * b)223 void delete_bvconstant(bvconstant_t *b) {
224   safe_free(b->data);
225   b->data = NULL;
226 }
227 
228 /*
229  * Resize: make the array large enough for n bits
230  */
bvconstant_set_bitsize(bvconstant_t * b,uint32_t n)231 void bvconstant_set_bitsize(bvconstant_t *b, uint32_t n) {
232   uint32_t k;
233 
234   k = (n + 31) >> 5; // ceil(n/32)
235   if (b->arraysize < k) {
236     b->data = (uint32_t *) safe_realloc(b->data, k * sizeof(uint32_t));
237     b->arraysize = k;
238     /*
239      * To prevent false alarms from valgrind, just make sure it's
240      * all initialized.
241      */
242     bvconst_clear(b->data, k);
243   }
244   b->bitsize = n;
245   b->width = k;
246 }
247 
248 
249 /*
250  * Resize and initialize to 0b0....0
251  */
bvconstant_set_all_zero(bvconstant_t * b,uint32_t n)252 void bvconstant_set_all_zero(bvconstant_t *b, uint32_t n) {
253   uint32_t k;
254 
255   k = (n + 31) >> 5;
256   bvconstant_set_bitsize(b, n);
257   bvconst_clear(b->data, k);
258 }
259 
260 
261 /*
262  * Resize and initialize to 0b1....1
263  */
bvconstant_set_all_one(bvconstant_t * b,uint32_t n)264 void bvconstant_set_all_one(bvconstant_t *b, uint32_t n) {
265   uint32_t k;
266 
267   k = (n + 31) >> 5;
268   bvconstant_set_bitsize(b, n);
269   bvconst_set_minus_one(b->data, k);
270   bvconst_normalize(b->data, n);
271 }
272 
273 
274 /*
275  * Resize and copy bitvector a into b.
276  */
bvconstant_copy(bvconstant_t * b,uint32_t n,const uint32_t * a)277 void bvconstant_copy(bvconstant_t *b, uint32_t n, const uint32_t *a) {
278   uint32_t k;
279 
280   k = (n + 31) >> 5;
281   bvconstant_set_bitsize(b, n);
282   bvconst_set(b->data, k, a);
283   bvconst_normalize(b->data, n);
284 }
285 
286 
287 
288 /*
289  * Resize and copy constant into b
290  */
bvconstant_copy64(bvconstant_t * b,uint32_t n,uint64_t a)291 void bvconstant_copy64(bvconstant_t *b, uint32_t n, uint64_t a) {
292   uint32_t k;
293 
294   k = (n + 31) >> 5;
295 
296   bvconstant_set_bitsize(b, n);
297   bvconst_set64(b->data, k, a);
298   bvconst_normalize(b->data, n);
299 }
300 
301 
302 
303 
304 /****************************************
305  *  OPERATIONS ON BIT-VECTOR CONSTANTS  *
306  ***************************************/
307 
308 /*
309  * Return 1^k where 0 <= k <= 31. This is just to avoid ((uint32_t) 1) << k
310  * everywhere
311  */
bitp(uint32_t k)312 static inline uint32_t bitp(uint32_t k) {
313   assert(k < 32);
314   return ((uint32_t) 1) << k;
315 }
316 
317 /*
318  * Normalize: n = number of bits in bv
319  * - let k = ceiling(n/32) and d = 32 k - n
320  * - then bv is an array of k words
321  * - normalization sets the d higher order bits of n to zero
322  */
bvconst_normalize(uint32_t * bv,uint32_t n)323 void bvconst_normalize(uint32_t *bv, uint32_t n) {
324   uint32_t k, r;
325 
326   r = n & 0x1f;      // r = n mod 32
327   if (r > 0) {
328     k = n >> 5;        // k = floor(n/32)
329     bv[k] &= (uint32_t) (bitp(r) - 1);
330   }
331 }
332 
333 /*
334  * Check whether bv is normalized modulo 2^n (i.e., whether the high
335  * order bits are 0).
336  */
bvconst_is_normalized(const uint32_t * bv,uint32_t n)337 bool bvconst_is_normalized(const uint32_t *bv, uint32_t n) {
338   uint32_t k, r;
339 
340   r = n & 0x1f;  // r = n mod 32
341   k = n >> 5;    // k = floor (n/32)
342   return r == 0 || (bv[k] & ~((uint32_t) (bitp(r) - 1))) == 0;
343 }
344 
345 
346 /*
347  * Operations on single bits
348  */
bvconst_tst_bit(const uint32_t * bv,uint32_t i)349 bool bvconst_tst_bit(const uint32_t *bv, uint32_t i) {
350   uint32_t j, mask;
351 
352   j = i >> 5;
353   mask = bitp(i & 0x1f);
354   return bv[j] & mask; // converted to bool --> 0 or 1
355 }
356 
bvconst_set_bit(uint32_t * bv,uint32_t i)357 void bvconst_set_bit(uint32_t *bv, uint32_t i) {
358   uint32_t j, mask;
359 
360   j = i >> 5;
361   mask = bitp(i & 0x1f);
362   bv[j] |= mask;
363 }
364 
bvconst_clr_bit(uint32_t * bv,uint32_t i)365 void bvconst_clr_bit(uint32_t *bv, uint32_t i) {
366   uint32_t j, mask;
367 
368   j = i >> 5;
369   mask = bitp(i & 0x1f);
370   bv[j] &= ~mask;
371 }
372 
bvconst_flip_bit(uint32_t * bv,uint32_t i)373 void bvconst_flip_bit(uint32_t *bv, uint32_t i) {
374   uint32_t j, mask;
375 
376   j = i >> 5;
377   mask = bitp(i & 0x1f);
378   bv[j] ^= mask;
379 }
380 
bvconst_assign_bit_old(uint32_t * bv,uint32_t i,bool bit)381 void bvconst_assign_bit_old(uint32_t *bv, uint32_t i, bool bit) {
382   uint32_t j, mask;
383 
384   j = i >> 5;
385   mask = bitp(i & 0x1f);
386   if (bit) {
387     bv[j] |= mask;
388   } else {
389     bv[j] &= ~mask;
390   }
391 }
392 
bvconst_assign_bit(uint32_t * bv,uint32_t i,bool bit)393 void bvconst_assign_bit(uint32_t *bv, uint32_t i, bool bit) {
394   uint32_t j, mask, x;
395 
396   j = i >> 5;
397   mask = bitp(i & 0x1f);
398   x = ((uint32_t) bit) << (i & 0x1f);
399   bv[j] ^= (bv[j] ^ x) & mask;
400 }
401 
402 
403 
404 
405 
406 /*
407  * Assignment operations: bv is the target
408  * - k = number of 32-bit words in bv
409  */
410 // set to 0b0000....000
bvconst_clear(uint32_t * bv,uint32_t k)411 void bvconst_clear(uint32_t *bv, uint32_t k) {
412   assert(k > 0);
413   do {
414     *bv++ = 0;
415     k --;
416   } while (k > 0);
417 }
418 
419 // set to 0b0000000...001
bvconst_set_one(uint32_t * bv,uint32_t k)420 void bvconst_set_one(uint32_t *bv, uint32_t k) {
421   assert(k > 0);
422   *bv ++ = 1;
423   while (k > 1) {
424     *bv++ = 0;
425     k --;
426   }
427 }
428 
429 // set to 0b1111....111
bvconst_set_minus_one(uint32_t * bv,uint32_t k)430 void bvconst_set_minus_one(uint32_t *bv, uint32_t k) {
431   assert(k > 0);
432   do {
433     *bv ++ = 0xffffffff;
434     k --;
435   } while (k>0);
436 }
437 
438 // set low-order word to a, rest to 0
bvconst_set32(uint32_t * bv,uint32_t k,uint32_t a)439 void bvconst_set32(uint32_t *bv, uint32_t k, uint32_t a) {
440   assert(k > 0);
441   * bv++ = a;
442   while (k > 1) {
443     *bv++ = 0;
444     k --;
445   }
446 }
447 
448 // set low-order word to a, then sign extend
bvconst_set32_signed(uint32_t * bv,uint32_t k,int32_t a)449 void bvconst_set32_signed(uint32_t *bv, uint32_t k, int32_t a) {
450   uint32_t p;
451 
452   assert(k > 0);
453   * bv ++ = a;
454   p = (a >= 0) ? 0 : 0xffffffff;
455   while (k > 1) {
456     *bv ++ = p;
457     k --;
458   }
459 }
460 
461 // set two low-order words to a, rest to 0
bvconst_set64(uint32_t * bv,uint32_t k,uint64_t a)462 void bvconst_set64(uint32_t *bv, uint32_t k, uint64_t a) {
463   assert(k > 0);
464 
465   if (k == 1) {
466     * bv= (uint32_t) (a & 0xffffffff);
467   } else {
468     * bv ++ = (uint32_t) (a & 0xffffffff);
469     * bv ++ = (uint32_t) (a >> 32);
470     while (k > 2) {
471       * bv ++ = 0;
472       k --;
473     }
474   }
475 }
476 
477 // set the two low-order words to a, then sign extend
bvconst_set64_signed(uint32_t * bv,uint32_t k,int64_t a)478 void bvconst_set64_signed(uint32_t *bv, uint32_t k, int64_t a) {
479   uint32_t p;
480 
481   assert(k > 0);
482 
483   if (k == 1) {
484     * bv = (uint32_t) (a & 0xffffffff);
485   } else {
486     * bv ++ = (uint32_t) (a & 0xffffffff);
487     * bv ++ = (uint32_t) (a >> 32);
488     p = (a >= 0) ? 0 : 0xffffffff;
489     while (k > 2) {
490       * bv ++ = p;
491       k --;
492     }
493   }
494 }
495 
496 
497 // assign 32k low-order bits of z
bvconst_set_mpz(uint32_t * bv,uint32_t k,const mpz_t z)498 void bvconst_set_mpz(uint32_t *bv, uint32_t k, const mpz_t z) {
499   mpz_t aux;
500 
501   assert(mpz_sgn(z) >= 0); // z must be  non-negative
502   assert(k > 0);
503 
504   if (k == 1) {
505     *bv = (uint32_t) mpz_get_ui(z);
506   } else {
507     mpz_init_set(aux, z);   // aux := copy of z
508 
509     do {
510       *bv++ = (uint32_t) mpz_get_ui(aux);
511       mpz_fdiv_q_2exp(aux, aux, 32);
512       k --;
513     } while (k > 0);
514 
515     mpz_clear(aux);
516   }
517 }
518 
519 // conversion from bv to mpz_t
bvconst_get_mpz(const uint32_t * bv,uint32_t k,mpz_t z)520 void bvconst_get_mpz(const uint32_t *bv, uint32_t k, mpz_t z) {
521   assert(k > 0);
522 
523   k --;
524   mpz_set_ui(z, bv[k]);
525   while (k > 0) {
526     k --;
527     mpz_mul_2exp(z, z, 32);
528     mpz_add_ui(z, z, bv[k]);
529   }
530 }
531 
532 // assign bv from r. r must be a positive integer.
bvconst_set_q(uint32_t * bv,uint32_t k,rational_t * r)533 void bvconst_set_q(uint32_t *bv, uint32_t k, rational_t *r) {
534   assert(q_is_integer(r));
535 
536   if (is_rat32(r)) {
537     bvconst_set32(bv, k, get_num(r));
538   } else {
539     bvconst_set_mpz(bv, k, mpq_numref(get_gmp(r)));
540   }
541 }
542 
543 // copy a into b: b has k words. a has k or more words
bvconst_set(uint32_t * bv,uint32_t k,const uint32_t * a)544 void bvconst_set(uint32_t *bv, uint32_t k, const uint32_t *a) {
545   assert(k > 0);
546   do {
547     *bv ++  = *a++;
548     k --;
549   } while (k > 0);
550 }
551 
552 
553 
554 /*
555  * Other constant assignments: n = number of bits
556  * - set_min_signed:  bv := 0b100...000
557  * - set_max_signed:  bv := 0b011...111
558  * - bv must be large enough (at least ceil(n/32) words)
559  * - n must be positive
560  * - bv is normalized
561  */
bvconst_set_min_signed(uint32_t * bv,uint32_t n)562 void bvconst_set_min_signed(uint32_t *bv, uint32_t n) {
563   assert(n > 0);
564   bvconst_clear(bv, (n + 31) >> 5);
565   bvconst_set_bit(bv, n-1);
566 }
567 
bvconst_set_max_signed(uint32_t * bv,uint32_t n)568 void bvconst_set_max_signed(uint32_t *bv, uint32_t n) {
569   assert(n > 0);
570   bvconst_set_minus_one(bv, (n + 31) >> 5);
571   bvconst_clr_bit(bv, n-1);
572   bvconst_normalize(bv, n);
573 }
574 
575 
576 /*
577  * Assign a to bv, padding with 0
578  * - bv has m = 32k bits
579  * - a has n = 32d + r bits with d < k and 0 <= r <= 31
580  */
bvconst_set_p0(uint32_t * bv,uint32_t k,const uint32_t * a,uint32_t d,uint32_t r)581 static void bvconst_set_p0(uint32_t *bv, uint32_t k, const uint32_t *a, uint32_t d, uint32_t r) {
582   assert(r <= 31);
583   assert(d < k);
584 
585   while (d > 0) {
586     *bv ++ = * a++;
587     d --;
588     k --;
589   }
590   if (r > 0) {
591     *bv ++ = (*a) & ((1<<r) - 1);
592     k --;
593   }
594   while (k > 0) {
595     *bv ++ = 0;
596     k --;
597   }
598 }
599 
600 // padding with 1
bvconst_set_p1(uint32_t * bv,uint32_t k,const uint32_t * a,uint32_t d,uint32_t r)601 static void bvconst_set_p1(uint32_t *bv, uint32_t k, const uint32_t *a, uint32_t d, uint32_t r) {
602   uint32_t mask;
603 
604   assert(r <= 31);
605   assert(d < k);
606 
607   while (d > 0) {
608     *bv ++ = * a++;
609     d --;
610     k --;
611   }
612   if (r > 0) {
613     mask = bitp(r) - 1;
614     *bv ++ = (~mask) | ((*a) & mask);
615     k --;
616   }
617   while (k > 0) {
618     *bv ++ = 0xffffffff;
619     k --;
620   }
621 }
622 
623 /*
624  * b: n-bit value, a: m-bit value with m <= n
625  * - assign a to b with padding defined by mode:
626  * - mode = 0: padding with 0
627  * - mode = 1: padding with 1
628  * - mode = -1: sign extension
629  */
bvconst_set_extend(uint32_t * bv,uint32_t n,const uint32_t * a,uint32_t m,int32_t mode)630 void bvconst_set_extend(uint32_t *bv, uint32_t n, const uint32_t *a, uint32_t m, int32_t mode) {
631   uint32_t k, d, r;
632 
633   assert(0 < m && m <= n);
634   assert(mode == 0 || mode == 1 || mode == -1);
635 
636   k = (n + 31) >> 5;
637 
638   if (n == m) {
639     bvconst_set(bv, k, a);
640     return;
641   }
642 
643   d = m >> 5;
644   r = m & 0x1f;
645 
646   if (mode < 0) {
647     mode = bvconst_tst_bit(a, m-1);
648   }
649 
650   if (mode) {
651     bvconst_set_p1(bv, k, a, d, r);
652   } else {
653     bvconst_set_p0(bv, k, a, d, r);
654   }
655 }
656 
657 
658 /*
659  * Assignment from array a of n elements
660  * - bit i of bv = 0 iff a[i] = 0
661  */
bvconst_set_array(uint32_t * bv,const int32_t * a,uint32_t n)662 void bvconst_set_array(uint32_t *bv, const int32_t *a, uint32_t n) {
663   uint32_t i;
664   assert(n > 0);
665 
666   // suboptimal but should be enough
667   for (i=0; i<n; i++) bvconst_assign_bit(bv, i, a[i]);
668 }
669 
670 
671 
672 /*
673  * Reverse operation: store bit i of bv in a[i].
674  */
bvconst_get_array(const uint32_t * bv,int32_t * a,uint32_t n)675 void bvconst_get_array(const uint32_t *bv, int32_t *a, uint32_t n) {
676   uint32_t i;
677   assert(n > 0);
678 
679   for (i=0; i<n; i++) a[i] = bvconst_tst_bit(bv, i);
680 }
681 
682 
683 /*
684  * Print bv constant of size n
685  */
bvconst_print(FILE * f,const uint32_t * bv,uint32_t n)686 void bvconst_print(FILE *f, const uint32_t *bv, uint32_t n) {
687   assert(n>0);
688 
689   fprintf(f, "0b");
690   do {
691     n --;
692     fprintf(f, "%u", (unsigned) bvconst_tst_bit(bv, n));
693   } while (n > 0);
694 }
695 
696 
697 
698 /*
699  * Convert a string of '0' and '1's to a bitvector constant.
700  * - n = number of bits
701  * - s must be at least n character long
702  *
703  * Reads the n first characters of s. All must be '0' or '1'
704  * - the string is assumed in big-endian format: the
705  *   first character is the high-order bit.
706  *
707  * Return code: -1 if the string format is wrong, 0 otherwise.
708  */
bvconst_set_from_string(uint32_t * bv,uint32_t n,const char * s)709 int32_t bvconst_set_from_string(uint32_t *bv, uint32_t n, const char *s) {
710   char c;
711 
712   assert(n > 0);
713 
714   do {
715     n --;
716     c = *s;
717     s ++;
718     if (c == '0') {
719       bvconst_clr_bit(bv, n);
720     } else if (c == '1') {
721       bvconst_set_bit(bv, n);
722     } else {
723       return -1;
724     }
725   } while (n > 0);
726 
727   return 0;
728 }
729 
730 
731 /*
732  * Convert a string, interpreted as hexadecimal digits to a bitvector constant.
733  * - n = number of characters, must be positive.
734  * - s must be at least n character long
735  *
736  * Reads the n first characters of s.
737  * All must be '0' to '9' or 'a' to 'f' or 'A' to 'F'
738  * - the string is assumed in big-endian format: the
739  *   first character defines the high-order 4 bits.
740  *
741  * Return code: -1 if the string format is wrong, 0 otherwise.
742  */
hextoint(char c)743 static uint32_t hextoint(char c) {
744   if ('0' <= c && c <= '9') {
745     return c - '0';
746   } else if ('a' <= c && c <= 'f') {
747     return 10 + (c - 'a');
748   } else {
749     assert('A' <= c && c <= 'F');
750     return 10 + (c - 'A');
751   }
752 }
753 
bvconst_set_from_hexa_string(uint32_t * bv,uint32_t n,const char * s)754 int32_t bvconst_set_from_hexa_string(uint32_t *bv, uint32_t n, const char *s) {
755   char c;
756   uint32_t hex;
757 
758   assert(n > 0);
759 
760   n <<= 2; // n * 4
761   do {
762     c = *s;
763     s ++;
764     if (isxdigit((int) c)) {
765       hex = hextoint(c);
766       // set bits n-1 to n-4
767       // suboptimal but that should do for now
768       n --;
769       bvconst_assign_bit(bv, n, hex & 8);
770       n --;
771       bvconst_assign_bit(bv, n, hex & 4);
772       n --;
773       bvconst_assign_bit(bv, n, hex & 2);
774       n --;
775       bvconst_assign_bit(bv, n, hex & 1);
776     } else {
777       return -1;
778     }
779   } while (n > 0);
780 
781   return 0;
782 }
783 
784 
785 
786 /*
787  * Population count: number of 1 bits in bv
788  * - bv must be normalized
789  * - k must be the size of bv in words
790  */
bvconst_popcount(const uint32_t * bv,uint32_t k)791 uint32_t bvconst_popcount(const uint32_t *bv, uint32_t k) {
792   uint32_t i, c;
793 
794   c = 0;
795   for (i=0; i<k; i++) {
796     c += popcount32(bv[i]);
797   }
798   return c;
799 }
800 
801 
802 
803 
804 /*
805  * Bitwise operations: all modify the first argument bv
806  * - bv and a are both assumed to have k words
807  */
bvconst_complement(uint32_t * bv,uint32_t k)808 void bvconst_complement(uint32_t *bv, uint32_t k) {
809   assert(k > 0);
810   do {
811     *bv = ~ (*bv);
812     bv ++;
813     k --;
814   } while (k > 0);
815 }
816 
bvconst_and(uint32_t * bv,uint32_t k,uint32_t * a)817 void bvconst_and(uint32_t *bv, uint32_t k, uint32_t *a) {
818   assert(k > 0);
819   do {
820     *bv ++ &= *a ++;
821     k --;
822   } while (k > 0);
823 }
824 
bvconst_or(uint32_t * bv,uint32_t k,uint32_t * a)825 void bvconst_or(uint32_t *bv, uint32_t k, uint32_t *a) {
826   assert(k > 0);
827   do {
828     *bv ++ |= *a ++;
829     k --;
830   } while (k > 0);
831 }
832 
bvconst_xor(uint32_t * bv,uint32_t k,uint32_t * a)833 void bvconst_xor(uint32_t *bv, uint32_t k, uint32_t *a) {
834   assert(k > 0);
835   do {
836     *bv ++ ^= *a ++;
837     k --;
838   } while (k > 0);
839 }
840 
841 
842 
843 /*
844  * - shift bv to the left by 32d + r, padding with 0
845  * - k = size of bv in words: bv has 32k bits
846  * - 0 <= r <= 31 and  0 <= d <= k-1
847  */
bvconst_shift_left0(uint32_t * bv,uint32_t k,uint32_t d,uint32_t r)848 static void bvconst_shift_left0(uint32_t *bv, uint32_t k, uint32_t d, uint32_t r) {
849   uint32_t i, j, s;
850   uint64_t aux;
851 
852   assert(r <= 31);
853   assert(d < k);
854 
855   s = 32 - r;
856   j = k - 1;
857   i = j - d;
858   aux = (uint64_t) bv[i];
859   while (i > 0) {
860     i --;
861     aux <<= 32;
862     aux |= bv[i];
863     bv[j] = (uint32_t) (aux >> s);
864     j --;
865   }
866   bv[j] = (uint32_t) (aux << r);
867   while (j > 0) {
868     j --;
869     bv[j] = 0;
870   }
871 }
872 
873 
874 /*
875  * - shift bv to the left by 32d + r, padding with 1
876  * - k = size of bv in words: bv has 32k bits
877  * - 0 <= r <= 31 and  0 <= d <= k-1
878  */
bvconst_shift_left1(uint32_t * bv,uint32_t k,uint32_t d,uint32_t r)879 static void bvconst_shift_left1(uint32_t *bv, uint32_t k, uint32_t d, uint32_t r) {
880   uint32_t i, j;
881   uint64_t aux;
882 
883   assert(r <= 31);
884   assert(d < k);
885 
886   r = 32 - r;
887   j = k - 1;
888   i = j - d;
889   aux = (uint64_t) bv[i];
890   while (i > 0) {
891     i --;
892     aux <<= 32;
893     aux |= bv[i];
894     bv[j] = (uint32_t) (aux >> r);
895     j --;
896   }
897   aux <<= 32;
898   aux |= 0xffffffff;
899   bv[j] = (uint32_t) (aux >> r);
900   while (j > 0) {
901     j --;
902     bv[j] = 0xffffffff;
903   }
904 }
905 
906 
907 /*
908  * Shift by m bits to the left, padding with b
909  * - n = size of bv in bits
910  * - m must be between 0 and n
911  * - b = false ==> padding with 0, otherwise padding with 1
912  */
bvconst_shift_left(uint32_t * bv,uint32_t n,uint32_t m,bool b)913 void bvconst_shift_left(uint32_t *bv, uint32_t n, uint32_t m, bool b) {
914   uint32_t k, d, r;
915   assert(0 < n && m <= n);
916 
917   if (m == 0) return;
918 
919   // special case: n == m
920   k = (n + 31) >> 5;  // bv contains k 32-bit words
921   if (m == n) {
922     if (b) {
923       bvconst_set_minus_one(bv, k);
924     } else {
925       bvconst_clear(bv, k);
926     }
927     return;
928   }
929 
930   d = m >> 5;
931   r = m & 0x1f;
932   if (b) {
933     bvconst_shift_left1(bv, k, d, r);
934   } else {
935     bvconst_shift_left0(bv, k, d, r);
936   }
937 }
938 
939 
940 
941 /*
942  * - shift bv to the right by 32d + r, padding with 0
943  * - k = size of bv in words: bv has 32k bits
944  * - 0 <= r <= 31 and  0 <= d <= k-1
945  */
bvconst_shift_right0(uint32_t * bv,uint32_t k,uint32_t d,uint32_t r)946 static void bvconst_shift_right0(uint32_t *bv, uint32_t k, uint32_t d, uint32_t r) {
947   uint32_t i, j;
948   uint64_t aux;
949 
950   assert(r <= 31);
951   assert(d < k);
952 
953   j = 0;
954   i = d;
955   aux = (uint64_t) bv[i];
956   while (i < k-1) {
957     i ++;
958     aux |= ((uint64_t) bv[i]) << 32;
959     bv[j] = (uint32_t) (aux >> r);
960     aux >>= 32;
961     j ++;
962   }
963   bv[j] = (uint32_t) (aux >> r);
964   while (j < k-1) {
965     j ++;
966     bv[j] = 0;
967   }
968 }
969 
970 /*
971  * - shift bv to the right by 32d + r, padding with 1
972  * - k = size of bv in words: bv has 32k bits
973  * - 0 <= r <= 31 and  0 <= d <= k-1
974  */
bvconst_shift_right1(uint32_t * bv,uint32_t k,uint32_t d,uint32_t r)975 static void bvconst_shift_right1(uint32_t *bv, uint32_t k, uint32_t d, uint32_t r) {
976   uint32_t i, j;
977   uint64_t aux;
978 
979   assert(r <= 31);
980   assert(d < k);
981 
982   j = 0;
983   i = d;
984   aux = (uint64_t) bv[i];
985   while (i < k-1) {
986     i ++;
987     aux |= ((uint64_t) bv[i]) << 32;
988     bv[j] = (uint32_t) (aux >> r);
989     aux >>= 32;
990     j ++;
991   }
992   aux |= ((uint64_t) 0xffffffff) << 32;
993   bv[j] = (uint32_t) (aux >> r);
994   while (j < k-1) {
995     j ++;
996     bv[j] = 0xffffffff;
997   }
998 }
999 
1000 
1001 /*
1002  * Shift by m bits to the right, padding with b
1003  * - n = size of bv in bits
1004  * - m must be between 0 and n
1005  * - b = false ==> padding with 0, otherwise padding with 1
1006  */
bvconst_shift_right(uint32_t * bv,uint32_t n,uint32_t m,bool b)1007 void bvconst_shift_right(uint32_t *bv, uint32_t n, uint32_t m, bool b) {
1008   uint32_t k, d, r, s;
1009   assert(0 < n && m <= n);
1010 
1011   if (m == 0) return;
1012 
1013   // special case: n == m
1014   k = (n + 31) >> 5;  // bv contains k 32-bit words
1015   if (m == n) {
1016     if (b) {
1017       bvconst_set_minus_one(bv, k);
1018     } else {
1019       bvconst_clear(bv, k);
1020     }
1021     return;
1022   }
1023 
1024   d = m >> 5;
1025   r = m & 0x1f;
1026   s = n & 0x1f;
1027   if (b) {
1028     if (s > 0) {
1029       // bvconst_shift_right requires bv to be 32k words
1030       // we force the (32-s) high order bits of bv[k-1] to 1
1031       bv[k-1] |= (((uint32_t) 0xffffffff) << s);
1032     }
1033     bvconst_shift_right1(bv, k, d, r);
1034   } else {
1035     if (s > 0) {
1036       // force the high-order bits to 0
1037       bv[k-1] &= ~(((uint32_t) 0xffffffff) << s);
1038     }
1039     bvconst_shift_right0(bv, k, d, r);
1040   }
1041 }
1042 
1043 
1044 /*
1045  * Convert constant c into a shift amount between 0 and n
1046  * - n = number of bits of c
1047  * - if c's value is more than n, return n.
1048  *   otherwise return c's value
1049  */
bvshift_amount(uint32_t * c,uint32_t n)1050 static uint32_t bvshift_amount(uint32_t *c, uint32_t n) {
1051   uint32_t k, i, s;
1052 
1053   assert(n > 0);
1054 
1055   k = (n + 31) >> 5;     // number of words in c
1056   s = bvconst_get32(c);  // s = lower order word of c
1057 
1058   // check that the high order words are zero
1059   for (i=1; i<k; i++) {
1060     if (c[i] != 0) return n;
1061   }
1062 
1063   return (s < n) ? s : n;
1064 }
1065 
1066 
1067 /*
1068  * Logical shift left: (a << b)
1069  * - store the result in *bv and normalize
1070  * - n = number of bits in a and b
1071  */
bvconst_lshl(uint32_t * bv,uint32_t * a,uint32_t * b,uint32_t n)1072 void bvconst_lshl(uint32_t *bv, uint32_t *a, uint32_t *b, uint32_t n) {
1073   uint32_t k, s;
1074 
1075   s = bvshift_amount(b, n);
1076   assert(0 <= s && s <= n);
1077   k = (n + 31) >> 5;
1078   bvconst_set(bv, k, a);
1079   bvconst_shift_left(bv, n, s, false);
1080   bvconst_normalize(bv, n);
1081 }
1082 
1083 // in-place variant: bv := bv << b
bvconst_lshl_inplace(uint32_t * bv,uint32_t * b,uint32_t n)1084 void bvconst_lshl_inplace(uint32_t *bv, uint32_t *b, uint32_t n) {
1085   uint32_t s;
1086 
1087   s = bvshift_amount(b, n);
1088   assert(0 <= s && s <= n);
1089   bvconst_shift_left(bv, n, s, false);
1090   bvconst_normalize(bv, n);
1091 }
1092 
1093 
1094 /*
1095  * Logical shift right: (a >> b)
1096  * - store the result in *bv and normalize
1097  * - n = number of bits in a and b
1098  */
bvconst_lshr(uint32_t * bv,uint32_t * a,uint32_t * b,uint32_t n)1099 void bvconst_lshr(uint32_t *bv, uint32_t *a, uint32_t *b, uint32_t n) {
1100   uint32_t k, s;
1101 
1102   s = bvshift_amount(b, n);
1103   assert(0 <= s && s <= n);
1104   k = (n + 31) >> 5;
1105   bvconst_set(bv, k, a);
1106   bvconst_shift_right(bv, n, s, false);
1107   bvconst_normalize(bv, n);
1108 }
1109 
1110 // in-place variant: bv := bv >> b
bvconst_lshr_inplace(uint32_t * bv,uint32_t * b,uint32_t n)1111 void bvconst_lshr_inplace(uint32_t *bv, uint32_t *b, uint32_t n) {
1112   uint32_t s;
1113 
1114   s = bvshift_amount(b, n);
1115   assert(0 <= s && s <= n);
1116   bvconst_shift_right(bv, n, s, false);
1117   bvconst_normalize(bv, n);
1118 }
1119 
1120 /*
1121  * Arithmetic shift right: (a >> b)
1122  * - store the result in *bv and normalize
1123  * - n = number of bits in a and b
1124  */
bvconst_ashr(uint32_t * bv,uint32_t * a,uint32_t * b,uint32_t n)1125 void bvconst_ashr(uint32_t *bv, uint32_t *a, uint32_t *b, uint32_t n) {
1126   uint32_t k, s;
1127   bool sign;
1128 
1129   s = bvshift_amount(b, n);
1130   assert(0 <= s && s <= n);
1131   k = (n + 31) >> 5;
1132   bvconst_set(bv, k, a);
1133   sign = bvconst_tst_bit(a, n-1);
1134   bvconst_shift_right(bv, n, s, sign);
1135   bvconst_normalize(bv, n);
1136 }
1137 
1138 // in-place variant: bv := bv >> b
bvconst_ashr_inplace(uint32_t * bv,uint32_t * b,uint32_t n)1139 void bvconst_ashr_inplace(uint32_t *bv, uint32_t *b, uint32_t n) {
1140   uint32_t s;
1141   bool sign;
1142 
1143   s = bvshift_amount(b, n);
1144   assert(0 <= s && s <= n);
1145   sign = bvconst_tst_bit(bv, n-1);
1146   bvconst_shift_right(bv, n, s, sign);
1147   bvconst_normalize(bv, n);
1148 }
1149 
1150 
1151 /*
1152  * Extract subvector a[l..(h-1)] and store it in bv
1153  * - bv must have size k = ceil((h - l) / 32)
1154  */
bvconst_extract(uint32_t * bv,uint32_t * a,uint32_t l,uint32_t h)1155 void bvconst_extract(uint32_t *bv, uint32_t *a, uint32_t l, uint32_t h) {
1156   uint32_t r, i, to_move;
1157   uint64_t aux;
1158 
1159   assert(l < h);
1160 
1161   i = l >> 5;       // first word to read
1162   r = l & 0x1f;     // shift
1163   to_move = h - l;  // number of bits to move
1164 
1165   aux = (uint64_t) a[i];
1166   while (to_move > 32) {
1167     i ++;
1168     aux |= ((uint64_t) a[i]) << 32;
1169     *bv = (uint32_t)(aux >> r);
1170     bv ++;
1171     aux >>= 32;
1172     to_move -= 32;
1173   }
1174 
1175   assert(to_move > 0);
1176   if (r > 0) {
1177     aux |= ((uint64_t) a[i+1]) << 32;
1178   }
1179   *bv = (uint32_t)(aux >> r);
1180 }
1181 
1182 
1183 /*
1184  * Concat(a, b): (a | (b << n))
1185  * - a has n bits
1186  * - b has m bits
1187  * - result is stored in bv
1188  * - bv must be large enough for n + m bits.
1189  * - low-order bits = a
1190  * - high-order bits = b
1191  */
bvconst_concat(uint32_t * bv,uint32_t * a,uint32_t n,uint32_t * b,uint32_t m)1192 void bvconst_concat(uint32_t *bv, uint32_t *a, uint32_t n, uint32_t *b, uint32_t m) {
1193   uint32_t r, s, t, i, x, y;
1194 
1195   //  assert(n > 0);
1196   //  assert(m > 0);
1197 
1198   i = n >> 5;
1199   r = n & 0x1f;
1200   while (i > 0) {
1201     *bv ++ = *a ++;
1202     i --;
1203   }
1204 
1205   if (r == 0) {
1206     i = (m + 31) >> 5;
1207     do {
1208       * bv ++ = *b ++;
1209       i --;
1210     } while (i > 0);
1211 
1212   } else {
1213     x = (*a) & (bitp(r) - 1);
1214     t = 32 - r;
1215     i = m >> 5;
1216     while (i > 0) {
1217       y = *b++;
1218       *bv ++ = (y << r) | x;
1219       x = y >> t;
1220       i --;
1221     }
1222     s = m & 0x1f;
1223     if (s == 0) {
1224       *bv = x;
1225     } else {
1226       y = *b;
1227       *bv ++ = (y << r) | x;
1228       if (s > t) {
1229         *bv = y >> t;
1230       }
1231     }
1232   }
1233 }
1234 
1235 
1236 /*
1237  * Arithmetic operations
1238  */
1239 // bv := - bv
bvconst_negate(uint32_t * bv,uint32_t k)1240 void bvconst_negate(uint32_t *bv, uint32_t k) {
1241   int64_t aux;
1242 
1243   assert (k > 0);
1244   aux = 0;
1245   do {
1246     aux -= *bv;
1247     *bv = (uint32_t) aux;
1248     aux >>= 32;
1249     bv ++;
1250     k --;
1251   } while (k > 0);
1252 }
1253 
1254 // bv := - a
bvconst_negate2(uint32_t * bv,uint32_t k,uint32_t * a)1255 void bvconst_negate2(uint32_t *bv, uint32_t k, uint32_t *a) {
1256   int64_t aux;
1257 
1258   assert (k > 0);
1259   aux = 0;
1260   do {
1261     aux -= *a;
1262     *bv = (uint32_t) aux;
1263     aux >>= 32;
1264     bv ++;
1265     a ++;
1266     k --;
1267   } while (k > 0);
1268 }
1269 
1270 // bv := bv + a
bvconst_add(uint32_t * bv,uint32_t k,uint32_t * a)1271 void bvconst_add(uint32_t *bv, uint32_t k, uint32_t *a) {
1272   uint64_t aux;
1273 
1274   assert(k>0);
1275   aux = 0;
1276   do {
1277     aux += ((uint64_t) (*bv)) + ((uint64_t) (*a));
1278     *bv = (uint32_t) aux;
1279     aux >>= 32;
1280     a ++;
1281     bv ++;
1282     k --;
1283   } while (k > 0);
1284 }
1285 
1286 // bv := bv + 1
bvconst_add_one(uint32_t * bv,uint32_t k)1287 void bvconst_add_one(uint32_t *bv, uint32_t k) {
1288   uint64_t aux;
1289 
1290   assert(k>0);
1291   aux = 1;
1292   do {
1293     aux += (uint64_t) (*bv);
1294     *bv = (uint32_t) aux;
1295     aux >>= 32;
1296     bv ++;
1297     k --;
1298   } while (k > 0);
1299 }
1300 
1301 // bv := a1 + a2
bvconst_add2(uint32_t * bv,uint32_t k,uint32_t * a1,uint32_t * a2)1302 void bvconst_add2(uint32_t *bv, uint32_t k, uint32_t *a1, uint32_t *a2) {
1303   uint64_t aux;
1304 
1305   assert(k>0);
1306   aux = 0;
1307   do {
1308     aux += ((uint64_t) (*a1)) + ((uint64_t) (*a2));
1309     *bv = (uint32_t) aux;
1310     aux >>= 32;
1311     a1 ++;
1312     a2 ++;
1313     bv ++;
1314     k --;
1315   } while (k > 0);
1316 }
1317 
1318 
1319 // bv := bv - a
bvconst_sub(uint32_t * bv,uint32_t k,uint32_t * a)1320 void bvconst_sub(uint32_t *bv, uint32_t k, uint32_t *a) {
1321   int64_t aux;
1322 
1323   assert(k > 0);
1324   aux = 0;
1325   do {
1326     aux += ((uint64_t) (*bv)) - ((uint64_t) (*a));
1327     *bv = (uint32_t) aux;
1328     aux >>= 32;
1329     a ++;
1330     bv ++;
1331     k --;
1332   } while (k > 0);
1333 }
1334 
1335 // bv := bv - 1
bvconst_sub_one(uint32_t * bv,uint32_t k)1336 void bvconst_sub_one(uint32_t *bv, uint32_t k) {
1337   int64_t aux;
1338 
1339   assert(k > 0);
1340   aux = -1;
1341   do {
1342     aux += (uint64_t) (*bv);
1343     *bv = (uint32_t) aux;
1344     aux >>= 32;
1345     bv ++;
1346     k --;
1347   } while (k > 0);
1348 }
1349 
1350 // bv := a1 - a2
bvconst_sub2(uint32_t * bv,uint32_t k,uint32_t * a1,uint32_t * a2)1351 void bvconst_sub2(uint32_t *bv, uint32_t k, uint32_t *a1, uint32_t *a2) {
1352   int64_t aux;
1353 
1354   assert(k > 0);
1355   aux = 0;
1356   do {
1357     aux += ((uint64_t) (*a1)) - ((uint64_t) (*a2));
1358     * bv = (uint32_t) aux;
1359     aux >>= 32;
1360     a1 ++;
1361     a2 ++;
1362     bv ++;
1363     k --;
1364   } while (k > 0);
1365 }
1366 
1367 
1368 // bv := bv + a1 * a2
bvconst_addmul(uint32_t * bv,uint32_t k,uint32_t * a1,uint32_t * a2)1369 void bvconst_addmul(uint32_t *bv, uint32_t k, uint32_t *a1, uint32_t *a2) {
1370   uint64_t aux, f;
1371   uint32_t j;
1372 
1373   assert (k > 0);
1374   do {
1375     f = ((uint64_t) (*a1));
1376     aux = 0;
1377     for (j=0; j<k; j++) {
1378       aux += bv[j] + f * a2[j];
1379       bv[j] = (uint32_t) aux;
1380       aux >>= 32;
1381     }
1382     bv ++;
1383     a1 ++;
1384     k --;
1385   } while (k > 0);
1386 }
1387 
1388 // bv := bv - a1 * a2
bvconst_submul(uint32_t * bv,uint32_t k,uint32_t * a1,uint32_t * a2)1389 void bvconst_submul(uint32_t *bv, uint32_t k, uint32_t *a1, uint32_t *a2) {
1390   assert (k > 0);
1391 
1392   /*
1393    * We can't adapt addmul in a simple way
1394    * since bv[i] - f * a2[j] may not fit in a signed 64bit integer
1395    * So we compute:
1396    *   bv_1 = (2^32k - 1) - bv = bitwise complement of bv
1397    *   bv_2 = (bv_1 + a1 * a2) mod (2^32k)
1398    *   bv_3 = (2^32k - 1) - bv_2 = bitwise complement of bv_2
1399    * then
1400    *   bv_2 = (a1 * a2 - bv - 1) mod (2^32k)
1401    *   bv_3 = (bv - a1 * a1) mod (2^32k) = what we want
1402    */
1403   bvconst_complement(bv, k);
1404   bvconst_addmul(bv, k, a1, a2);
1405   bvconst_complement(bv, k);
1406 }
1407 
1408 // bv := a1 * a2
bvconst_mul2(uint32_t * bv,uint32_t k,uint32_t * a1,uint32_t * a2)1409 void bvconst_mul2(uint32_t *bv, uint32_t k, uint32_t *a1, uint32_t *a2) {
1410   bvconst_clear(bv, k);
1411   bvconst_addmul(bv, k, a1, a2);
1412 }
1413 
1414 // bv := bv * a
bvconst_mul(uint32_t * bv,uint32_t k,uint32_t * a)1415 void bvconst_mul(uint32_t *bv, uint32_t k, uint32_t *a) {
1416   uint32_t tmp[k]; // Warning: this is a GCC extension of C. Dangerous is k is large
1417 
1418   bvconst_set(tmp, k, bv);
1419   bvconst_clear(bv, k);
1420   bvconst_addmul(bv, k, tmp, a);
1421 }
1422 
1423 
1424 /*
1425  * Multiplication by a power
1426  * - compute bv * a ^ d and store the result in bv
1427  * - bv and a must be distinct and have word size = k
1428  */
bvconst_mulpower(uint32_t * bv,uint32_t k,uint32_t * a,uint32_t d)1429 void bvconst_mulpower(uint32_t *bv, uint32_t k, uint32_t *a, uint32_t d) {
1430   uint32_t tmp[k];
1431   uint32_t sq[k];
1432 
1433   // deal with small exponents d
1434   if (d == 0) return; // we take 0^0 = 1
1435 
1436   if (d == 1) {
1437     bvconst_mul(bv, k, a);
1438     return;
1439   }
1440 
1441   if (d == 2) {
1442     bvconst_mul2(tmp, k, a, a);
1443     bvconst_mul(bv, k, tmp);
1444     return;
1445   }
1446 
1447   // general case: d>=3
1448   bvconst_set(tmp, k, a);
1449   for (;;) {
1450     assert(d > 0);
1451     if ((d & 1) != 0) {
1452       bvconst_mul(bv, k, tmp);
1453     }
1454     d >>= 1;
1455     if (d == 0) return;
1456     bvconst_mul2(sq, k, tmp, tmp);
1457     bvconst_set(tmp, k, sq);
1458   }
1459 }
1460 
1461 
1462 
1463 /*
1464  * COMPARISONS
1465  */
1466 
1467 /*
1468  * Tests and comparisons.
1469  * - k = word size
1470  * - bv must be normalized
1471  */
bvconst_is_zero(const uint32_t * bv,uint32_t k)1472 bool bvconst_is_zero(const uint32_t *bv, uint32_t k) {
1473   assert(k > 0);
1474   do {
1475     if (*bv != 0) return false;
1476     bv ++;
1477     k --;
1478   } while (k > 0);
1479 
1480   return true;
1481 }
1482 
bvconst_is_one(const uint32_t * bv,uint32_t k)1483 bool bvconst_is_one(const uint32_t *bv, uint32_t k) {
1484   assert(k > 0);
1485 
1486   if (*bv != 1) return false;
1487   bv ++;
1488   k --;
1489 
1490   while (k > 0) {
1491     if (*bv != 0) return false;
1492     bv++;
1493     k--;
1494   }
1495 
1496   return true;
1497 }
1498 
1499 
1500 /*
1501  * Check whether bv is equal to -1 (i.e., 0b11...1)
1502  * - n = number of bits in bv
1503  * - bv must be normalized
1504  */
bvconst_is_minus_one(const uint32_t * bv,uint32_t n)1505 bool bvconst_is_minus_one(const uint32_t *bv, uint32_t n) {
1506   uint32_t k, r;
1507 
1508   assert(n > 0);
1509   k = n >> 5; // n mod 32
1510   r = n & 31; // n rem 32
1511   assert(n == 32 * k + r && r < 32);
1512 
1513   while (k > 0) {
1514     if (*bv != ~((uint32_t) 0)) {
1515       return false;
1516     }
1517     bv ++;
1518     k --;
1519   }
1520 
1521   return r == 0 || *bv == (~((uint32_t) 0)) >> (32 - r);
1522 }
1523 
1524 
1525 /*
1526  * Check whether bv is 0b100..0 (smallest negative signed integer)
1527  * - n = number of bits in bv
1528  * - bv must be normalized.
1529  */
bvconst_is_min_signed(const uint32_t * bv,uint32_t n)1530 bool bvconst_is_min_signed(const uint32_t *bv, uint32_t n) {
1531   uint32_t k, r;
1532 
1533   assert(n > 0);
1534   k = (n + 31) >> 5;  // number of 32bit words
1535   r = n & 31;         // n rem 31
1536   if (r == 0) {
1537     r = 32;
1538   }
1539 
1540   // all low-order words must be 0
1541   while (k > 1) {
1542     if (*bv != 0) {
1543       return false;
1544     }
1545     bv ++;
1546     k --;
1547   }
1548 
1549   // the high-order word must be 0b10000000 >> (32 - r)
1550   assert(0 <= 32 - r && 32 - r <= 31);
1551   return *bv == ((uint32_t) INT32_MIN) >> (32 - r);
1552 }
1553 
1554 
1555 /*
1556  * Check whether bv is 0b0111..1 (largest positive signed integer)
1557  * - n = number of bits in bv
1558  * - bv must be normalized
1559  */
bvconst_is_max_signed(const uint32_t * bv,uint32_t n)1560 bool bvconst_is_max_signed(const uint32_t *bv, uint32_t n) {
1561   uint32_t k, r;
1562 
1563   assert(n > 0);
1564   k = (n + 31) >> 5;  // number of 32bit words
1565   r = n & 31;         // n rem 31
1566   if (r == 0) {
1567     r = 32;
1568   }
1569 
1570   // all low-order words must be 0xFFFF
1571   while (k > 1) {
1572     if (*bv != ~((uint32_t) 0)) {
1573       return false;
1574     }
1575     bv ++;
1576     k --;
1577   }
1578 
1579   // the high-order word must be 0b011111111 >> (32 - r)
1580   assert(0 <= 32 - r && 32 - r <= 31);
1581   return *bv == ((uint32_t) INT32_MAX) >> (32 - r);
1582 }
1583 
1584 
1585 
1586 
1587 /*
1588  * Check whether bv is a power of 2 and if so return n such
1589  * that bv = 2^n. Otherwise, return -1.
1590  * - bv must be normalized
1591  * - k = number of words in bv
1592  */
bvconst_is_power_of_two(const uint32_t * bv,uint32_t k)1593 int32_t bvconst_is_power_of_two(const uint32_t *bv, uint32_t k) {
1594   uint32_t u;
1595   int32_t n, p;
1596 
1597   assert(k > 0);
1598   n = 0;
1599 
1600   // skip low-order words that are zero
1601   while (*bv == 0) {
1602     bv ++;
1603     n += 32;
1604     k --;
1605 
1606     if (k == 0) return -1; // 0 not a power of 2
1607   }
1608 
1609   u = *bv;
1610   assert(k > 0 && u > 0);
1611 
1612   p = ctz(u);   // p = index of the rightmost 1 in u
1613   assert(0 <= p && p <= 31);
1614   if (u != (1<<p)) {
1615     // u is not equal to 2^p so bv is not a power of 2
1616     return -1;
1617   }
1618 
1619   // check whether the remaining words of bv are all zero
1620   bv ++;
1621   k --;
1622   while (k > 0) {
1623     if (*bv != 0) return -1;
1624     bv ++;
1625     k --;
1626   }
1627 
1628   return n + p;
1629 }
1630 
1631 /*
1632  * Number of trailing zeros, k = number of words in bv, bv must be normalized
1633  * - return the index of the lowest-order bit of bv that's not 0
1634  * - returns -1 if bv is zero
1635  */
bvconst_ctz(const uint32_t * bv,uint32_t k)1636 int32_t bvconst_ctz(const uint32_t *bv, uint32_t k) {
1637   uint32_t u;
1638   int32_t n, p;
1639 
1640   assert(k > 0);
1641   n = 0;
1642 
1643   // skip low-order words that are zero
1644   while (*bv == 0) {
1645     bv ++;
1646     n += 32;
1647     k --;
1648     if (k == 0) return -1; // 0 gives -1
1649   }
1650 
1651   u = *bv;
1652   assert(k > 0 && u > 0);
1653 
1654   p = ctz(u);   // p = index of the rightmost 1 in u
1655   return n + p;
1656 }
1657 
1658 
1659 
1660 /*
1661  * Check whether a and b are equal, both must be of size k.
1662  *
1663  * Both a and b must be normalized (modulo 2^n) if n = bitsize
1664  * of a and b, and n != 32 k
1665  */
bvconst_eq(const uint32_t * a,const uint32_t * b,uint32_t k)1666 bool bvconst_eq(const uint32_t *a, const uint32_t *b, uint32_t k) {
1667   assert(k > 0);
1668   do {
1669     if (*a != *b) return false;
1670     a ++;
1671     b ++;
1672     k --;
1673   } while (k > 0);
1674 
1675   return true;
1676 }
1677 
1678 
1679 /*
1680  * Check whether a <= b (interpreted as unsigned numbers)
1681  * - both a and b must be of bitsize n (with n>0) and normalized
1682  */
bvconst_le(const uint32_t * a,const uint32_t * b,uint32_t n)1683 bool bvconst_le(const uint32_t *a, const uint32_t *b, uint32_t n) {
1684   uint32_t k;
1685 
1686   assert(n > 0);
1687   n --;
1688   k = n >> 5; // k = n/52
1689   while (k > 0 && a[k] == b[k]) k--;
1690 
1691   return a[k] <= b[k];
1692 }
1693 
1694 
1695 /*
1696  * Check whether a <= b (interpreted as signed numbers)
1697  * - both a and b must be of bitsize n (with n>0) and normalized
1698  */
bvconst_sle(const uint32_t * a,const uint32_t * b,uint32_t n)1699 bool bvconst_sle(const uint32_t *a, const uint32_t *b, uint32_t n) {
1700   uint32_t k, mask, sign_a, sign_b;
1701 
1702   assert(n > 0);
1703 
1704   n --;
1705   k = n >> 5;
1706   mask = bitp(n & 0x1f);
1707 
1708   sign_a = a[k] & mask;
1709   sign_b = b[k] & mask;
1710 
1711   if (sign_a == sign_b) {
1712     while (k > 0 && a[k] == b[k]) k--;
1713     return a[k] <= b[k];
1714   } else {
1715     return sign_a > sign_b; //i.e. sign(a) = 1 and sign(b) = 0
1716   }
1717 }
1718 
1719 
1720 
1721 
1722 
1723 
1724 /*
1725  * Compute hash of constant a. n = bitsize
1726  * - a must be normalized for this to be compatible with eq
1727  */
bvconst_hash(const uint32_t * a,uint32_t n)1728 uint32_t bvconst_hash(const uint32_t *a, uint32_t n) {
1729   uint32_t k;
1730 
1731   assert(bvconst_is_normalized(a, n));
1732 
1733   k = (n + 31) >> 5;
1734   return jenkins_hash_array(a, k, 0x741a8d7a + n);
1735 }
1736 
1737 
1738 
1739 
1740 /*
1741  * DIVISIONS
1742  */
1743 
1744 /*
1745  * Initialize z and copy a into z
1746  * - a is interpreted as an unsigned n-bit integer
1747  * - a must be normalized modulo (2^n)
1748  */
unsigned_bv2mpz(mpz_t z,uint32_t n,const uint32_t * a)1749 static inline void unsigned_bv2mpz(mpz_t z, uint32_t n, const uint32_t *a) {
1750   uint32_t k;
1751 
1752   k = (n + 31) >> 5;
1753   mpz_init2(z, n);
1754   bvconst_get_mpz(a, k, z);
1755 }
1756 
1757 /*
1758  * Initialize z and copy a into z
1759  * - a is interpreted as a signed n-bit integer
1760  * - a must be normalized modulo (2^n)
1761  */
signed_bv2mpz(mpz_t z,uint32_t n,const uint32_t * a)1762 static void signed_bv2mpz(mpz_t z, uint32_t n, const uint32_t *a) {
1763   mpz_t aux;
1764   uint32_t k;
1765 
1766   assert(n > 0);
1767 
1768   k = (n + 31) >> 5;
1769   mpz_init2(z, n);
1770   bvconst_get_mpz(a, k, z);
1771   if (bvconst_tst_bit(a, n-1)) { // sign bit = 1
1772     mpz_init_set_si(aux, -1);
1773     mpz_mul_2exp(aux, aux, n);
1774     mpz_add(z, z, aux);
1775     mpz_clear(aux);
1776   }
1777 }
1778 
1779 /*
1780  * Copy z into a (like set_mpz but don't use an auxiliary mpz)
1781  * - n = number of bits
1782  * FIX THIS: THE SIGN OF Z IS IGNORED
1783  */
mpz2bv(uint32_t * a,uint32_t n,mpz_t z)1784 static void mpz2bv(uint32_t *a, uint32_t n, mpz_t z) {
1785   mpz_t aux;
1786   uint32_t k;
1787 
1788   if (mpz_sgn(z) < 0) {
1789     // add 2^n to z
1790     mpz_init_set_ui(aux, 1);
1791     mpz_mul_2exp(aux, aux, n);
1792     mpz_add(z, z, aux);
1793     mpz_clear(aux);
1794   }
1795 
1796   k = (n + 31) >> 5;
1797   do {
1798     *a++ = (uint32_t) mpz_get_ui(z);
1799     mpz_fdiv_q_2exp(z, z, 32);
1800     k --;
1801   } while (k > 0);
1802 }
1803 
1804 
1805 /*
1806  * Division and remainder
1807  * - a1 and a2 must be normalized
1808  * - n = number of bits in a1 and a2 (also in bv)
1809  * - a2 must be nonzero
1810  * - udiv2: bv := a1/a2
1811  */
1812 // unsigned division
bvconst_udiv2(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1813 void bvconst_udiv2(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1814   mpz_t z1, z2;
1815 
1816   assert(n > 0);
1817   if (n <= 32) {
1818     *bv = (*a1) / (*a2);
1819   } else {
1820     unsigned_bv2mpz(z1, n, a1);
1821     unsigned_bv2mpz(z2, n, a2);
1822     mpz_fdiv_q(z1, z1, z2); // z1 := z1/z2
1823     mpz2bv(bv, n, z1);
1824     mpz_clear(z1);
1825     mpz_clear(z2);
1826   }
1827 }
1828 
1829 // unsigned remainder
bvconst_urem2(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1830 void bvconst_urem2(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1831   mpz_t z1, z2;
1832 
1833   assert(n > 0);
1834   if (n <= 32) {
1835     *bv = (*a1) % (*a2);
1836   } else {
1837     unsigned_bv2mpz(z1, n, a1);
1838     unsigned_bv2mpz(z2, n, a2);
1839     mpz_fdiv_r(z1, z1, z2); // z1 := z1 mod z2
1840     mpz2bv(bv, n, z1);
1841     mpz_clear(z1);
1842     mpz_clear(z2);
1843   }
1844 }
1845 
1846 // signed division
bvconst_sdiv2(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1847 void bvconst_sdiv2(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1848   mpz_t z1, z2;
1849 
1850   signed_bv2mpz(z1, n, a1);
1851   signed_bv2mpz(z2, n, a2);
1852   mpz_tdiv_q(z1, z1, z2); // z1 := z1 div z2, rounding towards 0
1853   mpz2bv(bv, n, z1);
1854   mpz_clear(z1);
1855   mpz_clear(z2);
1856 }
1857 
1858 // signed rem
bvconst_srem2(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1859 void bvconst_srem2(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1860   mpz_t z1, z2;
1861 
1862   signed_bv2mpz(z1, n, a1);
1863   signed_bv2mpz(z2, n, a2);
1864   mpz_tdiv_r(z1, z1, z2); // z1 := remainder of z1 div z2, rounding towards 0
1865   mpz2bv(bv, n, z1);
1866   mpz_clear(z1);
1867   mpz_clear(z2);
1868 }
1869 
1870 // signed division
bvconst_smod2(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1871 void bvconst_smod2(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1872   mpz_t z1, z2;
1873 
1874   signed_bv2mpz(z1, n, a1);
1875   signed_bv2mpz(z2, n, a2);
1876   mpz_fdiv_r(z1, z1, z2); // z1 := remainder of z1 div z2, rounding towards - infinity
1877   mpz2bv(bv, n, z1);
1878   mpz_clear(z1);
1879   mpz_clear(z2);
1880 }
1881 
1882 
1883 /*
1884  * Variants: give a default value if the divisor (a2) is zero
1885  */
bvconst_udiv2z(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1886 void bvconst_udiv2z(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1887   uint32_t k;
1888 
1889   k = (n + 31) >> 5;
1890   if (bvconst_is_zero(a2, k)) {
1891     bvconst_set_minus_one(bv, k);
1892   } else {
1893     bvconst_udiv2(bv, n, a1,  a2);
1894   }
1895 }
1896 
bvconst_urem2z(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1897 void bvconst_urem2z(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1898   uint32_t k;
1899 
1900   k = (n + 31) >> 5;
1901   if (bvconst_is_zero(a2, k)) {
1902     bvconst_set(bv, k, a1);
1903   } else {
1904     bvconst_urem2(bv, n, a1,  a2);
1905   }
1906 }
1907 
bvconst_sdiv2z(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1908 void bvconst_sdiv2z(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1909   uint32_t k;
1910 
1911   k = (n + 31) >> 5;
1912   if (bvconst_is_zero(a2, k)) {
1913     if (bvconst_tst_bit(a1, n-1)) {
1914       // a1 is negative: quotient = 0b0000...01
1915       bvconst_set_one(bv, k);
1916     } else {
1917       // quotient = 0b1111..1
1918       bvconst_set_minus_one(bv, k);
1919     }
1920   } else {
1921     bvconst_sdiv2(bv, n, a1, a2);
1922   }
1923 }
1924 
bvconst_srem2z(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1925 void bvconst_srem2z(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1926   uint32_t k;
1927 
1928   k = (n + 31) >> 5;
1929   if (bvconst_is_zero(a2, k)) {
1930     bvconst_set(bv, k, a1);
1931   } else {
1932     bvconst_srem2(bv, n, a1, a2);
1933   }
1934 }
1935 
bvconst_smod2z(uint32_t * bv,uint32_t n,uint32_t * a1,uint32_t * a2)1936 void bvconst_smod2z(uint32_t *bv, uint32_t n, uint32_t *a1, uint32_t *a2) {
1937   uint32_t k;
1938 
1939   k = (n + 31) >> 5;
1940   if (bvconst_is_zero(a2, k)) {
1941     bvconst_set(bv, k, a1);
1942   } else {
1943     bvconst_smod2(bv, n, a1, a2);
1944   }
1945 }
1946 
1947