Debian Codegraph @

src/h/number.h

raw symbols SCIP
1 /* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=8 shiftwidth=4 expandtab: */
3
4 /*
5 * Copyright (c) 1995, Giuseppe Attardi.
6 *
7 * See file 'LICENSE' for the copyright details.
8 *
9 */
10
11 /* number.h -- INTEGER interface. */
12 #ifndef ECL_NUMBER_H
13 #define ECL_NUMBER_H
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /* Bignum internal protocol */
20
21 /* Allocate a number on the stack. */
22 #define ECL_WITH_TEMP_BIGNUM(name,n) \
23 mp_limb_t name##data[n]; \
24 volatile struct ecl_bignum name##aux; \
25 const cl_object name = (name##aux.value->_mp_alloc = n, \
26 name##aux.value->_mp_size = 0, \
27 name##aux.value->_mp_d = name##data, \
28 (cl_object)(&name##aux))
29
30 #define ECL_BIGNUM_LIMB_BITS GMP_LIMB_BITS
31 #define ECL_BIGNUM_DIM(x) (ecl_bignum(x)->_mp_alloc) /* number of allocated limbs */
32 #define ECL_BIGNUM_SIZE(x) (ecl_bignum(x)->_mp_size) /* number of limbs in use times sign of the bignum */
33 #define ECL_BIGNUM_USIZE(x) (mpz_size(ecl_bignum(x))) /* number of limbs */
34 #define ECL_BIGNUM_LIMBS(x) (ecl_bignum(x)->_mp_d) /* pointer to array of allocated limbs */
35
36 typedef void (*_ecl_big_binary_op)(cl_object out, cl_object o1, cl_object o2);
37 extern ECL_API _ecl_big_binary_op _ecl_big_boole_operator(int op);
38
39 /* Type conversion (setters mutate the bignum structure). */
40 #if ECL_LONG_BITS >= ECL_FIXNUM_BITS
41 # define _ecl_big_set_index(x, f) mpz_set_ui(ecl_bignum(x),(f))
42 # define _ecl_big_set_fixnum(x, f) mpz_set_si(ecl_bignum(x),(f))
43 # define _ecl_big_get_index(x) mpz_get_ui(ecl_bignum(x))
44 # define _ecl_big_get_fixnum(x) mpz_get_si(ecl_bignum(x))
45 #elif GMP_LIMB_BITS >= ECL_FIXNUM_BITS
46 # define ECL_GMP_FIXNUM_TO_LIMBS
47 extern ECL_API cl_index _ecl_big_get_idx(cl_object x);
48 extern ECL_API cl_fixnum _ecl_big_get_fix(cl_object x);
49 extern ECL_API void _ecl_big_set_idx(cl_object x, cl_index y);
50 extern ECL_API void _ecl_big_set_fix(cl_object x, cl_fixnum y);
51 # define _ecl_big_set_index(x, f) _ecl_big_set_idx((x),(f))
52 # define _ecl_big_get_index(x) _ecl_big_get_idx((x))
53 # define _ecl_big_set_fixnum(x, f) _ecl_big_set_fix((x),(f))
54 # define _ecl_big_get_fixnum(x) _ecl_big_get_fix((x))
55 #else
56 # error "ECL cannot build with GMP when both long and mp_limb_t are smaller than cl_fixnum"
57 #endif
58
59 #define _ecl_big_set_ui(x, i) mpz_set_ui(ecl_bignum(x),(unsigned long int)i)
60 #define _ecl_big_set_si(x, i) mpz_set_si(ecl_bignum(x),(long int)i)
61 #define _ecl_big_set_d(x, d) mpz_set_d(ecl_bignum(x),(d))
62
63 #define _ecl_big_get_ui(x) mpz_get_ui(ecl_bignum(x))
64 #define _ecl_big_get_si(x) mpz_get_si(ecl_bignum(x))
65 #define _ecl_big_get_d(x) mpz_get_d(ecl_bignum(x))
66 #define _ecl_big_get_str(buf,b,x) mpz_get_str(buf,-b,ecl_bignum(x))
67
68 #define _ecl_big_to_ulong(x) _ecl_big_get_ui(x)
69 #define _ecl_big_to_long(x) _ecl_big_get_si(x)
70 #define _ecl_big_to_double(x) _ecl_big_get_d(x)
71
72 extern ECL_API long double _ecl_big_get_lf(cl_object x);
73 #define _ecl_big_to_long_double(x) _ecl_big_get_lf((x))
74
75 /* Type conversion predicates */
76 #define _ecl_big_fits_ui(x) mpz_fits_ulong_p(ecl_bignum(x))
77 #define _ecl_big_fits_si(x) mpz_fits_slong_p(ecl_bignum(x))
78
79 /* Predicates */
80 #define _ecl_big_odd_p(x) ((mpz_get_ui(ecl_bignum(x)) & 1) != 0)
81 #define _ecl_big_even_p(x) ((mpz_get_ui(ecl_bignum(x)) & 1) == 0)
82 #define _ecl_big_sign(x) mpz_sgn(ecl_bignum(x))
83 #define _ecl_big_compare(x, y) mpz_cmp(ecl_bignum(x),ecl_bignum(y))
84
85 /* Bit fiddling */
86 #define _ecl_big_popcount(x) mpz_popcount(ecl_bignum(x))
87 #define _ecl_big_1com(x,y) mpz_com(ecl_bignum(x),ecl_bignum(y))
88 #define _ecl_big_tstbit(x,n) mpz_tstbit(ecl_bignum(x),n)
89 #define _ecl_big_bits(x) mpz_sizeinbase(ecl_bignum(x),2)
90 #define _ecl_big_sizeinbase(x,n) mpz_sizeinbase(ecl_bignum(x),n)
91
92 #define _ecl_big_div_2exp(z,y,i) mpz_div_2exp(ecl_bignum(z), ecl_bignum(y), i)
93 #define _ecl_big_mul_2exp(z,y,i) mpz_mul_2exp(ecl_bignum(z), ecl_bignum(y), i);
94 #define _ecl_big_fdiv_q_2exp(z,x,i) mpz_fdiv_q_2exp(ecl_bignum(z), ecl_bignum(x), i);
95 #define _ecl_big_fdiv_r_2exp(z,x,i) mpz_fdiv_r_2exp(ecl_bignum(z), ecl_bignum(x), i);
96
97 /* Arithmetic operations that are used for parsing numbers (and in big.d) */
98 #define _ecl_big_mul(z, x, y) mpz_mul(ecl_bignum(z),ecl_bignum(x),ecl_bignum(y))
99 #define _ecl_big_mul_ui(z, x, y) mpz_mul_ui(ecl_bignum(z),ecl_bignum(x),(y))
100 #define _ecl_big_add_ui(z, x, i) mpz_add_ui(ecl_bignum(z),ecl_bignum(x),(i))
101 #define _ecl_big_neg(z, x) mpz_neg(ecl_bignum(z),ecl_bignum(x))
102 #define _ecl_big_complement(z, x) _ecl_big_neg(z,x)
103
104 /* Other number operations (not bignum) */
105 #if ECL_CAN_INLINE
106 static ECL_INLINE cl_fixnum
107 ecl_to_fix(cl_object f)
108 {
109 if (ecl_unlikely(!ECL_FIXNUMP(f)))
110 FEtype_error_fixnum(f);
111 return ecl_fixnum(f);
112 }
113
114 static ECL_INLINE cl_index
115 ecl_to_size(cl_object f)
116 {
117 cl_fixnum aux = 0;
118 if (ecl_unlikely(!ECL_FIXNUMP(f) || ((aux = ecl_fixnum(f)) < 0)))
119 FEtype_error_size(f);
120 return aux;
121 }
122 #else
123 extern ECL_API cl_fixnum ecl_fixnum_value(cl_object f);
124 extern ECL_API cl_index ecl_to_size(cl_object f);
125 #endif
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif /* ECL_NUMBER_H */