6 #ifndef arithmetique_header_included
7 #define arithmetique_header_included
66 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
67 # define CURRENT_FUNCTION __func__
69 # if defined(__GNUC__)
70 # define CURRENT_FUNCTION __FUNCTION__
72 # define CURRENT_FUNCTION "<unknown>"
76 #define PUSH_TIMEOUT_ENV(env) \
77 push_timeout_env(env, CURRENT_FUNCTION, __FILE__, __LINE__)
79 #define POP_TIMEOUT_ENV(env) \
80 pop_timeout_env(env, CURRENT_FUNCTION, __FILE__, __LINE__)
82 #define PUSH_TIMEOUT(delay) \
83 push_timeout(delay, CURRENT_FUNCTION, __FILE__, __LINE__)
85 #define POP_TIMEOUT() \
86 pop_timeout(CURRENT_FUNCTION, __FILE__, __LINE__)
97 #ifndef __LONG_LONG_MAX__
98 #define __LONG_LONG_MAX__ 9223372036854775807LL
101 #define LONG_LONG_MAX __LONG_LONG_MAX__
103 #define LONG_LONG_MIN (-LONG_LONG_MAX-1)
104 #undef ULONG_LONG_MAX
105 #define ULONG_LONG_MAX (LONG_LONG_MAX * 2ULL + 1)
118 #if defined(LINEAR_VALUE_IS_INT128)
120 #error "not implemented yet"
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
131 #define VALUE_MIN (???_MIN+1LL)
132 #define VALUE_MAX ???_MAX
133 #if defined(LINEAR_VALUE_ASSUME_SOFTWARE_IDIV) && \
134 defined(__SIZEOF_???__) && \
136 #error "long long is expected to be 64-bit to use software idiv"
138 #define VALUE_SQRT_MIN ???LLL
139 #define VALUE_SQRT_MAX -???LLL
140 #define VALUE_ZERO (0LLL)
141 #define VALUE_ONE (1LLL)
142 #define VALUE_MONE (-1LLL)
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)))
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
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"
183 #define VALUE_SQRT_MIN 3037000499LL
184 #define VALUE_SQRT_MAX -3037000499LL
185 #define VALUE_ZERO (0LL)
186 #define VALUE_ONE (1LL)
187 #define VALUE_MONE (-1LL)
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))
199 #define VALUE_TO_FLOAT(val) ((float)((int)(val)))
214 #elif defined(LINEAR_VALUE_IS_LONG)
215 #define LINEAR_VALUE_STRING "long int"
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
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))
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)
303 #define LINEAR_VALUE_STRING "int"
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
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))
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))
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))
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)
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))
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))
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))
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))
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 ))))
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)
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)
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))
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))
429 #define value_protected_multiply(v,w,throw) \
430 value_protected_hard_idiv_multiply(v,w,throw)
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)
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)
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)
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)
478 #define value_uminus(v) (v)
480 #define value_mult(v1,v2) value_fake_binary(v1,v2)
482 #define value_mod(v1,v2) value_fake_binary(v1,v2)
484 #define value_ge(v1,v2) value_bool_binary(v1,v2)
486 #define value_gt(v1,v2) value_bool_binary(v1,v2)
488 #define value_le(v1,v2) value_bool_binary(v1,v2)
490 #define value_lt(v1,v2) value_bool_binary(v1,v2)
492 #define value_ne(v1,v2) value_bool_binary(v1,v2)
494 #define value_eq(v1,v2) value_bool_binary(v1,v2)
496 #define value_plus(v1,v2) value_fake_binary(v1,v2)
498 #define value_minus(v1,v2) value_fake_binary(v1,v2)
500 #define value_pdiv(v1,v2) value_fake_binary(v1,v2)
502 #define value_div(v1,v2) value_fake_binary(v1,v2)
504 #define value_mod(v1,v2) value_fake_binary(v1,v2)
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)
510 #define value_product(v1,v2) value_addto(v1,v2)
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)
520 #define value_orto(ref,val) value_addto(v1,v2)
522 #define value_andto(ref,val) value_addto(v1,v2)
524 #define value_or(v1,v2) value_fake_binary(v1,v2)
526 #define value_and(v1,v2) value_fake_binary(v1,v2)
528 #define value_lshift(v1,v2) value_fake_binary(v1,v2)
530 #define value_rshift(v1,v2) value_fake_binary(v1,v2)
537 #define ABS(x) (((x)>=0) ? (x) : -(x))
545 #define MIN(x,y) (((x)>=(y))?(y):(x))
548 #define MAX(x,y) (((x)>=(y))?(x):(y))
552 #define SIGN(x) (((x)>0)? 1 : ((x)==0? 0 : -1))
560 #define DIVIDE(x,y) ((y)>0? POSITIVE_DIVIDE(x,y) : \
561 -POSITIVE_DIVIDE((x),(-(y))))
564 #define POSITIVE_DIVIDE(x,y) ((x)>0 ? (x)/(y) : - (-(x)+(y)-1)/(y))
567 #define MODULO(x,y) ((y)>0 ? POSITIVE_MODULO(x,y) : POSITIVE_MODULO(-x,-y))
575 #define POSITIVE_MODULO(x,y) ((x) > 0 ? (x)%(y) : \
576 ((x)%(y) == 0 ? 0 : ((y)-(-(x))%(y))))
582 #define pgcd(a,b) pgcd_slow(a,b)
584 #define divide(a,b) DIVIDE(a,b)
586 #define modulo(a,b) MODULO(a,b)
596 #ifndef _has_yy_size_t
597 #define _has_yy_size_t
655 extern void push_timeout(
const unsigned int ,
const char *,
const char *,
const int );
656 extern bool push_timeout_env(
const char *,
const char *,
const char *,
const int );
658 extern void pop_timeout_env(
const char *,
const char *,
const char *,
const int );
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
void pop_timeout_env(const char *, const char *, const char *, const int)
linear_exception_t the_last_just_thrown_exception
Value divide_fast(Value, Value)
divide.c
void linear_reset_error_counters(void)
reset linear counters
void fprint_Value(FILE *, Value)
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...
Value abs_ofl_ctrl(Value, int)
_has_yy_size_t
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 ...
void pop_timeout(const char *, const char *, const int)
Value ppcm(Value, Value)
ppcm.c
int linear_number_of_exception_thrown
bool linear_require_gmp(void)
whether linear is asked to use gmp if possible (env variable)
int sscan_Value(char *, Value *)
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)
void push_timeout(const unsigned int, const char *, const char *, const int)
void set_timeout_callback(timeout_callback_f)
int value_comparison(Value, Value)
value.c
void sprint_Value(char *, Value)
void dump_exception_stack(void)
int fscan_Value(FILE *, Value *)
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...
void print_Value(Value)
io.c
int linear_assertion_result
errors.c
Value exponentiate(Value, int)
exp.c
bool linear_use_gmp(void)
whether linear is to use gmp
void dump_exception_stack_to_file(FILE *)
Value modulo_fast(Value, Value)
modulo.c
char * Value_to_string(Value)
Value gcd_subtract(Value, Value)
int gcd_subtract(int a, int b): find the gcd (pgcd) of two integers
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 ...
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...
Value pgcd_slow(Value, Value)
pgcd.c
bool push_timeout_env(const char *, const char *, const char *, const int)
Value absval_ofl(Value)
int absval_ofl(int i): absolute value of i (SUN version)
Value absval(Value)
int absval(int i): absolute value of i (SUN version)
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...
Value divide_slow(Value, Value)
void throw_exception(const int, const char *, const char *, const int)
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...
bool linear_with_gmp(void)
whether linear can use gmp (i.e.
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])