Polly 19.0.0git
isl_test_int.c
Go to the documentation of this file.
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
11#include <assert.h>
12#include <stdio.h>
13#include <isl_int.h>
14
15#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
16
17#ifdef USE_SMALL_INT_OPT
18/* Test whether small and big representation of the same number have the same
19 * hash.
20 */
21static void int_test_hash(isl_int val)
22{
23 uint32_t demotedhash, promotedhash;
24 isl_int demoted, promoted;
25
26 isl_int_init(demoted);
27 isl_int_set(demoted, val);
28
29 isl_int_init(promoted);
30 isl_int_set(promoted, val);
31
33 isl_sioimath_promote(promoted);
34
35 assert(isl_int_eq(demoted, promoted));
36
37 demotedhash = isl_int_hash(demoted, 0);
38 promotedhash = isl_int_hash(promoted, 0);
39 assert(demotedhash == promotedhash);
40
41 isl_int_clear(demoted);
42 isl_int_clear(promoted);
43}
44
45struct {
46 void (*fn)(isl_int);
47 char *val;
48} int_single_value_tests[] = {
49 { &int_test_hash, "0" },
50 { &int_test_hash, "1" },
51 { &int_test_hash, "-1" },
52 { &int_test_hash, "23" },
53 { &int_test_hash, "-23" },
54 { &int_test_hash, "107" },
55 { &int_test_hash, "32768" },
56 { &int_test_hash, "2147483647" },
57 { &int_test_hash, "-2147483647" },
58 { &int_test_hash, "2147483648" },
59 { &int_test_hash, "-2147483648" },
60};
61
62static void int_test_single_value()
63{
64 int i;
65
66 for (i = 0; i < ARRAY_SIZE(int_single_value_tests); i += 1) {
68
70 isl_int_read(val, int_single_value_tests[i].val);
71
72 (*int_single_value_tests[i].fn)(val);
73
75 }
76}
77
78static void invoke_alternate_representations_2args(char *arg1, char *arg2,
79 void (*fn)(isl_int, isl_int))
80{
81 int j;
82 isl_int int1, int2;
83
84 isl_int_init(int1);
85 isl_int_init(int2);
86
87 for (j = 0; j < 4; ++j) {
88 isl_int_read(int1, arg1);
89 isl_int_read(int2, arg2);
90
91 if (j & 1)
93 else
95
96 if (j & 2)
98 else
100
101 (*fn)(int1, int2);
102 }
103
104 isl_int_clear(int1);
105 isl_int_clear(int2);
106}
107
108static void invoke_alternate_representations_3args(char *arg1, char *arg2,
109 char *arg3, void (*fn)(isl_int, isl_int, isl_int))
110{
111 int j;
112 isl_int int1, int2, int3;
113
114 isl_int_init(int1);
115 isl_int_init(int2);
116 isl_int_init(int3);
117
118 for (j = 0; j < 8; ++j) {
119 isl_int_read(int1, arg1);
120 isl_int_read(int2, arg2);
121 isl_int_read(int3, arg3);
122
123 if (j & 1)
125 else
127
128 if (j & 2)
130 else
132
133 if (j & 4)
135 else
137
138 (*fn)(int1, int2, int3);
139 }
140
141 isl_int_clear(int1);
142 isl_int_clear(int2);
143 isl_int_clear(int3);
144}
145#else /* USE_SMALL_INT_OPT */
146
148{
149}
150
152 void (*fn)(isl_int, isl_int))
153{
154 isl_int int1, int2;
155
156 isl_int_init(int1);
157 isl_int_init(int2);
158
159 isl_int_read(int1, arg1);
160 isl_int_read(int2, arg2);
161
162 (*fn)(int1, int2);
163
164 isl_int_clear(int1);
165 isl_int_clear(int2);
166}
167
169 char *arg3, void (*fn)(isl_int, isl_int, isl_int))
170{
171 isl_int int1, int2, int3;
172
173 isl_int_init(int1);
174 isl_int_init(int2);
175 isl_int_init(int3);
176
177 isl_int_read(int1, arg1);
178 isl_int_read(int2, arg2);
179 isl_int_read(int3, arg3);
180
181 (*fn)(int1, int2, int3);
182
183 isl_int_clear(int1);
184 isl_int_clear(int2);
185 isl_int_clear(int3);
186}
187#endif /* USE_SMALL_INT_OPT */
188
190{
193
196
199
201}
202
204{
207
210
212}
213
214struct {
215 void (*fn)(isl_int, isl_int);
216 char *expected, *arg;
217} int_unary_tests[] = {
218 { &int_test_neg, "0", "0" },
219 { &int_test_neg, "-1", "1" },
220 { &int_test_neg, "-2147483647", "2147483647" },
221 { &int_test_neg, "-2147483648", "2147483648" },
222 { &int_test_neg, "-9223372036854775807", "9223372036854775807" },
223 { &int_test_neg, "-9223372036854775808", "9223372036854775808" },
224
225 { &int_test_abs, "0", "0" },
226 { &int_test_abs, "1", "1" },
227 { &int_test_abs, "1", "-1" },
228 { &int_test_abs, "2147483647", "2147483647" },
229 { &int_test_abs, "2147483648", "-2147483648" },
230 { &int_test_abs, "9223372036854775807", "9223372036854775807" },
231 { &int_test_abs, "9223372036854775808", "-9223372036854775808" },
233
235{
237 unsigned long rhsulong;
238
239 if (isl_int_sgn(rhs) == 0)
240 return;
241
243
246
249
252
255
256 if (isl_int_fits_ulong(rhs)) {
257 rhsulong = isl_int_get_ui(rhs);
258
259 isl_int_divexact_ui(result, lhs, rhsulong);
261
262 isl_int_fdiv_q_ui(result, lhs, rhsulong);
264
265 isl_int_cdiv_q_ui(result, lhs, rhsulong);
267 }
268
270}
271
273{
276
279
280 if (isl_int_fits_ulong(rhs)) {
281 unsigned long rhsulong = isl_int_get_ui(rhs);
282
283 isl_int_mul_ui(result, lhs, rhsulong);
285 }
286
287 if (isl_int_fits_slong(rhs)) {
288 unsigned long rhsslong = isl_int_get_si(rhs);
289
290 isl_int_mul_si(result, lhs, rhsslong);
292 }
293
295}
296
297/* Use a triple that satisfies 'product = factor1 * factor2' to check the
298 * operations mul, divexact, tdiv, fdiv and cdiv.
299 */
300static void int_test_product(isl_int product, isl_int factor1, isl_int factor2)
301{
302 int_test_divexact(factor1, product, factor2);
303 int_test_divexact(factor2, product, factor1);
304
305 int_test_mul(product, factor1, factor2);
306 int_test_mul(product, factor2, factor1);
307}
308
310{
313
316
318}
319
321{
324
327
329}
330
331/* Use a triple that satisfies 'sum = term1 + term2' to check the operations add
332 * and sub.
333 */
334static void int_test_sum(isl_int sum, isl_int term1, isl_int term2)
335{
336 int_test_sub(term1, sum, term2);
337 int_test_sub(term2, sum, term1);
338
339 int_test_add(sum, term1, term2);
340 int_test_add(sum, term2, term1);
341}
342
344{
345 unsigned long rhsulong;
348
351
352 if (isl_int_fits_ulong(rhs)) {
353 rhsulong = isl_int_get_ui(rhs);
354
355 isl_int_fdiv_q_ui(result, lhs, rhsulong);
357 }
358
360}
361
363{
364 unsigned long rhsulong;
367
370
371 if (isl_int_fits_ulong(rhs)) {
372 rhsulong = isl_int_get_ui(rhs);
373
374 isl_int_cdiv_q_ui(result, lhs, rhsulong);
376 }
377
379}
380
382{
385
388
390}
391
393{
396
399
401}
402
404{
407
410
413
415}
416
418{
421
424
427
429}
430
431static int sgn(int val)
432{
433 if (val > 0)
434 return 1;
435 if (val < 0)
436 return -1;
437 return 0;
438}
439
440static void int_test_cmp(int exp, isl_int lhs, isl_int rhs)
441{
442 long rhslong;
443
444 assert(exp == sgn(isl_int_cmp(lhs, rhs)));
445
446 if (isl_int_fits_slong(rhs)) {
447 rhslong = isl_int_get_si(rhs);
448 assert(exp == sgn(isl_int_cmp_si(lhs, rhslong)));
449 }
450}
451
452/* Test the comparison relations over two numbers.
453 * expected is the sign (1, 0 or -1) of 'lhs - rhs'.
454 */
456{
457 int exp;
458 isl_int diff;
459
461
462 isl_int_init(diff);
463 isl_int_sub(diff, lhs, rhs);
464 assert(exp == isl_int_sgn(diff));
465 isl_int_clear(diff);
466
467 int_test_cmp(exp, lhs, rhs);
468 int_test_cmp(-exp, rhs, lhs);
469}
470
472{
473 int exp;
474
476 assert(exp == sgn(isl_int_abs_cmp(lhs, rhs)));
477 assert(-exp == sgn(isl_int_abs_cmp(rhs, lhs)));
478}
479
480/* If "expected" is equal to 1, then check that "rhs" divides "lhs".
481 * If "expected" is equal to 0, then check that "rhs" does not divide "lhs".
482 */
484{
485 int exp;
486
489}
490
491struct {
492 void (*fn)(isl_int, isl_int, isl_int);
493 char *expected, *lhs, *rhs;
494} int_binary_tests[] = {
495 { &int_test_sum, "0", "0", "0" },
496 { &int_test_sum, "1", "1", "0" },
497 { &int_test_sum, "2", "1", "1" },
498 { &int_test_sum, "-1", "0", "-1" },
499 { &int_test_sum, "-2", "-1", "-1" },
500
501 { &int_test_sum, "2147483647", "1073741823", "1073741824" },
502 { &int_test_sum, "-2147483648", "-1073741824", "-1073741824" },
503
504 { &int_test_sum, "2147483648", "2147483647", "1" },
505 { &int_test_sum, "-2147483648", "-2147483647", "-1" },
506
507 { &int_test_product, "0", "0", "0" },
508 { &int_test_product, "0", "0", "1" },
509 { &int_test_product, "1", "1", "1" },
510
511 { &int_test_product, "6", "2", "3" },
512 { &int_test_product, "-6", "2", "-3" },
513 { &int_test_product, "-6", "-2", "3" },
514 { &int_test_product, "6", "-2", "-3" },
515
516 { &int_test_product, "2147483648", "65536", "32768" },
517 { &int_test_product, "-2147483648", "65536", "-32768" },
518
520 "4611686014132420609", "2147483647", "2147483647" },
522 "-4611686014132420609", "-2147483647", "2147483647" },
523
525 "4611686016279904256", "2147483647", "2147483648" },
527 "-4611686016279904256", "-2147483647", "2147483648" },
529 "-4611686016279904256", "2147483647", "-2147483648" },
531 "4611686016279904256", "-2147483647", "-2147483648" },
532
533 { &int_test_product, "85070591730234615847396907784232501249",
534 "9223372036854775807", "9223372036854775807" },
535 { &int_test_product, "-85070591730234615847396907784232501249",
536 "-9223372036854775807", "9223372036854775807" },
537
538 { &int_test_product, "85070591730234615856620279821087277056",
539 "9223372036854775807", "9223372036854775808" },
540 { &int_test_product, "-85070591730234615856620279821087277056",
541 "-9223372036854775807", "9223372036854775808" },
542 { &int_test_product, "-85070591730234615856620279821087277056",
543 "9223372036854775807", "-9223372036854775808" },
544 { &int_test_product, "85070591730234615856620279821087277056",
545 "-9223372036854775807", "-9223372036854775808" },
546
547 { &int_test_product, "340282366920938463426481119284349108225",
548 "18446744073709551615", "18446744073709551615" },
549 { &int_test_product, "-340282366920938463426481119284349108225",
550 "-18446744073709551615", "18446744073709551615" },
551
552 { &int_test_product, "340282366920938463444927863358058659840",
553 "18446744073709551615", "18446744073709551616" },
554 { &int_test_product, "-340282366920938463444927863358058659840",
555 "-18446744073709551615", "18446744073709551616" },
556 { &int_test_product, "-340282366920938463444927863358058659840",
557 "18446744073709551615", "-18446744073709551616" },
558 { &int_test_product, "340282366920938463444927863358058659840",
559 "-18446744073709551615", "-18446744073709551616" },
560
561 { &int_test_fdiv, "0", "1", "2" },
562 { &int_test_fdiv_r, "1", "1", "3" },
563 { &int_test_fdiv, "-1", "-1", "2" },
564 { &int_test_fdiv_r, "2", "-1", "3" },
565 { &int_test_fdiv, "-1", "1", "-2" },
566 { &int_test_fdiv_r, "-2", "1", "-3" },
567 { &int_test_fdiv, "0", "-1", "-2" },
568 { &int_test_fdiv_r, "-1", "-1", "-3" },
569
570 { &int_test_cdiv, "1", "1", "2" },
571 { &int_test_cdiv, "0", "-1", "2" },
572 { &int_test_cdiv, "0", "1", "-2" },
573 { &int_test_cdiv, "1", "-1", "-2" },
574
575 { &int_test_cdiv, "1073741824", "2147483647", "2" },
576 { &int_test_cdiv, "1073741824", "2147483648", "2" },
577 { &int_test_cdiv, "-1073741824", "-2147483648", "2" },
578 { &int_test_cdiv, "-1073741823", "-2147483647", "2" },
579
580 { &int_test_tdiv, "0", "1", "2" },
581 { &int_test_tdiv, "0", "-1", "2" },
582 { &int_test_tdiv, "0", "1", "-2" },
583 { &int_test_tdiv, "0", "-1", "-2" },
584
585 { &int_test_gcd, "0", "0", "0" },
586 { &int_test_lcm, "0", "0", "0" },
587 { &int_test_gcd, "7", "0", "7" },
588 { &int_test_lcm, "0", "0", "7" },
589 { &int_test_gcd, "1", "1", "1" },
590 { &int_test_lcm, "1", "1", "1" },
591 { &int_test_gcd, "1", "1", "-1" },
592 { &int_test_lcm, "1", "1", "-1" },
593 { &int_test_gcd, "1", "-1", "-1" },
594 { &int_test_lcm, "1", "-1", "-1" },
595 { &int_test_gcd, "3", "6", "9" },
596 { &int_test_lcm, "18", "6", "9" },
597 { &int_test_gcd, "1", "14", "2147483647" },
598 { &int_test_lcm, "15032385529", "7", "2147483647" },
599 { &int_test_gcd, "2", "6", "-2147483648" },
600 { &int_test_lcm, "6442450944", "6", "-2147483648" },
601 { &int_test_gcd, "1", "6", "9223372036854775807" },
602 { &int_test_lcm, "55340232221128654842", "6", "9223372036854775807" },
603 { &int_test_gcd, "2", "6", "-9223372036854775808" },
604 { &int_test_lcm, "27670116110564327424", "6", "-9223372036854775808" },
605 { &int_test_gcd, "1", "18446744073709551616", "18446744073709551615" },
606 { &int_test_lcm, "340282366920938463444927863358058659840",
607 "18446744073709551616", "18446744073709551615" },
608
609 { &int_test_cmps, "0", "0", "0" },
610 { &int_test_abs_cmp, "0", "0", "0" },
611 { &int_test_cmps, "1", "1", "0" },
612 { &int_test_abs_cmp, "1", "1", "0" },
613 { &int_test_cmps, "-1", "-1", "0" },
614 { &int_test_abs_cmp, "1", "-1", "0" },
615 { &int_test_cmps, "-1", "-1", "1" },
616 { &int_test_abs_cmp, "0", "-1", "1" },
617
618 { &int_test_cmps, "-1", "5", "2147483647" },
619 { &int_test_abs_cmp, "-1", "5", "2147483647" },
620 { &int_test_cmps, "1", "5", "-2147483648" },
621 { &int_test_abs_cmp, "-1", "5", "-2147483648" },
622 { &int_test_cmps, "-1", "5", "9223372036854775807" },
623 { &int_test_abs_cmp, "-1", "5", "9223372036854775807" },
624 { &int_test_cmps, "1", "5", "-9223372036854775809" },
625 { &int_test_abs_cmp, "-1", "5", "-9223372036854775809" },
626
627 { &int_test_divisible, "1", "0", "0" },
628 { &int_test_divisible, "0", "1", "0" },
629 { &int_test_divisible, "0", "2", "0" },
630 { &int_test_divisible, "0", "2147483647", "0" },
631 { &int_test_divisible, "0", "9223372036854775807", "0" },
632 { &int_test_divisible, "1", "0", "1" },
633 { &int_test_divisible, "1", "1", "1" },
634 { &int_test_divisible, "1", "2", "1" },
635 { &int_test_divisible, "1", "2147483647", "1" },
636 { &int_test_divisible, "1", "9223372036854775807", "1" },
637 { &int_test_divisible, "1", "0", "2" },
638 { &int_test_divisible, "0", "1", "2" },
639 { &int_test_divisible, "1", "2", "2" },
640 { &int_test_divisible, "0", "2147483647", "2" },
641 { &int_test_divisible, "0", "9223372036854775807", "2" },
643
644/* Tests the isl_int_* function to give the expected results. Tests are
645 * grouped by the number of arguments they take.
646 *
647 * If small integer optimization is enabled, we also test whether the results
648 * are the same in small and big representation.
649 */
650int main()
651{
652 int i;
653
655
656 for (i = 0; i < ARRAY_SIZE(int_unary_tests); i += 1) {
660 }
661
662 for (i = 0; i < ARRAY_SIZE(int_binary_tests); i += 1) {
666 }
667
668 return 0;
669}
polly print import Polly Print Scop import result
__isl_export __isl_give ISL_HMAP __isl_take ISL_KEY __isl_take ISL_VAL * val
Definition: hmap.h:32
#define isl_int_divexact_ui(r, i, j)
Definition: isl_int_gmp.h:45
#define isl_int_gcd(r, i, j)
Definition: isl_int_gmp.h:42
#define isl_int_neg(r, i)
Definition: isl_int_gmp.h:24
#define isl_int_add(r, i, j)
Definition: isl_int_gmp.h:30
#define isl_int_cmp(i, j)
Definition: isl_int_gmp.h:55
#define isl_int_cdiv_q_ui(r, i, j)
Definition: isl_int_gmp.h:48
#define isl_int_is_divisible_by(i, j)
Definition: isl_int_gmp.h:69
#define isl_int_eq(i, j)
Definition: isl_int_gmp.h:57
#define isl_int_fdiv_r(r, i, j)
Definition: isl_int_gmp.h:50
#define isl_int_tdiv_q(r, i, j)
Definition: isl_int_gmp.h:46
#define isl_int_set(r, i)
Definition: isl_int_gmp.h:14
#define isl_int_cdiv_q(r, i, j)
Definition: isl_int_gmp.h:47
#define isl_int_fits_slong(r)
Definition: isl_int_gmp.h:17
#define isl_int_mul_ui(r, i, j)
Definition: isl_int_gmp.h:35
#define isl_int_fits_ulong(r)
Definition: isl_int_gmp.h:19
#define isl_int_lcm(r, i, j)
Definition: isl_int_gmp.h:43
#define isl_int_abs_cmp(i, j)
Definition: isl_int_gmp.h:63
#define isl_int_fdiv_q_ui(r, i, j)
Definition: isl_int_gmp.h:51
#define isl_int_divexact(r, i, j)
Definition: isl_int_gmp.h:44
#define isl_int_sgn(i)
Definition: isl_int_gmp.h:54
#define isl_int_mul_si(r, i, j)
Definition: isl_int_gmp.h:34
#define isl_int_mul(r, i, j)
Definition: isl_int_gmp.h:32
#define isl_int_get_si(r)
Definition: isl_int_gmp.h:18
#define isl_int_read(r, s)
Definition: isl_int_gmp.h:53
mpz_t isl_int
Definition: isl_int_gmp.h:9
#define isl_int_fdiv_q(r, i, j)
Definition: isl_int_gmp.h:49
#define isl_int_sub(r, i, j)
Definition: isl_int_gmp.h:31
#define isl_int_init(i)
Definition: isl_int_gmp.h:11
#define isl_int_abs(r, i)
Definition: isl_int_gmp.h:23
#define isl_int_clear(i)
Definition: isl_int_gmp.h:12
#define isl_int_cmp_si(i, si)
Definition: isl_int_gmp.h:56
#define isl_int_hash(v, h)
Definition: isl_int_gmp.h:72
#define isl_int_get_ui(r)
Definition: isl_int_gmp.h:20
void isl_sioimath_try_demote(isl_sioimath_ptr dst)
void isl_sioimath_promote(isl_sioimath_ptr dst)
const char * arg1
Definition: isl_test.c:863
const char * arg2
Definition: isl_test.c:865
#define assert(exp)
static void int_test_divexact(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:234
static void int_test_single_value()
Definition: isl_test_int.c:147
static void int_test_lcm(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:417
static void int_test_product(isl_int product, isl_int factor1, isl_int factor2)
Definition: isl_test_int.c:300
char * rhs
Definition: isl_test_int.c:493
static void int_test_cdiv(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:362
char * lhs
Definition: isl_test_int.c:493
static void int_test_fdiv_r(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:392
static void int_test_tdiv(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:381
struct @89 int_unary_tests[]
static void int_test_gcd(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:403
static void int_test_mul(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:272
static void int_test_sub(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:320
#define ARRAY_SIZE(array)
Definition: isl_test_int.c:15
static void int_test_fdiv(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:343
static void int_test_cmps(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:455
char * arg
Definition: isl_test_int.c:216
char * expected
Definition: isl_test_int.c:216
static void int_test_abs(isl_int expected, isl_int arg)
Definition: isl_test_int.c:203
void(* fn)(isl_int, isl_int)
Definition: isl_test_int.c:215
static void int_test_sum(isl_int sum, isl_int term1, isl_int term2)
Definition: isl_test_int.c:334
static void invoke_alternate_representations_2args(char *arg1, char *arg2, void(*fn)(isl_int, isl_int))
Definition: isl_test_int.c:151
static void int_test_abs_cmp(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:471
static void int_test_divisible(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:483
static void int_test_neg(isl_int expected, isl_int arg)
Definition: isl_test_int.c:189
static void int_test_add(isl_int expected, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:309
static void int_test_cmp(int exp, isl_int lhs, isl_int rhs)
Definition: isl_test_int.c:440
static void invoke_alternate_representations_3args(char *arg1, char *arg2, char *arg3, void(*fn)(isl_int, isl_int, isl_int))
Definition: isl_test_int.c:168
int main()
Definition: isl_test_int.c:650
static int sgn(int val)
Definition: isl_test_int.c:431
struct @90 int_binary_tests[]