Coverage Report

Created: 2019-04-21 11:35

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_int_sioimath.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2015 INRIA Paris-Rocquencourt
3
 *
4
 * Use of this software is governed by the MIT license
5
 *
6
 * Written by Michael Kruse, INRIA Paris-Rocquencourt,
7
 * Domaine de Voluceau, Rocquenqourt, B.P. 105,
8
 * 78153 Le Chesnay Cedex France
9
 */
10
#ifndef ISL_INT_SIOIMATH_H
11
#define ISL_INT_SIOIMATH_H
12
13
#include <inttypes.h>
14
#include <limits.h>
15
#include <stdint.h>
16
#include <stdlib.h>
17
18
#include <isl_imath.h>
19
#include <isl/hash.h>
20
21
22.0M
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
22
23
/* Visual Studio before VS2015 does not support the inline keyword when
24
 * compiling in C mode because it was introduced in C99 which it does not
25
 * officially support.  Instead, it has a proprietary extension using __inline.
26
 */
27
#if defined(_MSC_VER) && (_MSC_VER < 1900)
28
#define inline __inline
29
#endif
30
31
/* The type to represent integers optimized for small values. It is either a
32
 * pointer to an mp_int ( = mpz_t*; big representation) or an int32_t (small
33
 * represenation) with a discriminator at the least significant bit. In big
34
 * representation it will be always zero because of heap alignment. It is set
35
 * to 1 for small representation and use the 32 most significant bits for the
36
 * int32_t.
37
 *
38
 * Structure on 64 bit machines, with 8-byte aligment (3 bits):
39
 *
40
 * Big representation:
41
 * MSB                                                          LSB
42
 * |------------------------------------------------------------000
43
 * |                            mpz_t*                            |
44
 * |                           != NULL                            |
45
 *
46
 * Small representation:
47
 * MSB                           32                             LSB
48
 * |------------------------------|00000000000000000000000000000001
49
 * |          int32_t             |
50
 * |  2147483647 ... -2147483647  |
51
 *                                                                ^
52
 *                                                                |
53
 *                                                        discriminator bit
54
 *
55
 * On 32 bit machines isl_sioimath type is blown up to 8 bytes, i.e.
56
 * isl_sioimath is guaranteed to be at least 8 bytes. This is to ensure the
57
 * int32_t can be hidden in that type without data loss. In the future we might
58
 * optimize this to use 31 hidden bits in a 32 bit pointer. We may also use 63
59
 * bits on 64 bit machines, but this comes with the cost of additional overflow
60
 * checks because there is no standardized 128 bit integer we could expand to.
61
 *
62
 * We use native integer types and avoid union structures to avoid assumptions
63
 * on the machine's endianness.
64
 *
65
 * This implementation makes the following assumptions:
66
 * - long can represent any int32_t
67
 * - mp_small is signed long
68
 * - mp_usmall is unsigned long
69
 * - adresses returned by malloc are aligned to 2-byte boundaries (leastmost
70
 *   bit is zero)
71
 */
72
#if UINT64_MAX > UINTPTR_MAX
73
typedef uint64_t isl_sioimath;
74
#else
75
typedef uintptr_t isl_sioimath;
76
#endif
77
78
/* The negation of the smallest possible number in int32_t, INT32_MIN
79
 * (0x80000000u, -2147483648), cannot be represented in an int32_t, therefore
80
 * every operation that may produce this value needs to special-case it.
81
 * The operations are:
82
 * abs(INT32_MIN)
83
 * -INT32_MIN   (negation)
84
 * -1 * INT32_MIN (multiplication)
85
 * INT32_MIN/-1 (any division: divexact, fdiv, cdiv, tdiv)
86
 * To avoid checking these cases, we exclude INT32_MIN from small
87
 * representation.
88
 */
89
345M
#define ISL_SIOIMATH_SMALL_MIN (-INT32_MAX)
90
91
/* Largest possible number in small representation */
92
342M
#define ISL_SIOIMATH_SMALL_MAX INT32_MAX
93
94
/* Used for function parameters the function modifies. */
95
typedef isl_sioimath *isl_sioimath_ptr;
96
97
/* Used for function parameters that are read-only. */
98
typedef isl_sioimath isl_sioimath_src;
99
100
/* Return whether the argument is stored in small representation.
101
 */
102
inline int isl_sioimath_is_small(isl_sioimath val)
103
2.46G
{
104
2.46G
  return val & 0x00000001;
105
2.46G
}
106
107
/* Return whether the argument is stored in big representation.
108
 */
109
inline int isl_sioimath_is_big(isl_sioimath val)
110
696M
{
111
696M
  return !isl_sioimath_is_small(val);
112
696M
}
113
114
/* Get the number of an isl_int in small representation. Result is undefined if
115
 * val is not stored in that format.
116
 */
117
inline int32_t isl_sioimath_get_small(isl_sioimath val)
118
1.58G
{
119
1.58G
  return val >> 32;
120
1.58G
}
121
122
/* Get the number of an in isl_int in big representation. Result is undefined if
123
 * val is not stored in that format.
124
 */
125
inline mp_int isl_sioimath_get_big(isl_sioimath val)
126
147M
{
127
147M
  return (mp_int)(uintptr_t) val;
128
147M
}
129
130
/* Return 1 if val is stored in small representation and store its value to
131
 * small. We rely on the compiler to optimize the isl_sioimath_get_small such
132
 * that the shift is moved into the branch that executes in case of small
133
 * representation. If there is no such branch, then a single shift is still
134
 * cheaper than introducing branching code.
135
 */
136
inline int isl_sioimath_decode_small(isl_sioimath val, int32_t *small)
137
1.30G
{
138
1.30G
  *small = isl_sioimath_get_small(val);
139
1.30G
  return isl_sioimath_is_small(val);
140
1.30G
}
141
142
/* Return 1 if val is stored in big representation and store its value to big.
143
 */
144
inline int isl_sioimath_decode_big(isl_sioimath val, mp_int *big)
145
56.0M
{
146
56.0M
  *big = isl_sioimath_get_big(val);
147
56.0M
  return isl_sioimath_is_big(val);
148
56.0M
}
149
150
/* Encode a small representation into an isl_int.
151
 */
152
inline isl_sioimath isl_sioimath_encode_small(int32_t val)
153
778M
{
154
778M
  return ((isl_sioimath) val) << 32 | 0x00000001;
155
778M
}
156
157
/* Encode a big representation.
158
 */
159
inline isl_sioimath isl_sioimath_encode_big(mp_int val)
160
18.0M
{
161
18.0M
  return (isl_sioimath)(uintptr_t) val;
162
18.0M
}
163
164
/* A common situation is to call an IMath function with at least one argument
165
 * that is currently in small representation or an integer parameter, i.e. a big
166
 * representation of the same number is required. Promoting the original
167
 * argument comes with multiple problems, such as modifying a read-only
168
 * argument, the responsibility of deallocation and the execution cost. Instead,
169
 * we make a copy by 'faking' the IMath internal structure.
170
 *
171
 * We reserve the maximum number of required digits on the stack to avoid heap
172
 * allocations.
173
 *
174
 * mp_digit can be uint32_t or uint16_t. This code must work for little and big
175
 * endian digits. The structure for an uint64_t argument and 32-bit mp_digits is
176
 * sketched below.
177
 *
178
 * |----------------------------|
179
 *            uint64_t
180
 *
181
 * |-------------||-------------|
182
 *    mp_digit        mp_digit
183
 *    digits[1]       digits[0]
184
 * Most sig digit  Least sig digit
185
 */
