00001
00002 #ifndef arithmetique_header_included
00003 #define arithmetique_header_included
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include <limits.h>
00031
00032 #ifdef GNUMP
00033 #include <gmp.h>
00034 #endif
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef LONG_LONG_MAX
00052
00053
00054
00055
00056
00057
00058 #ifndef __LONG_LONG_MAX__
00059 #define __LONG_LONG_MAX__ 9223372036854775807LL
00060 #endif
00061 #undef LONG_LONG_MAX
00062 #define LONG_LONG_MAX __LONG_LONG_MAX__
00063 #undef LONG_LONG_MIN
00064 #define LONG_LONG_MIN (-LONG_LONG_MAX-1)
00065 #undef ULONG_LONG_MAX
00066 #define ULONG_LONG_MAX (LONG_LONG_MAX * 2ULL + 1)
00067 #endif
00068
00069 #if defined(LINEAR_VALUE_IS_LONGLONG)
00070
00071 #define LINEAR_VALUE_STRING "long long int"
00072 typedef long long int Value;
00073 #define VALUE_FMT "%lld"
00074 #define VALUE_CONST(val) (val##LL)
00075
00076
00077
00078
00079
00080
00081 #define VALUE_NAN LONG_LONG_MIN
00082 #define VALUE_MIN (LONG_LONG_MIN+1LL)
00083 #define VALUE_MAX LONG_LONG_MAX
00084 #define VALUE_SQRT_MIN long_to_value(LONG_MIN)
00085 #define VALUE_SQRT_MAX long_to_value(LONG_MAX)
00086 #define VALUE_ZERO (0LL)
00087 #define VALUE_ONE (1LL)
00088 #define VALUE_MONE (-1LL)
00089
00090 #define VALUE_TO_LONG(val) \
00091 ((long)((val)>(Value)LONG_MIN&&(val)<=(Value)LONG_MAX)?\
00092 (val):(THROW(overflow_error), LONG_MIN))
00093
00094 #define VALUE_TO_INT(val) \
00095 ((int)((val)>(Value)INT_MIN&&(val)<=(Value)INT_MAX)?\
00096 (val):(THROW(overflow_error), INT_MIN))
00097
00098 #define VALUE_TO_DOUBLE(val) ((double)(val))
00099
00100
00101 #define VALUE_TO_FLOAT(val) ((float)((int)(val)))
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 #elif defined(LINEAR_VALUE_IS_LONG)
00117
00118 #define LINEAR_VALUE_STRING "long int"
00119 typedef long Value;
00120 #define VALUE_FMT "%ld"
00121 #define VALUE_CONST(val) (val##L)
00122 #define VALUE_NAN LONG_MIN
00123 #define VALUE_MIN (LONG_MIN+1L)
00124 #define VALUE_MAX LONG_MAX
00125 #define VALUE_ZERO 0L
00126 #define VALUE_ONE 1L
00127 #define VALUE_MONE -1L
00128 #define VALUE_TO_LONG(val) (val)
00129 #define VALUE_TO_INT(val) ((int)(val))
00130 #define VALUE_TO_FLOAT(val) ((float)(val))
00131 #define VALUE_TO_DOUBLE(val) ((double)(val))
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 #elif defined(LINEAR_VALUE_IS_CHARS)
00179
00180 #define LINEAR_VALUE_STRING "char"
00181 typedef union { char *s; long l; int i; float f; double d;} Value;
00182 #define VALUE_FMT "%s"
00183 #define VALUE_CONST(val) ((Value)(val))
00184 #define VALUE_NAN ((Value)(long)0xdadeebee)
00185 #define VALUE_MIN ((Value)(long)0xdeadbeef)
00186 #define VALUE_MAX ((Value)(long)0xfeedabee)
00187 #define VALUE_ZERO ((Value)0)
00188 #define VALUE_ONE ((Value)1)
00189 #define VALUE_MONE ((Value)-1)
00190 #define VALUE_TO_LONG(val) (val.l)
00191 #define VALUE_TO_INT(val) (val.i)
00192 #define VALUE_TO_FLOAT(val) (val.f)
00193 #define VALUE_TO_DOUBLE(val) (val.d)
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 #elif defined(LINEAR_VALUE_IS_INT)
00208
00209 #define LINEAR_VALUE_STRING "int"
00210 typedef int Value;
00211 #define VALUE_FMT "%d"
00212 #define VALUE_CONST(val) (val)
00213 #define VALUE_NAN INT_MIN
00214 #define VALUE_MIN (INT_MIN+1)
00215 #define VALUE_MAX INT_MAX
00216 #define VALUE_ZERO 0
00217 #define VALUE_ONE 1
00218 #define VALUE_MONE -1
00219 #define VALUE_TO_LONG(val) ((long)(val))
00220 #define VALUE_TO_INT(val) ((int)(val))
00221 #define VALUE_TO_FLOAT(val) ((float)(val))
00222 #define VALUE_TO_DOUBLE(val) ((double)(val))
00223
00224
00225
00226 #else
00227
00228 #define LINEAR_VALUE_STRING "gmp"
00229 typedef mpz_t Value;
00230 #define VALUE_FMT "%s"
00231
00232
00233 #undef VALUE_ZERO
00234 #undef VALUE_ONE
00235 #undef VALUE_MONE
00236 #define VALUE_TO_LONG(val) (mpz_get_si(val))
00237 #define VALUE_TO_INT(val) ((int)mpz_get_si(val))
00238 #define VALUE_TO_FLOAT(val) ((float)((int)mpz_get_si(val)))
00239 #define VALUE_TO_DOUBLE(val) (mpz_get_d(val))
00240
00241 #endif
00242
00243
00244
00245 #if defined(GNUMP)
00246
00247
00248
00249 #define value_init(val) (mpz_init((val)))
00250 #define value_assign(v1,v2) (mpz_set((v1),(v2)))
00251 #define value_set_si(val,i) (mpz_set_si((val),(i)))
00252 #define value_set_double(val,d)(mpz_set_d((val),(d)))
00253 #define value_clear(val) (mpz_clear((val)))
00254 #define value_read(val,str) (mpz_set_str((val),(str),10))
00255 #define value_print(Dst,fmt,val) {char *str; str = mpz_get_str(0,10,(val)); \
00256 fprintf((Dst),(fmt),str); free(str); \
00257 }
00258 #define value_swap(val1,val2) {mpz_t tmp; mpz_init_set(tmp,(val1)); \
00259 mpz_set((val1),(val2)); mpz_set((val2),tmp); \
00260 mpz_clear(tmp); \
00261 }
00262
00263
00264
00265 #define value_eq(v1,v2) (mpz_cmp((v1),(v2)) == 0)
00266 #define value_ne(v1,v2) (mpz_cmp((v1),(v2)) != 0)
00267 #define value_gt(v1,v2) (mpz_cmp((v1),(v2)) > 0)
00268 #define value_ge(v1,v2) (mpz_cmp((v1),(v2)) >= 0)
00269 #define value_lt(v1,v2) (mpz_cmp((v1),(v2)) < 0)
00270 #define value_le(v1,v2) (mpz_cmp((v1),(v2)) <= 0)
00271
00272
00273
00274 #define value_sign(val) (mpz_sgn(val))
00275 #define value_compare(v1,v2) (mpz_cmp((v1),(v2)))
00276
00277
00278
00279 #define value_addto(ref,val1,val2) (mpz_add((ref),(val1),(val2)))
00280 #define value_add_int(ref,val,vint) (mpz_add_ui((ref),(val),(long)(vint)))
00281 #define value_increment(ref,val) (mpz_add_ui((ref),(val),1))
00282 #define value_multiply(ref,val1,val2) (mpz_mul((ref),(val1),(val2)))
00283 #define value_substract(ref,val1,val2) (mpz_sub((ref),(val1),(val2)))
00284 #define value_sub_int(ref,val,vint) (mpz_sub_ui((ref),(val),(long)(vint)))
00285 #define value_decrement(ref,val) (mpz_sub_ui((ref),(val),1))
00286 #define value_division(ref,val1,val2) (mpz_tdiv_q((ref),(val1),(val2)))
00287 #define value_modulus(ref,val1,val2) (mpz_tdiv_r((ref),(val1),(val2)))
00288 #define value_pdivision(ref,val1,val2) (mpz_fdiv_q((ref),(val1),(val2)))
00289 #define value_oppose(ref,val) (mpz_neg((ref),(val)))
00290 #define value_absolute(ref,val) (mpz_abs((ref),(val)))
00291 #define value_minimum(ref,val1,val2) (value_le((val1),(val2)) ? \
00292 mpz_set((ref),(val1)) : \
00293 mpz_set((ref),(val2)))
00294 #define value_maximum(ref,val1,val2) (value_ge((val1),(val2)) ? \
00295 mpz_set((ref),(val1)) : \
00296 mpz_set((ref),(val2)))
00297 #define value_orto(ref,val1,val2) (mpz_ior((ref),(val1),(val2)))
00298 #define value_andto(ref,val1,val2) (mpz_and((ref),(val1),(val2)))
00299
00300
00301
00302 #define value_pos_p(val) (mpz_sgn(val) > 0)
00303 #define value_neg_p(val) (mpz_sgn(val) < 0)
00304 #define value_posz_p(val) (mpz_sgn(val) >= 0)
00305 #define value_negz_p(val) (mpz_sgn(val) <= 0)
00306 #define value_zero_p(val) (mpz_sgn(val) == 0)
00307 #define value_notzero_p(val) (mpz_sgn(val) != 0)
00308 #define value_one_p(val) (mpz_cmp_si(val,1) == 0)
00309 #define value_notone_p(val) (mpz_cmp_si(val,1) != 0)
00310 #define value_mone_p(val) (mpz_cmp_si(val,-1) ==0)
00311 #define value_notmone_p(val) (mpz_cmp_si(val,-1) !=0)
00312
00313
00314
00315 #else
00316
00317
00318 #define value_init(val) ((val) = 0)
00319 #define value_assign(v1,v2) ((v1) = (v2))
00320 #define value_set_si(val,i) ((val) = (Value)(i))
00321 #define value_set_double(val,d) ((val) = (Value)(d))
00322 #define value_clear(val) ((val) = 0)
00323 #define value_read(val,str) (sscanf((str),VALUE_FMT,&(val)))
00324 #define value_print(Dst,fmt,val) (fprintf((Dst),(fmt),(val)))
00325 #define value_swap(v1,v2) {Value tmp; tmp = v2; \
00326 v2 = v1; v1 = tmp; \
00327 }
00328
00329
00330 #define int_to_value(i) ((Value)(i))
00331 #define long_to_value(l) ((Value)(l))
00332 #define float_to_value(f) ((Value)(f))
00333 #define double_to_value(d) ((Value)(d))
00334
00335
00336
00337 #define value_eq(v1,v2) ((v1)==(v2))
00338 #define value_ne(v1,v2) ((v1)!=(v2))
00339 #define value_gt(v1,v2) ((v1)>(v2))
00340 #define value_ge(v1,v2) ((v1)>=(v2))
00341 #define value_lt(v1,v2) ((v1)<(v2))
00342 #define value_le(v1,v2) ((v1)<=(v2))
00343
00344
00345
00346 #define value_sign(v) (value_eq(v,VALUE_ZERO)?0:value_lt(v,VALUE_ZERO)?-1:1)
00347 #define value_compare(v1,v2) (value_eq(v1,v2)?0:value_lt(v1,v2)?-1:1)
00348
00349
00350
00351 #define value_plus(v1,v2) ((v1)+(v2))
00352 #define value_div(v1,v2) ((v1)/(v2))
00353 #define value_mod(v1,v2) ((v1)%(v2))
00354 #define value_direct_multiply(v1,v2) ((v1)*(v2))
00355 #define value_minus(v1,v2) ((v1)-(v2))
00356 #define value_pdiv(v1,v2) (divide((v1),(v2)))
00357 #define value_pmod(v1,v2) (modulo((v1),(v2)))
00358 #define value_min(v1,v2) (value_le((v1),(v2))? (v1): (v2))
00359 #define value_max(v1,v2) (value_ge((v1),(v2))? (v1): (v2))
00360 #define value_or(v1,v2) ((v1)|(v2))
00361 #define value_and(v1,v2) ((v1)&(v2))
00362 #define value_lshift(v1,v2) ((v1)<<(v2))
00363 #define value_rshift(v1,v2) ((v1)>>(v2))
00364
00365
00366
00367 #define value_addto(ref,val1,val2) ((ref) = (val1)+(val2))
00368 #define value_add_int(ref,val,vint) ((ref) = (val)+(Value)(vint))
00369 #define value_increment(ref,val) ((ref) = (val)+VALUE_ONE)
00370 #define value_direct_product(ref,val1,val2) ((ref) = (val1)*(val2))
00371 #define value_multiply(ref,val1,val2) ((ref) = value_mult((val1),(val2)))
00372 #define value_substract(ref,val1,val2) ((ref) = (val1)-(val2))
00373 #define value_sub_int(ref,val,vint) ((ref) = (val)-(Value)(vint))
00374 #define value_decrement(ref,val) ((ref) = (val)-VALUE_ONE)
00375 #define value_division(ref,val1,val2) ((ref) = (val1)/(val2))
00376 #define value_modulus(ref,val1,val2) ((ref) = (val1)%(val2))
00377 #define value_pdivision(ref,val1,val2) ((ref) = value_pdiv((val1),(val2)))
00378 #define value_oppose(ref,val) ((ref) = value_uminus((val)))
00379 #define value_absolute(ref,val) ((ref) = value_abs((val)))
00380 #define value_minimum(ref,val1,val2) ((ref) = value_min((val1),(val2)))
00381 #define value_maximum(ref,val1,val2) ((ref) = value_max((val1),(val2)))
00382 #define value_orto(ref,val1,val2) ((ref) = (val1)|(val2))
00383 #define value_andto(ref,val1,val2) ((ref) = (val1)&(val2))
00384
00385
00386
00387 #define value_uminus(val) (-(val))
00388 #define value_not(val) (~(val))
00389 #define value_abs(val) (value_posz_p(val)? \
00390 (val) : \
00391 (value_ne((val), VALUE_NAN) ? \
00392 value_uminus(val) : \
00393 (THROW (overflow_error), VALUE_NAN )))
00394
00395
00396
00397 #define value_pos_p(val) value_gt(val,VALUE_ZERO)
00398 #define value_neg_p(val) value_lt(val,VALUE_ZERO)
00399 #define value_posz_p(val) value_ge(val,VALUE_ZERO)
00400 #define value_negz_p(val) value_le(val,VALUE_ZERO)
00401 #define value_zero_p(val) value_eq(val,VALUE_ZERO)
00402 #define value_notzero_p(val) value_ne(val,VALUE_ZERO)
00403 #define value_one_p(val) value_eq(val,VALUE_ONE)
00404 #define value_notone_p(val) value_ne(val,VALUE_ONE)
00405 #define value_mone_p(val) value_eq(val,VALUE_MONE)
00406 #define value_notmone_p(val) value_ne(val,VALUE_MONE)
00407 #define value_min_p(val) value_eq(val,VALUE_MIN)
00408 #define value_max_p(val) value_eq(val,VALUE_MAX)
00409 #define value_notmin_p(val) value_ne(val,VALUE_MIN)
00410 #define value_notmax_p(val) value_ne(val,VALUE_MAX)
00411
00412 #endif
00413
00414
00415
00416
00417 #include "arithmetic_errors.h"
00418
00419
00420
00421
00422 #define value_protected_hard_idiv_multiply(v,w,throw) \
00423 ((value_zero_p(w) || value_zero_p(v))? VALUE_ZERO: \
00424 value_lt(value_abs(v),value_div(VALUE_MAX,value_abs(w)))? \
00425 value_direct_multiply(v,w): (throw, VALUE_NAN))
00426
00427
00428
00429 #if defined(LINEAR_VALUE_ASSUME_SOFTWARE_IDIV)
00430 #define value_protected_multiply(v,w,throw) \
00431 ((value_le(v,VALUE_SQRT_MAX) && value_le(w,VALUE_SQRT_MAX) && \
00432 value_ge(v,VALUE_SQRT_MIN) && value_ge(w,VALUE_SQRT_MIN))? \
00433 value_direct_multiply(v,w): value_protected_hard_idiv_multiply(v,w,throw))
00434 #else
00435 #define value_protected_multiply(v,w,throw) \
00436 value_protected_hard_idiv_multiply(v,w,throw)
00437 #endif
00438
00439
00440
00441 #define value_protected_mult(v,w) \
00442 value_protected_multiply(v,w,THROW(overflow_error))
00443 #define value_protected_product(v,w) \
00444 v=value_protected_mult(v,w)
00445
00446
00447
00448
00449 #if defined(LINEAR_VALUE_PROTECT_MULTIPLY)
00450 #define value_mult(v,w) value_protected_mult(v,w)
00451 #define value_product(v,w) value_protected_product(v,w)
00452 #else
00453
00454
00455
00456
00457
00458 #define value_mult(v,w) \
00459 value_protected_multiply(v,w, \
00460 (fprintf(stderr,"[value_mult] value overflow!\n"),THROW(overflow_error)))
00461 #define value_product(v,w) v=value_mult(v,w)
00462
00463
00464
00465
00466
00467
00468 #endif
00469
00470
00471
00472
00473
00474
00475
00476 #if defined(LINEAR_VALUE_IS_CHARS)
00477 #define value_fake_binary(v1,v2) ((Value)((v1).i+(v2).i))
00478 #define value_bool_binary(v1,v2) ((int)((v1).i+(v2).i))
00479 #undef float_to_value
00480 #define float_to_value(f) ((Value)f)
00481 #undef double_to_value
00482 #define double_to_value(f) ((Value)f)
00483 #undef value_uminus
00484 #define value_uminus(v) (v)
00485 #undef value_mult
00486 #define value_mult(v1,v2) value_fake_binary(v1,v2)
00487 #undef value_mod
00488 #define value_mod(v1,v2) value_fake_binary(v1,v2)
00489 #undef value_ge
00490 #define value_ge(v1,v2) value_bool_binary(v1,v2)
00491 #undef value_gt
00492 #define value_gt(v1,v2) value_bool_binary(v1,v2)
00493 #undef value_le
00494 #define value_le(v1,v2) value_bool_binary(v1,v2)
00495 #undef value_lt
00496 #define value_lt(v1,v2) value_bool_binary(v1,v2)
00497 #undef value_ne
00498 #define value_ne(v1,v2) value_bool_binary(v1,v2)
00499 #undef value_eq
00500 #define value_eq(v1,v2) value_bool_binary(v1,v2)
00501 #undef value_plus
00502 #define value_plus(v1,v2) value_fake_binary(v1,v2)
00503 #undef value_minus
00504 #define value_minus(v1,v2) value_fake_binary(v1,v2)
00505 #undef value_pdiv
00506 #define value_pdiv(v1,v2) value_fake_binary(v1,v2)
00507 #undef value_div
00508 #define value_div(v1,v2) value_fake_binary(v1,v2)
00509 #undef value_mod
00510 #define value_mod(v1,v2) value_fake_binary(v1,v2)
00511 #undef value_addto
00512 #define value_addto(v1,v2) value_assign(v1,value_plus(v1,v2))
00513 #undef value_substract
00514 #define value_substract(v1,v2) value_addto(v1,v2)
00515 #undef value_product
00516 #define value_product(v1,v2) value_addto(v1,v2)
00517 #undef value_modulus
00518 #define value_modulus(v1,v2) value_addto(v1,v2)
00519 #undef value_division
00520 #define value_division(v1,v2) value_addto(v1,v2)
00521 #undef value_increment
00522 #define value_increment(v) value_addto(v,VALUE_ONE)
00523 #undef value_decrement
00524 #define value_decrement(v) value_addto(v,VALUE_MONE)
00525 #undef value_orto
00526 #define value_orto(ref,val) value_addto(v1,v2)
00527 #undef value_andto
00528 #define value_andto(ref,val) value_addto(v1,v2)
00529 #undef value_or
00530 #define value_or(v1,v2) value_fake_binary(v1,v2)
00531 #undef value_and
00532 #define value_and(v1,v2) value_fake_binary(v1,v2)
00533 #undef value_lshift
00534 #define value_lshift(v1,v2) value_fake_binary(v1,v2)
00535 #undef value_rshift
00536 #define value_rshift(v1,v2) value_fake_binary(v1,v2)
00537 #endif
00538
00539
00540
00541
00542 #ifndef ABS
00543 #define ABS(x) (((x)>=0) ? (x) : -(x))
00544 #endif
00545
00546
00547
00548
00549
00550 #ifndef MIN
00551 #define MIN(x,y) (((x)>=(y))?(y):(x))
00552 #endif
00553 #ifndef MAX
00554 #define MAX(x,y) (((x)>=(y))?(x):(y))
00555 #endif
00556
00557
00558 #define SIGN(x) (((x)>0)? 1 : ((x)==0? 0 : -1))
00559
00560
00561
00562
00563
00564
00565
00566 #define DIVIDE(x,y) ((y)>0? POSITIVE_DIVIDE(x,y) : \
00567 -POSITIVE_DIVIDE((x),(-(y))))
00568
00569
00570 #define POSITIVE_DIVIDE(x,y) ((x)>0 ? (x)/(y) : - (-(x)+(y)-1)/(y))
00571
00572
00573 #define MODULO(x,y) ((y)>0 ? POSITIVE_MODULO(x,y) : POSITIVE_MODULO(-x,-y))
00574
00575
00576
00577
00578
00579
00580
00581 #define POSITIVE_MODULO(x,y) ((x) > 0 ? (x)%(y) : \
00582 ((x)%(y) == 0 ? 0 : ((y)-(-(x))%(y))))
00583
00584
00585
00586
00587
00588 #define divide(a,b) DIVIDE(a,b)
00589
00590 #define modulo(a,b) MODULO(a,b)
00591
00592 typedef struct {Value num, den; int numero ; } frac ;
00593 typedef struct col{int taille, existe ; frac *colonne ;} tableau ;
00594
00595
00596 extern unsigned int overflow_error;
00597 extern unsigned int simplex_arithmetic_error;
00598 extern unsigned int user_exception_error;
00599 extern unsigned int parser_exception_error;
00600 extern unsigned int any_exception_error;
00601 extern unsigned int the_last_just_thrown_exception;
00602 extern int linear_exception_debug_mode;
00603 extern void dump_exception_stack_to_file(FILE * );
00604 extern void dump_exception_stack(void);
00605 extern jmp_buf *push_exception_on_stack(int , char * , char * , int );
00606 extern void pop_exception_from_stack(int , char * , char * , int );
00607 extern void throw_exception(int , const char * , const char * , int );
00608
00609 #endif
00610
00611
00612