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