186
typedef struct {
187
  mpz_t big;
188
  mp_digit digits[(sizeof(uintmax_t) + sizeof(mp_digit) - 1) /
189
                  sizeof(mp_digit)];
190
} isl_sioimath_scratchspace_t;
191
192
/* Convert a native integer to IMath's digit representation. A native integer
193
 * might be big- or little endian, but IMath always stores the least significant
194
 * digit in the lowest array indices.  memcpy therefore is not possible.
195
 *
196
 * We also have to consider that long and mp_digit can be of different sizes,
197
 * depending on the compiler (LP64, LLP64) and IMath's USE_64BIT_WORDS. This
198
 * macro should work for all of them.
199
 *
200
 * "used" is set to the number of written digits. It must be minimal (IMath
201
 * checks zeroness using the used field), but always at least one.  Also note
202
 * that the result of num>>(sizeof(num)*CHAR_BIT) is undefined.
203
 */
204
#define ISL_SIOIMATH_TO_DIGITS(num, digits, used)                              \
205
30.7M
  do {                                                                   \
206
30.7M
    int i = 0;                                                     \
207
31.4M
    do {                                                           \
208
31.4M
      (digits)[i] =                                          \
209
31.4M
          ((num) >> (sizeof(mp_digit) * CHAR_BIT * i));      \
210
31.4M
      i += 1;                                                \
211
31.4M
      if (i >= (sizeof(num) + sizeof(mp_digit) - 1) /        \
212
31.4M
                   sizeof(mp_digit))                         \
213
31.4M
        
break30.6M
; \
214
31.4M
      
if (824k
((num) >> (sizeof(mp_digit) * CHAR_BIT * i)) == 0824k
) \
215
824k
        
break99.5k
; \
216
824k
    } while (
1725k
); \
217
30.7M
    (used) = i;                                                    \
218
30.7M
  } while (0)
219
220
inline void isl_siomath_uint32_to_digits(uint32_t num, mp_digit *digits,
221
  mp_size *used)
222
29.9M
{
223
29.9M
  ISL_SIOIMATH_TO_DIGITS(num, digits, *used);
224
29.9M
}
225
226
inline void isl_siomath_ulong_to_digits(unsigned long num, mp_digit *digits,
227
  mp_size *used)
228
3.20k
{
229
3.20k
  ISL_SIOIMATH_TO_DIGITS(num, digits, *used);
230
3.20k
}
231
232
inline void isl_siomath_uint64_to_digits(uint64_t num, mp_digit *digits,
233
  mp_size *used)
234
821k
{
235
821k
  ISL_SIOIMATH_TO_DIGITS(num, digits, *used);
236
821k
}
237
238
/* Get the IMath representation of an isl_int without modifying it.
239
 * For the case it is not in big representation yet, pass some scratch space we
240
 * can use to store the big representation in.
241
 * In order to avoid requiring init and free on the scratch space, we directly
242
 * modify the internal representation.
243
 *
244
 * The name derives from its indented use: getting the big representation of an
245
 * input (src) argument.
246
 */
247
inline mp_int isl_sioimath_bigarg_src(isl_sioimath arg,
248
  isl_sioimath_scratchspace_t *scratch)
249
56.0M
{
250
56.0M
  mp_int big;
251
56.0M
  int32_t small;
252
56.0M
  uint32_t num;
253
56.0M
254
56.0M
  if (isl_sioimath_decode_big(arg, &big))
255
34.8M
    return big;
256
21.2M
257
21.2M
  small = isl_sioimath_get_small(arg);
258
21.2M
  scratch->big.digits = scratch->digits;
259
21.2M
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
260
21.2M
  if (small >= 0) {
261
19.5M
    scratch->big.sign = MP_ZPOS;
262
19.5M
    num = small;
263
19.5M
  } else {
264
1.63M
    scratch->big.sign = MP_NEG;
265
1.63M
    num = -small;
266
1.63M
  }
267
21.2M
268
21.2M
  isl_siomath_uint32_to_digits(num, scratch->digits, &scratch->big.used);
269
21.2M
  return &scratch->big;
270
21.2M
}
271
272
/* Create a temporary IMath mp_int for a signed long.
273
 */
274
inline mp_int isl_sioimath_siarg_src(signed long arg,
275
  isl_sioimath_scratchspace_t *scratch)
276
0
{
277
0
  unsigned long num;
278
0
279
0
  scratch->big.digits = scratch->digits;
280
0
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
281
0
  if (arg >= 0) {
282
0
    scratch->big.sign = MP_ZPOS;
283
0
    num = arg;
284
0
  } else {
285
0
    scratch->big.sign = MP_NEG;
286
0
    num = (arg == LONG_MIN) ? ((unsigned long) LONG_MAX) + 1 : -arg;
287
0
  }
288
0
289
0
  isl_siomath_ulong_to_digits(num, scratch->digits, &scratch->big.used);
290
0
  return &scratch->big;
291
0
}
292
293
/* Create a temporary IMath mp_int for an int64_t.
294
 */
295
inline mp_int isl_sioimath_si64arg_src(int64_t arg,
296
  isl_sioimath_scratchspace_t *scratch)
297
821k
{
298
821k
  uint64_t num;
299
821k
300
821k
  scratch->big.digits = scratch->digits;
301
821k
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
302
821k
  if (arg >= 0) {
303
447k
    scratch->big.sign = MP_ZPOS;
304
447k
    num = arg;
305
447k
  } else {
306
373k
    scratch->big.sign = MP_NEG;
307
373k
    num = (arg == INT64_MIN) ? 
((uint64_t) INT64_MAX) + 10
: -arg;
308
373k
  }
309
821k
310
821k
  isl_siomath_uint64_to_digits(num, scratch->digits, &scratch->big.used);
311
821k
  return &scratch->big;
312
821k
}
313
314
/* Create a temporary IMath mp_int for an unsigned long.
315
 */
316
inline mp_int isl_sioimath_uiarg_src(unsigned long arg,
317
  isl_sioimath_scratchspace_t *scratch)
318
3.20k
{
319
3.20k
  scratch->big.digits = scratch->digits;
320
3.20k
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
321
3.20k
  scratch->big.sign = MP_ZPOS;
322
3.20k
323
3.20k
  isl_siomath_ulong_to_digits(arg, scratch->digits, &scratch->big.used);
324
3.20k
  return &scratch->big;
325
3.20k
}
326
327
/* Ensure big representation. Does not preserve the current number.
328
 * Callers may use the fact that the value _is_ preserved if the presentation
329
 * was big before.
330
 */
331
inline mp_int isl_sioimath_reinit_big(isl_sioimath_ptr ptr)
332
31.3M
{
333
31.3M
  if (isl_sioimath_is_small(*ptr))
334
18.0M
    *ptr = isl_sioimath_encode_big(mp_int_alloc());
335
31.3M
  return isl_sioimath_get_big(*ptr);
336
31.3M
}
337
338
/* Set ptr to a number in small representation.
339
 */
