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
|
/*
Copyright 2006-2022 by
Laboratoire de l'Informatique du Parallelisme,
UMR CNRS - ENS Lyon - UCB Lyon 1 - INRIA 5668,
Laboratoire d'Informatique de Paris 6, equipe PEQUAN,
UPMC Universite Paris 06 - CNRS - UMR 7606 - LIP6, Paris, France,
Laboratoire d'Informatique de Paris 6 - Équipe PEQUAN
Sorbonne Universités
UPMC Univ Paris 06
UMR 7606, LIP6
Boîte Courrier 169
4, place Jussieu
F-75252 Paris Cedex 05
France,
Sorbonne Université
CNRS, Laboratoire d'Informatique de Paris 6, LIP6
F - 75005 Paris
France,
LORIA (CNRS, INPL, INRIA, UHP, U-Nancy 2)
Centre de recherche INRIA Sophia Antipolis Mediterranee, equipe APICS,
Sophia Antipolis, France
and by
Department of Computer Science & Engineering
UAA College of Engineering
University of Alaska Anchorage.
Contributors Ch. Lauter, S. Chevillard
christoph.lauter@christoph-lauter.org
sylvain.chevillard@ens-lyon.org
This software is a computer program whose purpose is to provide an
environment for safe floating-point code development. It is
particularly targeted to the automated implementation of
mathematical floating-point libraries (libm). Amongst other features,
it offers a certified infinity norm, an automatic polynomial
implementer and a fast Remez algorithm.
This software is governed by the CeCILL-C license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL-C
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL-C license and that you accept its terms.
This program is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef EXPRESSION_H
#define EXPRESSION_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <mpfr.h>
#include <stdio.h>
#include "base-functions.h"
#include "chain.h"
#include "library.h"
#include "hooks.h"
#include "polynomials.h"
/* A macro to handle compiler warnings for intended fall-thrus in switch statements */
#ifdef HAVE_FALLTHRU_ATTRIBUTE
# if HAVE_FALLTHRU_ATTRIBUTE
# define SOLLYA_INTENDED_FALLTHRU __attribute__((fallthrough));
# else
# define SOLLYA_INTENDED_FALLTHRU
# endif
#else
# define SOLLYA_INTENDED_FALLTHRU
#endif
/* End of the fall-thru macro */
enum __point_eval_enum_t {
POINT_EVAL_FAILURE = 0,
POINT_EVAL_EXACT,
POINT_EVAL_CORRECTLY_ROUNDED,
POINT_EVAL_CORRECTLY_ROUNDED_PROVEN_INEXACT,
POINT_EVAL_FAITHFULLY_ROUNDED,
POINT_EVAL_FAITHFULLY_ROUNDED_PROVEN_INEXACT,
POINT_EVAL_BELOW_CUTOFF
};
typedef enum __point_eval_enum_t point_eval_t;
/* Possible values for nodeType */
#define VARIABLE 0
#define CONSTANT 1
#define ADD 2
#define SUB 3
#define MUL 4
#define DIV 5
#define NEG 6
#define UNARY_BASE_FUNC 7
#define POW 8
#define PI_CONST 9
#define POLYNOMIAL 10
#define LIBRARYFUNCTION 11
#define LIBRARYCONSTANT 12
#define PROCEDUREFUNCTION 13
/* Attention: other values for nodeType defined in execute.h, and also #define MEMREF 278 below */
#define SOLLYA_MAX_ARG_ARRAY_ALLOC_SIZE 33554432ull
#ifndef NODE_TYPEDEF
#define NODE_TYPEDEF
typedef struct nodeStruct node;
#endif
typedef struct memRefCacheStruct *memRefCache;
struct memRefCacheStruct {
node *prev;
node *next;
sollya_mpfi_t *evalCacheX, *evalCacheY;
mp_prec_t evalCachePrec;
node *derivCache;
node *derivUnsimplCache;
int simplifyCacheDoesNotSimplify;
int simplifyCacheRationalMode;
node *simplifyCache;
node *hornerCache;
int isCorrectlyTyped;
eval_hook_t *evaluationHook;
polynomial_t polynomialRepresentation;
int memRefChildFromPolynomial;
mpfr_t *pointEvalCacheX, *pointEvalCacheY;
mp_exp_t pointEvalCacheCutoff;
point_eval_t pointEvalCacheResultType;
uint64_t hash;
int hashComputed;
int treeSizeCache;
int treeSizeCacheFilled;
node *substituteCacheY;
node *substituteCacheX;
int substituteCacheMaySimplify;
int containsNotANumbersIsCached;
int containsNotANumbersCacheResult;
int containsHooksIsCached;
int containsHooksCacheResult;
unsigned int containsHooksCall;
int isPureTreeIsCached;
int isPureTreeCacheResult;
int isConstantIsCached;
int isConstantCacheResult;
int faithEvalOptFunSupportCached;
int faithEvalOptFunSupportCacheResult;
int containsPiCached;
int containsPiResult;
int evaluateSignCached;
int evaluateSignResultSign;
int evaluateSignResultOkay;
int evaluateSignAuxCached;
int evaluateSignAuxResultSign;
int evaluateSignAuxResultOkay;
int tryRepresentAsPolynomialDoesNotWork;
};
struct nodeStruct
{
int nodeType;
mpfr_t *value;
node *child1;
node *child2;
libraryFunction *libFun;
baseFunction const *baseFun;
long long int libFunDeriv;
char *string;
chain *arguments;
libraryProcedure *libProc;
libraryData *libData;
node **argArray;
size_t argArraySize;
size_t argArrayAllocSize;
memRefCache cache;
};
/* HELPER TYPE FOR THE PARSER */
typedef struct doubleNodeStruct doubleNode;
struct doubleNodeStruct
{
node *a;
node *b;
};
/* END HELPER TYPE */
typedef struct rangetypeStruct rangetype;
struct rangetypeStruct
{
mpfr_t *a;
mpfr_t *b;
};
#define MEMREF 278
void *safeMalloc(size_t);
void safeFree(void *);
/* Highly important and often used functions */
extern node *memref_chain_start;
void deferSignalHandling();
void resumeSignalHandling();
static inline void memref_chain_manipulate_start() {
deferSignalHandling();
}
static inline void memref_chain_manipulate_end() {
resumeSignalHandling();
}
static inline void link_memref(node *ptr) {
if (ptr == NULL) return;
if (ptr->nodeType != MEMREF) return;
if (ptr->cache == NULL) return;
memref_chain_manipulate_start();
ptr->cache->prev = NULL;
ptr->cache->next = memref_chain_start;
if (memref_chain_start != NULL) {
if (memref_chain_start->nodeType == MEMREF) {
if (memref_chain_start->cache != NULL) {
memref_chain_start->cache->prev = ptr;
}
}
}
memref_chain_start = ptr;
memref_chain_manipulate_end();
}
static inline void unlink_memref(node *ptr) {
if (ptr == NULL) return;
if (ptr->nodeType != MEMREF) return;
if (ptr->cache == NULL) return;
memref_chain_manipulate_start();
if (ptr->cache->prev == NULL) {
memref_chain_start = ptr->cache->next;
if (memref_chain_start != NULL) {
if (memref_chain_start->nodeType == MEMREF) {
if (memref_chain_start->cache != NULL) {
memref_chain_start->cache->prev = NULL;
}
}
}
} else {
if (ptr->cache->prev->nodeType == MEMREF) {
if (ptr->cache->prev->cache != NULL) {
ptr->cache->prev->cache->next = ptr->cache->next;
}
}
}
if (ptr->cache->next != NULL) {
if (ptr->cache->next->nodeType == MEMREF) {
if (ptr->cache->next->cache != NULL) {
ptr->cache->next->cache->prev = ptr->cache->prev;
}
}
}
ptr->cache->prev = NULL;
ptr->cache->next = NULL;
memref_chain_manipulate_end();
}
static inline node* allocateNode() {
node *res;
res = (node *) safeMalloc(sizeof(node));
return res;
}
static inline void freeNode(node *ptr) {
if (ptr == NULL) return;
safeFree(ptr);
}
static inline void freeMemRef(node *ptr) {
if (ptr == NULL) return;
unlink_memref(ptr);
safeFree(ptr);
}
static inline node* addMemRefEvenOnNull(node *tree) {
void *ptr, *ptrCache;
node *res;
ptr = safeMalloc(sizeof(node) + sizeof(struct memRefCacheStruct));
ptrCache = ptr + sizeof(node);
res = (node *) ptr;
res->nodeType = MEMREF;
res->child1 = tree;
res->libFunDeriv = 1;
res->child2 = NULL;
res->arguments = NULL;
res->value = NULL;
res->cache = (memRefCache) ptrCache;
res->cache->evalCacheX = NULL;
res->cache->evalCacheY = NULL;
res->cache->evalCachePrec = 0;
res->cache->derivCache = NULL;
res->cache->derivUnsimplCache = NULL;
res->cache->simplifyCacheDoesNotSimplify = -1;
res->cache->simplifyCacheRationalMode = -1;
res->cache->simplifyCache = NULL;
res->cache->hornerCache = NULL;
res->cache->isCorrectlyTyped = 0;
res->cache->evaluationHook = NULL;
res->cache->polynomialRepresentation = NULL;
res->cache->memRefChildFromPolynomial = 0;
res->cache->pointEvalCacheX = NULL;
res->cache->pointEvalCacheY = NULL;
res->cache->hashComputed = 0;
res->cache->treeSizeCache = -1;
res->cache->treeSizeCacheFilled = 0;
res->cache->substituteCacheY = NULL;
res->cache->substituteCacheX = NULL;
res->cache->substituteCacheMaySimplify = 0;
res->cache->containsNotANumbersIsCached = 0;
res->cache->containsNotANumbersCacheResult = 1;
res->cache->containsHooksIsCached = 0;
res->cache->containsHooksCacheResult = 0;
res->cache->containsHooksCall = 0u;
res->cache->isPureTreeIsCached = 0;
res->cache->isPureTreeCacheResult = 0;
res->cache->isConstantIsCached = 0;
res->cache->isConstantCacheResult = 0;
res->cache->faithEvalOptFunSupportCached = 0;
res->cache->faithEvalOptFunSupportCacheResult = 0;
res->cache->containsPiCached = 0;
res->cache->containsPiResult = 0;
res->cache->evaluateSignCached = 0;
res->cache->evaluateSignResultSign = 0;
res->cache->evaluateSignResultOkay = 0;
res->cache->evaluateSignAuxCached = 0;
res->cache->evaluateSignAuxResultSign = 0;
res->cache->evaluateSignAuxResultOkay = 0;
res->cache->tryRepresentAsPolynomialDoesNotWork = 0;
link_memref(res);
return res;
}
static inline node* addMemRef(node *tree) {
if (tree == NULL) return NULL;
if (tree->nodeType == MEMREF) return tree;
return addMemRefEvenOnNull(tree);
}
static inline node* getMemRefChild(node *tree) {
if (tree == NULL) return NULL;
if (tree->nodeType != MEMREF) return tree->child1;
if (tree->child1 != NULL) return tree->child1;
if (tree->cache->polynomialRepresentation == NULL) return NULL;
tree->child1 = polynomialGetExpressionExplicit(tree->cache->polynomialRepresentation);
tree->cache->memRefChildFromPolynomial = 1;
return tree->child1;
}
static inline node* accessThruMemRef(node *tree) {
node *res;
if (tree == NULL) return NULL;
res = tree;
while (res->nodeType == MEMREF) {
res = getMemRefChild(res);
}
return res;
}
/* End of highly used functions */
void printTree(node *tree);
char *sprintTreeImmediateWarning(node *tree);
node* differentiate(node *tree);
node* differentiateUnsimplified(node *tree);
node* simplifyTree(node *tree);
void free_memory(node *tree);
int evaluateConstantExpression(mpfr_t result, node *tree, mp_prec_t prec);
void evaluate(mpfr_t result, node *tree, mpfr_t x, mp_prec_t prec);
void printValue(mpfr_t *value);
char *sprintValueImmediateWarning(mpfr_t *value);
node* copyTree(node *tree);
int treeContainsHooks(node *tree);
node* horner(node *tree);
int getDegree(node *tree);
int getDegreeSilent(node *tree);
int getDegreeMpz(mpz_t res, node *tree);
int getDegreeMpzVerified(mpz_t res, node *tree);
int isPolynomialExtraSafe(node *tree);
node* expand(node *tree);
node* simplifyTreeErrorfree(node *tree);
int isNotUniformlyZero(node *tree);
int isNotUniformlyInfinite(node *tree);
int isIntegerConstant(node *tree);
int getNumeratorDenominator(node **numerator, node **denominator, node *tree);
node *substitute(node* tree, node *t);
node *substituteEnhanced(node* tree, node *t, int doNotEvaluate, int maySimplify);
void composePolynomials(node **, chain **, node *, node *, mp_prec_t);
int readDyadic(mpfr_t res, char *c);
int readHexadecimal(mpfr_t res, char *c);
int isPolynomial(node *tree);
int isRationalFunction(node *tree);
int isAffine(node *tree);
int arity(node *tree);
void fprintValue(FILE *fd, mpfr_t value);
void fprintTree(FILE *fd, node *tree);
int isSyntacticallyEqual(node *tree1, node *tree2);
void fprintHeadFunction(FILE *fd,node *tree, char *x, char *y);
int isConstant(node *tree);
void getCoefficients(int *degree, node ***coefficients, node *poly);
node *makePolynomial(mpfr_t *coefficients, int degree);
node *makePolynomialConstantExpressions(node **coeffs, int deg);
int treeSize(node *tree);
void printMpfr(mpfr_t x);
int highestDegreeOfPolynomialSubexpression(node *tree);
node *getIthCoefficient(node *poly, int i);
node *getSubpolynomial(node *poly, chain *monomials, int fillDegrees, mp_prec_t prec);
node *makeCanonical(node *func, mp_prec_t prec);
char *sprintTree(node *tree);
char *sprintValue(mpfr_t *value);
char *sPrintHexadecimal(mpfr_t x);
void printBinary(mpfr_t x);
int isHorner(node *tree);
int isCanonical(node *tree);
int isPowerOfVariable(node *);
char *sprintMidpointMode(mpfr_t a, mpfr_t b);
void fprintValueForXml(FILE *, mpfr_t );
void fprintValueWithPrintMode(FILE *, mpfr_t );
void fprintTreeWithPrintMode(FILE *, node *);
int readDecimalConstant(mpfr_t ,char *);
int getMaxPowerDivider(node *tree);
void simplifyMpfrPrec(mpfr_t rop, mpfr_t op);
node *simplifyRationalErrorfree(node *tree);
int tryEvaluateConstantTermToMpq(mpq_t res, node *tree);
node *simplifyAllButDivision(node *tree);
int sollya_mpfr_to_mpq( mpq_t y, mpfr_t x);
int sollya_mpfr_to_mpz( mpz_t y, mpfr_t x);
mp_prec_t getMpzPrecision(mpz_t x);
void mpz_to_mpfr(mpfr_t res, mpz_t a);
int mpqHoldsOnMpfr(mpfr_t res, mpq_t a);
int containsOnlyRealNumbers(node * tree);
void tryCopyTreeAnnotations(node *newTree, node *oldTree);
int containsNonDifferentiableSubfunctions(node *tree);
node *gcd(node *a, node *b);
node *eucldiv(node *a, node *b);
node *euclmod(node *a, node *b);
void bezout(node **g, node **r, node **a, node **b, node *p, node *q);
char *generateDecimalString(int, mpz_t, mpfr_exp_t, mp_prec_t);
node *makeVariable();
node *makeConstant(mpfr_t x);
node *makeConstantMinPrec(mpfr_t x);
node *makeConstantDouble(double x);
node *makeConstantInt(int x);
node *makeConstantUnsignedInt(unsigned int x);
node *makeConstantMpz(mpz_t x);
node *makeAdd(node *op1, node *op2);
node *makeSub(node *op1, node *op2);
node *makeMul(node *op1, node *op2);
node *makeDiv(node *op1, node *op2);
node *makeNeg(node *op1);
node *makePow(node *op1, node *op2);
node *makePi();
node *makeUnary(node *op1, baseFunction const *baseFun);
node *makeBinary(node *op1, node *op2, int opType);
node* addMemRef(node *);
node* accessThruMemRef(node *);
void freeVariableCache();
void resetVariableCacheHandling();
#endif /* ifdef EXPRESSION_H*/
|