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