340
inline void isl_sioimath_set_small(isl_sioimath_ptr ptr, int32_t val)
341
640M
{
342
640M
  if (isl_sioimath_is_big(*ptr))
343
13.9M
    mp_int_free(isl_sioimath_get_big(*ptr));
344
640M
  *ptr = isl_sioimath_encode_small(val);
345
640M
}
346
347
/* Set ptr to val, choosing small representation if possible.
348
 */
349
inline void isl_sioimath_set_int32(isl_sioimath_ptr ptr, int32_t val)
350
0
{
351
0
  if (ISL_SIOIMATH_SMALL_MIN <= val && val <= ISL_SIOIMATH_SMALL_MAX) {
352
0
    isl_sioimath_set_small(ptr, val);
353
0
    return;
354
0
  }
355
0
356
0
  mp_int_init_value(isl_sioimath_reinit_big(ptr), val);
357
0
}
358
359
/* Assign an int64_t number using small representation if possible.
360
 */
361
inline void isl_sioimath_set_int64(isl_sioimath_ptr ptr, int64_t val)
362
273M
{
363
273M
  if (ISL_SIOIMATH_SMALL_MIN <= val && 
val <= 273M
ISL_SIOIMATH_SMALL_MAX273M
) {
364
272M
    isl_sioimath_set_small(ptr, val);
365
272M
    return;
366
272M
  }
367
821k
368
821k
  isl_sioimath_scratchspace_t scratch;
369
821k
  mp_int_copy(isl_sioimath_si64arg_src(val, &scratch),
370
821k
      isl_sioimath_reinit_big(ptr));
371
821k
}
372
373
/* Convert to big representation while preserving the current number.
374
 */
375
inline void isl_sioimath_promote(isl_sioimath_ptr dst)
376
0
{
377
0
  int32_t small;
378
0
379
0
  if (isl_sioimath_is_big(*dst))
380
0
    return;
381
0
382
0
  small = isl_sioimath_get_small(*dst);
383
0
  mp_int_set_value(isl_sioimath_reinit_big(dst), small);
384
0
}
385
386
/* Convert to small representation while preserving the current number. Does
387
 * nothing if dst doesn't fit small representation.
388
 */
389
inline void isl_sioimath_try_demote(isl_sioimath_ptr dst)
390
25.7M
{
391
25.7M
  mp_small small;
392
25.7M
393
25.7M
  if (isl_sioimath_is_small(*dst))
394
0
    return;
395
25.7M
396
25.7M
  if (mp_int_to_int(isl_sioimath_get_big(*dst), &small) != MP_OK)
397
6.23M
    return;
398
19.5M
399
19.5M
  if (ISL_SIOIMATH_SMALL_MIN <= small && 
small <= 16.8M
ISL_SIOIMATH_SMALL_MAX16.8M
)
400
19.5M
    
isl_sioimath_set_small(dst, small)12.1M
;
401
19.5M
}
402
403
/* Initialize an isl_int. The implicit value is 0 in small representation.
404
 */
405
inline void isl_sioimath_init(isl_sioimath_ptr dst)
406
137M
{
407
137M
  *dst = isl_sioimath_encode_small(0);
408
137M
}
409
410
/* Free the resources taken by an isl_int.
411
 */
412
inline void isl_sioimath_clear(isl_sioimath_ptr dst)
413
137M
{
414
137M
  if (isl_sioimath_is_small(*dst))
415
133M
    return;
416
4.05M
417
4.05M
  mp_int_free(isl_sioimath_get_big(*dst));
418
4.05M
}
419
420
/* Copy the value of one isl_int to another.
421
 */
422
inline void isl_sioimath_set(isl_sioimath_ptr dst, isl_sioimath_src val)
423
192M
{
424
192M
  if (isl_sioimath_is_small(val)) {
425
188M
    isl_sioimath_set_small(dst, isl_sioimath_get_small(val));
426
188M
    return;
427
188M
  }
428
3.82M
429
3.82M
  mp_int_copy(isl_sioimath_get_big(val), isl_sioimath_reinit_big(dst));
430
3.82M
}
431
432
/* Store a signed long into an isl_int.
433
 */
434
inline void isl_sioimath_set_si(isl_sioimath_ptr dst, long val)
435
52.4M
{
436
52.4M
  if (ISL_SIOIMATH_SMALL_MIN <= val && val <= ISL_SIOIMATH_SMALL_MAX) {
437
52.4M
    isl_sioimath_set_small(dst, val);
438
52.4M
    return;
439
52.4M
  }
440
0
441
0
  mp_int_set_value(isl_sioimath_reinit_big(dst), val);
442
0
}
443
444
/* Store an unsigned long into an isl_int.
445
 */
446
inline void isl_sioimath_set_ui(isl_sioimath_ptr dst, unsigned long val)
447
1.98k
{
448
1.98k
  if (val <= ISL_SIOIMATH_SMALL_MAX) {
449
1.98k
    isl_sioimath_set_small(dst, val);
450
1.98k
    return;
451
1.98k
  }
452
0
453
0
  mp_int_set_uvalue(isl_sioimath_reinit_big(dst), val);
454
0
}
455
456
/* Return whether a number can be represented by a signed long.
457
 */
458
inline int isl_sioimath_fits_slong(isl_sioimath_src val)
459
665
{
460
665
  mp_small dummy;
461
665
462
665
  if (isl_sioimath_is_small(val))
463
665
    return 1;
464
0
465
0
  return mp_int_to_int(isl_sioimath_get_big(val), &dummy) == MP_OK;
466
0
}
467
468
/* Return a number as signed long. Result is undefined if the number cannot be
469
 * represented as long.
470
 */
471
inline long isl_sioimath_get_si(isl_sioimath_src val)
472
1.31k
{
473
1.31k
  mp_small result;
474
1.31k
475
1.31k
  if (isl_sioimath_is_small(val))
476
1.31k
    return isl_sioimath_get_small(val);
477
0
478
0
  mp_int_to_int(isl_sioimath_get_big(val), &result);
479
0
  return result;
480
0
}
481
482
/* Return whether a number can be represented as unsigned long.
483
 */
484
inline int isl_sioimath_fits_ulong(isl_sioimath_src val)
485
1.90k
{
486
1.90k
  mp_usmall dummy;
487
1.90k
488
1.90k
  if (isl_sioimath_is_small(val))
489
1.90k
    return isl_sioimath_get_small(val) >= 0;
490
0
491
0
  return mp_int_to_uint(isl_sioimath_get_big(val), &dummy) == MP_OK;
492
0
}
493
494
/* Return a number as unsigned long. Result is undefined if the number cannot be
495
 * represented as unsigned long.
496
 */
497
inline unsigned long isl_sioimath_get_ui(isl_sioimath_src val)
498
1.90k
{
499
1.90k
  mp_usmall result;
500
1.90k
501
1.90k
  if (isl_sioimath_is_small(val))
502
1.90k
    return isl_sioimath_get_small(val);
503
0
504
0
  mp_int_to_uint(isl_sioimath_get_big(val), &result);
505
0
  return result;
506
0
}
507
508
/* Return a number as floating point value.
509
 */
