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