PIPS
arithmetique.h
Go to the documentation of this file.
1 /* Warning! Do not modify this file that is automatically generated! */
2 /* Modify src/Libs/arithmetique/arithmetique-local.h instead, to add your own modifications. */
3 
4 /* header file built by cproto */
5 
6 #ifndef arithmetique_header_included
7 #define arithmetique_header_included
8 /* arithmetique-local.h */
9 /*
10 
11  $Id: arithmetique-local.h 1673 2023-07-10 12:21:00Z coelho $
12 
13  Copyright 1989-2016 MINES ParisTech
14 
15  This file is part of Linear/C3 Library.
16 
17  Linear/C3 Library is free software: you can redistribute it and/or modify it
18  under the terms of the GNU Lesser General Public License as published by
19  the Free Software Foundation, either version 3 of the License, or
20  any later version.
21 
22  Linear/C3 Library is distributed in the hope that it will be useful, but
23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24  FITNESS FOR A PARTICULAR PURPOSE.
25 
26  See the GNU Lesser General Public License for more details.
27 
28  You should have received a copy of the GNU Lesser General Public License
29  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
30 
31 */
32 
33 /* package arithmetique
34  *
35  * Francois Irigoin, mai 1989
36  *
37  * Modifications
38  * - reprise de DIVIDE qui etait faux (Remi Triolet, Francois Irigoin,
39  * april 90)
40  * - simplification de POSITIVE_DIVIDE par suppression d'un modulo
41  */
42 
43 /* We would like linear to be generic about the "integer" type used
44  * to represent integer values. Thus Value is defined here. It should
45  * be changed to "int" "long" or "long long". In an ideal world,
46  * any source modification should be limited to this package.
47  *
48  * Indeed, we cannot switch easily to bignums that need constructors
49  * dans destructors... That would lead to too many modifications...
50  * C++ would make things easier and cleaner...
51  *
52  * Fabien COELHO
53  */
54 
55 /* for FILE *
56  */
57 #include <stdio.h>
58 
59 /* to be included for _MIN and _MAX: #include <limits.h>
60  */
61 #include <limits.h>
62 #include "boolean.h"
63 
64 /******************************************************** TIMEOUT MANAGEMENT */
65 
66 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
67 # define CURRENT_FUNCTION __func__
68 # else
69 # if defined(__GNUC__)
70 # define CURRENT_FUNCTION __FUNCTION__
71 # else
72 # define CURRENT_FUNCTION "<unknown>"
73 # endif // __GNUC__
74 # endif // C99
75 
76 #define PUSH_TIMEOUT_ENV(env) \
77  push_timeout_env(env, CURRENT_FUNCTION, __FILE__, __LINE__)
78 
79 #define POP_TIMEOUT_ENV(env) \
80  pop_timeout_env(env, CURRENT_FUNCTION, __FILE__, __LINE__)
81 
82 #define PUSH_TIMEOUT(delay) \
83  push_timeout(delay, CURRENT_FUNCTION, __FILE__, __LINE__)
84 
85 #define POP_TIMEOUT() \
86  pop_timeout(CURRENT_FUNCTION, __FILE__, __LINE__)
87 
88 /* put there because I cannot have these constants with ansi options.
89  */
90 #ifndef LONG_LONG_MAX
91 
92 /* would fix on solaris:
93  * #define LONG_LONG_MAX LLONG_MAX
94  * #define LONG_LONG_MIN LLONG_MIN
95  */
96 
97 #ifndef __LONG_LONG_MAX__
98 #define __LONG_LONG_MAX__ 9223372036854775807LL
99 #endif
100 #undef LONG_LONG_MAX
101 #define LONG_LONG_MAX __LONG_LONG_MAX__
102 #undef LONG_LONG_MIN
103 #define LONG_LONG_MIN (-LONG_LONG_MAX-1)
104 #undef ULONG_LONG_MAX
105 #define ULONG_LONG_MAX (LONG_LONG_MAX * 2ULL + 1)
106 #endif
107 
108 /*
109  # ##### #####
110  # # # ##### ## # # # #
111  # ## # # # # # # #
112  # # # # # # ##### #####
113  # # # # # # # # #
114  # # ## # # # # #
115  # # # # ##### ####### #####
116  */
117 
118 #if defined(LINEAR_VALUE_IS_INT128)
119 // TODO
120 #error "not implemented yet"
121 
122 #include <inttypes.h>
123 #define LINEAR_VALUE_STRING "int128"
124 typedef __int128 Value;
125 #define VALUE_FMT "???"
126 #define VALUE_CONST(val) (val##LLL)
127 #define VALUE_NAN ???_MIN
128 /* CAUTION! the min is defined as hard-min+1 so as to preserve the
129  * symetry (-min==max) and to have a nan value. FC.
130  */
131 #define VALUE_MIN (???_MIN+1LL)
132 #define VALUE_MAX ???_MAX
133 #if defined(LINEAR_VALUE_ASSUME_SOFTWARE_IDIV) && \
134  defined(__SIZEOF_???__) && \
135  __SIZEOF_???__ != 8
136  #error "long long is expected to be 64-bit to use software idiv"
137 #endif
138 #define VALUE_SQRT_MIN ???LLL /* floor(sqrt(LONG_LONG_MAX)) */
139 #define VALUE_SQRT_MAX -???LLL
140 #define VALUE_ZERO (0LLL)
141 #define VALUE_ONE (1LLL)
142 #define VALUE_MONE (-1LLL)
143 /* I can just trust myself for this...
144  */
145 #define VALUE_TO_LONG(val) \
146  ((long)((val)>(Value)LONG_MIN&&(val)<=(Value)LONG_MAX)?\
147  (val):(THROW(overflow_error), LONG_MIN))
148 #define VALUE_TO_INT(val) \
149  ((int)((val)>(Value)INT_MIN&&(val)<=(Value)INT_MAX)?\
150  (val):(THROW(overflow_error), INT_MIN))
151 #define VALUE_TO_DOUBLE(val) ((double)(val))
152 #define VALUE_TO_FLOAT(val) ((float)((int)(val)))
153 
154 // end LINEAR_VALUE_IS_INT128
155 
156 
157 /*
158  # #### # # #### # #### # # ####
159  # # # ## # # # # # # ## # # #
160  # # # # # # # # # # # # # #
161  # # # # # # # ### # # # # # # # ###
162  # # # # ## # # # # # # ## # #
163  ###### #### # # #### ###### #### # # ####
164 
165  */
166 
167 #elif defined(LINEAR_VALUE_IS_LONGLONG)
168 #define LINEAR_VALUE_STRING "long long int"
169 typedef long long int Value;
170 #define VALUE_FMT "%lld"
171 #define VALUE_CONST(val) (val##LL)
172 #define VALUE_NAN LONG_LONG_MIN
173 /* CAUTION! the min is defined as hard-min+1 so as to preserve the
174  * symetry (-min==max) and to have a nan value. FC.
175  */
176 #define VALUE_MIN (LONG_LONG_MIN+1LL)
177 #define VALUE_MAX LONG_LONG_MAX
178 #if defined(LINEAR_VALUE_ASSUME_SOFTWARE_IDIV) && \
179  defined(__SIZEOF_LONG_LONG__) && \
180  __SIZEOF_LONG_LONG__ != 8
181  #error "long long is expected to be 64-bit to use software idiv"
182 #endif
183 #define VALUE_SQRT_MIN 3037000499LL /* floor(sqrt(LONG_LONG_MAX)) */
184 #define VALUE_SQRT_MAX -3037000499LL
185 #define VALUE_ZERO (0LL)
186 #define VALUE_ONE (1LL)
187 #define VALUE_MONE (-1LL)
188 /* I can just trust myself for this...
189  */
190 #define VALUE_TO_LONG(val) \
191  ((long)((val)>(Value)LONG_MIN&&(val)<=(Value)LONG_MAX)?\
192  (val):(THROW(overflow_error), LONG_MIN))
193 #define VALUE_TO_INT(val) \
194  ((int)((val)>(Value)INT_MIN&&(val)<=(Value)INT_MAX)?\
195  (val):(THROW(overflow_error), INT_MIN))
196 #define VALUE_TO_DOUBLE(val) ((double)(val))
197 /* FI: I do not understand why, but the first definition isn't working with gcc */
198 /* #define VALUE_TO_FLOAT(val) ((float)(val)) */
199 #define VALUE_TO_FLOAT(val) ((float)((int)(val)))
200 
201 /* end LINEAR_VALUE_IS_LONGLONG
202  */
203 
204 /*
205  # #### # # ####
206  # # # ## # # #
207  # # # # # # #
208  # # # # # # # ###
209  # # # # ## # #
210  ###### #### # # ####
211 
212  */
213 
214 #elif defined(LINEAR_VALUE_IS_LONG)
215 #define LINEAR_VALUE_STRING "long int"
216 typedef long Value;
217 #define VALUE_FMT "%ld"
218 #define VALUE_CONST(val) (val##L)
219 #define VALUE_NAN LONG_MIN
220 #define VALUE_MIN (LONG_MIN+1L)
221 #define VALUE_MAX LONG_MAX
222 #define VALUE_ZERO 0L
223 #define VALUE_ONE 1L
224 #define VALUE_MONE -1L
225 #define VALUE_TO_LONG(val) (val)
226 #define VALUE_TO_INT(val) ((int)(val))
227 #define VALUE_TO_FLOAT(val) ((float)(val))
228 #define VALUE_TO_DOUBLE(val) ((double)(val))
229 
230 /* end LINEAR_VALUE_IS_LONG
231  */
232 
233 /*
234  ###### # #### ## #####
235  # # # # # # #
236  ##### # # # # # #
237  # # # # ###### #
238  # # # # # # #
239  # ###### #### # # #
240 
241  */
242 /*
243 #elif defined(LINEAR_VALUE_IS_FLOAT)
244 #define LINEAR_VALUE_STRING "float"
245 typedef float Value;
246 #define VALUE_FMT "%f"
247 #define VALUE_CONST(val) (val)
248 #define VALUE_MIN FLOAT_MIN
249 #define VALUE_MAX FLOAT_MAX
250 #define VALUE_ZERO 0.0
251 #define VALUE_ONE 1.0
252 #define VALUE_MONE -1.0
253 #define VALUE_TO_LONG(val) ((long)(val))
254 #define VALUE_TO_INT(val) ((int)(val))
255 #define VALUE_TO_FLOAT(val) ((float)(val))
256 #define VALUE_TO_DOUBLE(val) ((double)(val))
257 */
258 
259 /* end LINEAR_VALUE_IS_FLOAT
260  */
261 
262 /*
263  #### # # ## ##### # #
264  # # # # # # # # # #
265  # ###### # # # # #######
266  # # # ###### ##### # #
267  # # # # # # # # # #
268  #### # # # # # #
269 
270  */
271 
272 /* the purpose of the chars version is to detect invalid assignments
273  */
274 #elif defined(LINEAR_VALUE_IS_CHARS)
275 #define LINEAR_VALUE_STRING "chars..."
276 typedef union { char *s; long l; int i; float f; double d;} Value;
277 #define VALUE_FMT "%s"
278 #define VALUE_CONST(val) ((Value)(val))
279 #define VALUE_NAN ((Value)(long)0xdadeebee)
280 #define VALUE_MIN ((Value)(long)0xdeadbeef)
281 #define VALUE_MAX ((Value)(long)0xfeedabee)
282 #define VALUE_ZERO ((Value)0)
283 #define VALUE_ONE ((Value)1)
284 #define VALUE_MONE ((Value)-1)
285 #define VALUE_TO_LONG(val) (val.l)
286 #define VALUE_TO_INT(val) (val.i)
287 #define VALUE_TO_FLOAT(val) (val.f)
288 #define VALUE_TO_DOUBLE(val) (val.d)
289 
290 /* end LINEAR_VALUE_IS_CHARS
291  */
292 
293 /*
294  # # # #####
295  # ## # #
296  # # # # #
297  # # # # #
298  # # ## #
299  # # # #
300 
301  */
302 #else /* default: LINEAR_VALUE_IS_INT */
303 #define LINEAR_VALUE_STRING "int"
304 typedef int Value;
305 #define VALUE_FMT "%d"
306 #define VALUE_CONST(val) (val)
307 #define VALUE_NAN INT_MIN
308 #define VALUE_MIN (INT_MIN+1)
309 #define VALUE_MAX INT_MAX
310 #define VALUE_ZERO 0
311 #define VALUE_ONE 1
312 #define VALUE_MONE -1
313 #define VALUE_TO_LONG(val) ((long)(val))
314 #define VALUE_TO_INT(val) ((int)(val))
315 #define VALUE_TO_FLOAT(val) ((float)(val))
316 #define VALUE_TO_DOUBLE(val) ((double)(val))
317 /* end LINEAR_VALUE_IS_INT
318  */
319 #endif
320 
321 /************************** ************ MACROS FOR MANIPULATING VALUES... */
322 
323 /* cast to value
324  */
325 #define int_to_value(i) ((Value)(i))
326 #define long_to_value(l) ((Value)(l))
327 #define float_to_value(f) ((Value)(f))
328 #define double_to_value(d) ((Value)(d))
329 
330 /* bool operators on values
331  */
332 #define value_eq(v1,v2) ((v1)==(v2))
333 #define value_ne(v1,v2) ((v1)!=(v2))
334 #define value_gt(v1,v2) ((v1)>(v2))
335 #define value_ge(v1,v2) ((v1)>=(v2))
336 #define value_lt(v1,v2) ((v1)<(v2))
337 #define value_le(v1,v2) ((v1)<=(v2))
338 
339 /* trian operators on values
340  */
341 #define value_sign(v) (value_eq(v,VALUE_ZERO)?0:value_lt(v,VALUE_ZERO)?-1:1)
342 #define value_compare(v1,v2) (value_eq(v1,v2)?0:value_lt(v1,v2)?-1:1)
343 
344 /* binary operators on values
345  *
346  * pdiv and pmod always return a positive remainder and a positive
347  * modulo. E.g. -1/100 = -1 and its remainder is 99. The modulo
348  * operator is periodic and not symmetric around zero.
349  */
350 #define value_plus(v1,v2) ((v1)+(v2))
351 #define value_div(v1,v2) ((v1)/(v2))
352 #define value_mod(v1,v2) ((v1)%(v2))
353 #define value_direct_multiply(v1,v2) ((v1)*(v2)) /* direct! */
354 #define value_minus(v1,v2) ((v1)-(v2))
355 #define value_pdiv(v1,v2) (divide(v1,v2))
356 #define value_pmod(v1,v2) (modulo(v1,v2))
357 #define value_min(v1,v2) (value_le(v1,v2)? (v1): (v2))
358 #define value_max(v1,v2) (value_ge(v1,v2)? (v1): (v2))
359 #define value_or(v1,v2) ((v1)|(v2))
360 #define value_and(v1,v2) ((v1)&(v2))
361 #define value_lshift(v1,v2) ((v1)<<(v2))
362 #define value_rshift(v1,v2) ((v1)>>(v2))
363 
364 /* assigments
365  */
366 #define value_assign(ref,val) (ref=(val))
367 #define value_addto(ref,val) (ref+=(val))
368 #define value_increment(ref) (ref++)
369 #define value_direct_product(ref,val) (ref*=(val)) /* direct! */
370 #define value_multiply(ref,val) value_assign(ref,value_mult(ref,val))
371 #define value_substract(ref,val) (ref-=(val))
372 #define value_decrement(ref) (ref--)
373 #define value_division(ref,val) (ref/=(val))
374 #define value_modulus(ref,val) (ref%=(val))
375 #define value_pdivision(ref,val) value_assign(ref,value_pdiv(ref,val))
376 #define value_oppose(ref) value_assign(ref,value_uminus(ref))
377 #define value_absolute(ref) value_assign(ref,value_abs(ref))
378 #define value_minimum(ref,val) value_assign(ref,value_min(ref,val))
379 #define value_maximum(ref,val) value_assign(ref,value_max(ref,val))
380 #define value_orto(ref,val) (ref |= (val))
381 #define value_andto(ref,val) (ref &= (val))
382 
383 /* unary operators on values
384  */
385 #define value_uminus(val) (-(val))
386 #define value_not(val) (~(val))
387 #define value_abs(val) \
388  ((value_posz_p(val) ? (val) : \
389  (value_ne((val), VALUE_NAN) ? value_uminus(val) : \
390  (THROW (overflow_error), VALUE_NAN ))))
391 
392 #define value_pos_p(val) value_gt(val,VALUE_ZERO)
393 #define value_neg_p(val) value_lt(val,VALUE_ZERO)
394 #define value_posz_p(val) value_ge(val,VALUE_ZERO)
395 #define value_negz_p(val) value_le(val,VALUE_ZERO)
396 #define value_zero_p(val) value_eq(val,VALUE_ZERO)
397 // No underscore between "not" and "zero": value_not_zero_p()
398 // Added to improve retrieval
399 #define value_notzero_p(val) value_ne(val,VALUE_ZERO)
400 #define value_one_p(val) value_eq(val,VALUE_ONE)
401 #define value_notone_p(val) value_ne(val,VALUE_ONE)
402 #define value_mone_p(val) value_eq(val,VALUE_MONE)
403 #define value_notmone_p(val) value_ne(val,VALUE_MONE)
404 #define value_min_p(val) value_eq(val,VALUE_MIN)
405 #define value_max_p(val) value_eq(val,VALUE_MAX)
406 #define value_notmin_p(val) value_ne(val,VALUE_MIN)
407 #define value_notmax_p(val) value_ne(val,VALUE_MAX)
408 
409 
410 /************************************************* PROTECTED MULTIPLICATION */
411 #include "arithmetic_errors.h"
412 
413 /* (|v| < MAX / |w|) => v*w is okay
414  * I could check ((v*w)/w)==v but a tmp would be useful
415  */
416 #define value_protected_hard_idiv_multiply(v,w,throw) \
417  ((value_zero_p(w) || value_zero_p(v))? VALUE_ZERO: \
418  value_lt(value_abs(v),value_div(VALUE_MAX,value_abs(w)))? \
419  value_direct_multiply(v,w): (throw, VALUE_NAN))
420 
421 /* is a software idiv is assumed, quick check performed first
422  */
423 #if defined(LINEAR_VALUE_ASSUME_SOFTWARE_IDIV)
424 #define value_protected_multiply(v,w,throw) \
425  ((value_le(v,VALUE_SQRT_MAX) && value_le(w,VALUE_SQRT_MAX) && \
426  value_ge(v,VALUE_SQRT_MIN) && value_ge(w,VALUE_SQRT_MIN))? \
427  value_direct_multiply(v,w): value_protected_hard_idiv_multiply(v,w,throw))
428 #else
429 #define value_protected_multiply(v,w,throw) \
430  value_protected_hard_idiv_multiply(v,w,throw)
431 #endif
432 
433 /* protected versions
434  */
435 #define value_protected_mult(v,w) \
436  value_protected_multiply(v,w,THROW(overflow_error))
437 #define value_protected_product(v,w) \
438  v=value_protected_mult(v,w)
439 
440 /* whether the default is protected or not
441  * this define makes no sense any more... well, doesn't matter. FC.
442  */
443 #if defined(LINEAR_VALUE_PROTECT_MULTIPLY)
444 #define value_mult(v,w) value_protected_mult(v,w)
445 #define value_product(v,w) value_protected_product(v,w)
446 #else
447 
448 /* I do enforce the protection whatever requested:-)
449  * prints out a message and throws the exception, hoping
450  * that some valid CATCH waits for it upwards.
451  */
452 #define value_mult(v,w) \
453  value_protected_multiply(v,w, \
454  (fprintf(stderr,"[value_mult] value overflow!\n"),THROW(overflow_error)))
455 #define value_product(v,w) v=value_mult(v,w)
456 
457 /* was:
458  * #define value_mult(v,w) value_direct_multiply(v,w)
459  * #define value_product(v,w) value_direct_product(v,w)
460  * could be: protected versions...
461  */
462 #endif
463 
464 /******************************************************* STATIC VALUE DEBUG */
465 
466 /* LINEAR_VALUE_IS_CHARS is used for type checking.
467  * some operations are not allowed on (char*), thus
468  * they are switched to some other operation here...
469  */
470 #if defined(LINEAR_VALUE_IS_CHARS)
471 #define value_fake_binary(v1,v2) ((Value)((v1).i+(v2).i))
472 #define value_bool_binary(v1,v2) ((int)((v1).i+(v2).i))
473 #undef float_to_value
474 #define float_to_value(f) ((Value)f)
475 #undef double_to_value
476 #define double_to_value(f) ((Value)f)
477 #undef value_uminus
478 #define value_uminus(v) (v)
479 #undef value_mult
480 #define value_mult(v1,v2) value_fake_binary(v1,v2)
481 #undef value_mod
482 #define value_mod(v1,v2) value_fake_binary(v1,v2)
483 #undef value_ge
484 #define value_ge(v1,v2) value_bool_binary(v1,v2)
485 #undef value_gt
486 #define value_gt(v1,v2) value_bool_binary(v1,v2)
487 #undef value_le
488 #define value_le(v1,v2) value_bool_binary(v1,v2)
489 #undef value_lt
490 #define value_lt(v1,v2) value_bool_binary(v1,v2)
491 #undef value_ne
492 #define value_ne(v1,v2) value_bool_binary(v1,v2)
493 #undef value_eq
494 #define value_eq(v1,v2) value_bool_binary(v1,v2)
495 #undef value_plus
496 #define value_plus(v1,v2) value_fake_binary(v1,v2)
497 #undef value_minus
498 #define value_minus(v1,v2) value_fake_binary(v1,v2)
499 #undef value_pdiv
500 #define value_pdiv(v1,v2) value_fake_binary(v1,v2)
501 #undef value_div
502 #define value_div(v1,v2) value_fake_binary(v1,v2)
503 #undef value_mod
504 #define value_mod(v1,v2) value_fake_binary(v1,v2)
505 #undef value_addto
506 #define value_addto(v1,v2) value_assign(v1,value_plus(v1,v2))
507 #undef value_substract
508 #define value_substract(v1,v2) value_addto(v1,v2)
509 #undef value_product
510 #define value_product(v1,v2) value_addto(v1,v2)
511 #undef value_modulus
512 #define value_modulus(v1,v2) value_addto(v1,v2)
513 #undef value_division
514 #define value_division(v1,v2) value_addto(v1,v2)
515 #undef value_increment
516 #define value_increment(v) value_addto(v,VALUE_ONE)
517 #undef value_decrement
518 #define value_decrement(v) value_addto(v,VALUE_MONE)
519 #undef value_orto
520 #define value_orto(ref,val) value_addto(v1,v2)
521 #undef value_andto
522 #define value_andto(ref,val) value_addto(v1,v2)
523 #undef value_or
524 #define value_or(v1,v2) value_fake_binary(v1,v2)
525 #undef value_and
526 #define value_and(v1,v2) value_fake_binary(v1,v2)
527 #undef value_lshift
528 #define value_lshift(v1,v2) value_fake_binary(v1,v2)
529 #undef value_rshift
530 #define value_rshift(v1,v2) value_fake_binary(v1,v2)
531 #endif
532 
533 
534 /* valeur absolue
535  */
536 #ifndef ABS
537 #define ABS(x) (((x)>=0) ? (x) : -(x))
538 #endif
539 
540 /* minimum and maximum
541  * if they are defined somewhere else, they are very likely
542  * to be defined the same way. Thus the previous def is not overwritten.
543  */
544 #ifndef MIN
545 #define MIN(x,y) (((x)>=(y))?(y):(x))
546 #endif
547 #ifndef MAX
548 #define MAX(x,y) (((x)>=(y))?(x):(y))
549 #endif
550 
551 /* signe d'un entier: -1, 0 ou 1 */
552 #define SIGN(x) (((x)>0)? 1 : ((x)==0? 0 : -1))
553 
554 /* division avec reste toujours positif
555  * basee sur les equations:
556  * a/(-b) = - (a/b)
557  * (-a)/b = - ((a+b-1)/b)
558  * ou a et b sont des entiers positifs
559  */
560 #define DIVIDE(x,y) ((y)>0? POSITIVE_DIVIDE(x,y) : \
561  -POSITIVE_DIVIDE((x),(-(y))))
562 
563 /* division avec reste toujours positif quand y est positif: assert(y>=0) */
564 #define POSITIVE_DIVIDE(x,y) ((x)>0 ? (x)/(y) : - (-(x)+(y)-1)/(y))
565 
566 /* modulo a resultat toujours positif */
567 #define MODULO(x,y) ((y)>0 ? POSITIVE_MODULO(x,y) : POSITIVE_MODULO(-x,-y))
568 
569 /* modulo par rapport a un nombre positif: assert(y>=0)
570  *
571  * Ce n'est pas la macro la plus efficace que j'aie jamais ecrite: il faut
572  * faire, dans le pire des cas, deux appels a la routine .rem, qui n'est
573  * surement pas plus cablee que la division ou la multiplication
574  */
575 #define POSITIVE_MODULO(x,y) ((x) > 0 ? (x)%(y) : \
576  ((x)%(y) == 0 ? 0 : ((y)-(-(x))%(y))))
577 
578 /* Pour la recherche de performance, selection d'une implementation
579  * particuliere des fonctions
580  */
581 
582 #define pgcd(a,b) pgcd_slow(a,b)
583 
584 #define divide(a,b) DIVIDE(a,b)
585 
586 #define modulo(a,b) MODULO(a,b)
587 
588 typedef struct { Value num, den; int numero ; } frac ;
589 typedef struct col{ int taille, existe ; frac *colonne ;} tableau ;
590 
591 // manual declaration, as cproto seems to ignore attributes
592 extern void __attribute__ ((weak, noreturn))
593 throw_exception(const int, const char *, const char *, const int);
594 
595 // for some yacc-generated code
596 #ifndef _has_yy_size_t
597 #define _has_yy_size_t
598 typedef size_t yy_size_t;
599 #endif /* _has_yy_size_t */
600 
601 /* end of arithmetique-local.h */
602 /* cproto-generated files */
603 /* abs.c */
604 extern Value abs_ofl_ctrl(Value /*i*/, int /*ofl_ctrl*/);
605 extern Value absval_ofl(Value /*i*/);
606 extern Value absval(Value /*i*/);
607 /* divide.c */
608 extern Value divide_fast(Value /*a*/, Value /*b*/);
609 extern Value divide_slow(Value /*a*/, Value /*b*/);
610 /* exp.c */
611 extern Value exponentiate(Value /*x*/, int /*n*/);
612 /* modulo.c */
613 extern Value modulo_fast(Value /*a*/, Value /*b*/);
614 /* pgcd.c */
615 extern Value pgcd_slow(Value /*a*/, Value /*b*/);
616 extern Value pgcd_fast(Value /*a*/, Value /*b*/);
617 extern Value pgcd_interne(Value /*a*/, Value /*b*/);
618 extern Value gcd_subtract(Value /*a*/, Value /*b*/);
619 extern Value vecteur_bezout(Value /*u*/[], Value /*v*/[], int /*l*/);
620 extern Value bezout(Value /*a*/, Value /*b*/, Value */*x*/, Value */*y*/);
621 extern Value bezout_grl(Value /*a*/, Value /*b*/, Value */*x*/, Value */*y*/);
622 /* ppcm.c */
623 extern Value ppcm(Value /*i*/, Value /*j*/);
624 /* io.c */
625 extern void print_Value(Value /*v*/);
626 extern void fprint_Value(FILE */*f*/, Value /*v*/);
627 extern void fprint_string_Value(FILE */*f*/, char */*blah*/, Value /*v*/);
628 extern void sprint_Value(char */*s*/, Value /*v*/);
629 extern int fscan_Value(FILE */*f*/, Value */*pv*/);
630 extern int scan_Value(Value */*pv*/);
631 extern int sscan_Value(char */*s*/, Value */*pv*/);
632 extern char *Value_to_string(Value /*v*/);
633 /* errors.c */
634 extern int linear_assertion_result;
635 extern char const *get_exception_name(const linear_exception_t /*exception*/);
636 extern void linear_reset_error_counters(void);
637 extern unsigned int linear_error_count(void);
638 extern void linear_get_error_counts(unsigned int */*poe*/, unsigned int */*pse*/, unsigned int */*pme*/);
642 extern void dump_exception_stack_to_file(FILE */*f*/);
643 extern void dump_exception_stack(void);
644 extern jmp_buf *linear_push_exception_on_stack(const int /*what*/, const char */*function*/, const char */*file*/, const int /*line*/);
645 extern jmp_buf *push_exception_on_stack(const int /*what*/, const char */*function*/, const char */*file*/, const int /*line*/);
646 extern void linear_pop_exception_from_stack(const int /*what*/, const char */*function*/, const char */*file*/, const int /*line*/);
647 extern void pop_exception_from_stack(const int /*what*/, const char */*function*/, const char */*file*/, const int /*line*/);
648 extern void linear_throw_exception(const int /*what*/, const char */*function*/, const char */*file*/, const int /*line*/);
649 extern void throw_exception(const int /*what*/, const char */*function*/, const char */*file*/, const int /*line*/);
650 extern void linear_initialize_exception_stack(unsigned int /*verbose_exceptions*/, exception_callback_t /*push*/, exception_callback_t /*pop*/);
651 extern bool linear_with_gmp(void);
652 extern bool linear_require_gmp(void);
653 extern bool linear_use_gmp(void);
654 extern void set_timeout_callback(timeout_callback_f /*callback*/);
655 extern void push_timeout(const unsigned int /*delay*/, const char */*funcname*/, const char */*filename*/, const int /*lineno*/);
656 extern bool push_timeout_env(const char */*env*/, const char */*funcname*/, const char */*filename*/, const int /*lineno*/);
657 extern void pop_timeout(const char */*funcname*/, const char */*filename*/, const int /*lineno*/);
658 extern void pop_timeout_env(const char */*env*/, const char */*funcname*/, const char */*filename*/, const int /*lineno*/);
659 /* value.c */
660 extern int value_comparison(Value /*v1*/, Value /*v2*/);
661 #endif /* arithmetique_header_included */
linear_exception_t
Global constants to designate exceptions.
void(* exception_callback_t)(char const *, char const *, int const)
void(* timeout_callback_f)(int, const char *, const char *, int)
callback for timeout expecting: delay, function, file, lineno
int Value
void pop_timeout_env(const char *, const char *, const char *, const int)
Definition: errors.c:619
linear_exception_t the_last_just_thrown_exception
Value divide_fast(Value, Value)
divide.c
Definition: divide.c:51
void linear_reset_error_counters(void)
reset linear counters
Definition: errors.c:75
void fprint_Value(FILE *, Value)
Definition: io.c:42
void set_exception_callbacks(exception_callback_t, exception_callback_t)
Value pgcd_interne(Value, Value)
int pgcd_interne(int a, int b): calcul iteratif du pgcd de deux entiers strictement positifs tels que...
Definition: pgcd.c:106
Value abs_ofl_ctrl(Value, int)
_has_yy_size_t
Definition: abs.c:38
void __attribute__((weak, noreturn)) throw_exception(const int
void linear_get_error_counts(unsigned int *, unsigned int *, unsigned int *)
return various errors counts through unsigned int pointer overflow, simplex & misc (aka others) NULL ...
Definition: errors.c:94
void pop_timeout(const char *, const char *, const int)
Value ppcm(Value, Value)
ppcm.c
Definition: ppcm.c:42
int linear_number_of_exception_thrown
bool linear_require_gmp(void)
whether linear is asked to use gmp if possible (env variable)
Definition: errors.c:446
int sscan_Value(char *, Value *)
Definition: io.c:68
void linear_initialize_exception_stack(unsigned int, exception_callback_t, exception_callback_t)
jmp_buf * linear_push_exception_on_stack(const int, const char *, const char *, const int)
jmp_buf * push_exception_on_stack(const int, const char *, const char *, const int)
void fprint_string_Value(FILE *, char *, Value)
Definition: io.c:47
void push_timeout(const unsigned int, const char *, const char *, const int)
Definition: errors.c:539
void set_timeout_callback(timeout_callback_f)
Definition: errors.c:485
int value_comparison(Value, Value)
value.c
Definition: value.c:36
void sprint_Value(char *, Value)
Definition: io.c:53
void dump_exception_stack(void)
int fscan_Value(FILE *, Value *)
Definition: io.c:58
void pop_exception_from_stack(const int, const char *, const char *, const int)
Value bezout_grl(Value, Value, Value *, Value *)
int bezout_grl(int a, int b, int *x, int *y): calcule x et y, les deux entiers quelconcs qui verifien...
Definition: pgcd.c:324
void print_Value(Value)
io.c
Definition: io.c:37
int Value
Definition: arithmetique.h:304
int linear_assertion_result
errors.c
Definition: errors.c:43
Value exponentiate(Value, int)
exp.c
Definition: exp.c:45
bool linear_use_gmp(void)
whether linear is to use gmp
Definition: errors.c:454
void dump_exception_stack_to_file(FILE *)
Value modulo_fast(Value, Value)
modulo.c
Definition: modulo.c:48
char * Value_to_string(Value)
Definition: io.c:76
Value gcd_subtract(Value, Value)
int gcd_subtract(int a, int b): find the gcd (pgcd) of two integers
Definition: pgcd.c:189
void linear_throw_exception(const int, const char *, const char *, const int)
Value bezout(Value, Value, Value *, Value *)
int bezout(int a, int b, int *x, int *y): calcule x et y, les deux nombres qui verifient le theoreme ...
Definition: pgcd.c:265
char const * get_exception_name(const linear_exception_t)
void linear_pop_exception_from_stack(const int, const char *, const char *, const int)
Value pgcd_fast(Value, Value)
int pgcd_fast(int a, int b): calcul iteratif du pgcd de deux entiers; le pgcd retourne est toujours p...
Definition: pgcd.c:82
Value pgcd_slow(Value, Value)
pgcd.c
Definition: pgcd.c:44
bool push_timeout_env(const char *, const char *, const char *, const int)
Definition: errors.c:575
size_t yy_size_t
Definition: arithmetique.h:598
int scan_Value(Value *)
Definition: io.c:63
Value absval_ofl(Value)
int absval_ofl(int i): absolute value of i (SUN version)
Definition: abs.c:50
Value absval(Value)
int absval(int i): absolute value of i (SUN version)
Definition: abs.c:58
unsigned int linear_error_count(void)
return number of linear errors may be used as a test after a reset to know whether new errors occured...
Definition: errors.c:85
Value divide_slow(Value, Value)
Definition: divide.c:114
void throw_exception(const int, const char *, const char *, const int)
struct col tableau
Value vecteur_bezout(Value[], Value[], int)
int vecteur_bezout(int u[], int v[], int l): calcul du vecteur v qui verifie le theoreme de bezout po...
Definition: pgcd.c:220
bool linear_with_gmp(void)
whether linear can use gmp (i.e.
Definition: errors.c:435
static int num
Definition: bourdoncle.c:137
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
frac * colonne