510
inline double isl_sioimath_get_d(isl_sioimath_src val)
511
0
{
512
0
  mp_int big;
513
0
  double result = 0;
514
0
  int i;
515
0
516
0
  if (isl_sioimath_is_small(val))
517
0
    return isl_sioimath_get_small(val);
518
0
519
0
  big = isl_sioimath_get_big(val);
520
0
  for (i = 0; i < big->used; ++i)
521
0
    result = result * (double) ((uintmax_t) MP_DIGIT_MAX + 1) +
522
0
             (double) big->digits[i];
523
0
524
0
  if (big->sign == MP_NEG)
525
0
    result = -result;
526
0
527
0
  return result;
528
0
}
529
530
/* Format a number as decimal string.
531
 *
532
 * The largest possible string from small representation is 12 characters
533
 * ("-2147483647").
534
 */
535
inline char *isl_sioimath_get_str(isl_sioimath_src val)
536
4.57k
{
537
4.57k
  char *result;
538
4.57k
539
4.57k
  if (isl_sioimath_is_small(val)) {
540
4.00k
    result = malloc(12);
541
4.00k
    snprintf(result, 12, "%" PRIi32, isl_sioimath_get_small(val));
542
4.00k
    return result;
543
4.00k
  }
544
571
545
571
  return impz_get_str(NULL, 10, isl_sioimath_get_big(val));
546
571
}
547
548
/* Return the absolute value.
549
 */
550
inline void isl_sioimath_abs(isl_sioimath_ptr dst, isl_sioimath_src arg)
551
10.8M
{
552
10.8M
  if (isl_sioimath_is_small(arg)) {
553
10.4M
    isl_sioimath_set_small(dst, labs(isl_sioimath_get_small(arg)));
554
10.4M
    return;
555
10.4M
  }
556
431k
557
431k
  mp_int_abs(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
558
431k
}
559
560
/* Return the negation of a number.
561
 */
562
inline void isl_sioimath_neg(isl_sioimath_ptr dst, isl_sioimath_src arg)
563
62.8M
{
564
62.8M
  if (isl_sioimath_is_small(arg)) {
565
62.2M
    isl_sioimath_set_small(dst, -isl_sioimath_get_small(arg));
566
62.2M
    return;
567
62.2M
  }
568
553k
569
553k
  mp_int_neg(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
570
553k
}
571
572
/* Swap two isl_ints.
573
 *
574
 * isl_sioimath can be copied bytewise; nothing depends on its address. It can
575
 * also be stored in a CPU register.
576
 */
577
inline void isl_sioimath_swap(isl_sioimath_ptr lhs, isl_sioimath_ptr rhs)
578
23.8M
{
579
23.8M
  isl_sioimath tmp = *lhs;
580
23.8M
  *lhs = *rhs;
581
23.8M
  *rhs = tmp;
582
23.8M
}
583
584
/* Add an unsigned long to the number.
585
 *
586
 * On LP64 unsigned long exceeds the range of an int64_t, therefore we check in
587
 * advance whether small representation possibly overflows.
588
 */
589
inline void isl_sioimath_add_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
590
  unsigned long rhs)
591
138k
{
592
138k
  int32_t smalllhs;
593
138k
  isl_sioimath_scratchspace_t lhsscratch;
594
138k
595
138k
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
596
138k
      
(rhs <= (uint64_t) INT64_MAX - (uint64_t) 118k
ISL_SIOIMATH_SMALL_MAX118k
)) {
597
118k
    isl_sioimath_set_int64(dst, (int64_t) smalllhs + rhs);
598
118k
    return;
599
118k
  }
600
20.6k
601
20.6k
  impz_add_ui(isl_sioimath_reinit_big(dst),
602
20.6k
      isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
603
20.6k
  isl_sioimath_try_demote(dst);
604
20.6k
}
605
606
/* Subtract an unsigned long.
607
 *
608
 * On LP64 unsigned long exceeds the range of an int64_t.  If
609
 * ISL_SIOIMATH_SMALL_MIN-rhs>=INT64_MIN we can do the calculation using int64_t
610
 * without risking an overflow.
611
 */
612
inline void isl_sioimath_sub_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
613
        unsigned long rhs)
614
164k
{
615
164k
  int32_t smalllhs;
616
164k
  isl_sioimath_scratchspace_t lhsscratch;
617
164k
618
164k
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
619
164k
      
(rhs < (uint64_t) INT64_MIN - (uint64_t) 136k
ISL_SIOIMATH_SMALL_MIN136k
)) {
620
136k
    isl_sioimath_set_int64(dst, (int64_t) smalllhs - rhs);
621
136k
    return;
622
136k
  }
623
28.5k
624
28.5k
  impz_sub_ui(isl_sioimath_reinit_big(dst),
625
28.5k
      isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
626
28.5k
  isl_sioimath_try_demote(dst);
627
28.5k
}
628
629
/* Sum of two isl_ints.
630
 */
631
inline void isl_sioimath_add(isl_sioimath_ptr dst, isl_sioimath_src lhs,
632
  isl_sioimath_src rhs)
633
106M
{
634
106M
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
635
106M
  int32_t smalllhs, smallrhs;
636
106M
637
106M
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
638
106M
      
isl_sioimath_decode_small(rhs, &smallrhs)101M
) {
639
100M
    isl_sioimath_set_int64(
640
100M
        dst, (int64_t) smalllhs + (int64_t) smallrhs);
641
100M
    return;
642
100M
  }
643
6.79M
644
6.79M
  mp_int_add(isl_sioimath_bigarg_src(lhs, &scratchlhs),
645
6.79M
      isl_sioimath_bigarg_src(rhs, &scratchrhs),
646
6.79M
      isl_sioimath_reinit_big(dst));
647
6.79M
  isl_sioimath_try_demote(dst);
648
6.79M
}
649
650
/* Subtract two isl_ints.
651
 */
652
inline void isl_sioimath_sub(isl_sioimath_ptr dst, isl_sioimath_src lhs,
653
  isl_sioimath_src rhs)
654
3.60M
{
655
3.60M
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
656
3.60M
  int32_t smalllhs, smallrhs;
657
3.60M
658
3.60M
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
659
3.60M
      
isl_sioimath_decode_small(rhs, &smallrhs)3.31M
) {
660
3.21M
    isl_sioimath_set_int64(
661
3.21M
        dst, (int64_t) smalllhs - (int64_t) smallrhs);
662
3.21M
    return;
663
3.21M
  }
664
384k
665
384k
  mp_int_sub(isl_sioimath_bigarg_src(lhs, &scratchlhs),
666
384k
      isl_sioimath_bigarg_src(rhs, &scratchrhs),
667
384k
      isl_sioimath_reinit_big(dst));
668
384k
  isl_sioimath_try_demote(dst);
669
384k
}
670
671
/* Multiply two isl_ints.
672
 */
673
inline void isl_sioimath_mul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
674
  isl_sioimath_src rhs)
675
180M
{
676
180M
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
677
180M
  int32_t smalllhs, smallrhs;
678
180M
679
180M
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
680
180M
      
isl_sioimath_decode_small(rhs, &smallrhs)171M
) {
681
167M
    isl_sioimath_set_int64(
682
167M
        dst, (int64_t) smalllhs * (int64_t) smallrhs);
683
167M
    return;
684
167M
  }
