isl_printer_print_pw_qpolynomial{,_fold}: allow printing in "C" format
[isl.git] / include / isl_int.h
blob9f5958ba605ad71d269266882feb3b4bb4cceb3f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #ifndef ISL_INT_H
11 #define ISL_INT_H
13 #include <isl_hash.h>
14 #include <string.h>
15 #include <gmp.h>
16 #if defined(__cplusplus)
17 #include <iostream>
18 #endif
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
24 #ifndef mp_get_memory_functions
25 void mp_get_memory_functions(
26 void *(**alloc_func_ptr) (size_t),
27 void *(**realloc_func_ptr) (void *, size_t, size_t),
28 void (**free_func_ptr) (void *, size_t));
29 #endif
31 /* isl_int is the basic integer type. It currently always corresponds
32 * to a gmp mpz_t, but in the future, different types such as long long
33 * or cln::cl_I will be supported.
35 typedef mpz_t isl_int;
37 #define isl_int_init(i) mpz_init(i)
38 #define isl_int_clear(i) mpz_clear(i)
40 #define isl_int_set(r,i) mpz_set(r,i)
41 #define isl_int_set_si(r,i) mpz_set_si(r,i)
42 #define isl_int_get_si(r) mpz_get_si(r)
43 #define isl_int_abs(r,i) mpz_abs(r,i)
44 #define isl_int_neg(r,i) mpz_neg(r,i)
45 #define isl_int_swap(i,j) mpz_swap(i,j)
46 #define isl_int_swap_or_set(i,j) mpz_swap(i,j)
47 #define isl_int_add_ui(r,i,j) mpz_add_ui(r,i,j)
48 #define isl_int_sub_ui(r,i,j) mpz_sub_ui(r,i,j)
50 #define isl_int_add(r,i,j) mpz_add(r,i,j)
51 #define isl_int_sub(r,i,j) mpz_sub(r,i,j)
52 #define isl_int_mul(r,i,j) mpz_mul(r,i,j)
53 #define isl_int_mul_ui(r,i,j) mpz_mul_ui(r,i,j)
54 #define isl_int_addmul(r,i,j) mpz_addmul(r,i,j)
55 #define isl_int_submul(r,i,j) mpz_submul(r,i,j)
57 #define isl_int_gcd(r,i,j) mpz_gcd(r,i,j)
58 #define isl_int_lcm(r,i,j) mpz_lcm(r,i,j)
59 #define isl_int_divexact(r,i,j) mpz_divexact(r,i,j)
60 #define isl_int_divexact_ui(r,i,j) mpz_divexact_ui(r,i,j)
61 #define isl_int_tdiv_q(r,i,j) mpz_tdiv_q(r,i,j)
62 #define isl_int_cdiv_q(r,i,j) mpz_cdiv_q(r,i,j)
63 #define isl_int_fdiv_q(r,i,j) mpz_fdiv_q(r,i,j)
64 #define isl_int_fdiv_r(r,i,j) mpz_fdiv_r(r,i,j)
65 #define isl_int_fdiv_q_ui(r,i,j) mpz_fdiv_q_ui(r,i,j)
67 #define isl_int_read(r,s) mpz_set_str(r,s,10)
68 typedef void (*isl_int_print_gmp_free_t)(void *, size_t);
69 #define isl_int_print(out,i,width) \
70 do { \
71 char *s; \
72 isl_int_print_gmp_free_t gmp_free; \
73 s = mpz_get_str(0, 10, i); \
74 fprintf(out, "%*s", width, s); \
75 mp_get_memory_functions(NULL, NULL, &gmp_free); \
76 (*gmp_free)(s, strlen(s)+1); \
77 } while (0)
79 #define isl_int_sgn(i) mpz_sgn(i)
80 #define isl_int_cmp(i,j) mpz_cmp(i,j)
81 #define isl_int_cmp_si(i,si) mpz_cmp_si(i,si)
82 #define isl_int_eq(i,j) (mpz_cmp(i,j) == 0)
83 #define isl_int_ne(i,j) (mpz_cmp(i,j) != 0)
84 #define isl_int_lt(i,j) (mpz_cmp(i,j) < 0)
85 #define isl_int_le(i,j) (mpz_cmp(i,j) <= 0)
86 #define isl_int_gt(i,j) (mpz_cmp(i,j) > 0)
87 #define isl_int_ge(i,j) (mpz_cmp(i,j) >= 0)
88 #define isl_int_abs_eq(i,j) (mpz_cmpabs(i,j) == 0)
89 #define isl_int_abs_ne(i,j) (mpz_cmpabs(i,j) != 0)
90 #define isl_int_abs_lt(i,j) (mpz_cmpabs(i,j) < 0)
91 #define isl_int_abs_gt(i,j) (mpz_cmpabs(i,j) > 0)
92 #define isl_int_abs_ge(i,j) (mpz_cmpabs(i,j) >= 0)
95 #define isl_int_is_zero(i) (isl_int_sgn(i) == 0)
96 #define isl_int_is_one(i) (isl_int_cmp_si(i,1) == 0)
97 #define isl_int_is_negone(i) (isl_int_cmp_si(i,-1) == 0)
98 #define isl_int_is_pos(i) (isl_int_sgn(i) > 0)
99 #define isl_int_is_neg(i) (isl_int_sgn(i) < 0)
100 #define isl_int_is_nonpos(i) (isl_int_sgn(i) <= 0)
101 #define isl_int_is_nonneg(i) (isl_int_sgn(i) >= 0)
102 #define isl_int_is_divisible_by(i,j) mpz_divisible_p(i,j)
104 uint32_t isl_gmp_hash(mpz_t v, uint32_t hash);
105 #define isl_int_hash(v,h) isl_gmp_hash(v,h)
107 #if defined(__cplusplus)
109 #endif
111 #if defined(__cplusplus)
112 extern "C" { typedef void (*isl_gmp_free_t)(void *, size_t); }
114 static inline std::ostream &operator<<(std::ostream &os, isl_int i)
116 char *s;
117 isl_gmp_free_t gmp_free;
118 s = mpz_get_str(0, 10, i);
119 os << s;
120 mp_get_memory_functions(NULL, NULL, &gmp_free);
121 (*gmp_free)(s, strlen(s)+1);
122 return os;
124 #endif
126 #endif