1 /* $NetBSD: math.h,v 1.72 2024/09/09 15:06:29 riastradh Exp $ */ 2 3 /* 4 * ==================================================== 5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6 * 7 * Developed at SunPro, a Sun Microsystems, Inc. business. 8 * Permission to use, copy, modify, and distribute this 9 * software is freely granted, provided that this notice 10 * is preserved. 11 * ==================================================== 12 */ 13 14 /* 15 * @(#)fdlibm.h 5.1 93/09/24 16 */ 17 18 #ifndef _MATH_H_ 19 #define _MATH_H_ 20 21 #include <sys/cdefs.h> 22 #include <sys/featuretest.h> 23 24 union __float_u { 25 unsigned char __dummy[sizeof(float)]; 26 float __val; 27 }; 28 29 union __double_u { 30 unsigned char __dummy[sizeof(double)]; 31 double __val; 32 }; 33 34 union __long_double_u { 35 unsigned char __dummy[sizeof(long double)]; 36 long double __val; 37 }; 38 39 #include <machine/math.h> /* may use __float_u, __double_u, 40 or __long_double_u */ 41 #include <limits.h> /* for INT_{MIN,MAX} */ 42 43 #if (!defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \ 44 !defined(_XOPEN_SOURCE)) || ((_POSIX_C_SOURCE - 0) >= 200809L || \ 45 defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L || \ 46 (__cplusplus - 0) >= 201103L || defined(_NETBSD_SOURCE)) 47 #define __MATH_C99_FEATURES 48 #endif 49 50 #ifdef __MATH_C99_FEATURES 51 # if defined(__FLT_EVAL_METHOD__) && (__FLT_EVAL_METHOD__ - 0) == 0 52 typedef double double_t; 53 typedef float float_t; 54 # elif (__FLT_EVAL_METHOD__ - 0) == 1 55 typedef double double_t; 56 typedef double float_t; 57 # elif (__FLT_EVAL_METHOD__ - 0) == 2 58 typedef long double double_t; 59 typedef long double float_t; 60 # endif 61 #endif 62 63 #ifdef __HAVE_LONG_DOUBLE 64 #define __fpmacro_unary_floating(__name, __arg0) \ 65 /* LINTED */ \ 66 ((sizeof (__arg0) == sizeof (float)) \ 67 ? __ ## __name ## f (__arg0) \ 68 : (sizeof (__arg0) == sizeof (double)) \ 69 ? __ ## __name ## d (__arg0) \ 70 : __ ## __name ## l (__arg0)) 71 #else 72 #define __fpmacro_unary_floating(__name, __arg0) \ 73 /* LINTED */ \ 74 ((sizeof (__arg0) == sizeof (float)) \ 75 ? __ ## __name ## f (__arg0) \ 76 : __ ## __name ## d (__arg0)) 77 #endif /* __HAVE_LONG_DOUBLE */ 78 79 /* 80 * ANSI/POSIX 81 */ 82 /* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */ 83 #if __GNUC_PREREQ__(3, 3) 84 #define HUGE_VAL __builtin_huge_val() 85 #else 86 extern const union __double_u __infinity; 87 #define HUGE_VAL __infinity.__val 88 #endif 89 90 /* 91 * ISO C99 92 */ 93 #if defined(__MATH_C99_FEATURES) || \ 94 (_POSIX_C_SOURCE - 0) >= 200112L || (_XOPEN_SOURCE - 0) >= 600 95 /* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */ 96 #if __GNUC_PREREQ__(3, 3) 97 #define HUGE_VALF __builtin_huge_valf() 98 #define HUGE_VALL __builtin_huge_vall() 99 #else 100 extern const union __float_u __infinityf; 101 #define HUGE_VALF __infinityf.__val 102 103 extern const union __long_double_u __infinityl; 104 #define HUGE_VALL __infinityl.__val 105 #endif 106 107 /* 7.12#4 INFINITY */ 108 #if defined(__INFINITY) 109 #define INFINITY __INFINITY /* float constant which overflows */ 110 #elif __GNUC_PREREQ__(3, 3) 111 #define INFINITY __builtin_inff() 112 #else 113 #define INFINITY HUGE_VALF /* positive infinity */ 114 #endif /* __INFINITY */ 115 116 /* 7.12#5 NAN: a quiet NaN, if supported */ 117 #ifdef __HAVE_NANF 118 #if __GNUC_PREREQ__(3,3) 119 #define NAN __builtin_nanf("") 120 #else 121 extern const union __float_u __nanf; 122 #define NAN __nanf.__val 123 #endif 124 #endif /* __HAVE_NANF */ 125 126 /* 7.12#6 number classification macros */ 127 #define FP_INFINITE 0x00 128 #define FP_NAN 0x01 129 #define FP_NORMAL 0x02 130 #define FP_SUBNORMAL 0x03 131 #define FP_ZERO 0x04 132 /* NetBSD extensions */ 133 #define _FP_LOMD 0x80 /* range for machine-specific classes */ 134 #define _FP_HIMD 0xff 135 136 /* 7.12#7 fast fma(3) feature test macros */ 137 #if __GNUC_PREREQ__(4, 4) 138 # ifdef __FP_FAST_FMA 139 # define FP_FAST_FMA 1 140 # endif 141 # ifdef __FP_FAST_FMAF 142 # define FP_FAST_FMAF 1 143 # endif 144 # ifdef __FP_FAST_FMAL 145 # define FP_FAST_FMAL 1 146 # endif 147 #endif 148 149 /* 7.12#8 ilogb exceptional input result value macros */ 150 #define FP_ILOGB0 INT_MIN 151 #define FP_ILOGBNAN INT_MAX 152 153 /* 7.12#9 error handling (__math_errhandling from machine/math.h) */ 154 #define MATH_ERRNO 1 155 #define MATH_ERREXCEPT 2 156 #ifdef __vax__ /* XXX !__HAVE_FENV */ 157 #define math_errhandling MATH_ERRNO 158 #else 159 #define math_errhandling MATH_ERREXCEPT 160 #endif 161 162 #endif /* C99 || _XOPEN_SOURCE >= 600 */ 163 164 /* 165 * XOPEN/SVID 166 */ 167 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) 168 #define M_E 2.7182818284590452354 /* e */ 169 #define M_LOG2E 1.4426950408889634074 /* log 2e */ 170 #define M_LOG10E 0.43429448190325182765 /* log 10e */ 171 #define M_LN2 0.69314718055994530942 /* log e2 */ 172 #define M_LN10 2.30258509299404568402 /* log e10 */ 173 #define M_PI 3.14159265358979323846 /* pi */ 174 #define M_PI_2 1.57079632679489661923 /* pi/2 */ 175 #define M_PI_4 0.78539816339744830962 /* pi/4 */ 176 #define M_1_PI 0.31830988618379067154 /* 1/pi */ 177 #define M_2_PI 0.63661977236758134308 /* 2/pi */ 178 #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ 179 #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ 180 #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ 181 182 #define MAXFLOAT ((float)3.40282346638528860e+38) 183 extern int signgam; 184 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */ 185 186 #if defined(_NETBSD_SOURCE) 187 enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix}; 188 189 #define _LIB_VERSION_TYPE enum fdversion 190 #define _LIB_VERSION _fdlib_version 191 192 /* if global variable _LIB_VERSION is not desirable, one may 193 * change the following to be a constant by: 194 * #define _LIB_VERSION_TYPE const enum version 195 * In that case, after one initializes the value _LIB_VERSION (see 196 * s_lib_version.c) during compile time, it cannot be modified 197 * in the middle of a program 198 */ 199 extern _LIB_VERSION_TYPE _LIB_VERSION; 200 201 #define _IEEE_ fdlibm_ieee 202 #define _SVID_ fdlibm_svid 203 #define _XOPEN_ fdlibm_xopen 204 #define _POSIX_ fdlibm_posix 205 206 #ifndef __cplusplus 207 struct exception { 208 int type; 209 const char *name; 210 double arg1; 211 double arg2; 212 double retval; 213 }; 214 #endif 215 216 #define HUGE MAXFLOAT 217 218 /* 219 * set X_TLOSS = pi*2**52, which is possibly defined in <values.h> 220 * (one may replace the following line by "#include <values.h>") 221 */ 222 223 #define X_TLOSS 1.41484755040568800000e+16 224 225 #define DOMAIN 1 226 #define SING 2 227 #define OVERFLOW 3 228 #define UNDERFLOW 4 229 #define TLOSS 5 230 #define PLOSS 6 231 232 #endif /* _NETBSD_SOURCE */ 233 234 __BEGIN_DECLS 235 /* 236 * ANSI/POSIX 237 */ 238 double acos(double); 239 double asin(double); 240 double atan(double); 241 double atan2(double, double); 242 double cos(double); 243 double sin(double); 244 double tan(double); 245 246 double cosh(double); 247 double sinh(double); 248 double tanh(double); 249 250 double exp(double); 251 double exp2(double); 252 double frexp(double, int *); 253 double ldexp(double, int); 254 double log(double); 255 double log2(double); 256 double log10(double); 257 double modf(double, double *); 258 259 double pow(double, double); 260 double sqrt(double); 261 262 double ceil(double); 263 double fabs(double); 264 double floor(double); 265 double fmod(double, double); 266 267 #if defined(__MATH_C99_FEATURES) || defined(_XOPEN_SOURCE) 268 double erf(double); 269 double erfc(double); 270 double hypot(double, double); 271 #endif 272 273 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) 274 int finite(double); 275 double gamma(double); 276 double j0(double); 277 double j1(double); 278 double jn(int, double); 279 double y0(double); 280 double y1(double); 281 double yn(int, double); 282 283 #if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE) 284 double scalb(double, double); 285 #endif /* (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)*/ 286 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */ 287 288 /* 289 * ISO C99 290 */ 291 #if defined(__MATH_C99_FEATURES) || (_XOPEN_SOURCE - 0) >= 500 292 double acosh(double); 293 double asinh(double); 294 double atanh(double); 295 double cbrt(double); 296 double expm1(double); 297 int ilogb(double); 298 double log1p(double); 299 double logb(double); 300 double nextafter(double, double); 301 double remainder(double, double); 302 double rint(double); 303 #endif 304 305 #if defined(__MATH_C99_FEATURES) || (_XOPEN_SOURCE - 0) >= 600 || \ 306 (_POSIX_C_SOURCE - 0) >= 200112L 307 /* 7.12.3.1 int fpclassify(real-floating x) */ 308 #define fpclassify(__x) __fpmacro_unary_floating(fpclassify, __x) 309 310 /* 7.12.3.2 int isfinite(real-floating x) */ 311 #define isfinite(__x) __fpmacro_unary_floating(isfinite, __x) 312 313 /* 7.12.3.5 int isnormal(real-floating x) */ 314 #define isnormal(__x) (fpclassify(__x) == FP_NORMAL) 315 316 /* 7.12.3.6 int signbit(real-floating x) */ 317 #define signbit(__x) __fpmacro_unary_floating(signbit, __x) 318 319 /* 7.12.4 trigonometric */ 320 321 float acosf(float); 322 float asinf(float); 323 float atanf(float); 324 float atan2f(float, float); 325 float cosf(float); 326 float sinf(float); 327 float tanf(float); 328 329 long double acosl(long double); 330 long double asinl(long double); 331 long double atanl(long double); 332 long double atan2l(long double, long double); 333 long double cosl(long double); 334 long double sinl(long double); 335 long double tanl(long double); 336 337 /* 7.12.5 hyperbolic */ 338 339 float acoshf(float); 340 float asinhf(float); 341 float atanhf(float); 342 float coshf(float); 343 float sinhf(float); 344 float tanhf(float); 345 long double acoshl(long double); 346 long double asinhl(long double); 347 long double atanhl(long double); 348 long double coshl(long double); 349 long double sinhl(long double); 350 long double tanhl(long double); 351 352 /* 7.12.6 exp / log */ 353 double scalbn(double, int); 354 double scalbln(double, long); 355 356 float expf(float); 357 float exp2f(float); 358 float expm1f(float); 359 float frexpf(float, int *); 360 int ilogbf(float); 361 float ldexpf(float, int); 362 float logf(float); 363 float log2f(float); 364 float log10f(float); 365 float log1pf(float); 366 float logbf(float); 367 float modff(float, float *); 368 float scalbnf(float, int); 369 float scalblnf(float, long); 370 371 long double expl(long double); 372 long double exp2l(long double); 373 long double expm1l(long double); 374 long double frexpl(long double, int *); 375 int ilogbl(long double); 376 long double ldexpl(long double, int); 377 long double logl(long double); 378 long double log2l(long double); 379 long double log10l(long double); 380 long double log1pl(long double); 381 long double logbl(long double); 382 long double modfl(long double, long double *); 383 long double scalbnl(long double, int); 384 long double scalblnl(long double, long); 385 386 387 /* 7.12.7 power / absolute */ 388 389 float cbrtf(float); 390 float fabsf(float); 391 float hypotf(float, float); 392 float powf(float, float); 393 float sqrtf(float); 394 long double cbrtl(long double); 395 long double fabsl(long double); 396 long double hypotl(long double, long double); 397 long double powl(long double, long double); 398 long double sqrtl(long double); 399 400 /* 7.12.8 error / gamma */ 401 402 double lgamma(double); 403 double tgamma(double); 404 float erff(float); 405 float erfcf(float); 406 float lgammaf(float); 407 float tgammaf(float); 408 long double erfl(long double); 409 long double erfcl(long double); 410 long double lgammal(long double); 411 long double tgammal(long double); 412 413 /* 7.12.9 nearest integer */ 414 415 /* LONGLONG */ 416 long long int llrint(double); 417 long int lround(double); 418 /* LONGLONG */ 419 long long int llround(double); 420 long int lrint(double); 421 double round(double); 422 double trunc(double); 423 424 float ceilf(float); 425 float floorf(float); 426 /* LONGLONG */ 427 long long int llrintf(float); 428 long int lroundf(float); 429 /* LONGLONG */ 430 long long int llroundf(float); 431 long int lrintf(float); 432 float rintf(float); 433 float roundf(float); 434 float truncf(float); 435 long double ceill(long double); 436 long double floorl(long double); 437 /* LONGLONG */ 438 long long int llrintl(long double); 439 long int lroundl(long double); 440 /* LONGLONG */ 441 long long int llroundl(long double); 442 long int lrintl(long double); 443 long double rintl(long double); 444 long double roundl(long double); 445 long double truncl(long double); 446 447 /* 7.12.10 remainder */ 448 449 float fmodf(float, float); 450 float remainderf(float, float); 451 long double fmodl(long double, long double); 452 long double remainderl(long double, long double); 453 454 /* 7.12.10.3 The remquo functions */ 455 double remquo(double, double, int *); 456 float remquof(float, float, int *); 457 long double remquol(long double, long double, int *); 458 459 /* 7.12.11 manipulation */ 460 461 double copysign(double, double); 462 double nan(const char *); 463 double nearbyint(double); 464 double nexttoward(double, long double); 465 float copysignf(float, float); 466 float nanf(const char *); 467 float nearbyintf(float); 468 float nextafterf(float, float); 469 float nexttowardf(float, long double); 470 long double copysignl(long double, long double); 471 long double nanl(const char *); 472 long double nearbyintl(long double); 473 long double nextafterl(long double, long double); 474 long double nexttowardl(long double, long double); 475 476 /* 7.12.14 comparison */ 477 478 #define isunordered(x, y) (isnan(x) || isnan(y)) 479 #define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y)) 480 #define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y)) 481 #define isless(x, y) (!isunordered((x), (y)) && (x) < (y)) 482 #define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y)) 483 #define islessgreater(x, y) (!isunordered((x), (y)) && \ 484 ((x) > (y) || (y) > (x))) 485 double fdim(double, double); 486 double fma(double, double, double); 487 double fmax(double, double); 488 double fmin(double, double); 489 float fdimf(float, float); 490 float fmaf(float, float, float); 491 float fmaxf(float, float); 492 float fminf(float, float); 493 long double fdiml(long double, long double); 494 long double fmal(long double, long double, long double); 495 long double fmaxl(long double, long double); 496 long double fminl(long double, long double); 497 498 #endif /* !_ANSI_SOURCE && ... */ 499 500 #if defined(__MATH_C99_FEATURES) || (_POSIX_C_SOURCE - 0) >= 200112L 501 /* 7.12.3.3 int isinf(real-floating x) */ 502 #if defined(__isinf) || defined(__HAVE_INLINE___ISINF) 503 #define isinf(__x) __isinf(__x) 504 #else 505 #define isinf(__x) __fpmacro_unary_floating(isinf, __x) 506 #endif 507 508 /* 7.12.3.4 int isnan(real-floating x) */ 509 #if defined(__isnan) || defined(__HAVE_INLINE___ISNAN) 510 #define isnan(__x) __isnan(__x) 511 #else 512 #define isnan(__x) __fpmacro_unary_floating(isnan, __x) 513 #endif 514 #endif /* !_ANSI_SOURCE && ... */ 515 516 #if defined(_NETBSD_SOURCE) 517 518 #ifndef __cplusplus 519 int matherr(struct exception *); 520 #endif 521 522 /* 523 * IEEE Test Vector 524 */ 525 double significand(double); 526 527 /* 528 * BSD math library entry points 529 */ 530 double drem(double, double); 531 532 void sincos(double, double *, double *); 533 void sincosf(float, float *, float *); 534 void sincosl(long double, long double *, long double *); 535 536 double cospi(double); 537 float cospif(float); 538 long double cospil(long double); 539 540 double sinpi(double); 541 float sinpif(float); 542 long double sinpil(long double); 543 544 double tanpi(double); 545 float tanpif(float); 546 long double tanpil(long double); 547 548 #endif /* _NETBSD_SOURCE */ 549 550 #if defined(_NETBSD_SOURCE) || defined(_REENTRANT) 551 /* 552 * Reentrant version of gamma & lgamma; passes signgam back by reference 553 * as the second argument; user must allocate space for signgam. 554 */ 555 double gamma_r(double, int *); 556 double lgamma_r(double, int *); 557 long double lgammal_r(long double, int *); 558 #endif /* _NETBSD_SOURCE || _REENTRANT */ 559 560 #if defined(_NETBSD_SOURCE) 561 562 /* float versions of ANSI/POSIX functions */ 563 564 float gammaf(float); 565 int isinff(float); 566 int isnanf(float); 567 int finitef(float); 568 float j0f(float); 569 float j1f(float); 570 float jnf(int, float); 571 float y0f(float); 572 float y1f(float); 573 float ynf(int, float); 574 575 float scalbf(float, float); 576 577 /* 578 * float version of IEEE Test Vector 579 */ 580 float significandf(float); 581 582 /* 583 * float versions of BSD math library entry points 584 */ 585 float dremf(float, float); 586 #endif /* _NETBSD_SOURCE */ 587 588 #if defined(_NETBSD_SOURCE) || defined(_REENTRANT) 589 /* 590 * Float versions of reentrant version of gamma & lgamma; passes 591 * signgam back by reference as the second argument; user must 592 * allocate space for signgam. 593 */ 594 float gammaf_r(float, int *); 595 float lgammaf_r(float, int *); 596 #endif /* !... || _REENTRANT */ 597 598 /* 599 * Library implementation 600 */ 601 int __fpclassifyf(float); 602 int __fpclassifyd(double); 603 int __isfinitef(float); 604 int __isfinited(double); 605 int __isinff(float); 606 int __isinfd(double); 607 int __isnanf(float); 608 int __isnand(double); 609 int __signbitf(float); 610 int __signbitd(double); 611 612 #ifdef __HAVE_LONG_DOUBLE 613 int __fpclassifyl(long double); 614 int __isfinitel(long double); 615 int __isinfl(long double); 616 int __isnanl(long double); 617 int __signbitl(long double); 618 #endif 619 620 __END_DECLS 621 622 #endif /* _MATH_H_ */ 623