685
13.0M
686
13.0M
  mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
687
13.0M
      isl_sioimath_bigarg_src(rhs, &scratchrhs),
688
13.0M
      isl_sioimath_reinit_big(dst));
689
13.0M
  isl_sioimath_try_demote(dst);
690
13.0M
}
691
692
/* Shift lhs by rhs bits to the left and store the result in dst. Effectively,
693
 * this operation computes 'lhs * 2^rhs'.
694
 */
695
inline void isl_sioimath_mul_2exp(isl_sioimath_ptr dst, isl_sioimath lhs,
696
  unsigned long rhs)
697
1.90k
{
698
1.90k
  isl_sioimath_scratchspace_t scratchlhs;
699
1.90k
  int32_t smalllhs;
700
1.90k
701
1.90k
  if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs <= 32ul)) {
702
715
    isl_sioimath_set_int64(dst, ((int64_t) smalllhs) << rhs);
703
715
    return;
704
715
  }
705
1.19k
706
1.19k
  mp_int_mul_pow2(isl_sioimath_bigarg_src(lhs, &scratchlhs), rhs,
707
1.19k
      isl_sioimath_reinit_big(dst));
708
1.19k
}
709
710
/* Multiply an isl_int and a signed long.
711
 */
712
inline void isl_sioimath_mul_si(isl_sioimath_ptr dst, isl_sioimath lhs,
713
  signed long rhs)
714
268
{
715
268
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
716
268
  int32_t smalllhs;
717
268
718
268
  if (isl_sioimath_decode_small(lhs, &smalllhs) && (rhs > LONG_MIN) &&
719
268
      (labs(rhs) <= UINT32_MAX)) {
720
268
    isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
721
268
    return;
722
268
  }
723
0
724
0
  mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
725
0
      isl_sioimath_siarg_src(rhs, &scratchrhs),
726
0
      isl_sioimath_reinit_big(dst));
727
0
  isl_sioimath_try_demote(dst);
728
0
}
729
730
/* Multiply an isl_int and an unsigned long.
731
 */
732
inline void isl_sioimath_mul_ui(isl_sioimath_ptr dst, isl_sioimath lhs,
733
  unsigned long rhs)
734
54.6k
{
735
54.6k
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
736
54.6k
  int32_t smalllhs;
737
54.6k
738
54.6k
  if (isl_sioimath_decode_small(lhs, &smalllhs) && 
(rhs <= UINT32_MAX)51.6k
) {
739
51.6k
    isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
740
51.6k
    return;
741
51.6k
  }
742
3.04k
743
3.04k
  mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
744
3.04k
      isl_sioimath_uiarg_src(rhs, &scratchrhs),
745
3.04k
      isl_sioimath_reinit_big(dst));
746
3.04k
  isl_sioimath_try_demote(dst);
747
3.04k
}
748
749
/* Compute the power of an isl_int to an unsigned long.
750
 * Always let IMath do it; the result is unlikely to be small except in some
751
 * special cases.
752
 * Note: 0^0 == 1
753
 */
754
inline void isl_sioimath_pow_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
755
  unsigned long rhs)
756
0
{
757
0
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
758
0
  int32_t smalllhs;
759
0
760
0
  switch (rhs) {
761
0
  case 0:
762
0
    isl_sioimath_set_small(dst, 1);
763
0
    return;
764
0
  case 1:
765
0
    isl_sioimath_set(dst, lhs);
766
0
    return;
767
0
  case 2:
768
0
    isl_sioimath_mul(dst, lhs, lhs);
769
0
    return;
770
0
  }
771
0
772
0
  if (isl_sioimath_decode_small(lhs, &smalllhs)) {
773
0
    switch (smalllhs) {
774
0
    case 0:
775
0
      isl_sioimath_set_small(dst, 0);
776
0
      return;
777
0
    case 1:
778
0
      isl_sioimath_set_small(dst, 1);
779
0
      return;
780
0
    case 2:
781
0
      isl_sioimath_set_small(dst, 1);
782
0
      isl_sioimath_mul_2exp(dst, *dst, rhs);
783
0
      return;
784
0
    default:
785
0
      if ((MP_SMALL_MIN <= rhs) && (rhs <= MP_SMALL_MAX)) {
786
0
        mp_int_expt_value(smalllhs, rhs,
787
0
            isl_sioimath_reinit_big(dst));
788
0
        isl_sioimath_try_demote(dst);
789
0
        return;
790
0
      }
791
0
    }
792
0
  }
793
0
794
0
  mp_int_expt_full(isl_sioimath_bigarg_src(lhs, &scratchlhs),
795
0
      isl_sioimath_uiarg_src(rhs, &scratchrhs),
796
0
      isl_sioimath_reinit_big(dst));
797
0
  isl_sioimath_try_demote(dst);
798
0
}
799
800
/* Fused multiply-add.
801
 */
802
inline void isl_sioimath_addmul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
803
  isl_sioimath_src rhs)
804
104M
{
805
104M
  isl_sioimath tmp;
806
104M
  isl_sioimath_init(&tmp);
807
104M
  isl_sioimath_mul(&tmp, lhs, rhs);
808
104M
  isl_sioimath_add(dst, *dst, tmp);
809
104M
  isl_sioimath_clear(&tmp);
810
104M
}
811
812
/* Fused multiply-add with an unsigned long.
813
 */
814
inline void isl_sioimath_addmul_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
815
  unsigned long rhs)
816
1.90k
{
817
1.90k
  isl_sioimath tmp;
818
1.90k
  isl_sioimath_init(&tmp);
819
1.90k
  isl_sioimath_mul_ui(&tmp, lhs, rhs);
820
1.90k
  isl_sioimath_add(dst, *dst, tmp);
821
1.90k
  isl_sioimath_clear(&tmp);
822
1.90k
}
823
824
/* Fused multiply-subtract.
825
 */
826
inline void isl_sioimath_submul(isl_sioimath_ptr dst, isl_sioimath_src lhs,
827
  isl_sioimath_src rhs)
828
3.48M
{
829
3.48M
  isl_sioimath tmp;
830
3.48M
  isl_sioimath_init(&tmp);
831
3.48M
  isl_sioimath_mul(&tmp, lhs, rhs);
832
3.48M
  isl_sioimath_sub(dst, *dst, tmp);
833
3.48M
  isl_sioimath_clear(&tmp);
834
3.48M
}
835
836
/* Fused multiply-add with an unsigned long.
837
 */
838
inline void isl_sioimath_submul_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
839
  unsigned long rhs)
840
39
{
841
39
  isl_sioimath tmp;
842
39
  isl_sioimath_init(&tmp);
843
39
  isl_sioimath_mul_ui(&tmp, lhs, rhs);
844
39
  isl_sioimath_sub(dst, *dst, tmp);
845
39
  isl_sioimath_clear(&tmp);
846
39
}
847
848
void isl_sioimath_gcd(isl_sioimath_ptr dst, isl_sioimath_src lhs,
849
          isl_sioimath_src rhs);
850
void isl_sioimath_lcm(isl_sioimath_ptr dst, isl_sioimath_src lhs,
851
          isl_sioimath_src rhs);
