/usr/include/numrat_native.h is in libapron-dev 0.9.10-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | /* ********************************************************************** */
/* rational_native.h */
/* ********************************************************************** */
#ifndef _NUMRAT_NATIVE_H_
#define _NUMRAT_NATIVE_H_
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include "gmp.h"
#include "mpfr.h"
#include "ap_scalar.h"
#include "num_config.h"
/* We assume that the basic NUMINT on which rational is built is properly
defined */
#include "numint.h"
#ifndef NUMINT_NATIVE
#error "HERE"
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _rat_t {
numint_t n; /* numerator */
numint_t d; /* denominator, >=0 */
} numrat_t[1];
/* ====================================================================== */
/* Rational operations */
/* ====================================================================== */
static inline void numrat_canonicalize(numrat_t r)
{
if (r->d){
numint_t pgcd;
numint_gcd(pgcd,r->n,r->d);
if (*pgcd==0 || (*pgcd==-1 && (*r->d==NUMINT_MIN || *r->n==NUMINT_MIN))) {
fprintf(stderr,"overflow in numrat_canonicalize\n");
return;
}
*r->n /= *pgcd;
*r->d /= *pgcd;
}
else {
numint_set_int(r->n,1);
}
}
#define numrat_numref(a) (a)->n
#define numrat_denref(a) (a)->d
static inline bool numrat_set_numint2(numrat_t a, numint_t b, numint_t c)
{
numint_set(numrat_numref(a),b);
numint_set(numrat_denref(a),c);
numrat_canonicalize(a);
return true;
}
/* ====================================================================== */
/* Assignement */
/* ====================================================================== */
static inline void numrat_set(numrat_t a, numrat_t b)
{ *a = *b; }
static inline void numrat_set_array(numrat_t* a, numrat_t* b, size_t size)
{ memcpy(a,b,size*sizeof(numrat_t)); }
static inline void numrat_set_int(numrat_t a, long int i)
{
numint_set_int(a->n,i);
numint_set_int(a->d,1);
}
/* ====================================================================== */
/* Constructors and Destructors */
/* ====================================================================== */
static inline void numrat_init(numrat_t a)
{
numint_init(a->n);
numint_init_set_int(a->d,1);
}
static inline void numrat_init_array(numrat_t* a, size_t size)
{
size_t i;
for (i=0; i<size; i++) numrat_init(a[i]);
}
static inline void numrat_init_set(numrat_t a, numrat_t b)
{ *a = *b; }
static inline void numrat_init_set_int(numrat_t a, long int i)
{ numint_set_int(a->n,i); numint_set_int(a->d,1); }
static inline void numrat_clear(numrat_t a)
{}
static inline void numrat_clear_array(numrat_t* a, size_t size)
{}
/* ====================================================================== */
/* Arithmetic Operations */
/* ====================================================================== */
static inline void numrat_neg(numrat_t a, numrat_t b)
{ numint_neg(a->n,b->n); numint_set(a->d,b->d); }
static inline void numrat_inv(numrat_t a, numrat_t b)
{
if (a!=b)
*a = *b;
numint_swap(a->n,a->d);
if (*a->d<0){
numint_neg(a->n,a->n);
numint_neg(a->d,a->d);
}
}
static inline void numrat_abs(numrat_t a, numrat_t b)
{ numint_abs(a->n,b->n); numint_set(a->d,b->d); }
static inline void numrat_add(numrat_t a, numrat_t b, numrat_t c)
{
numint_t d;
#if 0
*d = *b->d * *c->d;
*a->n = *b->n * *c->d + *b->d * *c->n;
*a->d = *d;
#else /* more cost but less overflows */
numint_lcm(d,b->d,c->d);
*a->n = *b->n * (*d / *b->d) + (*d / *c->d) * *c->n;
*a->d = *d;
#endif
numrat_canonicalize(a);
}
static inline void numrat_add_uint(numrat_t a, numrat_t b, unsigned long int c)
{
*a->n = *b->n + (numint_native)c * (*b->d);
*a->d = *b->d;
numrat_canonicalize(a);
}
static inline void numrat_sub(numrat_t a, numrat_t b, numrat_t c)
{
numint_t d;
#if 0
*d = *b->d * *c->d;
*a->n = *b->n * *c->d - *b->d * *c->n;
*a->d = *d;
#else /* more cost but less overflows */
numint_lcm(d,b->d,c->d);
*a->n = *b->n * (*d / *b->d) - (*d / *c->d) * *c->n;
*a->d = *d;
#endif
numrat_canonicalize(a);
}
static inline void numrat_sub_uint(numrat_t a, numrat_t b, unsigned long int c)
{
*a->n = *b->n - (numint_native)c * (*b->d);
*a->d = *b->d;
numrat_canonicalize(a);
}
static inline void numrat_mul(numrat_t a, numrat_t b, numrat_t c)
{
*a->n = *b->n * *c->n;
*a->d = *b->d * *c->d;
numrat_canonicalize(a);
}
static inline void numrat_mul_2(numrat_t a, numrat_t b)
{
if (*b->d % 2 == 0){
*a->n = *b->n;
*a->d = *b->d / 2;
}
else {
*a->n = *b->n * 2;
*a->d = *b->d;
}
}
static inline void numrat_div(numrat_t a, numrat_t b, numrat_t c)
{
numint_t d;
*d = *b->d * *c->n;
if (*d<0) {
*a->n = - *b->n * *c->d;
*a->d = - *d;
}
else {
*a->n = *b->n * *c->d;
*a->d = *d;
}
numrat_canonicalize(a);
}
static inline void numrat_div_2(numrat_t a, numrat_t b)
{
if (*b->n % 2 == 0){
*a->n = *b->n / 2;
*a->d = *b->d;
}
else {
*a->n = *b->n;
*a->d = *b->d * 2;
}
}
static inline void numrat_floor(numrat_t a, numrat_t b)
{
numint_fdiv_q(a->n, b->n, b->d);
numint_set_int(a->d,1);
}
static inline void numrat_ceil(numrat_t a, numrat_t b)
{
numint_cdiv_q(a->n, b->n, b->d);
numint_set_int(a->d,1);
}
static inline void numrat_trunc(numrat_t a, numrat_t b)
{
numint_tdiv_q(a->n, b->n, b->d);
numint_set_int(a->d,1);
}
static inline void numrat_sqrt(numrat_t up, numrat_t down, numrat_t b)
{
/* compute sqrt(p/q) as sqrt(p*q)/q */
numint_t pq;
assert(*b->n>=0);
numint_mul(pq, numrat_numref(b), numrat_denref(b));
numint_sqrt(numrat_numref(up), numrat_numref(down), pq);
numint_set(numrat_denref(up),numrat_denref(b));
numint_set(numrat_denref(down),numrat_denref(b));
numrat_canonicalize(up);
numrat_canonicalize(down);
}
static inline void numrat_mul_2exp(numrat_t a, numrat_t b, int c)
{
if (c>=0) {
*a->n = *b->n << c;
*a->d = *b->d;
}
else {
*a->n = *b->n;
*a->d = *b->d << (-c);
}
numrat_canonicalize(a);
}
/* ====================================================================== */
/* Arithmetic Tests */
/* ====================================================================== */
static inline int numrat_sgn(numrat_t a)
{ return numint_sgn(a->n); }
static inline int numrat_cmp(numrat_t a, numrat_t b)
{
numint_t aa,bb;
#if 0
*aa = *a->n * *b->d;
*bb = *a->d * *b->n;
#else /* more cost but less overflows */
numint_t d;
numint_lcm(d,a->d,b->d);
*aa = *a->n * (*d / *a->d);
*bb = (*d / *b->d) * *b->n;
#endif
return numint_cmp(aa,bb);
}
static inline int numrat_cmp_int(numrat_t a, long int b)
{
numint_t aa,bb;
*aa = *a->n;
*bb = *a->d * b;
return numint_cmp(aa,bb);
}
static inline bool numrat_equal(numrat_t a, numrat_t b)
{ return numint_equal(a->n,b->n) && numint_equal(a->d,b->d); }
static inline void numrat_min(numrat_t a, numrat_t b, numrat_t c)
{ numrat_set(a, numrat_cmp(b,c)<=0 ? b : c); }
static inline void numrat_max(numrat_t a, numrat_t b, numrat_t c)
{ numrat_set(a, numrat_cmp(b,c)>=0 ? b : c); }
static inline bool numrat_integer(numrat_t a)
{ return (*a->d==1); }
/* ====================================================================== */
/* Printing */
/* ====================================================================== */
static inline void numrat_fprint(FILE* stream, numrat_t a)
{
if (*a->n==0)
fprintf(stream,"0");
else if (*a->d==1)
numint_fprint(stream,a->n);
else {
numint_fprint(stream,a->n);
fprintf(stream,"/");
numint_fprint(stream,a->d);
}
}
static inline void numrat_print(numrat_t a)
{
numrat_fprint(stdout,a);
}
static inline int numrat_snprint(char* s, size_t size, numrat_t a)
{
int res;
if (*a->n==0)
res = snprintf(s,size, "0");
else if (*a->d==1)
res = numint_snprint(s,size,a->n);
else {
res = numint_snprint(s,size,a->n);
res += snprintf(s+res,size-res,"/");
res += numint_snprint(s+res,size-res,a->n);
}
return res;
}
/* ====================================================================== */
/* Conversions */
/* ====================================================================== */
/* int2 -> numrat */
static inline bool numrat_set_int2(numrat_t a, long int i, long int j)
{
assert(j>0);
numint_set_int(a->n,i);
numint_set_int(a->d,j);
numrat_canonicalize(a);
return true;
}
/* mpz -> numrat */
static inline bool numrat_set_mpz(numrat_t a, mpz_t b)
{
numint_set_mpz(a->n,b);
numint_set_int(a->d,1);
return true;
}
/* mpq -> numrat */
static inline bool numrat_set_mpq(numrat_t a, mpq_t b)
{
numint_set_mpz(a->n,mpq_numref(b));
numint_set_mpz(a->d,mpq_denref(b));
return true;
}
/* double -> numrat */
static inline bool numrat_set_double_tmp(numrat_t a, double k, mpq_t mpq)
{
if (!isfinite(k)) { DEBUG_SPECIAL; numrat_set_int(a,0); return false; }
mpq_set_d(mpq,k);
numrat_set_mpq(a,mpq);
return true;
}
static inline bool numrat_set_double(numrat_t a, double k)
{
mpq_t mpq;
mpq_init(mpq);
numrat_set_double_tmp(a,k,mpq);
mpq_clear(mpq);
return true;
}
/* mpfr -> numrat */
static inline bool numrat_set_mpfr(numrat_t a, mpfr_t b)
{
mpq_t q;
mp_exp_t e;
if (!mpfr_number_p(b)) { DEBUG_SPECIAL; numrat_set_int(a,0); return false; }
mpq_init(q);
/* XXX might fail if scaled exponent not representable in mp_exp_t */
e = mpfr_get_z_exp(mpq_numref(q),b);
mpz_set_si(mpq_denref(q),1);
if (e>0) mpq_mul_2exp(q,q,e);
if (e<0) mpq_div_2exp(q,q,-e);
numrat_set_mpq(a,q);
mpq_clear(q);
return true;
}
/* numrat -> int */
static inline bool int_set_numrat(long int* a, numrat_t b)
{
numint_t q,r;
numint_cdiv_qr(q,r,b->n,b->d);
*a = *q;
return (*r==0);
}
static inline bool int_set_numrat_tmp(long int* a, numrat_t b,
mpz_t q, mpz_t r)
{ return int_set_numrat(a,b); }
/* numrat -> mpz */
static inline bool mpz_set_numrat(mpz_t a, numrat_t b)
{
numint_t q,r;
numint_cdiv_qr(q,r,numrat_numref(b),numrat_denref(b));
mpz_set_numint(a,q);
return (numint_sgn(r)==0);
}
static inline bool mpz_set_numrat_tmp(mpz_t a, numrat_t b, mpz_t mpz)
{ return mpz_set_numrat(a,b); }
/* numrat -> mpq */
static inline bool mpq_set_numrat(mpq_t a, numrat_t b)
{
mpz_set_numint(mpq_numref(a), numrat_numref(b));
mpz_set_numint(mpq_denref(a), numrat_denref(b));
return true;
}
/* numrat -> double */
/* mpfr should have exactly a precision of 53 bits */
static inline bool double_set_numrat_tmp(double* a, numrat_t b,
mpq_t mpq, mpfr_t mpfr)
{
mpq_set_numrat(mpq,b);
int res = mpfr_set_q(mpfr,mpq,GMP_RNDU);
*a = mpfr_get_d(mpfr,GMP_RNDU); /* should be exact */
return (res==0);
}
static inline bool double_set_numrat(double* a, numrat_t b)
{
mpq_t mpq;
mpfr_t mpfr;
mpq_init(mpq);
mpfr_init2(mpfr,53);
bool res = double_set_numrat_tmp(a,b,mpq,mpfr);
mpq_clear(mpq);
mpfr_clear(mpfr);
return res;
}
/* numrat -> mpfr */
static inline bool mpfr_set_numrat(mpfr_t a, numrat_t b)
{
int r = mpfr_set_si(a,*numrat_numref(b),GMP_RNDU);
return !mpfr_div_si(a,a,*numrat_denref(b),GMP_RNDU) && !r;
}
static inline bool mpz_fits_numrat(mpz_t a)
{ return mpz_fits_numint(a); }
static inline bool mpq_fits_numrat(mpq_t a)
{ return mpz_fits_numint(mpq_numref(a)) && mpz_fits_numint(mpq_denref(a)); }
static inline bool double_fits_numrat_tmp(double k, mpq_t mpq)
{
if (!isfinite(k)) return false;
mpq_set_d(mpq,k);
bool res = mpq_fits_numrat(mpq);
return res;
}
static inline bool double_fits_numrat(double k)
{
mpq_t mpq;
mpq_init(mpq);
bool res = double_fits_numrat_tmp(k,mpq);
mpq_clear(mpq);
return res;
}
static inline bool mpfr_fits_numrat(mpfr_t a)
{
mpz_t z;
mp_exp_t e;
if (!mpfr_number_p(a)) return false;
mpz_init(z);
e = mpfr_get_z_exp(z,a);
if (!mpz_fits_numrat(z)) {
mpz_clear(z);
return false;
}
mpz_clear(z);
return e<(mp_exp_t)sizeof(numint_t)*8-1;
}
static inline bool numrat_fits_int(numrat_t a)
{
numint_t b;
numint_cdiv_q(b,a->n,a->d);
return *b<=LONG_MAX && *b>=-LONG_MAX;
}
static inline bool numrat_fits_float(numrat_t a)
{ return true; }
static inline bool numrat_fits_double(numrat_t a)
{ return true; }
static inline bool numrat_fits_mpfr(numrat_t a)
{ return true; }
/* ====================================================================== */
/* Serialization */
/* ====================================================================== */
static inline unsigned char numrat_serialize_id(void)
{ return 0x10 + sizeof(numint_t)/4; }
static inline size_t numrat_serialize(void* dst, numrat_t src)
{
size_t x = numint_serialize(dst,numrat_numref(src));
return x + numint_serialize((char*)dst+x,numrat_denref(src));
}
static inline size_t numrat_deserialize(numrat_t dst, const void* src)
{
size_t x = numint_deserialize(numrat_numref(dst),src);
return x + numint_deserialize(numrat_denref(dst),(const char*)src+x);
}
static inline size_t numrat_serialized_size(numrat_t a)
{
return numint_serialized_size(numrat_numref(a)) +
numint_serialized_size(numrat_denref(a));
}
#ifdef __cplusplus
}
#endif
#endif
|