1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 2000-2001, 2006-2008, 2010, 2013-2014, 2017 Free
3 Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Build using only the exported interfaces, unless NDEBUG is set, in
24 which case use private symbols to speed things up as much as possible. */
26 # include <m4/m4module.h>
28 # include "m4private.h"
35 /* Maintain each of the builtins implemented in this modules along
36 with their details in a single table for easy maintenance.
38 function macros blind side minargs maxargs */
39 #define builtin_functions \
40 BUILTIN (mpeval, false, true, true, 1, 3 ) \
44 #define numb_set(ans, i) mpq_set (ans, i)
45 #define numb_set_si(ans, i) mpq_set_si (*(ans), (long) i, (unsigned long) 1)
47 #define numb_init(x) mpq_init (x)
48 #define numb_fini(x) mpq_clear (x)
50 #define numb_zerop(x) (mpq_cmp (x, numb_ZERO) == 0)
51 #define numb_positivep(x) (mpq_cmp (x, numb_ZERO) > 0)
52 #define numb_negativep(x) (mpq_cmp (x, numb_ZERO) < 0)
54 #define numb_eq(x, y) numb_set (x, mpq_cmp (x, y) == 0 ? numb_ONE : numb_ZERO)
55 #define numb_ne(x, y) numb_set (x, mpq_cmp (x, y) != 0 ? numb_ONE : numb_ZERO)
56 #define numb_lt(x, y) numb_set (x, mpq_cmp (x, y) < 0 ? numb_ONE : numb_ZERO)
57 #define numb_le(x, y) numb_set (x, mpq_cmp (x, y) <= 0 ? numb_ONE : numb_ZERO)
58 #define numb_gt(x, y) numb_set (x, mpq_cmp (x, y) > 0 ? numb_ONE : numb_ZERO)
59 #define numb_ge(x, y) numb_set (x, mpq_cmp (x, y) >= 0 ? numb_ONE : numb_ZERO)
61 #define numb_lnot(x) numb_set (x, numb_zerop (x) ? numb_ONE : numb_ZERO)
62 #define numb_lior(x, y) numb_set (x, numb_zerop (x) ? y : x)
63 #define numb_land(x, y) numb_set (x, numb_zerop (x) ? numb_ZERO : y)
65 #define reduce1(f1, x) \
75 #define reduce2(f2,x,y) \
86 #define numb_plus(x, y) reduce2 (mpq_add, x, y)
87 #define numb_minus(x, y) reduce2 (mpq_sub, x, y)
88 #define numb_negate(x) reduce1 (mpq_neg, x)
90 #define numb_times(x, y) reduce2 (mpq_mul, x, y)
91 #define numb_ratio(x, y) reduce2 (mpq_div, x, y)
92 #define numb_invert(x) reduce1 (mpq_inv, x)
94 #define numb_incr(n) numb_plus (n, numb_ONE)
95 #define numb_decr(n) numb_minus (n, numb_ONE)
97 /* Generate prototypes for each builtin handler function. */
98 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler)
103 /* Generate a table for mapping m4 symbol names to handler functions. */
104 static const m4_builtin m4_builtin_table
[] =
106 #define BUILTIN(handler, macros, blind, side, min, max) \
107 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
112 { NULL
, NULL
, 0, 0, 0 },
116 /* A table for mapping m4 symbol names to simple expansion text. */
117 static const m4_macro m4_macro_table
[] =
119 /* name text min max */
120 { "__mpeval__", "", 0, 0 },
121 { NULL
, NULL
, 0, 0 },
126 include_mpeval (m4
*context
, m4_module
*module
, m4_obstack
*obs
)
128 m4_install_builtins (context
, module
, m4_builtin_table
);
129 m4_install_macros (context
, module
, m4_macro_table
);
133 /* GMP defines mpq_t as a 1-element array of struct. Therefore, `mpq_t'
134 is not compatible with `const mpq_t'. */
135 typedef mpq_t number
;
137 static void numb_initialise (void);
138 static void numb_obstack (m4_obstack
*obs
, const number value
,
139 const int radix
, int min
);
140 static void mpq2mpz (m4
*context
, mpz_t z
, const number q
, const char *noisily
);
141 static void mpz2mpq (number q
, const mpz_t z
);
142 static void numb_divide (number
*x
, number
*y
);
143 static void numb_modulo (m4
*context
, number
*x
, number
*y
);
144 static void numb_and (m4
*context
, number
*x
, number
*y
);
145 static void numb_ior (m4
*context
, number
*x
, number
*y
);
146 static void numb_eor (m4
*context
, number
*x
, number
*y
);
147 static void numb_not (m4
*context
, number
*x
);
148 static void numb_lshift (m4
*context
, number
*x
, number
*y
);
149 static void numb_rshift (m4
*context
, number
*x
, number
*y
);
150 #define numb_urshift(c, x, y) numb_rshift (c, x, y)
153 static number numb_ZERO
;
154 static number numb_ONE
;
156 static int numb_initialised
= 0;
159 numb_initialise (void)
161 if (numb_initialised
)
164 numb_init (numb_ZERO
);
165 numb_set_si (&numb_ZERO
, 0);
167 numb_init (numb_ONE
);
168 numb_set_si (&numb_ONE
, 1);
170 numb_initialised
= 1;
174 numb_obstack (m4_obstack
*obs
, const number value
, const int radix
,
183 mpq_get_num (i
, value
);
184 s
= mpz_get_str (NULL
, radix
, i
);
188 obstack_1grow (obs
, '-');
192 for (min
-= len
; --min
>= 0;)
193 obstack_1grow (obs
, '0');
195 obstack_grow (obs
, s
, len
);
197 mpq_get_den (i
, value
);
198 if (mpz_cmp_si (i
, (long) 1) != 0)
200 obstack_1grow (obs
, '\\');
201 s
= mpz_get_str ((char *) 0, radix
, i
);
202 obstack_grow (obs
, s
, strlen (s
));
209 #define QUIET (char *)0
212 mpq2mpz (m4
*context
, mpz_t z
, const number q
, const char *noisily
)
214 if (noisily
&& mpz_cmp_si (mpq_denref (q
), (long) 1) != 0)
215 m4_warn (context
, 0, NULL
, _("loss of precision in eval: %s"), noisily
);
217 mpz_div (z
, mpq_numref (q
), mpq_denref (q
));
221 mpz2mpq (number q
, const mpz_t z
)
223 mpq_set_si (q
, (long) 0, (unsigned long) 1);
228 numb_divide (number
* x
, number
* y
)
234 mpq_div (qres
, *x
, *y
);
237 mpz_div (zres
, mpq_numref (qres
), mpq_denref (qres
));
245 numb_modulo (m4
*context
, number
* x
, number
* y
)
249 /* x should be integral */
250 /* y should be integral */
253 mpq2mpz (context
, xx
, *x
, NOISY
);
256 mpq2mpz (context
, yy
, *y
, NOISY
);
259 mpz_mod (res
, xx
, yy
);
269 numb_and (m4
*context
, number
* x
, number
* y
)
273 /* x should be integral */
274 /* y should be integral */
277 mpq2mpz (context
, xx
, *x
, NOISY
);
280 mpq2mpz (context
, yy
, *y
, NOISY
);
283 mpz_and (res
, xx
, yy
);
293 numb_ior (m4
*context
, number
* x
, number
* y
)
297 /* x should be integral */
298 /* y should be integral */
301 mpq2mpz (context
, xx
, *x
, NOISY
);
304 mpq2mpz (context
, yy
, *y
, NOISY
);
307 mpz_ior (res
, xx
, yy
);
317 numb_eor (m4
*context
, number
* x
, number
* y
)
321 /* x should be integral */
322 /* y should be integral */
325 mpq2mpz (context
, xx
, *x
, NOISY
);
328 mpq2mpz (context
, yy
, *y
, NOISY
);
333 mpz_xor (res
, xx
, yy
);
335 /* a^b = (a|b) & !(a&b) */
337 mpz_t and_ab
, ior_ab
, nand_ab
;
340 mpz_ior (ior_ab
, xx
, yy
);
343 mpz_and (and_ab
, xx
, yy
);
346 mpz_com (nand_ab
, and_ab
);
348 mpz_and (res
, ior_ab
, nand_ab
);
364 numb_not (m4
*context
, number
* x
)
368 /* x should be integral */
371 mpq2mpz (context
, xx
, *x
, NOISY
);
383 numb_lshift (m4
*context
, number
* x
, number
* y
)
387 /* x should be integral */
388 /* y should be integral */
391 mpq2mpz (context
, xx
, *x
, NOISY
);
394 mpq2mpz (context
, yy
, *y
, NOISY
);
398 /* bug: need to determine if y is too big or negative. */
399 long int exp
= mpz_get_si (yy
);
402 mpz_mul_2exp (res
, xx
, (unsigned) exp
);
406 mpz_div_2exp (res
, xx
, (unsigned) -exp
);
418 numb_rshift (m4
*context
, number
* x
, number
* y
)
422 /* x should be integral */
423 /* y should be integral */
426 mpq2mpz (context
, xx
, *x
, NOISY
);
429 mpq2mpz (context
, yy
, *y
, NOISY
);
433 /* FIXME: bug - need to determine if y is too big or negative */
434 long int exp
= mpz_get_si (yy
);
437 mpz_div_2exp (res
, xx
, (unsigned) exp
);
441 mpz_mul_2exp (res
, xx
, (unsigned) -exp
);
452 #define m4_evaluate builtin_mpeval
453 #include "evalparse.c"