852
853
/* Divide lhs by rhs, rounding to zero (Truncate).
854
 */
855
inline void isl_sioimath_tdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
856
  isl_sioimath_src rhs)
857
34.8M
{
858
34.8M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
859
34.8M
  int32_t lhssmall, rhssmall;
860
34.8M
861
34.8M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
862
34.8M
      
isl_sioimath_decode_small(rhs, &rhssmall)32.3M
) {
863
31.7M
    isl_sioimath_set_small(dst, lhssmall / rhssmall);
864
31.7M
    return;
865
31.7M
  }
866
3.03M
867
3.03M
  mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
868
3.03M
      isl_sioimath_bigarg_src(rhs, &rhsscratch),
869
3.03M
      isl_sioimath_reinit_big(dst), NULL);
870
3.03M
  isl_sioimath_try_demote(dst);
871
3.03M
  return;
872
3.03M
}
873
874
/* Divide lhs by an unsigned long rhs, rounding to zero (Truncate).
875
 */
876
inline void isl_sioimath_tdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
877
  unsigned long rhs)
878
49.6k
{
879
49.6k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
880
49.6k
  int32_t lhssmall;
881
49.6k
882
49.6k
  if (isl_sioimath_is_small(lhs) && 
(rhs <= (unsigned long) INT32_MAX)46.2k
) {
883
46.2k
    lhssmall = isl_sioimath_get_small(lhs);
884
46.2k
    isl_sioimath_set_small(dst, lhssmall / (int32_t) rhs);
885
46.2k
    return;
886
46.2k
  }
887
3.36k
888
3.36k
  if (rhs <= MP_SMALL_MAX) {
889
3.36k
    mp_int_div_value(isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs,
890
3.36k
        isl_sioimath_reinit_big(dst), NULL);
891
3.36k
    isl_sioimath_try_demote(dst);
892
3.36k
    return;
893
3.36k
  }
894
0
895
0
  mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
896
0
      isl_sioimath_uiarg_src(rhs, &rhsscratch),
897
0
      isl_sioimath_reinit_big(dst), NULL);
898
0
  isl_sioimath_try_demote(dst);
899
0
}
900
901
/* Divide lhs by rhs, rounding to positive infinity (Ceil).
902
 */
903
inline void isl_sioimath_cdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
904
  isl_sioimath_src rhs)
905
50.6k
{
906
50.6k
  int32_t lhssmall, rhssmall;
907
50.6k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
908
50.6k
  int32_t q;
909
50.6k
910
50.6k
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
911
50.6k
      
isl_sioimath_decode_small(rhs, &rhssmall)41.9k
) {
912
41.9k
    if ((lhssmall >= 0) && 
(rhssmall >= 0)29.5k
)
913
29.5k
      q = ((int64_t) lhssmall + (int64_t) rhssmall - 1) /
914
29.5k
          rhssmall;
915
12.3k
    else if ((lhssmall < 0) && (rhssmall < 0))
916
0
      q = ((int64_t) lhssmall + (int64_t) rhssmall + 1) /
917
0
          rhssmall;
918
12.3k
    else
919
12.3k
      q = lhssmall / rhssmall;
920
41.9k
    isl_sioimath_set_small(dst, q);
921
41.9k
    return;
922
41.9k
  }
923
8.76k
924
8.76k
  impz_cdiv_q(isl_sioimath_reinit_big(dst),
925
8.76k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
926
8.76k
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
927
8.76k
  isl_sioimath_try_demote(dst);
928
8.76k
}
929
930
/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
931
 * positive infinity (Ceil).
932
 */
933
inline void isl_sioimath_cdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
934
  unsigned long rhs)
935
268
{
936
268
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
937
268
  int32_t lhssmall, q;
938
268
939
268
  if (isl_sioimath_decode_small(lhs, &lhssmall) && (rhs <= INT32_MAX)) {
940
268
    if (lhssmall >= 0)
941
57
      q = ((int64_t) lhssmall + ((int64_t) rhs - 1)) /
942
57
          (int64_t) rhs;
943
211
    else
944
211
      q = lhssmall / (int32_t) rhs;
945
268
    isl_sioimath_set_small(dst, q);
946
268
    return;
947
268
  }
948
0
949
0
  impz_cdiv_q(isl_sioimath_reinit_big(dst),
950
0
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
951
0
      isl_sioimath_uiarg_src(rhs, &rhsscratch));
952
0
  isl_sioimath_try_demote(dst);
953
0
}
954
955
/* Divide lhs by rhs, rounding to negative infinity (Floor).
956
 */
957
inline void isl_sioimath_fdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
958
  isl_sioimath_src rhs)
959
508k
{
960
508k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
961
508k
  int32_t lhssmall, rhssmall;
962
508k
  int32_t q;
963
508k
964
508k
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
965
508k
      
isl_sioimath_decode_small(rhs, &rhssmall)366k
) {
966
357k
    if ((lhssmall < 0) && 
(rhssmall >= 0)70.3k
)
967
70.3k
      q = ((int64_t) lhssmall - ((int64_t) rhssmall - 1)) /
968
70.3k
          rhssmall;
969
287k
    else if ((lhssmall >= 0) && (rhssmall < 0))
970
0
      q = ((int64_t) lhssmall - ((int64_t) rhssmall + 1)) /
971
0
          rhssmall;
972
287k
    else
973
287k
      q = lhssmall / rhssmall;
974
357k
    isl_sioimath_set_small(dst, q);
975
357k
    return;
976
357k
  }
977
150k
978
150k
  impz_fdiv_q(isl_sioimath_reinit_big(dst),
979
150k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
980
150k
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
981
150k
  isl_sioimath_try_demote(dst);
982
150k
}
983
984
/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
985
 * negative infinity (Floor).
986
 */
987
inline void isl_sioimath_fdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
988
  unsigned long rhs)
989
4.10k
{
990
4.10k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
991
4.10k
  int32_t lhssmall, q;
992
4.10k
993
4.10k
  if (isl_sioimath_decode_small(lhs, &lhssmall) && 
(rhs <= INT32_MAX)3.93k
) {
994
3.93k
    if (lhssmall >= 0)
995
3.04k
      q = (uint32_t) lhssmall / rhs;
996
895
    else
997
895
      q = ((int64_t) lhssmall - ((int64_t) rhs - 1)) /
998
895
          (int64_t) rhs;
999
3.93k
    isl_sioimath_set_small(dst, q);
1000
3.93k
    return;
1001
3.93k
  }
1002
163
1003
163
  impz_fdiv_q(isl_sioimath_reinit_big(dst),
1004
163
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
1005
163
      isl_sioimath_uiarg_src(rhs, &rhsscratch));
1006
163
  isl_sioimath_try_demote(dst);
1007
163
}
1008
1009
/* Get the remainder of: lhs divided by rhs rounded towards negative infinite
1010
 * (Floor).
1011
 */
1012
inline void isl_sioimath_fdiv_r(isl_sioimath_ptr dst, isl_sioimath_src lhs,
1013
  isl_sioimath_src rhs)
