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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
|
/*
* Copyright (c) 1994 Cygnus Support.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* and/or other materials related to such
* distribution and use acknowledge that the software was developed
* at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
Test the library maths functions using trusted precomputed test
vectors.
These vectors were originally generated on a sun3 with a 68881 using
80 bit precision, but ...
Each function is called with a variety of interesting arguments.
Note that many of the polynomials we use behave badly when the
domain is stressed, so the numbers in the vectors depend on what is
useful to test - eg sin(1e30) is pointless - the arg has to be
reduced modulo pi, and after that there's no bits of significance
left to evaluate with - any number would be just as precise as any
other.
*/
#include "test.h"
#include <math.h>
#include <float.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
extern int inacc;
int merror;
double mretval = 64;
int traperror = 1;
char *mname;
void
translate_to (FILE *file,
double r)
{
__ieee_double_shape_type bits;
bits.value = r;
fprintf(file, "0x%08lx, 0x%08lx", (unsigned long) bits.parts.msw, (unsigned long) bits.parts.lsw);
}
/* Convert double to float, preserving issignaling status and NaN sign */
#define to_float(f,d) do { \
if (__issignaling(d)) { \
if (signbit(d)) \
(f) = -__builtin_nansf(""); \
else \
(f) = __builtin_nansf(""); \
} else if (isnan(d)) { \
if (signbit(d)) \
(f) = -__builtin_nanf(""); \
else \
(f) = __builtin_nanf(""); \
} else \
(f) = (float) (d); \
} while(0)
#define to_double(d,f) do { \
if (__issignalingf(f)) { \
if (signbit(f)) \
(d) = -__builtin_nans(""); \
else \
(d) = __builtin_nans(""); \
} else if (isnanf(f)) { \
if (signbit(f)) \
(d) = -__builtin_nan(""); \
else \
(d) = __builtin_nan(""); \
} else \
(d) = (double) (f); \
} while(0)
int
ffcheck_id(double is,
one_line_type *p,
char *name,
int serrno,
int merror,
int id)
{
/* Make sure the answer isn't to far wrong from the correct value */
__ieee_double_shape_type correct, isbits;
int mag;
isbits.value = is;
correct.parts.msw = p->qs[id].msw;
correct.parts.lsw = p->qs[id].lsw;
int error_bit = p->error_bit;
mag = mag_of_error(correct.value, is);
if (error_bit > 63)
error_bit = 63;
/* All signaling nans are the "same", as are all quiet nans.
* On i386, just returning a value converts signaling nans to quiet
* nans, let that pass
*/
if (isnan(correct.value) && isnan(is)
#ifndef __i386__
&& (issignaling(correct.value) == issignaling(is))
#endif
)
{
mag = 64;
}
if (mag < error_bit)
{
inacc ++;
printf("%s:%d, inaccurate answer: bit %d (%08lx%08lx %08lx%08lx) (%g %g)\n",
name, p->line, mag,
(unsigned long) correct.parts.msw,
(unsigned long) correct.parts.lsw,
(unsigned long) isbits.parts.msw,
(unsigned long) isbits.parts.lsw,
correct.value, is);
}
#if 0
if (p->qs[id].merror != merror)
{
/* Beware, matherr doesn't exist anymore. */
printf("testing %s_vec.c:%d, matherr wrong: %d %d\n",
name, p->line, merror, p->qs[id].merror);
}
if (p->qs[id].errno_val != errno)
{
printf("testing %s_vec.c:%d, errno wrong: %d %d\n",
name, p->line, errno, p->qs[id].errno_val);
}
#endif
return mag;
}
int
ffcheck(double is,
one_line_type *p,
char *name,
int serrno,
int merror)
{
return ffcheck_id(is, p, name, serrno, merror, 0);
}
int
fffcheck_id (float is,
one_line_type *p,
char *name,
int serrno,
int merror,
int id)
{
/* Make sure the answer isn't to far wrong from the correct value */
__ieee_float_shape_type correct, isbits;
__ieee_double_shape_type correct_double;
__ieee_double_shape_type is_double;
int mag;
isbits.value = is;
to_double(is_double.value, is);
correct_double.parts.msw = p->qs[id].msw;
correct_double.parts.lsw = p->qs[id].lsw;
to_float(correct.value, correct_double.value);
// printf("%s got 0x%08x want 0x%08x\n", name, isbits.p1, correct.p1);
int error_bit = p->error_bit;
if (error_bit > 31)
error_bit = 31;
mag = fmag_of_error(correct.value, is);
/* All signaling nans are the "same", as are all quiet nans */
if (isnan(correct.value) && isnan(is)
#ifndef __i386__
/* i386 calling convention ends up always converting snan into qnan */
&& (__issignalingf(correct.value) == __issignalingf(is))
#endif
)
{
mag = 32;
}
if (mag < error_bit)
{
inacc ++;
printf("%s:%d, inaccurate answer: bit %d (%08lx %08lx) (%g %g) (is double 0x%08lx, 0x%08lx)\n",
name, p->line, mag,
(unsigned long) (uint32_t) correct.p1,
(unsigned long) (uint32_t) isbits.p1,
(double) correct.value,
(double) is,
(unsigned long) (uint32_t) is_double.parts.msw,
(unsigned long) (uint32_t) is_double.parts.lsw);
}
#if 0
if (p->qs[id].merror != merror)
{
/* Beware, matherr doesn't exist anymore. */
printf("testing %s_vec.c:%d, matherr wrong: %d %d\n",
name, p->line, merror, p->qs[id].merror);
}
if (p->qs[id].errno_val != errno)
{
printf("testing %s_vec.c:%d, errno wrong: %d %d\n",
name, p->line, errno, p->qs[id].errno_val);
}
#endif
return mag;
}
int
fffcheck (float is,
one_line_type *p,
char *name,
int serrno,
int merror)
{
return fffcheck_id(is, p, name, serrno, merror, 0);
}
double
thedouble (uint32_t msw,
uint32_t lsw, double *r)
{
__ieee_double_shape_type x;
x.parts.msw = msw;
x.parts.lsw = lsw;
if (r)
memcpy(r, &x.value, sizeof(double));
return x.value;
}
int calc;
extern int reduce;
void
frontline (FILE *f,
int mag,
one_line_type *p,
double result,
int merror,
int my_errno,
char *args,
char *name)
{
/* float returns can never have more than 32 bits of accuracy */
if (*args == 'f' && mag > 32)
mag = 32;
if (reduce && p->error_bit < mag)
{
fprintf(f, "{%2d,", p->error_bit);
}
else
{
fprintf(f, "{%2d,",mag);
}
fprintf(f,"%2d,%3d,", merror,my_errno);
fprintf(f, "__LINE__, ");
if (calc)
{
translate_to(f, result);
}
else
{
translate_to(f, thedouble(p->qs[0].msw, p->qs[0].lsw, NULL));
}
fprintf(f, ", ");
fprintf(f,"0x%08lx, 0x%08lx", (unsigned long) p->qs[1].msw, (unsigned long) p->qs[1].lsw);
if (args[2])
{
fprintf(f, ", ");
fprintf(f,"0x%08lx, 0x%08lx", (unsigned long) p->qs[2].msw, (unsigned long) p->qs[2].lsw);
}
fprintf(f,"}, /* %g=f(%g",result,
thedouble(p->qs[1].msw, p->qs[1].lsw, NULL));
if (args[2])
{
fprintf(f,", %g", thedouble(p->qs[2].msw,p->qs[2].lsw, NULL));
}
fprintf(f, ")*/\n");
}
void
finish (FILE *f,
int vector,
double result,
one_line_type *p,
char *args,
char *name)
{
int mag;
mag = ffcheck(result, p,name, merror, errno);
if (vector)
{
frontline(f, mag, p, result, merror, errno, args , name);
}
}
void
finish2 (FILE *f,
int vector,
double result,
double result2,
one_line_type *p,
char *args,
char *name)
{
int mag, mag2;
mag = ffcheck(result, p,name, merror, errno);
mag2 = ffcheck_id(result2, p,name, merror, errno, 2);
if (mag2 < mag)
mag = mag2;
if (vector)
{
__ieee_double_shape_type result2_double;
result2_double.value = result2;
p->qs[2].msw = result2_double.parts.msw;
p->qs[2].lsw = result2_double.parts.lsw;
frontline(f, mag, p, result, merror, errno, args , name);
}
}
void
ffinish (FILE *f,
int vector,
float fresult,
one_line_type *p,
char *args,
char *name)
{
int mag;
mag = fffcheck(fresult, p,name, merror, errno);
if (vector)
{
frontline(f, mag, p, (double) fresult, merror, errno, args , name);
}
}
void
ffinish2 (FILE *f,
int vector,
float fresult,
float fresult2,
one_line_type *p,
char *args,
char *name)
{
int mag, mag2;
mag = fffcheck(fresult, p,name, merror, errno);
mag2 = fffcheck_id(fresult2, p, name, merror, errno, 2);
if (mag2 < mag)
mag = mag2;
if (vector)
{
__ieee_double_shape_type result2_double;
to_double(result2_double.value, (double) fresult2);
p->qs[2].msw = result2_double.parts.msw;
p->qs[2].lsw = result2_double.parts.lsw;
frontline(f, mag, p, (double) fresult, merror, errno, args , name);
}
}
extern int redo;
bool
in_float_range(double arg)
{
if (isinf(arg) || isnan(arg))
return true;
return -(double) FLT_MAX <= arg && arg < (double) FLT_MAX;
}
void
run_vector_1 (int vector,
one_line_type *p,
char *func,
char *name,
char *args)
{
FILE *f = NULL;
double result;
float fresult;
if (vector)
{
VECOPEN(name, f);
if (redo)
{
double k;
union {
struct { uint32_t a, b; };
double d;
} d, d4;
for (k = -.2; k < .2; k+= 0.00132)
{
d.d = k;
d4.d = k + 4;
fprintf(f,"{1,1, 1,1, 0,0,0x%08lx,0x%08lx, 0x%08lx, 0x%08lx},\n",
(unsigned long) d.a, (unsigned long) d.b, (unsigned long) d4.a, (unsigned long) d4.b);
}
for (k = -1.2; k < 1.2; k+= 0.01)
{
d.d = k;
d4.d = k + 4;
fprintf(f,"{1,1, 1,1, 0,0,0x%08lx,0x%08lx, 0x%08lx, 0x%08lx},\n",
(unsigned long) d.a, (unsigned long) d.b, (unsigned long) d4.a, (unsigned long) d4.b);
}
for (k = -M_PI *2; k < M_PI *2; k+= M_PI/2)
{
d.d = k;
d4.d = k + 4;
fprintf(f,"{1,1, 1,1, 0,0,0x%08lx,0x%08lx, 0x%08lx, 0x%08lx},\n",
(unsigned long) d.a, (unsigned long) d.b, (unsigned long) d4.a, (unsigned long) d4.b);
}
for (k = -30; k < 30; k+= 1.7)
{
d.d = k;
d4.d = k + 4;
fprintf(f,"{2,2, 1,1, 0,0, 0x%08lx,0x%08lx, 0x%08lx, 0x%08lx},\n",
(unsigned long) d.a, (unsigned long) d.b, (unsigned long) d4.a, (unsigned long) d4.b);
}
VECCLOSE(f, name, args);
return;
}
}
newfunc(name);
while (p->line)
{
double arg1;
thedouble(p->qs[1].msw, p->qs[1].lsw, &arg1);
double arg2;
thedouble(p->qs[2].msw, p->qs[2].lsw, &arg2);
errno = 0;
merror = 0;
mname = 0;
line(p->line);
merror = 0;
errno = 123;
if (strcmp(args,"dd")==0)
{
typedef double (*pdblfunc) (double);
/* Double function returning a double */
result = ((pdblfunc)(func))(arg1);
finish(f,vector, result, p, args, name);
}
else if (strcmp(args,"ff")==0)
{
float arga;
typedef float (*pdblfunc) (float);
/* Double function returning a double */
if (in_float_range(arg1))
{
to_float(arga, arg1);
fresult = ((pdblfunc)(func))(arga);
ffinish(f, vector, fresult, p,args, name);
}
}
else if (strcmp(args,"ddd")==0)
{
typedef double (*pdblfunc) (double,double);
result = ((pdblfunc)(func))(arg1,arg2);
finish(f, vector, result, p,args, name);
}
else if (strcmp(args,"fff")==0)
{
float arga;
float argb;
typedef float (*pdblfunc) (float,float);
if (in_float_range(arg1) && in_float_range(arg2))
{
to_float(arga, arg1);
to_float(argb, arg2);
fresult = ((pdblfunc)(func))(arga, argb);
// printf("0x%08x = %s(0x%08x, 0x%08x)\n",
// float_bits(fresult), name, float_bits(arga), float_bits(argb));
ffinish(f, vector, fresult, p,args, name);
}
}
else if (strcmp(args,"did")==0)
{
typedef double (*pdblfunc) (int,double);
result = ((pdblfunc)(func))((int)arg1,arg2);
finish(f, vector, result, p,args, name);
}
else if (strcmp(args,"fif")==0)
{
float argb;
typedef float (*pdblfunc) (int,float);
if (in_float_range(arg2))
{
to_float(argb, arg2);
fresult = ((pdblfunc)(func))((int)arg1, argb);
ffinish(f, vector, fresult, p,args, name);
}
}
else if (strcmp(args,"id")==0)
{
typedef int (*pdblfunc)(double);
result = (double) ((pdblfunc)(func))(arg1);
finish(f, vector, result, p, args, name);
}
else if (strcmp(args,"ddp") == 0)
{
typedef double (*pdblfunc)(double, double *);
double result2;
result = (double) ((pdblfunc)(func))(arg1, &result2);
finish2(f, vector, result, result2, p, args, name);
}
else if (strcmp(args,"ffp") == 0)
{
typedef float (*pdblfunc)(float, float *);
float result2;
fresult = (float) ((pdblfunc)(func))(arg1, &result2);
ffinish2(f, vector, fresult, result2, p, args, name);
}
p++;
}
if (vector)
{
VECCLOSE(f, name, args);
}
}
void
test_math (int vector)
{
test_acos(vector);
test_acosf(vector);
test_acosh(vector);
test_acoshf(vector);
test_asin(vector);
test_asinf(vector);
test_asinh(vector);
test_asinhf(vector);
test_atan(vector);
test_atan2(vector);
test_atan2f(vector);
test_atanf(vector);
test_atanh(vector);
test_atanhf(vector);
test_ceil(vector);
test_ceilf(vector);
test_copysign(vector);
test_copysignf(vector);
test_cos(vector);
test_cosf(vector);
test_cosh(vector);
test_coshf(vector);
test_erf(vector);
test_erfc(vector);
test_erfcf(vector);
test_erff(vector);
test_exp(vector);
test_expf(vector);
test_fabs(vector);
test_fabsf(vector);
test_floor(vector);
test_floorf(vector);
test_fmod(vector);
test_fmodf(vector);
test_gamma(vector);
test_gammaf(vector);
test_hypot(vector);
test_hypotf(vector);
test_issignaling(vector);
test_j0(vector);
test_j0f(vector);
test_j1(vector);
test_j1f(vector);
test_jn(vector);
test_jnf(vector);
test_log(vector);
test_log10(vector);
test_log10f(vector);
test_log1p(vector);
test_log1pf(vector);
test_log2(vector);
test_log2f(vector);
test_logf(vector);
test_modf(vector);
test_modff(vector);
test_pow_vec(vector);
test_powf_vec(vector);
test_sin(vector);
test_sinf(vector);
test_sinh(vector);
test_sinhf(vector);
test_sqrt(vector);
test_sqrtf(vector);
test_tan(vector);
test_tanf(vector);
test_tanh(vector);
test_tanhf(vector);
test_trunc(vector);
test_truncf(vector);
test_y0(vector);
test_y0f(vector);
test_y1(vector);
test_y1f(vector);
test_y1f(vector);
test_ynf(vector);
}
/* These have to be played with to get to compile on machines which
don't have the fancy <foo>f entry points
*/
#if 0
float cosf (float a) { return cos((double)a); }
float sinf (float a) { return sin((double)a); }
float log1pf (float a) { return log1p((double)a); }
float tanf (float a) { return tan((double)a); }
float ceilf (float a) { return ceil(a); }
float floorf (float a) { return floor(a); }
#endif
/*ndef HAVE_FLOAT*/
#if 0
float fmodf(a,b) float a,b; { return fmod(a,b); }
float hypotf(a,b) float a,b; { return hypot(a,b); }
float acosf(a) float a; { return acos(a); }
float acoshf(a) float a; { return acosh(a); }
float asinf(a) float a; { return asin(a); }
float asinhf(a) float a; { return asinh(a); }
float atanf(a) float a; { return atan(a); }
float atanhf(a) float a; { return atanh(a); }
float coshf(a) float a; { return cosh(a); }
float erff(a) float a; { return erf(a); }
float erfcf(a) float a; { return erfc(a); }
float expf(a) float a; { return exp(a); }
float fabsf(a) float a; { return fabs(a); }
float gammaf(a) float a; { return gamma(a); }
float j0f(a) float a; { return j0(a); }
float j1f(a) float a; { return j1(a); }
float log10f(a) float a; { return log10(a); }
float logf(a) float a; { return log(a); }
float sinhf(a) float a; { return sinh(a); }
float sqrtf(a) float a; { return sqrt(a); }
float tanhf(a) float a; { return tanh(a); }
float y0f(a) float a; { return y0(a); }
float y1f(a) float a; { return y1(a); }
#endif
|