Coverage Report

Created: 2017-04-27 19:33

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
27.5M
#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
510M
#define ISL_SIOIMATH_SMALL_MIN (-INT32_MAX)
90
91
/* Largest possible number in small representation */
92
506M
#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
3.42G
{
104
3.42G
  return val & 0x00000001;
105
3.42G
}
106
107
/* Return whether the argument is stored in big representation.
108
 */
109
inline int isl_sioimath_is_big(isl_sioimath val)
110
954M
{
111
954M
  return !isl_sioimath_is_small(val);
112
954M
}
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
2.21G
{
119
2.21G
  return val >> 32;
120
2.21G
}
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
181M
{
127
181M
  return (mp_int)(uintptr_t) val;
128
181M
}
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.85G
{
138
1.85G
  *small = isl_sioimath_get_small(val);
139
1.85G
  return isl_sioimath_is_small(val);
140
1.85G
}
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
69.1M
{
146
69.1M
  *big = isl_sioimath_get_big(val);
147
69.1M
  return isl_sioimath_is_big(val);
148
69.1M
}
149
150
/* Encode a small representation into an isl_int.
151
 */
152
inline isl_sioimath isl_sioimath_encode_small(int32_t val)
153
1.08G
{
154
1.08G
  return ((isl_sioimath) val) << 32 | 0x00000001;
155
1.08G
}
156
157
/* Encode a big representation.
158
 */
