1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 2000, 2001, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GNU M4.
6 GNU M4 is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU M4 is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* Build using only the exported interfaces, unless NDEBUG is set, in
23 which case use private symbols to speed things up as much as possible. */
25 # include <m4/m4module.h>
27 # include "m4private.h"
34 /* Rename exported symbols for dlpreload()ing. */
35 #define m4_builtin_table mpeval_LTX_m4_builtin_table
36 #define m4_macro_table mpeval_LTX_m4_macro_table
39 /* Maintain each of the builtins implemented in this modules along
40 with their details in a single table for easy maintenance.
42 function macros blind side minargs maxargs */
43 #define builtin_functions \
44 BUILTIN (mpeval, false, true, true, 1, 3 ) \
48 #define numb_set(ans, i) mpq_set (ans, i)
49 #define numb_set_si(ans, i) mpq_set_si (*(ans), (long) i, (unsigned long) 1)
51 #define numb_init(x) mpq_init (x)
52 #define numb_fini(x) mpq_clear (x)
54 #define numb_zerop(x) (mpq_cmp (x, numb_ZERO) == 0)
55 #define numb_positivep(x) (mpq_cmp (x, numb_ZERO) > 0)
56 #define numb_negativep(x) (mpq_cmp (x, numb_ZERO) < 0)
58 #define numb_eq(x, y) numb_set (x, mpq_cmp (x, y) == 0 ? numb_ONE : numb_ZERO)
59 #define numb_ne(x, y) numb_set (x, mpq_cmp (x, y) != 0 ? numb_ONE : numb_ZERO)
60 #define numb_lt(x, y) numb_set (x, mpq_cmp (x, y) < 0 ? numb_ONE : numb_ZERO)
61 #define numb_le(x, y) numb_set (x, mpq_cmp (x, y) <= 0 ? numb_ONE : numb_ZERO)
62 #define numb_gt(x, y) numb_set (x, mpq_cmp (x, y) > 0 ? numb_ONE : numb_ZERO)
63 #define numb_ge(x, y) numb_set (x, mpq_cmp (x, y) >= 0 ? numb_ONE : numb_ZERO)
65 #define numb_lnot(x) numb_set (x, numb_zerop (x) ? numb_ONE : numb_ZERO)
66 #define numb_lior(x, y) numb_set (x, numb_zerop (x) ? y : x)
67 #define numb_land(x, y) numb_set (x, numb_zerop (x) ? numb_ZERO : y)
69 #define reduce1(f1, x) \
79 #define reduce2(f2,x,y) \
90 #define numb_plus(x, y) reduce2 (mpq_add, x, y)
91 #define numb_minus(x, y) reduce2 (mpq_sub, x, y)
92 #define numb_negate(x) reduce1 (mpq_neg, x)
94 #define numb_times(x, y) reduce2 (mpq_mul, x, y)
95 #define numb_ratio(x, y) reduce2 (mpq_div, x, y)
96 #define numb_invert(x) reduce1 (mpq_inv, x)
98 #define numb_incr(n) numb_plus (n, numb_ONE)
99 #define numb_decr(n) numb_minus (n, numb_ONE)
101 /* Generate prototypes for each builtin handler function. */
102 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN(handler)
107 /* Generate a table for mapping m4 symbol names to handler functions. */
108 m4_builtin m4_builtin_table
[] =
110 #define BUILTIN(handler, macros, blind, side, min, max) \
111 { CONC(builtin_, handler), STR(handler), \
112 ((macros ? M4_BUILTIN_GROKS_MACRO : 0) \
113 | (blind ? M4_BUILTIN_BLIND : 0) \
114 | (side ? M4_BUILTIN_SIDE_EFFECT : 0)), \
119 { NULL
, NULL
, 0, 0, 0 },
123 /* A table for mapping m4 symbol names to simple expansion text. */
124 m4_macro m4_macro_table
[] =
127 { "__mpeval__", "" },
132 /* GMP defines mpq_t as a 1-element array of struct. Therefore, `mpq_t'
133 is not compatible with `const mpq_t'. */
134 typedef mpq_t number
;
136 static void numb_initialise (void);
137 static void numb_obstack (m4_obstack
*obs
, const number value
,
138 const int radix
, int min
);
139 static void mpq2mpz (m4
*context
, mpz_t z
, const number q
, const char *noisily
);
140 static void mpz2mpq (number q
, const mpz_t z
);
141 static void numb_divide (number
*x
, number
*y
);
142 static void numb_modulo (m4
*context
, number
*x
, number
*y
);
143 static void numb_and (m4
*context
, number
*x
, number
*y
);
144 static void numb_ior (m4
*context
, number
*x
, number
*y
);
145 static void numb_eor (m4
*context
, number
*x
, number
*y
);
146 static void numb_not (m4
*context
, number
*x
);
147 static void numb_lshift (m4
*context
, number
*x
, number
*y
);
148 static void numb_rshift (m4
*context
, number
*x
, number
*y
);
149 #define numb_urshift(c, x, y) numb_rshift (c, x, y)
152 static number numb_ZERO
;
153 static number numb_ONE
;
155 static int numb_initialised
= 0;
158 numb_initialise (void)
160 if (numb_initialised
)
163 numb_init (numb_ZERO
);
164 numb_set_si (&numb_ZERO
, 0);
166 numb_init (numb_ONE
);
167 numb_set_si (&numb_ONE
, 1);
169 numb_initialised
= 1;
173 numb_obstack (m4_obstack
*obs
, const number value
, const int radix
,
181 mpq_get_num (i
, value
);
182 s
= mpz_get_str (NULL
, radix
, i
);
186 obstack_1grow (obs
, '-');
189 for (min
-= strlen (s
); --min
>= 0;)
190 obstack_1grow (obs
, '0');
192 obstack_grow (obs
, s
, strlen (s
));
194 mpq_get_den (i
, value
);
195 if (mpz_cmp_si (i
, (long) 1) != 0)
197 obstack_1grow (obs
, '\\');
198 s
= mpz_get_str ((char *) 0, radix
, i
);
199 obstack_grow (obs
, s
, strlen (s
));
206 #define QUIET (char *)0
209 mpq2mpz (m4
*context
, mpz_t z
, const number q
, const char *noisily
)
211 if (noisily
&& mpz_cmp_si (mpq_denref (q
), (long) 1) != 0)
212 m4_warn (context
, 0, NULL
, _("loss of precision in eval: %s"), noisily
);
214 mpz_div (z
, mpq_numref (q
), mpq_denref (q
));
218 mpz2mpq (number q
, const mpz_t z
)
220 mpq_set_si (q
, (long) 0, (unsigned long) 1);
225 numb_divide (number
* x
, number
* y
)
231 mpq_div (qres
, *x
, *y
);
234 mpz_div (zres
, mpq_numref (qres
), mpq_denref (qres
));
242 numb_modulo (m4
*context
, number
* x
, number
* y
)
246 /* x should be integral */
247 /* y should be integral */
250 mpq2mpz (context
, xx
, *x
, NOISY
);
253 mpq2mpz (context
, yy
, *y
, NOISY
);
256 mpz_mod (res
, xx
, yy
);
266 numb_and (m4
*context
, number
* x
, number
* y
)
270 /* x should be integral */
271 /* y should be integral */
274 mpq2mpz (context
, xx
, *x
, NOISY
);
277 mpq2mpz (context
, yy
, *y
, NOISY
);
280 mpz_and (res
, xx
, yy
);
290 numb_ior (m4
*context
, number
* x
, number
* y
)
294 /* x should be integral */
295 /* y should be integral */
298 mpq2mpz (context
, xx
, *x
, NOISY
);
301 mpq2mpz (context
, yy
, *y
, NOISY
);
304 mpz_ior (res
, xx
, yy
);
314 numb_eor (m4
*context
, number
* x
, number
* y
)
318 /* x should be integral */
319 /* y should be integral */
322 mpq2mpz (context
, xx
, *x
, NOISY
);
325 mpq2mpz (context
, yy
, *y
, NOISY
);
330 mpz_xor (res
, xx
, yy
);
332 /* a^b = (a|b) & !(a&b) */
334 mpz_t and_ab
, ior_ab
, nand_ab
;
337 mpz_ior (ior_ab
, xx
, yy
);
340 mpz_and (and_ab
, xx
, yy
);
343 mpz_com (nand_ab
, and_ab
);
345 mpz_and (res
, ior_ab
, nand_ab
);
361 numb_not (m4
*context
, number
* x
)
365 /* x should be integral */
368 mpq2mpz (context
, xx
, *x
, NOISY
);
380 numb_lshift (m4
*context
, number
* x
, number
* y
)
384 /* x should be integral */
385 /* y should be integral */
388 mpq2mpz (context
, xx
, *x
, NOISY
);
391 mpq2mpz (context
, yy
, *y
, NOISY
);
395 /* bug: need to determine if y is too big or negative. */
396 long int exp
= mpz_get_si (yy
);
399 mpz_mul_2exp (res
, xx
, (unsigned) exp
);
403 mpz_div_2exp (res
, xx
, (unsigned) -exp
);
415 numb_rshift (m4
*context
, number
* x
, number
* y
)
419 /* x should be integral */
420 /* y should be integral */
423 mpq2mpz (context
, xx
, *x
, NOISY
);
426 mpq2mpz (context
, yy
, *y
, NOISY
);
430 /* FIXME: bug - need to determine if y is too big or negative */
431 long int exp
= mpz_get_si (yy
);
434 mpz_div_2exp (res
, xx
, (unsigned) exp
);
438 mpz_mul_2exp (res
, xx
, (unsigned) -exp
);
449 #define m4_evaluate builtin_mpeval
450 #include "evalparse.c"