1014
34.4k
{
1015
34.4k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1016
34.4k
  int64_t lhssmall, rhssmall;
1017
34.4k
  int32_t r;
1018
34.4k
1019
34.4k
  if (isl_sioimath_is_small(lhs) && 
isl_sioimath_is_small(rhs)33.6k
) {
1020
32.2k
    lhssmall = isl_sioimath_get_small(lhs);
1021
32.2k
    rhssmall = isl_sioimath_get_small(rhs);
1022
32.2k
    r = (rhssmall + lhssmall % rhssmall) % rhssmall;
1023
32.2k
    isl_sioimath_set_small(dst, r);
1024
32.2k
    return;
1025
32.2k
  }
1026
2.19k
1027
2.19k
  impz_fdiv_r(isl_sioimath_reinit_big(dst),
1028
2.19k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
1029
2.19k
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
1030
2.19k
  isl_sioimath_try_demote(dst);
1031
2.19k
}
1032
1033
void isl_sioimath_read(isl_sioimath_ptr dst, const char *str);
1034
1035
/* Return:
1036
 *   +1 for a positive number
1037
 *   -1 for a negative number
1038
 *    0 if the number is zero
1039
 */
1040
inline int isl_sioimath_sgn(isl_sioimath_src arg)
1041
424M
{
1042
424M
  int32_t small;
1043
424M
1044
424M
  if (isl_sioimath_decode_small(arg, &small))
1045
416M
    return (small > 0) - (small < 0);
1046
8.04M
1047
8.04M
  return mp_int_compare_zero(isl_sioimath_get_big(arg));
1048
8.04M
}
1049
1050
/* Return:
1051
 *   +1 if lhs > rhs
1052
 *   -1 if lhs < rhs
1053
 *    0 if lhs = rhs
1054
 */
1055
inline int isl_sioimath_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1056
55.4M
{
1057
55.4M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1058
55.4M
  int32_t lhssmall, rhssmall;
1059
55.4M
1060
55.4M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1061
55.4M
      
isl_sioimath_decode_small(rhs, &rhssmall)55.2M
)
1062
55.2M
    return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1063
247k
1064
247k
  if (isl_sioimath_decode_small(rhs, &rhssmall))
1065
68.3k
    return mp_int_compare_value(
1066
68.3k
        isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1067
179k
1068
179k
  if (isl_sioimath_decode_small(lhs, &lhssmall))
1069
20.0k
    return -mp_int_compare_value(
1070
20.0k
               isl_sioimath_bigarg_src(rhs, &rhsscratch), lhssmall);
1071
159k
1072
159k
  return mp_int_compare(
1073
159k
      isl_sioimath_get_big(lhs), isl_sioimath_get_big(rhs));
1074
159k
}
1075
1076
/* As isl_sioimath_cmp, but with signed long rhs.
1077
 */
1078
inline int isl_sioimath_cmp_si(isl_sioimath_src lhs, signed long rhs)
1079
57.9M
{
1080
57.9M
  int32_t lhssmall;
1081
57.9M
1082
57.9M
  if (isl_sioimath_decode_small(lhs, &lhssmall))
1083
55.2M
    return (lhssmall > rhs) - (lhssmall < rhs);
1084
2.71M
1085
2.71M
  return mp_int_compare_value(isl_sioimath_get_big(lhs), rhs);
1086
2.71M
}
1087
1088
/* Return:
1089
 *   +1 if |lhs| > |rhs|
1090
 *   -1 if |lhs| < |rhs|
1091
 *    0 if |lhs| = |rhs|
1092
 */
1093
inline int isl_sioimath_abs_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1094
19.5M
{
1095
19.5M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1096
19.5M
  int32_t lhssmall, rhssmall;
1097
19.5M
1098
19.5M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1099
19.5M
      
isl_sioimath_decode_small(rhs, &rhssmall)17.5M
) {
1100
17.3M
    lhssmall = labs(lhssmall);
1101
17.3M
    rhssmall = labs(rhssmall);
1102
17.3M
    return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1103
17.3M
  }
1104
2.23M
1105
2.23M
  return mp_int_compare_unsigned(
1106
2.23M
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
1107
2.23M
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
1108
2.23M
}
1109
1110
/* Return whether lhs is divisible by rhs.
1111
 * In particular, can rhs be multiplied by some integer to result in lhs?
1112
 * If rhs is zero, then this means lhs has to be zero too.
1113
 */
1114
inline int isl_sioimath_is_divisible_by(isl_sioimath_src lhs,
1115
          isl_sioimath_src rhs)
1116
1.27M
{
1117
1.27M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1118
1.27M
  int32_t lhssmall, rhssmall;
1119
1.27M
  mpz_t rem;
1120
1.27M
  int cmp;
1121
1.27M
1122
1.27M
  if (isl_sioimath_sgn(rhs) == 0)
1123
0
    return isl_sioimath_sgn(lhs) == 0;
1124
1.27M
1125
1.27M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1126
1.27M
      
isl_sioimath_decode_small(rhs, &rhssmall)1.24M
)
1127
1.24M
    return lhssmall % rhssmall == 0;
1128
32.7k
1129
32.7k
  if (isl_sioimath_decode_small(rhs, &rhssmall))
1130
21.0k
    return mp_int_divisible_value(
1131
21.0k
        isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1132
11.7k
1133
11.7k
  mp_int_init(&rem);
1134
11.7k
  mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
1135
11.7k
      isl_sioimath_bigarg_src(rhs, &rhsscratch), NULL, &rem);
1136
11.7k
  cmp = mp_int_compare_zero(&rem);
1137
11.7k
  mp_int_clear(&rem);
1138
11.7k
  return cmp == 0;
1139
11.7k
}
1140
1141
/* Return a hash code of an isl_sioimath.
1142
 * The hash code for a number in small and big representation must be identical
1143
 * on the same machine because small representation if not obligatory if fits.
1144
 */
1145
inline uint32_t isl_sioimath_hash(isl_sioimath_src arg, uint32_t hash)
1146
8.92M
{
1147
8.92M
  int32_t small;
1148
8.92M
  int i;
1149
8.92M
  uint32_t num;
1150
8.92M
  mp_digit digits[(sizeof(uint32_t) + sizeof(mp_digit) - 1) /
1151
8.92M
                  sizeof(mp_digit)];
1152
8.92M
  mp_size used;
1153
8.92M
  const unsigned char *digitdata = (const unsigned char *) &digits;
1154
8.92M
1155
8.92M
  if (isl_sioimath_decode_small(arg, &small)) {
1156
8.72M
    if (small < 0)
1157
8.72M
      
isl_hash_byte4.44M
(hash, 0xFF);
1158
8.72M
    num = labs(small);
1159
8.72M
1160
8.72M
    isl_siomath_uint32_to_digits(num, digits, &used);
1161
43.6M
    for (i = 0; i < used * sizeof(mp_digit); 
i += 134.9M
)
1162
34.9M
      isl_hash_byte(hash, digitdata[i]);
1163
8.72M
    return hash;
1164
8.72M
  }
1165
193k
1166
193k
  return isl_imath_hash(isl_sioimath_get_big(arg), hash);
1167
193k
}
1168
1169
/* Return the number of digits in a number of the given base or more, i.e. the
1170
 * string length without sign and null terminator.
1171
 *
1172
 * Current implementation for small representation returns the maximal number
1173
 * of binary digits in that representation, which can be much larger than the
1174
 * smallest possible solution.
1175
 */