159
inline isl_sioimath isl_sioimath_encode_big(mp_int val)
160
22.0M
{
161
22.0M
  return (isl_sioimath)(uintptr_t) val;
162
22.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
42.4M
  
do 42.4M
{ \42.4M
206
42.4M
    int i = 0;                                                     \
207
43.2M
    do {                                                           \
208
43.2M
      (digits)[i] =                                          \
209
43.2M
          ((num) >> (sizeof(mp_digit) * CHAR_BIT * i));      \
210
43.2M
      i += 1;                                                \
211
43.2M
      if (i >= (sizeof(num) + sizeof(mp_digit) - 1) /        \
212
43.2M
                   sizeof(mp_digit))                         \
213
42.3M
        break;                                         \
214
958k
      
if (958k
((num) >> (sizeof(mp_digit) * CHAR_BIT * i)) == 0958k
) \
215
143k
        break;                                         \
216
815k
    } while (1);                                                   \
217
42.4M
    (used) = i;                                                    \
218
42.4M
  } while (0)
219
220
inline void isl_siomath_uint32_to_digits(uint32_t num, mp_digit *digits,
221
  mp_size *used)
222
41.5M
{
223
41.5M
  ISL_SIOIMATH_TO_DIGITS(num, digits, *used);
224
41.5M
}
225
226
inline void isl_siomath_ulong_to_digits(unsigned long num, mp_digit *digits,
227
  mp_size *used)
228
10.6k
{
229
10.6k
  ISL_SIOIMATH_TO_DIGITS(num, digits, *used);
230
10.6k
}
231
232
inline void isl_siomath_uint64_to_digits(uint64_t num, mp_digit *digits,
233
  mp_size *used)
234
947k
{
235
947k
  ISL_SIOIMATH_TO_DIGITS(num, digits, *used);
236
947k
}
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
69.1M
{
250
69.1M
  mp_int big;
251
69.1M
  int32_t small;
252
69.1M
  uint32_t num;
253
69.1M
254
69.1M
  if (isl_sioimath_decode_big(arg, &big))
255
42.5M
    return big;
256
69.1M
257
26.5M
  small = isl_sioimath_get_small(arg);
258
26.5M
  scratch->big.digits = scratch->digits;
259
26.5M
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
260
26.5M
  if (
small >= 026.5M
)
{24.4M
261
24.4M
    scratch->big.sign = MP_ZPOS;
262
24.4M
    num = small;
263
2.13M
  } else {
264
2.13M
    scratch->big.sign = MP_NEG;
265
2.13M
    num = -small;
266
2.13M
  }
267
26.5M
268
26.5M
  isl_siomath_uint32_to_digits(num, scratch->digits, &scratch->big.used);
269
26.5M
  return &scratch->big;
270
69.1M
}
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 >= 00
)
{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) + 10
:
-arg0
;
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
947k
{
298
947k
  uint64_t num;
299
947k
300
947k
  scratch->big.digits = scratch->digits;
301
947k
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
302
947k
  if (
arg >= 0947k
)
{524k
303
524k
    scratch->big.sign = MP_ZPOS;
304
524k
    num = arg;
305
423k
  } else {
306
423k
    scratch->big.sign = MP_NEG;
307
423k
    num = (arg == INT64_MIN) ? 
((uint64_t) INT64_MAX) + 10
:
-arg423k
;
308
423k
  }
309
947k
310
947k
  isl_siomath_uint64_to_digits(num, scratch->digits, &scratch->big.used);
311
947k
  return &scratch->big;
312
947k
}
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
10.6k
{
319
10.6k
  scratch->big.digits = scratch->digits;
320
10.6k
  scratch->big.alloc = ARRAY_SIZE(scratch->digits);
321
10.6k
  scratch->big.sign = MP_ZPOS;
322
10.6k
323
10.6k
  isl_siomath_ulong_to_digits(arg, scratch->digits, &scratch->big.used);
324
10.6k
  return &scratch->big;
325
10.6k
}
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
38.7M
{
333
38.7M
  if (isl_sioimath_is_small(*ptr))
334
21.9M
    *ptr = isl_sioimath_encode_big(mp_int_alloc());
335
38.7M
  return isl_sioimath_get_big(*ptr);
336
38.7M
}
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
885M
{
342
885M
  if (isl_sioimath_is_big(*ptr))
343
16.9M
    mp_int_free(isl_sioimath_get_big(*ptr));
344
885M
  *ptr = isl_sioimath_encode_small(val);
345
885M
}
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_MIN0
<= val && 0
val <= 0
ISL_SIOIMATH_SMALL_MAX0
)
{0
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
406M
{
363
406M
  if (
ISL_SIOIMATH_SMALL_MIN406M
<= val && 406M
val <= 405M
ISL_SIOIMATH_SMALL_MAX405M
)
{405M
364
405M
    isl_sioimath_set_small(ptr, val);
365
405M
    return;
366
405M
  }
367
406M
368
947k
  isl_sioimath_scratchspace_t scratch;
369
947k
  mp_int_copy(isl_sioimath_si64arg_src(val, &scratch),
370
947k
      isl_sioimath_reinit_big(ptr));
371
947k
}
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
31.7M
{
391
31.7M
  mp_small small;
392
31.7M
393
31.7M
  if (isl_sioimath_is_small(*dst))
394
0
    return;
395
31.7M
396
31.7M
  
if (31.7M
mp_int_to_int(isl_sioimath_get_big(*dst), &small) != MP_OK31.7M
)
397
8.29M
    return;
398
31.7M
399
23.4M
  
if (23.4M
ISL_SIOIMATH_SMALL_MIN23.4M
<= small && 23.4M
small <= 20.0M
ISL_SIOIMATH_SMALL_MAX20.0M
)
400
14.2M
    isl_sioimath_set_small(dst, small);
401
23.4M
}
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
204M
{
407
204M
  *dst = isl_sioimath_encode_small(0);
408
204M
}
409
410
/* Free the resources taken by an isl_int.
411
 */
412
inline void isl_sioimath_clear(isl_sioimath_ptr dst)
413
204M
{
414
204M
  if (isl_sioimath_is_small(*dst))
415
199M
    return;
416
204M
417
5.07M
  mp_int_free(isl_sioimath_get_big(*dst));
418
5.07M
}
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
224M
{
424
224M
  if (
isl_sioimath_is_small(val)224M
)
{220M
425
220M
    isl_sioimath_set_small(dst, isl_sioimath_get_small(val));
426
220M
    return;
427
220M
  }
428
224M
429
4.83M
  mp_int_copy(isl_sioimath_get_big(val), isl_sioimath_reinit_big(dst));
430
4.83M
}
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
80.4M
{
436
80.4M
  if (
ISL_SIOIMATH_SMALL_MIN80.4M
<= val && 80.4M
val <= 80.4M
ISL_SIOIMATH_SMALL_MAX80.4M
)
{80.4M
437
80.4M
    isl_sioimath_set_small(dst, val);
438
80.4M
    return;
439
80.4M
  }
440
80.4M
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
5.43k
{
448
5.43k
  if (
val <= 5.43k
ISL_SIOIMATH_SMALL_MAX5.43k
)
{5.43k
449
5.43k
    isl_sioimath_set_small(dst, val);
450
5.43k
    return;
451
5.43k
  }
452
5.43k
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
779
{
460
779
  mp_small dummy;
461
779
462
779
  if (isl_sioimath_is_small(val))
463
779
    return 1;
464
779
465
0
  return mp_int_to_int(isl_sioimath_get_big(val), &dummy) == MP_OK;
466
779
}
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.42k
{
473
1.42k
  mp_small result;
474
1.42k
475
1.42k
  if (isl_sioimath_is_small(val))
476
1.42k
    return isl_sioimath_get_small(val);
477
1.42k
478
0
  mp_int_to_int(isl_sioimath_get_big(val), &result);
479
0
  return result;
480
1.42k
}
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
5.44k
{
486
5.44k
  mp_usmall dummy;
487
5.44k
488
5.44k
  if (isl_sioimath_is_small(val))
489
5.44k
    return isl_sioimath_get_small(val) >= 0;
490
5.44k
491
0
  return mp_int_to_uint(isl_sioimath_get_big(val), &dummy) == MP_OK;
492
5.44k
}
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
5.44k
{
499
5.44k
  mp_usmall result;
500
5.44k
501
5.44k
  if (isl_sioimath_is_small(val))
502
5.44k
    return isl_sioimath_get_small(val);
503
5.44k
504
0
  mp_int_to_uint(isl_sioimath_get_big(val), &result);
505
0
  return result;
506
5.44k
}
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->used0
;
++i0
)
521
0
    
result = result * (double) ((uintmax_t) 0
MP_DIGIT_MAX0
+ 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
11.1k
{
537
11.1k
  char *result;
538
11.1k
539
11.1k
  if (
isl_sioimath_is_small(val)11.1k
)
{9.76k
540
9.76k
    result = malloc(12);
541
9.76k
    snprintf(result, 12, "%" PRIi32, isl_sioimath_get_small(val));
542
9.76k
    return result;
543
9.76k
  }
544
11.1k
545
1.35k
  return impz_get_str(NULL, 10, isl_sioimath_get_big(val));
546
11.1k
}
547
548
/* Return the absolute value.
549
 */
550
inline void isl_sioimath_abs(isl_sioimath_ptr dst, isl_sioimath_src arg)
551
17.5M
{
552
17.5M
  if (
isl_sioimath_is_small(arg)17.5M
)
{17.1M
553
17.1M
    isl_sioimath_set_small(dst, labs(isl_sioimath_get_small(arg)));
554
17.1M
    return;
555
17.1M
  }
556
17.5M
557
455k
  mp_int_abs(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
558
455k
}
559
560
/* Return the negation of a number.
561
 */
562
inline void isl_sioimath_neg(isl_sioimath_ptr dst, isl_sioimath_src arg)
563
93.6M
{
564
93.6M
  if (
isl_sioimath_is_small(arg)93.6M
)
{92.8M
565
92.8M
    isl_sioimath_set_small(dst, -isl_sioimath_get_small(arg));
566
92.8M
    return;
567
92.8M
  }
568
93.6M
569
753k
  mp_int_neg(isl_sioimath_get_big(arg), isl_sioimath_reinit_big(dst));
570
753k
}
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
33.5M
{
579
33.5M
  isl_sioimath tmp = *lhs;
580
33.5M
  *lhs = *rhs;
581
33.5M
  *rhs = tmp;
582
33.5M
}
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
306k
{
592
306k
  int32_t smalllhs;
593
306k
  isl_sioimath_scratchspace_t lhsscratch;
594
306k
595
306k
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
596
242k
      
(rhs <= (uint64_t) INT64_MAX - (uint64_t) 242k
ISL_SIOIMATH_SMALL_MAX242k
))
{242k
597
242k
    isl_sioimath_set_int64(dst, (int64_t) smalllhs + rhs);
598
242k
    return;
599
242k
  }
600
306k
601
63.9k
  impz_add_ui(isl_sioimath_reinit_big(dst),
602
63.9k
      isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
603
63.9k
  isl_sioimath_try_demote(dst);
604
63.9k
}
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
373k
{
615
373k
  int32_t smalllhs;
616
373k
  isl_sioimath_scratchspace_t lhsscratch;
617
373k
618
373k
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
619
281k
      
(rhs < (uint64_t) INT64_MIN - (uint64_t) 281k
ISL_SIOIMATH_SMALL_MIN281k
))
{281k
620
281k
    isl_sioimath_set_int64(dst, (int64_t) smalllhs - rhs);
621
281k
    return;
622
281k
  }
623
373k
624
91.2k
  impz_sub_ui(isl_sioimath_reinit_big(dst),
625
91.2k
      isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs);
626
91.2k
  isl_sioimath_try_demote(dst);
627
91.2k
}
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
158M
{
634
158M
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
635
158M
  int32_t smalllhs, smallrhs;
636
158M
637
158M
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
638
150M
      
isl_sioimath_decode_small(rhs, &smallrhs)150M
)
{148M
639
148M
    isl_sioimath_set_int64(
640
148M
        dst, (int64_t) smalllhs + (int64_t) smallrhs);
641
148M
    return;
642
148M
  }
643
158M
644
9.31M
  mp_int_add(isl_sioimath_bigarg_src(lhs, &scratchlhs),
645
9.31M
      isl_sioimath_bigarg_src(rhs, &scratchrhs),
646
9.31M
      isl_sioimath_reinit_big(dst));
647
9.31M
  isl_sioimath_try_demote(dst);
648
9.31M
}
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
5.69M
{
655
5.69M
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
656
5.69M
  int32_t smalllhs, smallrhs;
657
5.69M
658
5.69M
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
659
5.35M
      
isl_sioimath_decode_small(rhs, &smallrhs)5.35M
)
{5.24M
660
5.24M
    isl_sioimath_set_int64(
661
5.24M
        dst, (int64_t) smalllhs - (int64_t) smallrhs);
662
5.24M
    return;
663
5.24M
  }
664
5.69M
665
455k
  mp_int_sub(isl_sioimath_bigarg_src(lhs, &scratchlhs),
666
455k
      isl_sioimath_bigarg_src(rhs, &scratchrhs),
667
455k
      isl_sioimath_reinit_big(dst));
668
455k
  isl_sioimath_try_demote(dst);
669
455k
}
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
262M
{
676
262M
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
677
262M
  int32_t smalllhs, smallrhs;
678
262M
679
262M
  if (isl_sioimath_decode_small(lhs, &smalllhs) &&
680
251M
      
isl_sioimath_decode_small(rhs, &smallrhs)251M
)
{246M
681
246M
    isl_sioimath_set_int64(
682
246M
        dst, (int64_t) smalllhs * (int64_t) smallrhs);
683
246M
    return;
684
246M
  }
685
262M
686
15.8M
  mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
687
15.8M
      isl_sioimath_bigarg_src(rhs, &scratchrhs),
688
15.8M
      isl_sioimath_reinit_big(dst));
689
15.8M
  isl_sioimath_try_demote(dst);
690
15.8M
}
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
5.44k
{
698
5.44k
  isl_sioimath_scratchspace_t scratchlhs;
699
5.44k
  int32_t smalllhs;
700
5.44k
701
5.44k
  if (
isl_sioimath_decode_small(lhs, &smalllhs) && 5.44k
(rhs <= 32ul)5.44k
)
{1.58k
702
1.58k
    isl_sioimath_set_int64(dst, ((int64_t) smalllhs) << rhs);
703
1.58k
    return;
704
1.58k
  }
705
5.44k
706
3.85k
  mp_int_mul_pow2(isl_sioimath_bigarg_src(lhs, &scratchlhs), rhs,
707
3.85k
      isl_sioimath_reinit_big(dst));
708
3.85k
}
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
337
{
715
337
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
716
337
  int32_t smalllhs;
717
337
718
337
  if (
isl_sioimath_decode_small(lhs, &smalllhs) && 337
(rhs > LONG_MIN)337
&&
719
337
      
(labs(rhs) <= UINT32_MAX)337
)
{337
720
337
    isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
721
337
    return;
722
337
  }
723
337
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
146k
{
735
146k
  isl_sioimath_scratchspace_t scratchlhs, scratchrhs;
736
146k
  int32_t smalllhs;
737
146k
738
146k
  if (
isl_sioimath_decode_small(lhs, &smalllhs) && 146k
(rhs <= UINT32_MAX)137k
)
{137k
739
137k
    isl_sioimath_set_int64(dst, (int64_t) smalllhs * (int64_t) rhs);
740
137k
    return;
741
137k
  }
742
146k
743
9.22k
  mp_int_mul(isl_sioimath_bigarg_src(lhs, &scratchlhs),
744
9.22k
      isl_sioimath_uiarg_src(rhs, &scratchrhs),
745
9.22k
      isl_sioimath_reinit_big(dst));
746
9.22k
  isl_sioimath_try_demote(dst);
747
9.22k
}
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 (0
isl_sioimath_decode_small(lhs, &smalllhs)0
)
{0
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 (
(0
MP_SMALL_MIN0
<= rhs) &&
(rhs <= 0
MP_SMALL_MAX0
))
{0
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
154M
{
805
154M
  isl_sioimath tmp;
806
154M
  isl_sioimath_init(&tmp);
807
154M
  isl_sioimath_mul(&tmp, lhs, rhs);
808
154M
  isl_sioimath_add(dst, *dst, tmp);
809
154M
  isl_sioimath_clear(&tmp);
810
154M
}
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
3.31k
{
817
3.31k
  isl_sioimath tmp;
818
3.31k
  isl_sioimath_init(&tmp);
819
3.31k
  isl_sioimath_mul_ui(&tmp, lhs, rhs);
820
3.31k
  isl_sioimath_add(dst, *dst, tmp);
821
3.31k
  isl_sioimath_clear(&tmp);
822
3.31k
}
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
5.47M
{
829
5.47M
  isl_sioimath tmp;
830
5.47M
  isl_sioimath_init(&tmp);
831
5.47M
  isl_sioimath_mul(&tmp, lhs, rhs);
832
5.47M
  isl_sioimath_sub(dst, *dst, tmp);
833
5.47M
  isl_sioimath_clear(&tmp);
834
5.47M
}
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
112
{
841
112
  isl_sioimath tmp;
842
112
  isl_sioimath_init(&tmp);
843
112
  isl_sioimath_mul_ui(&tmp, lhs, rhs);
844
112
  isl_sioimath_sub(dst, *dst, tmp);
845
112
  isl_sioimath_clear(&tmp);
846
112
}
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
45.3M
{
858
45.3M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
859
45.3M
  int32_t lhssmall, rhssmall;
860
45.3M
861
45.3M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
862
42.7M
      
isl_sioimath_decode_small(rhs, &rhssmall)42.7M
)
{42.0M
863
42.0M
    isl_sioimath_set_small(dst, lhssmall / rhssmall);
864
42.0M
    return;
865
42.0M
  }
866
45.3M
867
3.29M
  mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
868
3.29M
      isl_sioimath_bigarg_src(rhs, &rhsscratch),
869
3.29M
      isl_sioimath_reinit_big(dst), NULL);
870
3.29M
  isl_sioimath_try_demote(dst);
871
3.29M
  return;
872
45.3M
}
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
135k
{
879
135k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
880
135k
  int32_t lhssmall;
881
135k
882
135k
  if (
isl_sioimath_is_small(lhs) && 135k
(rhs <= (unsigned long) INT32_MAX)125k
)
{125k
883
125k
    lhssmall = isl_sioimath_get_small(lhs);
884
125k
    isl_sioimath_set_small(dst, lhssmall / (int32_t) rhs);
885
125k
    return;
886
125k
  }
887
135k
888
9.88k
  
if (9.88k
rhs <= 9.88k
MP_SMALL_MAX9.88k
)
{9.88k
889
9.88k
    mp_int_div_value(isl_sioimath_bigarg_src(lhs, &lhsscratch), rhs,
890
9.88k
        isl_sioimath_reinit_big(dst), NULL);
891
9.88k
    isl_sioimath_try_demote(dst);
892
9.88k
    return;
893
9.88k
  }
894
9.88k
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
132k
{
906
132k
  int32_t lhssmall, rhssmall;
907
132k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
908
132k
  int32_t q;
909
132k
910
132k
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
911
103k
      
isl_sioimath_decode_small(rhs, &rhssmall)103k
)
{103k
912
103k
    if (
(lhssmall >= 0) && 103k
(rhssmall >= 0)66.2k
)
913
66.2k
      q = ((int64_t) lhssmall + (int64_t) rhssmall - 1) /
914
66.2k
          rhssmall;
915
37.2k
    else 
if (37.2k
(lhssmall < 0) && 37.2k
(rhssmall < 0)37.2k
)
916
0
      q = ((int64_t) lhssmall + (int64_t) rhssmall + 1) /
917
0
          rhssmall;
918
37.2k
    else
919
37.2k
      q = lhssmall / rhssmall;
920
103k
    isl_sioimath_set_small(dst, q);
921
103k
    return;
922
103k
  }
923
132k
924
28.9k
  impz_cdiv_q(isl_sioimath_reinit_big(dst),
925
28.9k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
926
28.9k
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
927
28.9k
  isl_sioimath_try_demote(dst);
928
28.9k
}
929
930
/* Divide lhs by rhs, rounding to negative infinity (Floor).
931
 */
932
inline void isl_sioimath_fdiv_q(isl_sioimath_ptr dst, isl_sioimath_src lhs,
933
  isl_sioimath_src rhs)
934
757k
{
935
757k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
936
757k
  int32_t lhssmall, rhssmall;
937
757k
  int32_t q;
938
757k
939
757k
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
940
592k
      
isl_sioimath_decode_small(rhs, &rhssmall)592k
)
{578k
941
578k
    if (
(lhssmall < 0) && 578k
(rhssmall >= 0)144k
)
942
144k
      q = ((int64_t) lhssmall - ((int64_t) rhssmall - 1)) /
943
144k
          rhssmall;
944
434k
    else 
if (434k
(lhssmall >= 0) && 434k
(rhssmall < 0)434k
)
945
0
      q = ((int64_t) lhssmall - ((int64_t) rhssmall + 1)) /
946
0
          rhssmall;
947
434k
    else
948
434k
      q = lhssmall / rhssmall;
949
578k
    isl_sioimath_set_small(dst, q);
950
578k
    return;
951
578k
  }
952
757k
953
178k
  impz_fdiv_q(isl_sioimath_reinit_big(dst),
954
178k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
955
178k
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
956
178k
  isl_sioimath_try_demote(dst);
957
178k
}
958
959
/* Compute the division of lhs by a rhs of type unsigned long, rounding towards
960
 * negative infinity (Floor).
961
 */
962
inline void isl_sioimath_fdiv_q_ui(isl_sioimath_ptr dst, isl_sioimath_src lhs,
963
  unsigned long rhs)
964
17.4k
{
965
17.4k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
966
17.4k
  int32_t lhssmall, q;
967
17.4k
968
17.4k
  if (
isl_sioimath_decode_small(lhs, &lhssmall) && 17.4k
(rhs <= INT32_MAX)16.0k
)
{16.0k
969
16.0k
    if (lhssmall >= 0)
970
11.7k
      q = (uint32_t) lhssmall / rhs;
971
16.0k
    else
972
4.26k
      q = ((int64_t) lhssmall - ((int64_t) rhs - 1)) /
973
4.26k
          (int64_t) rhs;
974
16.0k
    isl_sioimath_set_small(dst, q);
975
16.0k
    return;
976
16.0k
  }
977
17.4k
978
1.42k
  impz_fdiv_q(isl_sioimath_reinit_big(dst),
979
1.42k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
980
1.42k
      isl_sioimath_uiarg_src(rhs, &rhsscratch));
981
1.42k
  isl_sioimath_try_demote(dst);
982
1.42k
}
983
984
/* Get the remainder of: lhs divided by rhs rounded towards negative infinite
985
 * (Floor).
986
 */
987
inline void isl_sioimath_fdiv_r(isl_sioimath_ptr dst, isl_sioimath_src lhs,
988
  isl_sioimath_src rhs)
989
81.3k
{
990
81.3k
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
991
81.3k
  int64_t lhssmall, rhssmall;
992
81.3k
  int32_t r;
993
81.3k
994
81.3k
  if (
isl_sioimath_is_small(lhs) && 81.3k
isl_sioimath_is_small(rhs)78.9k
)
{74.6k
995
74.6k
    lhssmall = isl_sioimath_get_small(lhs);
996
74.6k
    rhssmall = isl_sioimath_get_small(rhs);
997
74.6k
    r = (rhssmall + lhssmall % rhssmall) % rhssmall;
998
74.6k
    isl_sioimath_set_small(dst, r);
999
74.6k
    return;
1000
74.6k
  }
1001
81.3k
1002
6.69k
  impz_fdiv_r(isl_sioimath_reinit_big(dst),
1003
6.69k
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
1004
6.69k
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
1005
6.69k
  isl_sioimath_try_demote(dst);
1006
6.69k
}
1007
1008
void isl_sioimath_read(isl_sioimath_ptr dst, const char *str);
1009
1010
/* Return:
1011
 *   +1 for a positive number
1012
 *   -1 for a negative number
1013
 *    0 if the number is zero
1014
 */
1015
inline int isl_sioimath_sgn(isl_sioimath_src arg)
1016
599M
{
1017
599M
  int32_t small;
1018
599M
1019
599M
  if (isl_sioimath_decode_small(arg, &small))
1020
589M
    return (small > 0) - (small < 0);
1021
599M
1022
10.3M
  return mp_int_compare_zero(isl_sioimath_get_big(arg));
1023
599M
}
1024
1025
/* Return:
1026
 *   +1 if lhs > rhs
1027
 *   -1 if lhs < rhs
1028
 *    0 if lhs = rhs
1029
 */
1030
inline int isl_sioimath_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1031
70.2M
{
1032
70.2M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1033
70.2M
  int32_t lhssmall, rhssmall;
1034
70.2M
1035
70.2M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1036
69.8M
      isl_sioimath_decode_small(rhs, &rhssmall))
1037
69.8M
    return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1038
70.2M
1039
405k
  
if (405k
isl_sioimath_decode_small(rhs, &rhssmall)405k
)
1040
127k
    return mp_int_compare_value(
1041
127k
        isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1042
405k
1043
278k
  
if (278k
isl_sioimath_decode_small(lhs, &lhssmall)278k
)
1044
31.7k
    return -mp_int_compare_value(
1045
31.7k
               isl_sioimath_bigarg_src(rhs, &rhsscratch), lhssmall);
1046
278k
1047
246k
  return mp_int_compare(
1048
246k
      isl_sioimath_get_big(lhs), isl_sioimath_get_big(rhs));
1049
278k
}
1050
1051
/* As isl_sioimath_cmp, but with signed long rhs.
1052
 */
1053
inline int isl_sioimath_cmp_si(isl_sioimath_src lhs, signed long rhs)
1054
82.4M
{
1055
82.4M
  int32_t lhssmall;
1056
82.4M
1057
82.4M
  if (isl_sioimath_decode_small(lhs, &lhssmall))
1058
79.4M
    return (lhssmall > rhs) - (lhssmall < rhs);
1059
82.4M
1060
2.97M
  return mp_int_compare_value(isl_sioimath_get_big(lhs), rhs);
1061
82.4M
}
1062
1063
/* Return:
1064
 *   +1 if |lhs| > |rhs|
1065
 *   -1 if |lhs| < |rhs|
1066
 *    0 if |lhs| = |rhs|
1067
 */
1068
inline int isl_sioimath_abs_cmp(isl_sioimath_src lhs, isl_sioimath_src rhs)
1069
27.9M
{
1070
27.9M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1071
27.9M
  int32_t lhssmall, rhssmall;
1072
27.9M
1073
27.9M
  if (isl_sioimath_decode_small(lhs, &lhssmall) &&
1074
25.4M
      
isl_sioimath_decode_small(rhs, &rhssmall)25.4M
)
{25.1M
1075
25.1M
    lhssmall = labs(lhssmall);
1076
25.1M
    rhssmall = labs(rhssmall);
1077
25.1M
    return (lhssmall > rhssmall) - (lhssmall < rhssmall);
1078
25.1M
  }
1079
27.9M
1080
2.73M
  return mp_int_compare_unsigned(
1081
2.73M
      isl_sioimath_bigarg_src(lhs, &lhsscratch),
1082
2.73M
      isl_sioimath_bigarg_src(rhs, &rhsscratch));
1083
27.9M
}
1084
1085
/* Return whether lhs is divisible by rhs.
1086
 * In particular, can rhs be multiplied by some integer to result in lhs?
1087
 * If rhs is zero, then this means lhs has to be zero too.
1088
 */
1089
inline int isl_sioimath_is_divisible_by(isl_sioimath_src lhs,
1090
          isl_sioimath_src rhs)
1091
2.03M
{
1092
2.03M
  isl_sioimath_scratchspace_t lhsscratch, rhsscratch;
1093
2.03M
  int32_t lhssmall, rhssmall;
1094
2.03M
  mpz_t rem;
1095
2.03M
  int cmp;
1096
2.03M
1097
2.03M
  if (isl_sioimath_sgn(rhs) == 0)
1098
0
    return isl_sioimath_sgn(lhs) == 0;
1099
2.03M
1100
2.03M
  
if (2.03M
isl_sioimath_decode_small(lhs, &lhssmall) &&2.03M
1101
1.97M
      isl_sioimath_decode_small(rhs, &rhssmall))
1102
1.96M
    return lhssmall % rhssmall == 0;
1103
2.03M
1104
67.4k
  
if (67.4k
isl_sioimath_decode_small(rhs, &rhssmall)67.4k
)
1105
49.4k
    return mp_int_divisible_value(
1106
49.4k
        isl_sioimath_bigarg_src(lhs, &lhsscratch), rhssmall);
1107
67.4k
1108
17.9k
  mp_int_init(&rem);
1109
17.9k
  mp_int_div(isl_sioimath_bigarg_src(lhs, &lhsscratch),
1110
17.9k
      isl_sioimath_bigarg_src(rhs, &rhsscratch), NULL, &rem);
1111
17.9k
  cmp = mp_int_compare_zero(&rem);
1112
17.9k
  mp_int_clear(&rem);
1113
17.9k
  return cmp == 0;
1114
67.4k
}
1115
1116
/* Return a hash code of an isl_sioimath.
1117
 * The hash code for a number in small and big representation must be identical
1118
 * on the same machine because small representation if not obligatory if fits.
1119
 */
1120
inline uint32_t isl_sioimath_hash(isl_sioimath_src arg, uint32_t hash)
1121
15.1M
{
1122
15.1M
  int32_t small;
1123
15.1M
  int i;
1124
15.1M
  uint32_t num;
1125
15.1M
  mp_digit digits[(sizeof(uint32_t) + sizeof(mp_digit) - 1) /
1126
15.1M
                  sizeof(mp_digit)];
1127
15.1M
  mp_size used;
1128
15.1M
  const unsigned char *digitdata = (const unsigned char *) &digits;
1129
15.1M
1130
15.1M
  if (
isl_sioimath_decode_small(arg, &small)15.1M
)
{14.9M
1131
14.9M
    if (small < 0)
1132
7.56M
      isl_hash_byte(hash, 0xFF);
1133
14.9M
    num = labs(small);
1134
14.9M
1135
14.9M
    isl_siomath_uint32_to_digits(num, digits, &used);
1136
74.5M
    for (i = 0; 
i < used * sizeof(mp_digit)74.5M
;
i += 159.6M
)
1137
59.6M
      isl_hash_byte(hash, digitdata[i]);
1138
14.9M
    return hash;
1139
14.9M
  }
1140
15.1M
1141
256k
  return isl_imath_hash(isl_sioimath_get_big(arg), hash);
1142
15.1M
}
1143
1144
/* Return the number of digits in a number of the given base or more, i.e. the
1145
 * string length without sign and null terminator.
1146
 *
1147
 * Current implementation for small representation returns the maximal number
1148
 * of binary digits in that representation, which can be much larger than the
1149
 * smallest possible solution.
1150
 */
1151
inline size_t isl_sioimath_sizeinbase(isl_sioimath_src arg, int base)
1152
1.98k
{
1153
1.98k
  int32_t small;
1154
1.98k
1155
1.98k
  if (isl_sioimath_decode_small(arg, &small))
1156
1.89k
    return sizeof(int32_t) * CHAR_BIT - 1;
1157
1.98k
1158
85
  return impz_sizeinbase(isl_sioimath_get_big(arg), base);
1159
1.98k
}
1160
1161
void isl_sioimath_print(FILE *out, isl_sioimath_src i, int width);
1162
void isl_sioimath_dump(isl_sioimath_src arg);
1163
1164
typedef isl_sioimath isl_int[1];
1165
44.7M
#define isl_int_init(i)     isl_sioimath_init((i))
1166
44.2M
#define isl_int_clear(i)    isl_sioimath_clear((i))
1167
1168
224M
#define isl_int_set(r, i)   isl_sioimath_set((r), *(i))
1169
80.7M
#define isl_int_set_si(r, i)    isl_sioimath_set_si((r), i)
1170
5.43k
#define isl_int_set_ui(r, i)    isl_sioimath_set_ui((r), i)
1171
779
#define isl_int_fits_slong(r)   isl_sioimath_fits_slong(*(r))
1172
1.42k
#define isl_int_get_si(r)   isl_sioimath_get_si(*(r))
1173
5.44k
#define isl_int_fits_ulong(r)   isl_sioimath_fits_ulong(*(r))
1174
5.44k
#define isl_int_get_ui(r)   isl_sioimath_get_ui(*(r))
1175
0
#define isl_int_get_d(r)    isl_sioimath_get_d(*(r))
1176
11.1k
#define isl_int_get_str(r)    isl_sioimath_get_str(*(r))
1177
17.5M
#define isl_int_abs(r, i)   isl_sioimath_abs((r), *(i))
1178
93.6M
#define isl_int_neg(r, i)   isl_sioimath_neg((r), *(i))
1179
21.9M
#define isl_int_swap(i, j)    isl_sioimath_swap((i), (j))
1180
12.0M
#define isl_int_swap_or_set(i, j) isl_sioimath_swap((i), (j))
1181
306k
#define isl_int_add_ui(r, i, j)   isl_sioimath_add_ui((r), *(i), j)
1182
373k
#define isl_int_sub_ui(r, i, j)   isl_sioimath_sub_ui((r), *(i), j)
1183
1184
3.38M
#define isl_int_add(r, i, j)    isl_sioimath_add((r), *(i), *(j))
1185
220k
#define isl_int_sub(r, i, j)    isl_sioimath_sub((r), *(i), *(j))
1186
102M
#define isl_int_mul(r, i, j)    isl_sioimath_mul((r), *(i), *(j))
1187
5.44k
#define isl_int_mul_2exp(r, i, j) isl_sioimath_mul_2exp((r), *(i), j)
1188
337
#define isl_int_mul_si(r, i, j)   isl_sioimath_mul_si((r), *(i), j)
1189
142k
#define isl_int_mul_ui(r, i, j)   isl_sioimath_mul_ui((r), *(i), j)
1190
0
#define isl_int_pow_ui(r, i, j)   isl_sioimath_pow_ui((r), *(i), j)
1191
165M
#define isl_int_addmul(r, i, j)   isl_sioimath_addmul((r), *(i), *(j))
1192
3.31k
#define isl_int_addmul_ui(r, i, j)  isl_sioimath_addmul_ui((r), *(i), j)
1193
5.47M
#define isl_int_submul(r, i, j)   isl_sioimath_submul((r), *(i), *(j))
1194
112
#define isl_int_submul_ui(r, i, j)  isl_sioimath_submul_ui((r), *(i), j)
1195
1196
14.1M
#define isl_int_gcd(r, i, j)    isl_sioimath_gcd((r), *(i), *(j))
1197
5.29M
#define isl_int_lcm(r, i, j)    isl_sioimath_lcm((r), *(i), *(j))
1198
45.3M
#define isl_int_divexact(r, i, j) isl_sioimath_tdiv_q((r), *(i), *(j))
1199
135k
#define isl_int_divexact_ui(r, i, j)  isl_sioimath_tdiv_q_ui((r), *(i), j)
1200
0
#define isl_int_tdiv_q(r, i, j)   isl_sioimath_tdiv_q((r), *(i), *(j))
1201
124k
#define isl_int_cdiv_q(r, i, j)   isl_sioimath_cdiv_q((r), *(i), *(j))
1202
748k
#define isl_int_fdiv_q(r, i, j)   isl_sioimath_fdiv_q((r), *(i), *(j))
1203
81.7k
#define isl_int_fdiv_r(r, i, j)   isl_sioimath_fdiv_r((r), *(i), *(j))
1204
17.4k
#define isl_int_fdiv_q_ui(r, i, j)  isl_sioimath_fdiv_q_ui((r), *(i), j)
1205
1206
9.53k
#define isl_int_read(r, s)    isl_sioimath_read((r), s)
1207
597M
#define isl_int_sgn(i)      isl_sioimath_sgn(*(i))
1208
20.5M
#define isl_int_cmp(i, j)   isl_sioimath_cmp(*(i), *(j))
1209
82.2M
#define isl_int_cmp_si(i, si)   isl_sioimath_cmp_si(*(i), si)
1210
572k
#define isl_int_eq(i, j)    (isl_sioimath_cmp(*(i), *(j)) == 0)
1211
44.7M
#define isl_int_ne(i, j)    (isl_sioimath_cmp(*(i), *(j)) != 0)
1212
4.33M
#define isl_int_lt(i, j)    (isl_sioimath_cmp(*(i), *(j)) < 0)
1213
15.9k
#define isl_int_le(i, j)    (isl_sioimath_cmp(*(i), *(j)) <= 0)
1214
82.6k
#define isl_int_gt(i, j)    (isl_sioimath_cmp(*(i), *(j)) > 0)
1215
22.6k
#define isl_int_ge(i, j)    (isl_sioimath_cmp(*(i), *(j)) >= 0)
1216
426k
#define isl_int_abs_cmp(i, j)   isl_sioimath_abs_cmp(*(i), *(j))
1217
25.8k
#define isl_int_abs_eq(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) == 0)
1218
397k
#define isl_int_abs_ne(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) != 0)
1219
26.3M
#define isl_int_abs_lt(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) < 0)
1220
299k
#define isl_int_abs_gt(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) > 0)
1221
377k
#define isl_int_abs_ge(i, j)    (isl_sioimath_abs_cmp(*(i), *(j)) >= 0)
1222
2.03M
#define isl_int_is_divisible_by(i, j) isl_sioimath_is_divisible_by(*(i), *(j))
1223
1224
15.1M
#define isl_int_hash(v, h)    isl_sioimath_hash(*(v), h)
1225
11.1k
#define isl_int_free_str(s)   free(s)
1226
0
#define isl_int_print(out, i, width)  isl_sioimath_print(out, *(i), width)
1227
1228
#endif /* ISL_INT_SIOIMATH_H */