67 #ifdef OSL_GMP_IS_HERE
92 case OSL_PRECISION_SP:
94 case OSL_PRECISION_DP:
96 #ifdef OSL_GMP_IS_HERE
97 case OSL_PRECISION_MP:
115 case OSL_PRECISION_SP:
116 fprintf(file,
"32 bits");
118 case OSL_PRECISION_DP:
119 fprintf(file,
"64 bits");
121 #ifdef OSL_GMP_IS_HERE
122 case OSL_PRECISION_MP:
123 fprintf(file,
"GMP");
127 fprintf(file,
"unknown precision %d", precision);
139 case OSL_PRECISION_SP:
143 case OSL_PRECISION_DP:
147 #ifdef OSL_GMP_IS_HERE
148 case OSL_PRECISION_MP:
149 OSL_malloc(variable->mp,
void*,
sizeof(mpz_t));
150 mpz_init(*variable->mp);
155 OSL_error(
"unknown precision");
182 case OSL_PRECISION_SP:
183 variable->sp = value.sp;
186 case OSL_PRECISION_DP:
187 variable->dp = value.dp;
190 #ifdef OSL_GMP_IS_HERE
191 case OSL_PRECISION_MP:
192 mpz_set(*variable->mp, *value.mp);
197 OSL_error(
"unknown precision");
210 case OSL_PRECISION_SP:
211 variable->sp = (
long int)i;
214 case OSL_PRECISION_DP:
215 variable->dp = (
long long int)i;
218 #ifdef OSL_GMP_IS_HERE
219 case OSL_PRECISION_MP:
220 mpz_set_si(*variable->mp, i);
225 OSL_error(
"unknown precision");
238 case OSL_PRECISION_SP:
240 if (value.sp > (
long int)(INT_MAX))
241 OSL_overflow(
"osl_int_get_si overflow");
242 if (value.sp < (
long int)(INT_MIN))
243 OSL_overflow(
"osl_int_get_si overflow");
245 return (
int)value.sp;
247 case OSL_PRECISION_DP:
249 if (value.dp > (
long long int)(INT_MAX))
250 OSL_overflow(
"osl_int_get_si overflow");
251 if (value.dp < (
long long int)(INT_MIN))
252 OSL_overflow(
"osl_int_get_si overflow");
254 return (
int)value.dp;
256 #ifdef OSL_GMP_IS_HERE
257 case OSL_PRECISION_MP:
259 if (mpz_fits_sint_p(*value.mp) == 0)
260 OSL_overflow(
"osl_int_get_si overflow");
262 return mpz_get_si(*value.mp);
266 OSL_error(
"unknown precision");
279 case OSL_PRECISION_SP:
282 case OSL_PRECISION_DP:
285 #ifdef OSL_GMP_IS_HERE
286 case OSL_PRECISION_MP:
287 return mpz_get_d(*i.mp);
291 OSL_error(
"unknown precision");
316 case OSL_PRECISION_SP:
317 variable->sp = (
long int)i;
320 case OSL_PRECISION_DP:
321 variable->dp = (
long long int)i;
324 #ifdef OSL_GMP_IS_HERE
325 case OSL_PRECISION_MP:
326 OSL_malloc(variable->mp,
void*,
sizeof(mpz_t));
327 mpz_init_set_si(*variable->mp, i);
332 OSL_error(
"unknown precision");
345 case OSL_PRECISION_SP: {
346 long int temp = var1->sp;
352 case OSL_PRECISION_DP: {
353 long long int temp = var1->dp;
359 #ifdef OSL_GMP_IS_HERE
360 case OSL_PRECISION_MP: {
363 mpz_set(temp, *var1->mp);
364 mpz_set(*var1->mp, *var2->mp);
365 mpz_set(*var2->mp, temp);
372 OSL_error(
"unknown precision");
384 case OSL_PRECISION_SP:
388 case OSL_PRECISION_DP:
392 #ifdef OSL_GMP_IS_HERE
393 case OSL_PRECISION_MP:
394 mpz_clear(*variable->mp);
400 OSL_error(
"unknown precision");
424 char string[OSL_MAX_STRING];
427 fprintf(file,
"%s",
string);
441 case OSL_PRECISION_SP:
442 sprintf(
string, OSL_FMT_SP, value.sp);
445 case OSL_PRECISION_DP:
446 sprintf(
string, OSL_FMT_DP, value.dp);
449 #ifdef OSL_GMP_IS_HERE
450 case OSL_PRECISION_MP: {
452 str = mpz_get_str(0, 10, *value.mp);
453 sprintf(
string, OSL_FMT_MP, str);
460 OSL_error(
"unknown precision");
473 case OSL_PRECISION_SP:
474 sprintf(
string, OSL_FMT_TXT_SP, value.sp);
477 case OSL_PRECISION_DP:
478 sprintf(
string, OSL_FMT_TXT_DP, value.dp);
481 #ifdef OSL_GMP_IS_HERE
482 case OSL_PRECISION_MP: {
484 str = mpz_get_str(0, 10, *value.mp);
485 sprintf(
string, OSL_FMT_TXT_MP, str);
492 OSL_error(
"unknown precision");
508 case OSL_PRECISION_SP:
509 nb_read = sscanf(
string, OSL_FMT_TXT_SP, &(i->sp));
512 case OSL_PRECISION_DP:
513 nb_read = sscanf(
string, OSL_FMT_TXT_DP, &(i->dp));
516 #ifdef OSL_GMP_IS_HERE
517 case OSL_PRECISION_MP: {
518 nb_read = gmp_sscanf(
string,
"%lZd", i->mp);
524 OSL_error(
"unknown precision");
527 if (nb_read == 0) { OSL_error(
"failed to read an integer"); }
584 case OSL_PRECISION_SP:
587 if (LONG_MAX - val1.sp < val2.sp)
588 OSL_overflow(
"osl_int_add overflow");
591 if (val1.sp - LONG_MIN < -val2.sp)
592 OSL_overflow(
"osl_int_add overflow");
595 variable->sp = val1.sp + val2.sp;
598 case OSL_PRECISION_DP:
601 if (LLONG_MAX - val1.dp < val2.dp)
602 OSL_overflow(
"osl_int_add overflow");
605 if (val1.dp - LLONG_MIN < -val2.dp)
606 OSL_overflow(
"osl_int_add overflow");
609 variable->dp = val1.dp + val2.dp;
612 #ifdef OSL_GMP_IS_HERE
613 case OSL_PRECISION_MP:
614 mpz_add(*variable->mp, *val1.mp, *val2.mp);
619 OSL_error(
"unknown precision");
634 case OSL_PRECISION_SP:
637 if (LONG_MAX - value.sp < i)
638 OSL_overflow(
"osl_int_add_si overflow");
641 if (value.sp - LONG_MIN < -i)
642 OSL_overflow(
"osl_int_add_si overflow");
645 variable->sp = value.sp + (
long int)i;
648 case OSL_PRECISION_DP:
651 if (LLONG_MAX - value.dp < i)
652 OSL_overflow(
"osl_int_add_si overflow");
655 if (value.dp - LLONG_MIN < -i)
656 OSL_overflow(
"osl_int_add_si overflow");
659 variable->dp = value.dp + (
long long int)i;
662 #ifdef OSL_GMP_IS_HERE
663 case OSL_PRECISION_MP: {
665 mpz_init_set_si(si, i);
666 mpz_add(*variable->mp, *value.mp, si);
673 OSL_error(
"unknown precision");
687 #ifdef OSL_GMP_IS_HERE
688 if (precision == OSL_PRECISION_MP) {
689 mpz_sub(*variable->mp, *val1.mp, *val2.mp);
711 case OSL_PRECISION_SP:
712 variable->sp = val1.sp * val2.sp;
715 if (variable->sp / val2.sp != val1.sp) {
716 OSL_overflow(
"osl_int_mul overflow");
722 case OSL_PRECISION_DP:
723 variable->dp = val1.dp * val2.dp;
726 if (variable->dp / val2.dp != val1.dp) {
727 OSL_overflow(
"osl_int_mul overflow");
733 #ifdef OSL_GMP_IS_HERE
734 case OSL_PRECISION_MP:
735 mpz_mul(*variable->mp, *val1.mp, *val2.mp);
740 OSL_error(
"unknown precision");
755 case OSL_PRECISION_SP:
756 variable->sp = value.sp * (
long int)i;
759 if (variable->sp / (
long int)i != value.sp) {
760 OSL_overflow(
"osl_int_mul_si overflow");
766 case OSL_PRECISION_DP:
767 variable->dp = value.dp * (
long long int)i;
770 if (variable->dp / (
long long int)i != value.dp) {
771 OSL_overflow(
"osl_int_mul_si overflow");
777 #ifdef OSL_GMP_IS_HERE
778 case OSL_PRECISION_MP:
779 mpz_mul_si(*variable->mp, *value.mp, i);
784 OSL_error(
"unknown precision");
801 case OSL_PRECISION_SP: q->sp = a.sp / b.sp;
return;
803 case OSL_PRECISION_DP: q->dp = a.dp / b.dp;
return;
805 #ifdef OSL_GMP_IS_HERE
806 case OSL_PRECISION_MP: mpz_divexact(*q->mp, *a.mp, *b.mp);
return;
809 default: OSL_error(
"unknown precision");
825 case OSL_PRECISION_SP:
827 if (q->sp < 0) {
if (a.sp % b.sp != 0) --q->sp; }
828 else if (q->sp == 0) {
836 case OSL_PRECISION_DP:
838 if (q->dp < 0) {
if (a.dp % b.dp != 0) --q->dp; }
839 else if (q->dp == 0) {
847 #ifdef OSL_GMP_IS_HERE
848 case OSL_PRECISION_MP: mpz_fdiv_q(*q->mp, *a.mp, *b.mp);
return;
851 default: OSL_error(
"unknown precision");
867 case OSL_PRECISION_SP:
869 r->sp = a.sp - r->sp * b.sp;
872 case OSL_PRECISION_DP:
874 r->dp = a.dp - r->dp * b.dp;
877 #ifdef OSL_GMP_IS_HERE
878 case OSL_PRECISION_MP: mpz_fdiv_r(*r->mp, *a.mp, *b.mp);
return;
881 default: OSL_error(
"unknown precision");
898 case OSL_PRECISION_SP:
900 r->sp = a.sp - q->sp * b.sp;
903 case OSL_PRECISION_DP:
905 r->dp = a.dp - q->dp * b.dp;
908 #ifdef OSL_GMP_IS_HERE
909 case OSL_PRECISION_MP: mpz_fdiv_qr(*q->mp, *r->mp, *a.mp, *b.mp);
return;
912 default: OSL_error(
"unknown precision");
928 case OSL_PRECISION_SP:
929 mod->sp = a.sp % b.sp;
930 if (mod->sp < 0) mod->sp += labs(b.sp);
933 case OSL_PRECISION_DP:
934 mod->dp = a.dp % b.dp;
935 if (mod->dp < 0) mod->dp += llabs(b.dp);
938 #ifdef OSL_GMP_IS_HERE
939 case OSL_PRECISION_MP: mpz_mod(*mod->mp, *a.mp, *b.mp);
return;
942 default: OSL_error(
"unknown precision");
948 static long long int llgcd(
long long int const a,
long long int const b) {
949 return (b ?
llgcd(b, a % b) : a);
962 case OSL_PRECISION_SP: gcd->sp = labs(
llgcd(a.sp, b.sp));
return;
964 case OSL_PRECISION_DP: gcd->dp = llabs(
llgcd(a.dp, b.dp));
return;
966 #ifdef OSL_GMP_IS_HERE
967 case OSL_PRECISION_MP: mpz_gcd(*gcd->mp, *a.mp, *b.mp);
return;
970 default: OSL_error(
"unknown precision");
984 case OSL_PRECISION_SP:
985 variable->sp = -value.sp;
988 case OSL_PRECISION_DP:
989 variable->dp = -value.dp;
992 #ifdef OSL_GMP_IS_HERE
993 case OSL_PRECISION_MP:
994 mpz_neg(*variable->mp, *value.mp);
999 OSL_error(
"unknown precision");
1012 switch (precision) {
1013 case OSL_PRECISION_SP:
1014 variable->sp = (value.sp > 0) ? value.sp : -value.sp;
1017 case OSL_PRECISION_DP:
1018 variable->dp = (value.dp > 0) ? value.dp : -value.dp;
1021 #ifdef OSL_GMP_IS_HERE
1022 case OSL_PRECISION_MP:
1023 mpz_abs(*variable->mp, *value.mp);
1028 OSL_error(
"unknown precision");
1039 while (x) { x >>= 1; ++n; }
1041 return ((n == 0) ? 1 : n);
1052 switch (precision) {
1053 case OSL_PRECISION_SP:
return lllog2(value.sp);
1055 case OSL_PRECISION_DP:
return lllog2(value.dp);
1057 #ifdef OSL_GMP_IS_HERE
1058 case OSL_PRECISION_MP:
return mpz_sizeinbase(*value.mp, 2);
1061 default: OSL_error(
"unknown precision");
1072 while (x) { x /= 10; ++n; }
1086 switch (precision) {
1087 case OSL_PRECISION_SP:
return lllog10(value.sp);
1089 case OSL_PRECISION_DP:
return lllog10(value.dp);
1091 #ifdef OSL_GMP_IS_HERE
1092 case OSL_PRECISION_MP:
return mpz_sizeinbase(*value.mp, 10);
1095 default: OSL_error(
"unknown precision");
1113 switch (precision) {
1114 case OSL_PRECISION_SP:
1115 return (val1.sp == val2.sp);
1117 case OSL_PRECISION_DP:
1118 return (val1.dp == val2.dp);
1120 #ifdef OSL_GMP_IS_HERE
1121 case OSL_PRECISION_MP:
1122 return (mpz_cmp(*val1.mp, *val2.mp) == 0);
1126 OSL_error(
"unknown precision");
1150 switch (precision) {
1151 case OSL_PRECISION_SP:
1152 return (value.sp > 0);
1154 case OSL_PRECISION_DP:
1155 return (value.dp > 0);
1157 #ifdef OSL_GMP_IS_HERE
1158 case OSL_PRECISION_MP:
1159 return (mpz_sgn(*value.mp) > 0);
1163 OSL_error(
"unknown precision");
1175 switch (precision) {
1176 case OSL_PRECISION_SP:
1177 return (value.sp < 0);
1179 case OSL_PRECISION_DP:
1180 return (value.dp < 0);
1182 #ifdef OSL_GMP_IS_HERE
1183 case OSL_PRECISION_MP:
1184 return (mpz_sgn(*value.mp) < 0);
1188 OSL_error(
"unknown precision");
1200 switch (precision) {
1201 case OSL_PRECISION_SP:
1202 return (value.sp == 0);
1204 case OSL_PRECISION_DP:
1205 return (value.dp == 0);
1207 #ifdef OSL_GMP_IS_HERE
1208 case OSL_PRECISION_MP:
1209 return (mpz_sgn(*value.mp) == 0);
1213 OSL_error(
"unknown precision");
1225 switch (precision) {
1226 case OSL_PRECISION_SP:
1227 return (value.sp == (
long int)1);
1229 case OSL_PRECISION_DP:
1230 return (value.dp == (
long long int)1);
1232 #ifdef OSL_GMP_IS_HERE
1233 case OSL_PRECISION_MP:
1234 return (mpz_cmp_si(*value.mp, 1) == 0);
1238 OSL_error(
"unknown precision");
1250 switch (precision) {
1251 case OSL_PRECISION_SP:
1252 return (value.sp == (
long int)-1);
1254 case OSL_PRECISION_DP:
1255 return (value.dp == (
long long int)-1);
1257 #ifdef OSL_GMP_IS_HERE
1258 case OSL_PRECISION_MP:
1259 return (mpz_cmp_si(*value.mp, -1) == 0);
1263 OSL_error(
"unknown precision");
1277 switch (precision) {
1278 case OSL_PRECISION_SP:
1279 return ((val1.sp % val2.sp) == 0);
1281 case OSL_PRECISION_DP:
1282 return ((val1.dp % val2.dp) == 0);
1284 #ifdef OSL_GMP_IS_HERE
1285 case OSL_PRECISION_MP:
1286 return mpz_divisible_p(*val1.mp, *val2.mp);
1290 OSL_error(
"unknown precision");
1308 if (i != NULL && precision != new_precision) {
int osl_int_get_si(int precision, osl_const_int_t value)
Get the value in a int.
void osl_int_init_set(int precision, osl_int_const_p variable, osl_const_int_t i)
variable = i // including initialization for GMP
int osl_int_one(int precision, osl_const_int_t value)
value == 1
union osl_int *const osl_int_const_p
void osl_int_oppose(int precision, osl_int_const_p variable, osl_const_int_t value)
variable = - value
int osl_int_divisible(int precision, osl_const_int_t val1, osl_const_int_t val2)
(val1 % val2) == 0
void osl_int_free(int precision, osl_int_const_p variable)
Free thr osl int.
void osl_int_swap(int precision, osl_int_const_p var1, osl_int_const_p var2)
Swap the osl ints.
void osl_int_gcd(int const precision, osl_int_const_p gcd, osl_const_int_t a, osl_const_int_t b)
Compute the gcd (greatest common divisor) of a and b.
int osl_int_mone(int precision, osl_const_int_t value)
value == -1
void osl_int_sread(char **string, int precision, osl_int_const_p i)
sread for osl int
int osl_int_neg(int precision, osl_const_int_t value)
value < 0
int osl_int_is_precision_supported(int precision)
union osl_int const osl_const_int_t
int osl_int_ne(int precision, osl_const_int_t val1, osl_const_int_t val2)
val1 != val2
void osl_int_sub(int precision, osl_int_const_p variable, osl_const_int_t val1, osl_const_int_t val2)
variable = val1 - val2
size_t osl_int_size_in_base_2(int const precision, osl_const_int_t const value)
Get the size in base 2.
void osl_int_increment(int precision, osl_int_const_p variable, osl_const_int_t value)
variable = value + 1
size_t osl_int_size_in_base_10(int const precision, osl_const_int_t const value)
Get the size in base 10.
void osl_int_mul(int precision, osl_int_const_p variable, osl_const_int_t val1, osl_const_int_t val2)
variable = val1 * val2
void osl_int_clear(int precision, osl_int_const_p variable)
variable = 0 // including cleaning for GMP
double osl_int_get_d(int precision, osl_const_int_t i)
Get the value in a double.
static size_t lllog10(long long int x)
void osl_int_floor_div_r(int const precision, osl_int_const_p r, osl_const_int_t a, osl_const_int_t b)
r = a - b * (a / b)
void osl_int_set_precision(int const precision, int const new_precision, osl_int_p i)
Change the precision of the osl_int.
void osl_int_init_set_si(int precision, osl_int_const_p variable, int i)
variable = i // including initialization for GMP
int osl_int_eq(int precision, osl_const_int_t val1, osl_const_int_t val2)
val1 == val2
void osl_int_print(FILE *file, int precision, osl_const_int_t value)
void osl_int_init(int precision, osl_int_const_p variable)
Initialize the osl int.
void osl_int_abs(int precision, osl_int_const_p variable, osl_const_int_t value)
variable = | value |
void osl_int_set_si(int precision, osl_int_const_p variable, int i)
variable = i
void osl_int_sprint_txt(char *string, int precision, osl_const_int_t value)
void osl_int_add(int precision, osl_int_const_p variable, osl_const_int_t val1, osl_const_int_t val2)
variable = val1 + val2
void osl_int_floor_div_q(int const precision, osl_int_const_p q, osl_const_int_t a, osl_const_int_t b)
q = floor(a / b)
static size_t lllog2(long long int x)
static long long int llgcd(long long int const a, long long int const b)
void osl_int_div_exact(int const precision, osl_int_const_p q, osl_const_int_t a, osl_const_int_t b)
q = a / b
void osl_int_floor_div_q_r(int const precision, osl_int_const_p q, osl_int_const_p r, osl_const_int_t a, osl_const_int_t b)
Compute (q, r) such that a = b * q + r.
int osl_int_sscanf(char *string, int precision, osl_int_const_p i)
sscanf for osl int
void osl_int_add_si(int precision, osl_int_const_p variable, osl_const_int_t value, int i)
variable = val1 + i
void osl_int_sprint(char *string, int precision, osl_const_int_t value)
osl_int_p osl_int_malloc(int precision)
Initialize the osl int.
void osl_int_assign(int precision, osl_int_const_p variable, osl_const_int_t value)
variable = value
void osl_int_mul_si(int precision, osl_int_const_p variable, osl_const_int_t value, int i)
variable = val1 * i
int osl_int_zero(int precision, osl_const_int_t value)
value == 0
int osl_int_pos(int precision, osl_const_int_t value)
value > 0
void osl_int_decrement(int precision, osl_int_const_p variable, osl_const_int_t value)
variable = value - 1
void osl_int_mod(int const precision, osl_int_const_p mod, osl_const_int_t a, osl_const_int_t b)
mod = a % b
void osl_int_dump_precision(FILE *file, int precision)