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 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/*
Copyright 2006-2018 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)
and by
Centre de recherche INRIA Sophia Antipolis Mediterranee, equipe APICS,
Sophia Antipolis, France.
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.
*/
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#include <gmp.h>
#include <mpfr.h>
#include <stdio.h> /* fprintf, fopen, fclose, */
#include <sys/wait.h> /* wait */
#include <unistd.h> /* execve, fork, daemon */
#include <stdlib.h> /* exit, free, mktemp */
#include <errno.h>
#include <string.h>
#include "plot.h"
#include "expression.h"
#include "general.h"
#include "infnorm.h"
#include "chain.h"
#include "printf.h"
#include "signalhandling.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
extern int fileNumber;
int checkFileDescriptor(FILE *fd, char *s) {
if (fd == NULL) {
printMessage(1,SOLLYA_MSG_PLOT_COULD_NOT_OPEN_FILE,"Error: the file %s requested by plot could not be opened for writing: ",s);
printMessage(1,SOLLYA_MSG_CONTINUATION,"\"%s\".\n",strerror(errno));
return 0;
}
return 1;
}
void plotTree(chain *treeList, mpfr_t a, mpfr_t b, unsigned long int points, mp_prec_t prec, char *name, int type) {
int test, i, flush;
chain *list;
node *tree;
mpfr_t current_x, x, y, step, perturb, cutoff;
gmp_randstate_t random_state;
double xd, yd, ad, bd;
FILE *file;
char *gplotname;
char *dataname;
char *outputname;
int tern;
mp_prec_t p, pp;
int overflow;
int plotPossible;
int n;
char *gnuplotname;
gnuplotname = getGnuplotName();
plotPossible = 0;
pp = prec;
if (prec < 64) pp = 64;
mpfr_init2(x, pp);
mpfr_init2(step, pp);
mpfr_init2(y, 64);
mpfr_sub(step, b, a, GMP_RNDN);
mpfr_div_ui(step, step, points, GMP_RNDN);
if (mpfr_sgn(step) == 0) {
list = treeList;
mpfr_set_prec(y,prec);
while(list != NULL) {
tree = (node *)(list->value);
evaluateFaithful(y,tree,a,prec);
if (!mpfr_number_p(y)) {
printMessage(1,SOLLYA_MSG_CONSTANT_EXPR_CANNOT_BE_EVALUATED_AT_ALL,"Warning: this constant function is not evaluable by this tool.\n");
}
outputMode();
printValue(&y);
sollyaPrintf("\n");
list = list->next;
}
mpfr_clear(x); mpfr_clear(y); mpfr_clear(step);
return;
}
if (mpfr_sgn(step) < 0) {
printMessage(1,SOLLYA_MSG_DOMAIN_IS_EMPTY,"Warning: the interval is empty\n");
mpfr_clear(x); mpfr_clear(y); mpfr_clear(step);
return;
}
test=1;
list = treeList;
while(list != NULL && (test==1)) {
tree = (node *)(list->value);
if(!isConstant(tree)) test=0;
list = list->next;
}
if (test) {
mpfr_set_prec(y,prec);
mpfr_set_d(x,1.0,GMP_RNDN);
list = treeList;
while(list != NULL && (test==1)) {
tree = (node *)(list->value);
evaluateFaithful(y,tree,x,prec);
if (!mpfr_number_p(y)) {
printMessage(1,SOLLYA_MSG_CONSTANT_EXPR_CANNOT_BE_EVALUATED_AT_ALL,"Warning: this constant function is not evaluable by this tool.\n");
}
outputMode();
printValue(&y);
sollyaPrintf("\n");
list = list->next;
}
mpfr_clear(x); mpfr_clear(y); mpfr_clear(step);
return;
}
if(name==NULL) {
n = sollya_snprintf(NULL, 0, "%s/%s%s-%04d", getTempDir(), PACKAGE_NAME, getUniqueId(), fileNumber);
gplotname = (char *)safeCalloc(n+3, sizeof(char));
sollya_snprintf(gplotname, n+3, "%s/%s%s-%04d.p",getTempDir(), PACKAGE_NAME, getUniqueId(), fileNumber);
dataname = (char *)safeCalloc(n+5, sizeof(char));
sollya_snprintf(dataname, n+5, "%s/%s%s-%04d.dat",getTempDir(),PACKAGE_NAME,getUniqueId(), fileNumber);
outputname = (char *)safeCalloc(1, sizeof(char));
fileNumber++;
if (fileNumber >= NUMBEROFFILES) fileNumber=0;
}
else {
gplotname = (char *)safeCalloc(strlen(name)+3,sizeof(char));
sprintf(gplotname,"%s.p",name);
dataname = (char *)safeCalloc(strlen(name)+5,sizeof(char));
sprintf(dataname,"%s.dat",name);
outputname = (char *)safeCalloc(strlen(name)+5,sizeof(char));
if ((type==PLOTPOSTSCRIPT) || (type==PLOTPOSTSCRIPTFILE)) sprintf(outputname,"%s.eps",name);
}
file = fopen(gplotname, "w");
if (checkFileDescriptor(file, gplotname)) {
sollyaFprintf(file, "# Gnuplot script generated by %s\n",PACKAGE_NAME);
ad = sollya_mpfr_get_d(a, GMP_RNDD);
bd = sollya_mpfr_get_d(b, GMP_RNDU);
if ((!(ad-ad == 0.0)) || (!(bd-bd == 0.0)))
printMessage(1, SOLLYA_MSG_PLOT_OVERFLOW_OCCURRED_ON_CONVERSION_TO_DOUBLE, "Warning: an overflow occurred in a conversion mpfr to double while plotting.\n");
if (!(ad-ad == 0.0))
ad = -MAX_VALUE_GNUPLOT;
if (!(bd-bd == 0.0))
bd = MAX_VALUE_GNUPLOT;
if ((type==PLOTPOSTSCRIPT) || (type==PLOTPOSTSCRIPTFILE)) sollyaFprintf(file,"set terminal postscript eps color\nset out \"%s\"\n",outputname);
sollyaFprintf(file, "set format x \"%%g\"\n");
sollyaFprintf(file, "set format y \"%%g\"\n");
sollyaFprintf(file, "set xrange [%1.50e:%1.50e]\n", ad, bd);
sollyaFprintf(file, "plot ");
i=2;
list = treeList;
while(list != NULL) {
sollyaFprintf(file,"\"%s\" using 1:%d with lines t \"\"",dataname,i);
if(list->next != NULL) sollyaFprintf(file,",");
i++;
list = list->next;
}
sollyaFprintf(file,"\n");
if ((name==NULL) || (type==PLOTFILE)) {
sollyaFprintf(file,"pause mouse close\n");
}
fclose(file);
file = fopen(dataname, "w");
if (checkFileDescriptor(file, dataname)) {
plotPossible = 1;
mpfr_init2(cutoff, pp);
mpfr_set_d(cutoff,1.0,GMP_RNDN);
p = prec;
if (p < 64) p = 64;
mpfr_div_2ui(cutoff,cutoff,4*p,GMP_RNDN);
overflow = 0;
flush = 0;
mpfr_init2(current_x, pp);
gmp_randinit_default(random_state);
gmp_randseed_ui(random_state, 65845285);
mpfr_init2(perturb, pp);
mpfr_set(current_x,a,GMP_RNDN);
mpfr_set(x, current_x, GMP_RNDN);
while(mpfr_lessequal_p(x,b)) {
xd = sollya_mpfr_get_d(x, GMP_RNDN);
if (xd >= MAX_VALUE_GNUPLOT) xd = MAX_VALUE_GNUPLOT;
if (xd <= -MAX_VALUE_GNUPLOT) xd = -MAX_VALUE_GNUPLOT;
sollyaFprintf(file, "%1.50e",xd);
list = treeList;
while(list != NULL) {
tree = (node *)(list->value);
tern = evaluateFaithfulWithCutOff(y, tree, x, cutoff, prec);
if (tern == 2) {
flush = 1;
printMessage(2,SOLLYA_MSG_PLOT_FUNC_PROVEN_LESS_THAN_2_TO_MINUS_PREC,"Information: function image proven to be less than 2^(-%d) on point %s = %v\nThis point will be plotted as the midpoint of the proof interval.\n",(int)p,((variablename == NULL) ? "_x_" : variablename),x);
}
if (!mpfr_number_p(y)) {
printMessage(2,SOLLYA_MSG_PLOT_FUNC_UNDEFINED_OR_UNSTABLE_AT_POINT,"Information: function undefined or not evaluable in point %s = %v\nThis point will not be plotted.\n",((variablename == NULL) ? "_x_" : variablename),x);
}
yd = sollya_mpfr_get_d(y, GMP_RNDN);
if (!(yd-yd == 0.0)) overflow = 1;
if (yd >= MAX_VALUE_GNUPLOT) {
yd = MAX_VALUE_GNUPLOT;
overflow = 1;
}
if (yd <= -MAX_VALUE_GNUPLOT) {
yd = -MAX_VALUE_GNUPLOT;
overflow = 1;
}
sollyaFprintf(file, "\t%1.50e", yd);
list = list->next;
}
sollyaFprintf(file,"\n");
mpfr_add(current_x, current_x,step,GMP_RNDU);
mpfr_urandomb(perturb, random_state); mpfr_mul_2ui(perturb, perturb, 1, GMP_RNDN);
mpfr_sub_ui(perturb, perturb, 1, GMP_RNDN); mpfr_div_2ui(perturb, perturb, 2, GMP_RNDN);
mpfr_mul(perturb, perturb, step, GMP_RNDN); /* perturb \in [-step/4; step/4] */
mpfr_add(x, current_x, perturb, GMP_RNDU);
}
mpfr_clear(cutoff);
mpfr_clear(perturb);
mpfr_clear(current_x);
gmp_randclear(random_state);
fclose(file);
if (overflow)
printMessage(1, SOLLYA_MSG_PLOT_OVERFLOW_OCCURRED_ON_CONVERSION_TO_DOUBLE, "Warning: an overflow occurred in a conversion mpfr to double while plotting.\n");
if (flush) {
printMessage(1,SOLLYA_MSG_PLOT_NOT_FAITHFULLY_EVALUATED_AT_SOME_POINT,"Warning: the evaluation of the image on at least one point of at least one function has not been faithfully accurate.\n");
}
}
}
mpfr_clear(x); mpfr_clear(y); mpfr_clear(step);
deferSignalHandling();
fflush(NULL);
parserFlushInput();
resumeSignalHandling();
if (plotPossible) {
if ((name==NULL) || (type==PLOTFILE)) {
if (fork()==0) { /* The daemon call could fail (and will on certain Cygwin), in which case a double fork yields (almost (mod HUP etc.)) the same result. If daemon works, the fork has no effect */
if (daemon(1,0) == 0) { /* We want the prompt to return, so we need a daemon. We need to connect that daemon to /dev/null, which we do by setting the 2nd arg to zero */
if (fork() == 0) { /* We need a child of the daemon that waits for gnuplot to remove the file */
execlp(gnuplotname, gnuplotname, gplotname, NULL);
perror("An error occurred when calling gnuplot ");
exit(1);
} else {
wait(NULL);
sleep(1);
if (name == NULL) {
remove(gplotname);
remove(dataname);
}
sleep(1);
exit(0);
}
} else {
fprintf(stderr, "Could not create a daemon for gnuplot\n");
if (fork() == 0) { /* We could not create a daemon but we still want a child to wait for us */
execlp(gnuplotname, gnuplotname, gplotname, NULL);
perror("An error occurred when calling gnuplot ");
exit(1);
} else {
wait(NULL);
sleep(2);
exit(0);
}
}
}
else {
wait(NULL);
sleep(1);
}
}
else { /* Case we have an output: no daemon */
if (fork()==0) { /* We do not need no daemon so no stupid double/triple fork. This fork is necessary just to spawn the new process */
execlp(gnuplotname, gnuplotname, gplotname, NULL);
perror("An error occurred when calling gnuplot ");
exit(1);
}
else {
wait(NULL);
if(type==PLOTPOSTSCRIPT) {
remove(gplotname);
remove(dataname);
}
sleep(1);
}
}
}
deferSignalHandling();
fflush(NULL);
parserFlushInput();
resumeSignalHandling();
safeFree(gplotname);
safeFree(dataname);
safeFree(outputname);
return;
}
void removePlotFiles(void) {
int i;
char *name;
int n;
n = sollya_snprintf(NULL, 0, "%s/%s%s-%04d", getTempDir(), PACKAGE_NAME, getUniqueId(), NUMBEROFFILES);
name = (char *)safeCalloc(n+5, sizeof(char));
for(i=0;i<NUMBEROFFILES;i++) {
sollya_snprintf(name, n+3, "%s/%s%s-%04d.p",getTempDir(), PACKAGE_NAME, getUniqueId(), i);
remove(name);
sollya_snprintf(name, n+5, "%s/%s%s-%04d.dat",getTempDir(),PACKAGE_NAME,getUniqueId(), i);
remove(name);
}
safeFree(name);
return;
}
void asciiPlotTree(node *tree, mpfr_t a, mpfr_t b, mp_prec_t prec) {
mpfr_t y, x, step, minValue, maxValue;
int sizeX, sizeY, i, k, drawXAxis, drawYAxis, xAxis, yAxis;
#if defined(TIOCGWINSZ) && defined(STDOUT_FILENO) && defined(HAVE_SYS_IOCTL_H)
#if defined(__CYGWIN__)
struct winsize {
unsigned short ws_row; /* rows in characters */
unsigned short ws_col; /* columns in characters */
unsigned short ws_xpixel; /* horizontal size in pixels (not used) */
unsigned short ws_ypixel; /* vertical size in pixels (not used) */
} size;
#else
struct winsize size;
#endif
#endif
mpfr_t *values;
char **lines;
char *curr;
int oldColor;
mpfr_init2(y,prec);
if ((mpfr_cmp(a,b) == 0) || isConstant(tree)) {
evaluateFaithful(y,tree,a,prec);
if (!mpfr_number_p(y)) {
printMessage(1,SOLLYA_MSG_CONSTANT_EXPR_CANNOT_BE_EVALUATED_AT_ALL,"Warning: this constant function is not evaluable by this tool.\n");
}
printValue(&y);
sollyaPrintf("\n");
mpfr_clear(y);
return;
}
#if defined(TIOCGWINSZ) && defined(STDOUT_FILENO) && defined(HAVE_SYS_IOCTL_H)
if ((eliminatePromptBackup == 1) && (!libraryMode)) {
sizeX = 77;
sizeY = 25;
} else {
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *) &size) >= 0) {
sizeX = size.ws_col;
sizeY = size.ws_row;
if ((sizeX < 2) || (sizeY < 2)) {
sizeX = 77;
sizeY = 25;
}
if (sizeX > 500) sizeX = 500;
if (sizeY > 500) sizeY = 500;
} else {
sizeX = 77;
sizeY = 25;
}
}
#else
sizeX = 77;
sizeY = 25;
#endif
values = (mpfr_t *) safeCalloc(sizeX-1,sizeof(mpfr_t));
for (i=0;i<sizeX-1;i++) {
mpfr_init2(values[i],2*prec);
}
mpfr_init2(x,prec);
mpfr_init2(step,prec);
mpfr_sub(step,b,a,GMP_RNDN);
mpfr_set_si(x,sizeX-2,GMP_RNDN);
mpfr_div(step,step,x,GMP_RNDN);
for (i=0;i<sizeX-1;i++) {
mpfr_set_si(x,i,GMP_RNDN);
mpfr_mul(x,x,step,GMP_RNDN);
mpfr_add(x,x,a,GMP_RNDN);
evaluateFaithful(values[i],tree,x,prec);
if (!mpfr_number_p(values[i])) {
mpfr_set_d(values[i],0.0,GMP_RNDN);
}
}
mpfr_init2(minValue,prec);
mpfr_init2(maxValue,prec);
mpfr_set(minValue,values[0],GMP_RNDN);
mpfr_set(maxValue,values[0],GMP_RNDN);
for (i=1;i<sizeX-1;i++) {
if (mpfr_cmp(values[i],minValue) < 0) {
mpfr_set(minValue, values[i], GMP_RNDN);
}
if (mpfr_cmp(values[i],maxValue) > 0) {
mpfr_set(maxValue, values[i], GMP_RNDN);
}
}
drawXAxis = 0; drawYAxis = 0; xAxis = 0; yAxis = 0;
if (mpfr_sgn(minValue) != mpfr_sgn(maxValue)) {
drawXAxis = 1;
mpfr_neg(x,minValue,GMP_RNDN);
mpfr_sub(y,maxValue,minValue,GMP_RNDN);
mpfr_div(x,x,y,GMP_RNDN);
mpfr_mul_si(x,x,sizeY-2,GMP_RNDN);
xAxis = mpfr_get_si(x,GMP_RNDN);
if (xAxis < 0) xAxis = 0;
if (xAxis > (sizeY - 2)) xAxis = sizeY - 2;
}
if (mpfr_sgn(a) != mpfr_sgn(b)) {
drawYAxis = 1;
mpfr_neg(x,a,GMP_RNDN);
mpfr_div(x,x,step,GMP_RNDN);
yAxis = mpfr_get_si(x,GMP_RNDN);
if (yAxis < 0) yAxis = 0;
if (yAxis > (sizeX - 2)) yAxis = 2;
}
for (i=0;i<sizeX-1;i++) {
mpfr_sub(values[i],values[i],minValue,GMP_RNDN);
}
mpfr_sub(maxValue,maxValue,minValue,GMP_RNDN);
for (i=0;i<sizeX-1;i++) {
mpfr_div(values[i],values[i],maxValue,GMP_RNDN);
mpfr_mul_si(values[i],values[i],sizeY-2,GMP_RNDN);
}
lines = (char **) safeCalloc(sizeY,sizeof(char *));
for (k=0;k<sizeY-1;k++) {
lines[k] = (char *) safeCalloc(sizeX,sizeof(char));
for (i=0;i<sizeX-1;i++)
lines[k][i] = ' ';
}
if (drawXAxis) {
for (i=0;i<sizeX-1;i++)
lines[(sizeY - 2) - xAxis][i] = '-';
}
if (drawYAxis) {
for (k=0;k<sizeY-1;k++)
lines[k][yAxis] = '|';
}
if (drawXAxis && drawYAxis) {
lines[(sizeY - 2) - xAxis][yAxis] = '+';
}
for (i=0;i<sizeX-1;i++) {
k = mpfr_get_si(values[i],GMP_RNDN);
if (k < 0) k = 0;
if (k > (sizeY - 2)) k = sizeY - 2;
lines[(sizeY - 2) - k][i] = 'x';
}
saveMode();
for (k=0;k<sizeY-1;k++) {
curr = lines[k];
while (*curr != '\0') {
if (*curr != 'x')
sollyaPrintf("%c",*(curr++));
else {
oldColor = getDisplayColor();
redMode();
sollyaPrintf("%c",*(curr++));
setDisplayColor(oldColor);
}
}
sollyaPrintf("\n");
}
restoreMode();
for (k=0;k<sizeY-1;k++) safeFree(lines[k]);
safeFree(lines);
mpfr_clear(minValue);
mpfr_clear(maxValue);
mpfr_clear(y);
mpfr_clear(x);
mpfr_clear(step);
for (i=0;i<sizeX-1;i++) {
mpfr_clear(values[i]);
}
safeFree(values);
return;
}
|