/usr/include/numflt_native.h is in libapron-dev 0.9.10-9.
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 | /* ********************************************************************** */
/* numflt_native.h */
/* ********************************************************************** */
#ifndef _NUMFLT_NATIVE_H_
#define _NUMFLT_NATIVE_H_
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <stdint.h>
#include "gmp.h"
#include "mpfr.h"
#include "ap_scalar.h"
#include "num_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(NUMFLT_DOUBLE)
typedef double numflt_native;
#define NUMFLT_ZERO 0.0
#define NUMFLT_ONE 1.0
#define NUMFLT_MANT_DIG DBL_MANT_DIG
#elif defined(NUMFLT_LONGDOUBLE)
typedef long double numflt_native;
#define NUMFLT_ZERO 0.0L
#define NUMFLT_ONE 1.0L
#define NUMFLT_MANT_DIG LDBL_MANT_DIG
#else
#error "HERE"
#endif
typedef numflt_native numflt_t[1];
#define NUMFLT_MAX NUMFLT_ONE/NUMFLT_ZERO
#define NUMFLT_NATIVE
/* ====================================================================== */
/* Assignement */
/* ====================================================================== */
static inline void numflt_set(numflt_t a, numflt_t b)
{ *a = *b; }
static inline void numflt_set_array(numflt_t* a, numflt_t* b, size_t size)
{ memcpy(a,b,size*sizeof(numflt_t)); }
static inline void numflt_set_int(numflt_t a, long int i)
{ *a = (numflt_native)i; }
/* ====================================================================== */
/* Constructors and Destructors */
/* ====================================================================== */
static inline void numflt_init(numflt_t a)
{ *a = NUMFLT_ZERO; }
static inline void numflt_init_array(numflt_t* a, size_t size)
{
size_t i;
for (i=0; i<size; i++) *(a[i]) = NUMFLT_ZERO;
}
static inline void numflt_init_set(numflt_t a, numflt_t b)
{ numflt_set(a,b); }
static inline void numflt_init_set_int(numflt_t a, long int i)
{ numflt_set_int(a,i); }
static inline void numflt_clear(numflt_t a)
{}
static inline void numflt_clear_array(numflt_t* a, size_t size)
{}
static inline void numflt_swap(numflt_t a, numflt_t b)
{ numflt_t t; *t=*a;*a=*b;*b=*t; }
/* ====================================================================== */
/* Arithmetic Operations */
/* ====================================================================== */
static inline void numflt_neg(numflt_t a, numflt_t b)
{ *a = -(*b); }
static inline void numflt_add(numflt_t a, numflt_t b, numflt_t c)
{ *a = *b + *c; }
static inline void numflt_add_uint(numflt_t a, numflt_t b, unsigned long int c)
{ *a = *b + (numflt_native)c; }
static inline void numflt_sub(numflt_t a, numflt_t b, numflt_t c)
{ *a = *b - *c; }
static inline void numflt_sub_uint(numflt_t a, numflt_t b, unsigned long int c)
{ *a = *b - (numflt_native)c; }
static inline void numflt_mul(numflt_t a, numflt_t b, numflt_t c)
{ *a = *b * *c; }
static inline void numflt_div(numflt_t a, numflt_t b, numflt_t c)
{ *a = *b / *c; }
#if defined(NUMFLT_DOUBLE)
static inline void numflt_abs(numflt_t a, numflt_t b)
{ *a = fabs(*b); }
static inline void numflt_mul_2(numflt_t a, numflt_t b)
{ *a = ldexp(*b,1); }
static inline void numflt_div_2(numflt_t a, numflt_t b)
{ *a = ldexp(*b,-1); }
static inline void numflt_min(numflt_t a, numflt_t b, numflt_t c)
{ *a = fmin(*b,*c); }
static inline void numflt_max(numflt_t a, numflt_t b, numflt_t c)
{ *a = fmax(*b,*c); }
static inline void numflt_floor(numflt_t a, numflt_t b)
{ *a = floor(*b); }
static inline void numflt_ceil(numflt_t a, numflt_t b)
{ *a = ceil(*b); }
static inline void numflt_trunc(numflt_t a, numflt_t b)
{ *a = trunc(*b); }
static inline void numflt_sqrt(numflt_t up, numflt_t down, numflt_t b)
{
numflt_t x;
assert(*b>=0);
*x = sqrt(*b);
assert(*x**x>=*b); /* assumes round towards +oo! */
if (*x**x==*b) *down = *x;
else *down = nextafter(*x,0);
*up = *x;
}
static inline void numflt_mul_2exp(numflt_t a, numflt_t b, int c)
{ *a = ldexp(*b,c); }
#else
static inline void numflt_abs(numflt_t a, numflt_t b)
{ *a = fabsl(*b); }
static inline void numflt_mul_2(numflt_t a, numflt_t b)
{ *a = ldexpl(*b,1); }
static inline void numflt_div_2(numflt_t a, numflt_t b)
{ *a = ldexpl(*b,-1); }
static inline void numflt_min(numflt_t a, numflt_t b, numflt_t c)
{ *a = fminl(*b,*c); }
static inline void numflt_max(numflt_t a, numflt_t b, numflt_t c)
{ *a = fmaxl(*b,*c); }
static inline void numflt_floor(numflt_t a, numflt_t b)
{ *a = floorl(*b); }
static inline void numflt_ceil(numflt_t a, numflt_t b)
{ *a = ceill(*b); }
static inline void numflt_trunc(numflt_t a, numflt_t b)
{ *a = truncl(*b); }
static inline void numflt_sqrt(numflt_t up, numflt_t down, numflt_t b)
{
assert(*b>=0);
*up = sqrtl(*b);
assert(*up**up>=*b); /* assumes round towards +oo! */
if (*up**up==*b) *down = *up;
else *down = nextafterl(*up,0);
}
static inline void numflt_mul_2exp(numflt_t a, numflt_t b, int c)
{ *a = ldexpl(*b,c); }
#endif
/* ====================================================================== */
/* Arithmetic Tests */
/* ====================================================================== */
static inline int numflt_sgn(numflt_t a)
{ return (*a==NUMFLT_ZERO ? 0 : (*a>NUMFLT_ZERO ? 1 : -1)); }
static inline int numflt_cmp(numflt_t a, numflt_t b)
{ return (*a==*b ? 0 : (*a>*b ? 1 : -1)); }
static inline int numflt_cmp_int(numflt_t a, long int b)
{
numflt_native bb = (numflt_native)b;
return (*a==bb ? 0 : (*a>bb ? 1 : -1));
}
static inline bool numflt_equal(numflt_t a, numflt_t b)
{ return *a==*b; }
static inline bool numflt_integer(numflt_t a)
#if defined(NUMFLT_DOUBLE)
{ return ceil(*a) == *a; }
#else
{ return ceill(*a) == *a; }
#endif
/* ====================================================================== */
/* Printing */
/* ====================================================================== */
#if defined(NUMFLT_DOUBLE)
static inline void numflt_print(numflt_t a)
{ printf("%.*g",NUMFLT_PRINT_PREC,*a+NUMFLT_ZERO); }
static inline void numflt_fprint(FILE* stream, numflt_t a)
{ fprintf(stream,"%.*g",NUMFLT_PRINT_PREC,*a+NUMFLT_ZERO); }
static inline int numflt_snprint(char* s, size_t size, numflt_t a)
{ return snprintf(s,size,"%.*g",NUMFLT_PRINT_PREC,*a+NUMFLT_ZERO); }
#else
static inline void numflt_print(numflt_t a)
{ printf("%.*Lg",NUMFLT_PRINT_PREC,*a+NUMFLT_ZERO); }
static inline void numflt_fprint(FILE* stream, numflt_t a)
{ fprintf(stream,"%.*Lg",NUMFLT_PRINT_PREC,*a+NUMFLT_ZERO); }
static inline int numflt_snprint(char* s, size_t size, numflt_t a)
{ return snprintf(s,size,"%.*Lg",NUMFLT_PRINT_PREC,*a+NUMFLT_ZERO); }
#endif
/* ====================================================================== */
/* Conversions */
/* ====================================================================== */
/* int2 -> numflt */
static inline bool numflt_set_int2(numflt_t a, long int i, long int j)
{
assert(j>0);
*a = (numflt_native)i/(numflt_native)j;
return (-*a==(numflt_native)(-i)/(numflt_native)j);
}
/* mpz -> numflt */
/* mpfr is supposed to have exactly the IEEE754 double precision of NUMFLT_MANT_DIG bits */
static inline bool numflt_set_mpz_tmp(numflt_t a, mpz_t b, mpfr_t mpfr)
{
int res = mpfr_set_z(mpfr,b,GMP_RNDU);
#if defined(NUMFLT_DOUBLE)
*a = mpfr_get_d(mpfr,GMP_RNDU);/* Normally, exact conversion here (unless overfloww) */
#else
*a = mpfr_get_ld(mpfr,GMP_RNDU);/* Normally, exact conversion here (unless overfloww) */
#endif
return (res==0);
}
static inline bool numflt_set_mpz(numflt_t a, mpz_t b)
{
mpfr_t mpfr;
mpfr_init2(mpfr,NUMFLT_MANT_DIG);
bool res = numflt_set_mpz_tmp(a,b,mpfr);
mpfr_clear(mpfr);
return res;
}
/* mpq -> numflt */
/* mpfr is supposed to have exactly the IEEE754 double precision of NUMFLT_MANT_DIG bits */
static inline bool numflt_set_mpq_tmp(numflt_t a, mpq_t b, mpfr_t mpfr)
{
int res = mpfr_set_q(mpfr,b,GMP_RNDU);
#if defined(NUMFLT_DOUBLE)
*a = mpfr_get_d(mpfr,GMP_RNDU);/* Normally, exact conversion here (unless overfloww) */
#else
*a = mpfr_get_ld(mpfr,GMP_RNDU);/* Normally, exact conversion here (unless overfloww) */
#endif
return (res==0);
}
static inline bool numflt_set_mpq(numflt_t a, mpq_t b)
{
mpfr_t mpfr;
mpfr_init2(mpfr,NUMFLT_MANT_DIG);
bool res = numflt_set_mpq_tmp(a,b,mpfr);
mpfr_clear(mpfr);
return res;
}
/* double -> numflt */
static inline bool numflt_set_double(numflt_t a, double k)
{ *a = (numflt_native)k; return true; }
/* mpfr -> numflt */
static inline bool numflt_set_mpfr(numflt_t a, mpfr_t b)
{
#if defined(NUMFLT_DOUBLE)
*a = mpfr_get_d(b,GMP_RNDU);
return !mpfr_cmp_d(b,*a);
#else
*a = mpfr_get_ld(b,GMP_RNDU);
return !mpfr_cmp_ld(b,*a);
#endif
}
/* numflt -> int */
static inline bool int_set_numflt(long int* a, numflt_t b)
{
numflt_native c;
numflt_ceil(&c,b);
if (!isfinite(c)) { DEBUG_SPECIAL; *a = 0; return false; }
*a = (long int)c;
return (*b==c);
}
/* numflt -> mpz */
static inline bool mpz_set_numflt(mpz_t a, numflt_t b)
{
double c = ceil(*b);
if (!isfinite(c)) { DEBUG_SPECIAL; mpz_set_ui(a,0); return false; }
mpz_set_d(a,c);
return (*b==(numflt_native)c);
}
/* numflt -> mpq */
static inline bool mpq_set_numflt(mpq_t a, numflt_t b)
#if defined(NUMFLT_DOUBLE)
{
if (!isfinite(*b)) { DEBUG_SPECIAL; mpq_set_ui(a,0,1); return false; }
mpq_set_d(a,*b);
return true;
}
#else
{
double c = (double)(*b);
if (!isfinite(c)) { DEBUG_SPECIAL; mpq_set_ui(a,0,1); return false; }
mpq_set_d(a,c);
return (*b==(numflt_native)c);
}
#endif
/* numflt -> double */
static inline bool double_set_numflt(double* a, numflt_t b)
#if defined(NUMFLT_DOUBLE)
{ *a = *b; return true; }
#else
{ *a = *b; return ((numflt_native)(*a)==*b); }
#endif
/* numflt -> mpfr */
static inline bool mpfr_set_numflt(mpfr_t a, numflt_t b)
#if defined(NUMFLT_DOUBLE)
{ return !mpfr_set_d(a,*b,GMP_RNDU); }
#else
{ return !mpfr_set_ld(a,*b,GMP_RNDU); }
#endif
static inline bool mpz_fits_numflt(mpz_t a)
{
double k = mpz_get_d(a);
return (fabs(k)+1.0) != (double)1.0/(double)0.0;
}
static inline bool mpq_fits_numflt(mpq_t a)
{
double k = mpq_get_d(a);
return (fabs(k)+1.0) != (double)1.0/(double)0.0;
}
static inline bool double_fits_numflt(double a)
{ return true; }
static inline bool mpfr_fits_numflt(mpfr_t a)
{ return mpfr_get_exp(a)<1022; /* XXX >1022 for long double */ }
static inline bool numflt_fits_int(numflt_t a)
{
numflt_native d;
numflt_ceil(&d,a);
return isfinite(d) && d >= (numflt_native)(-LONG_MAX) && d<= (numflt_native)LONG_MAX;
}
static inline bool numflt_fits_float(numflt_t a)
{
int e;
#if defined(NUMFLT_DOUBLE)
frexp(*a,&e);
#else
frexpl(*a,&e);
#endif
return (e<127);
}
static inline bool numflt_fits_double(numflt_t a)
{
#if defined(NUMFLT_DOUBLE)
return true;
#else
int e;
frexpl(*a,&e);
return (e<1023);
#endif
}
static inline bool numflt_fits_mpfr(numflt_t a)
{
int e;
#if defined(NUMFLT_DOUBLE)
frexp(*a,&e);
#else
frexpl(*a,&e);
#endif
return (e<mpfr_get_emax());
}
/* ====================================================================== */
/* Only for floating point */
/* ====================================================================== */
static inline bool numflt_infty(numflt_t a)
{ return !isfinite(*a); }
static inline void numflt_set_infty(numflt_t a, int sgn)
{ *a = sgn>0 ? NUMFLT_MAX : -NUMFLT_MAX; }
/* ====================================================================== */
/* Serialization */
/* ====================================================================== */
static inline size_t numflt_serialize(void* dst, numflt_t src)
{
num_store_words8(dst,src,sizeof(numflt_t));
return sizeof(numflt_t);
}
static inline size_t numflt_deserialize(numflt_t dst, const void* src)
{
num_store_words8(dst,src,sizeof(numflt_t));
return sizeof(numflt_t);
}
static inline size_t numflt_serialized_size(numflt_t a)
{ return sizeof(numflt_t); }
/* */
static inline bool ap_scalar_set_numflt(ap_scalar_t* a, numflt_t b)
{
ap_scalar_reinit(a,AP_SCALAR_DOUBLE);
return double_set_numflt(&a->val.dbl,b);
}
#ifdef __cplusplus
}
#endif
#endif
|