Improve web-manual maintainer rule.
[m4.git] / modules / mpeval.c
blob2d63d6ec6fd7c8bf0443ec84b6d06e2148fc3781
1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 2000, 2001, 2006, 2007, 2008 Free Software
3 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/>.
21 #include <config.h>
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. */
25 #ifndef NDEBUG
26 # include <m4/m4module.h>
27 #else
28 # include "m4private.h"
29 #endif
31 #if HAVE_GMP_H
32 # include <gmp.h>
33 #endif
35 #include "quotearg.h"
37 /* Rename exported symbols for dlpreload()ing. */
38 #define m4_builtin_table mpeval_LTX_m4_builtin_table
39 #define m4_macro_table mpeval_LTX_m4_macro_table
42 /* Maintain each of the builtins implemented in this modules along
43 with their details in a single table for easy maintenance.
45 function macros blind side minargs maxargs */
46 #define builtin_functions \
47 BUILTIN (mpeval, false, true, true, 1, 3 ) \
51 #define numb_set(ans, i) mpq_set (ans, i)
52 #define numb_set_si(ans, i) mpq_set_si (*(ans), (long) i, (unsigned long) 1)
54 #define numb_init(x) mpq_init (x)
55 #define numb_fini(x) mpq_clear (x)
57 #define numb_zerop(x) (mpq_cmp (x, numb_ZERO) == 0)
58 #define numb_positivep(x) (mpq_cmp (x, numb_ZERO) > 0)
59 #define numb_negativep(x) (mpq_cmp (x, numb_ZERO) < 0)
61 #define numb_eq(x, y) numb_set (x, mpq_cmp (x, y) == 0 ? numb_ONE : numb_ZERO)
62 #define numb_ne(x, y) numb_set (x, mpq_cmp (x, y) != 0 ? numb_ONE : numb_ZERO)
63 #define numb_lt(x, y) numb_set (x, mpq_cmp (x, y) < 0 ? numb_ONE : numb_ZERO)
64 #define numb_le(x, y) numb_set (x, mpq_cmp (x, y) <= 0 ? numb_ONE : numb_ZERO)
65 #define numb_gt(x, y) numb_set (x, mpq_cmp (x, y) > 0 ? numb_ONE : numb_ZERO)
66 #define numb_ge(x, y) numb_set (x, mpq_cmp (x, y) >= 0 ? numb_ONE : numb_ZERO)
68 #define numb_lnot(x) numb_set (x, numb_zerop (x) ? numb_ONE : numb_ZERO)
69 #define numb_lior(x, y) numb_set (x, numb_zerop (x) ? y : x)
70 #define numb_land(x, y) numb_set (x, numb_zerop (x) ? numb_ZERO : y)
72 #define reduce1(f1, x) \
73 do \
74 { \
75 number T; \
76 mpq_init (T); \
77 f1 (T, x); \
78 mpq_set (x, T); \
79 mpq_clear (T); \
80 } \
81 while (0)
82 #define reduce2(f2,x,y) \
83 do \
84 { \
85 number T; \
86 mpq_init (T); \
87 f2 (T, (x), (y)); \
88 mpq_set ((x), T); \
89 mpq_clear (T); \
90 } \
91 while (0)
93 #define numb_plus(x, y) reduce2 (mpq_add, x, y)
94 #define numb_minus(x, y) reduce2 (mpq_sub, x, y)
95 #define numb_negate(x) reduce1 (mpq_neg, x)
97 #define numb_times(x, y) reduce2 (mpq_mul, x, y)
98 #define numb_ratio(x, y) reduce2 (mpq_div, x, y)
99 #define numb_invert(x) reduce1 (mpq_inv, x)
101 #define numb_incr(n) numb_plus (n, numb_ONE)
102 #define numb_decr(n) numb_minus (n, numb_ONE)
104 /* Generate prototypes for each builtin handler function. */
105 #define BUILTIN(handler, macros, blind, side, min, max) M4BUILTIN (handler)
106 builtin_functions
107 #undef BUILTIN
110 /* Generate a table for mapping m4 symbol names to handler functions. */
111 const m4_builtin m4_builtin_table[] =
113 #define BUILTIN(handler, macros, blind, side, min, max) \
114 M4BUILTIN_ENTRY (handler, #handler, macros, blind, side, min, max)
116 builtin_functions
117 #undef BUILTIN
119 { NULL, NULL, 0, 0, 0 },
123 /* A table for mapping m4 symbol names to simple expansion text. */
124 const m4_macro m4_macro_table[] =
126 /* name text min max */
127 { "__mpeval__", "", 0, 0 },
128 { NULL, NULL, 0, 0 },
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;
157 static void
158 numb_initialise (void)
160 if (numb_initialised)
161 return;
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;
172 static void
173 numb_obstack (m4_obstack *obs, const number value, const int radix,
174 int min)
176 const char *s;
177 size_t len;
179 mpz_t i;
180 mpz_init (i);
182 mpq_get_num (i, value);
183 s = mpz_get_str (NULL, radix, i);
185 if (*s == '-')
187 obstack_1grow (obs, '-');
188 s++;
190 len = strlen (s);
191 for (min -= len; --min >= 0;)
192 obstack_1grow (obs, '0');
194 obstack_grow (obs, s, len);
196 mpq_get_den (i, value);
197 if (mpz_cmp_si (i, (long) 1) != 0)
199 obstack_1grow (obs, '\\');
200 s = mpz_get_str ((char *) 0, radix, i);
201 obstack_grow (obs, s, strlen (s));
204 mpz_clear (i);
207 #define NOISY ""
208 #define QUIET (char *)0
210 static void
211 mpq2mpz (m4 *context, mpz_t z, const number q, const char *noisily)
213 if (noisily && mpz_cmp_si (mpq_denref (q), (long) 1) != 0)
214 m4_warn (context, 0, NULL, _("loss of precision in eval: %s"), noisily);
216 mpz_div (z, mpq_numref (q), mpq_denref (q));
219 static void
220 mpz2mpq (number q, const mpz_t z)
222 mpq_set_si (q, (long) 0, (unsigned long) 1);
223 mpq_set_num (q, z);
226 static void
227 numb_divide (number * x, number * y)
229 mpq_t qres;
230 mpz_t zres;
232 mpq_init (qres);
233 mpq_div (qres, *x, *y);
235 mpz_init (zres);
236 mpz_div (zres, mpq_numref (qres), mpq_denref (qres));
237 mpq_clear (qres);
239 mpz2mpq (*x, zres);
240 mpz_clear (zres);
243 static void
244 numb_modulo (m4 *context, number * x, number * y)
246 mpz_t xx, yy, res;
248 /* x should be integral */
249 /* y should be integral */
251 mpz_init (xx);
252 mpq2mpz (context, xx, *x, NOISY);
254 mpz_init (yy);
255 mpq2mpz (context, yy, *y, NOISY);
257 mpz_init (res);
258 mpz_mod (res, xx, yy);
260 mpz_clear (xx);
261 mpz_clear (yy);
263 mpz2mpq (*x, res);
264 mpz_clear (res);
267 static void
268 numb_and (m4 *context, number * x, number * y)
270 mpz_t xx, yy, res;
272 /* x should be integral */
273 /* y should be integral */
275 mpz_init (xx);
276 mpq2mpz (context, xx, *x, NOISY);
278 mpz_init (yy);
279 mpq2mpz (context, yy, *y, NOISY);
281 mpz_init (res);
282 mpz_and (res, xx, yy);
284 mpz_clear (xx);
285 mpz_clear (yy);
287 mpz2mpq (*x, res);
288 mpz_clear (res);
291 static void
292 numb_ior (m4 *context, number * x, number * y)
294 mpz_t xx, yy, res;
296 /* x should be integral */
297 /* y should be integral */
299 mpz_init (xx);
300 mpq2mpz (context, xx, *x, NOISY);
302 mpz_init (yy);
303 mpq2mpz (context, yy, *y, NOISY);
305 mpz_init (res);
306 mpz_ior (res, xx, yy);
308 mpz_clear (xx);
309 mpz_clear (yy);
311 mpz2mpq (*x, res);
312 mpz_clear (res);
315 static void
316 numb_eor (m4 *context, number * x, number * y)
318 mpz_t xx, yy, res;
320 /* x should be integral */
321 /* y should be integral */
323 mpz_init (xx);
324 mpq2mpz (context, xx, *x, NOISY);
326 mpz_init (yy);
327 mpq2mpz (context, yy, *y, NOISY);
329 mpz_init (res);
331 #if 0
332 mpz_xor (res, xx, yy);
333 #else /* 0 */
334 /* a^b = (a|b) & !(a&b) */
336 mpz_t and_ab, ior_ab, nand_ab;
338 mpz_init (ior_ab);
339 mpz_ior (ior_ab, xx, yy);
341 mpz_init (and_ab);
342 mpz_and (and_ab, xx, yy);
344 mpz_init (nand_ab);
345 mpz_com (nand_ab, and_ab);
347 mpz_and (res, ior_ab, nand_ab);
349 mpz_clear (and_ab);
350 mpz_clear (ior_ab);
351 mpz_clear (nand_ab);
353 #endif /* 0 */
355 mpz_clear (xx);
356 mpz_clear (yy);
358 mpz2mpq (*x, res);
359 mpz_clear (res);
362 static void
363 numb_not (m4 *context, number * x)
365 mpz_t xx, res;
367 /* x should be integral */
369 mpz_init (xx);
370 mpq2mpz (context, xx, *x, NOISY);
372 mpz_init (res);
373 mpz_com (res, xx);
375 mpz_clear (xx);
377 mpz2mpq (*x, res);
378 mpz_clear (res);
381 static void
382 numb_lshift (m4 *context, number * x, number * y)
384 mpz_t xx, yy, res;
386 /* x should be integral */
387 /* y should be integral */
389 mpz_init (xx);
390 mpq2mpz (context, xx, *x, NOISY);
392 mpz_init (yy);
393 mpq2mpz (context, yy, *y, NOISY);
395 mpz_init (res);
397 /* bug: need to determine if y is too big or negative. */
398 long int exp = mpz_get_si (yy);
399 if (exp >= 0)
401 mpz_mul_2exp (res, xx, (unsigned) exp);
403 else
405 mpz_div_2exp (res, xx, (unsigned) -exp);
409 mpz_clear (xx);
410 mpz_clear (yy);
412 mpz2mpq (*x, res);
413 mpz_clear (res);
416 static void
417 numb_rshift (m4 *context, number * x, number * y)
419 mpz_t xx, yy, res;
421 /* x should be integral */
422 /* y should be integral */
424 mpz_init (xx);
425 mpq2mpz (context, xx, *x, NOISY);
427 mpz_init (yy);
428 mpq2mpz (context, yy, *y, NOISY);
430 mpz_init (res);
432 /* FIXME: bug - need to determine if y is too big or negative */
433 long int exp = mpz_get_si (yy);
434 if (exp >= 0)
436 mpz_div_2exp (res, xx, (unsigned) exp);
438 else
440 mpz_mul_2exp (res, xx, (unsigned) -exp);
444 mpz_clear (xx);
445 mpz_clear (yy);
447 mpz2mpq (*x, res);
448 mpz_clear (res);
451 #define m4_evaluate builtin_mpeval
452 #include "evalparse.c"