1176
inline size_t isl_sioimath_sizeinbase(isl_sioimath_src arg, int base)
1177
899
{
1178
899
  int32_t small;
1179
899
1180
899
  if (isl_sioimath_decode_small(arg, &small))
1181
877
    return sizeof(int32_t) * CHAR_BIT - 1;
1182
22
1183
22
  return impz_sizeinbase(isl_sioimath_get_big(arg), base);
1184
22
}
1185
1186
void isl_sioimath_print(FILE *out, isl_sioimath_src i, int width);
1187
void isl_sioimath_dump(isl_sioimath_src arg);
1188
1189
typedef isl_sioimath isl_int[1];
1190
34.6M
#define isl_int_init(i)     isl_sioimath_init((i))
1191
29.3M
#define isl_int_clear(i)    isl_sioimath_clear((i))
1192
1193
192M
#define isl_int_set(r, i)   isl_sioimath_set((r), *(i))
1194
53.2M
#define isl_int_set_si(r, i)    isl_sioimath_set_si((r), i)
1195
3.96k
#define isl_int_set_ui(r, i)    isl_sioimath_set_ui((r), i)
1196
665
#define isl_int_fits_slong(r)   isl_sioimath_fits_slong(*(r))
1197
1.31k
#define isl_int_get_si(r)   isl_sioimath_get_si(*(r))
1198
1.90k
#define isl_int_fits_ulong(r)   isl_sioimath_fits_ulong(*(r))
1199
1.90k
#define isl_int_get_ui(r)   isl_sioimath_get_ui(*(r))
1200
0
#define isl_int_get_d(r)    isl_sioimath_get_d(*(r))
1201
4.57k
#define isl_int_get_str(r)    isl_sioimath_get_str(*(r))
1202
20.8M
#define isl_int_abs(r, i)   isl_sioimath_abs((r), *(i))
1203
66.7M
#define isl_int_neg(r, i)   isl_sioimath_neg((r), *(i))
1204
16.6M
#define isl_int_swap(i, j)    isl_sioimath_swap((i), (j))
1205
8.36M
#define isl_int_swap_or_set(i, j) isl_sioimath_swap((i), (j))
1206
138k
#define isl_int_add_ui(r, i, j)   isl_sioimath_add_ui((r), *(i), j)
1207
165k
#define isl_int_sub_ui(r, i, j)   isl_sioimath_sub_ui((r), *(i), j)
1208
1209
1.93M
#define isl_int_add(r, i, j)    isl_sioimath_add((r), *(i), *(j))
1210
124k
#define isl_int_sub(r, i, j)    isl_sioimath_sub((r), *(i), *(j))
1211
117M
#define isl_int_mul(r, i, j)    isl_sioimath_mul((r), *(i), *(j))
1212
1.90k
#define isl_int_mul_2exp(r, i, j) isl_sioimath_mul_2exp((r), *(i), j)
1213
268
#define isl_int_mul_si(r, i, j)   isl_sioimath_mul_si((r), *(i), j)
1214
98.9k
#define isl_int_mul_ui(r, i, j)   isl_sioimath_mul_ui((r), *(i), j)
1215
0
#define isl_int_pow_ui(r, i, j)   isl_sioimath_pow_ui((r), *(i), j)
1216
104M
#define isl_int_addmul(r, i, j)   isl_sioimath_addmul((r), *(i), *(j))
1217
3.81k
#define isl_int_addmul_ui(r, i, j)  isl_sioimath_addmul_ui((r), *(i), j)
1218
3.48M
#define isl_int_submul(r, i, j)   isl_sioimath_submul((r), *(i), *(j))
1219
78
#define isl_int_submul_ui(r, i, j)  isl_sioimath_submul_ui((r), *(i), j)
1220
1221
21.3M
#define isl_int_gcd(r, i, j)    isl_sioimath_gcd((r), *(i), *(j))
1222
3.51M
#define isl_int_lcm(r, i, j)    isl_sioimath_lcm((r), *(i), *(j))
1223
34.8M
#define isl_int_divexact(r, i, j) isl_sioimath_tdiv_q((r), *(i), *(j))
1224
49.6k
#define isl_int_divexact_ui(r, i, j)  isl_sioimath_tdiv_q_ui((r), *(i), j)
1225
0
#define isl_int_tdiv_q(r, i, j)   isl_sioimath_tdiv_q((r), *(i), *(j))
1226
48.7k
#define isl_int_cdiv_q(r, i, j)   isl_sioimath_cdiv_q((r), *(i), *(j))
1227
501
#define isl_int_cdiv_q_ui(r, i, j)  isl_sioimath_cdiv_q_ui((r), *(i), j)
1228
588k
#define isl_int_fdiv_q(r, i, j)   isl_sioimath_fdiv_q((r), *(i), *(j))
1229
34.7k
#define isl_int_fdiv_r(r, i, j)   isl_sioimath_fdiv_r((r), *(i), *(j))
1230
4.10k
#define isl_int_fdiv_q_ui(r, i, j)  isl_sioimath_fdiv_q_ui((r), *(i), j)
1231
1232
9.97k
#define isl_int_read(r, s)    isl_sioimath_read((r), s)
1233
422M
#define isl_int_sgn(i)      isl_sioimath_sgn(*(i))
1234
19.3M
#define isl_int_cmp(i, j)   isl_sioimath_cmp(*(i), *(j))
1235
57.9M
#define isl_int_cmp_si(i, si)   isl_sioimath_cmp_si(*(i), si)
1236
296k
#define isl_int_eq(i, j)    (isl_sioimath_cmp(*(i), *(j)) == 0)
1237
32.9M
#define isl_int_ne(i, j)    (isl_sioimath_cmp(*(i), *(j)) != 0)
1238
2.80M
#define isl_int_lt(i, j)    (isl_sioimath_cmp(*(i), *(j)) < 0)
1239
4.21k
#define isl_int_le(i, j)    (isl_sioimath_cmp(*(i), *(j)) <= 0)
1240
82.7k
#define isl_int_gt(i, j)    (isl_sioimath_cmp(*(i), *(j)) > 0)
1241
8.14k
#define isl_int_ge(i, j)    (isl_sioimath_cmp(*(i), *(j)) >= 0)
1242
312k
#define isl_int_abs_cmp(i, j)   isl_sioimath_abs_cmp(*(i), *(j))
1243
10.4k
#define isl_int_abs_eq(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) == 0)
1244
358k
#define isl_int_abs_ne(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) != 0)
1245
18.5M
#define isl_int_abs_lt(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) < 0)
1246
149k
#define isl_int_abs_gt(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) > 0)
1247
229k
#define isl_int_abs_ge(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) >= 0)
1248
1.27M
#define isl_int_is_divisible_by(i, j) isl_sioimath_is_divisible_by(*(i), *(j))
1249
1250
8.92M
#define isl_int_hash(v, h)    isl_sioimath_hash(*(v), h)
1251
4.57k
#define isl_int_free_str(s)   free(s)
1252
0
#define isl_int_print(out, i, width)  isl_sioimath_print(out, *(i), width)
1253
1254
#endif /* ISL_INT_SIOIMATH_H */