Home | History | Annotate | Line # | Download | only in include
math.h revision 1.50
      1 /*	$NetBSD: math.h,v 1.50 2010/01/11 16:28:39 christos 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 
     42 #ifdef __HAVE_LONG_DOUBLE
     43 #define	__fpmacro_unary_floating(__name, __arg0)			\
     44 	/* LINTED */							\
     45 	((sizeof (__arg0) == sizeof (float))				\
     46 	?	__ ## __name ## f (__arg0)				\
     47 	: (sizeof (__arg0) == sizeof (double))				\
     48 	?	__ ## __name ## d (__arg0)				\
     49 	:	__ ## __name ## l (__arg0))
     50 #else
     51 #define	__fpmacro_unary_floating(__name, __arg0)			\
     52 	/* LINTED */							\
     53 	((sizeof (__arg0) == sizeof (float))				\
     54 	?	__ ## __name ## f (__arg0)				\
     55 	:	__ ## __name ## d (__arg0))
     56 #endif /* __HAVE_LONG_DOUBLE */
     57 
     58 /*
     59  * ANSI/POSIX
     60  */
     61 /* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */
     62 extern const union __double_u __infinity;
     63 #define HUGE_VAL	__infinity.__val
     64 
     65 /*
     66  * ISO C99
     67  */
     68 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
     69     !defined(_XOPEN_SOURCE) || \
     70     ((__STDC_VERSION__ - 0) >= 199901L) || \
     71     ((_POSIX_C_SOURCE - 0) >= 200112L) || \
     72     ((_XOPEN_SOURCE  - 0) >= 600) || \
     73     defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
     74 /* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */
     75 extern const union __float_u __infinityf;
     76 #define	HUGE_VALF	__infinityf.__val
     77 
     78 extern const union __long_double_u __infinityl;
     79 #define	HUGE_VALL	__infinityl.__val
     80 
     81 /* 7.12#4 INFINITY */
     82 #ifdef __INFINITY
     83 #define	INFINITY	__INFINITY	/* float constant which overflows */
     84 #else
     85 #define	INFINITY	HUGE_VALF	/* positive infinity */
     86 #endif /* __INFINITY */
     87 
     88 /* 7.12#5 NAN: a quiet NaN, if supported */
     89 #ifdef __HAVE_NANF
     90 #if __GNUC_PREREQ__(3,3)
     91 #define	NAN	__builtin_nanf("")
     92 #else
     93 extern const union __float_u __nanf;
     94 #define	NAN		__nanf.__val
     95 #endif
     96 #endif /* __HAVE_NANF */
     97 
     98 /* 7.12#6 number classification macros */
     99 #define	FP_INFINITE	0x00
    100 #define	FP_NAN		0x01
    101 #define	FP_NORMAL	0x02
    102 #define	FP_SUBNORMAL	0x03
    103 #define	FP_ZERO		0x04
    104 /* NetBSD extensions */
    105 #define	_FP_LOMD	0x80		/* range for machine-specific classes */
    106 #define	_FP_HIMD	0xff
    107 
    108 #endif /* !_ANSI_SOURCE && ... */
    109 
    110 /*
    111  * XOPEN/SVID
    112  */
    113 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
    114 #define	M_E		2.7182818284590452354	/* e */
    115 #define	M_LOG2E		1.4426950408889634074	/* log 2e */
    116 #define	M_LOG10E	0.43429448190325182765	/* log 10e */
    117 #define	M_LN2		0.69314718055994530942	/* log e2 */
    118 #define	M_LN10		2.30258509299404568402	/* log e10 */
    119 #define	M_PI		3.14159265358979323846	/* pi */
    120 #define	M_PI_2		1.57079632679489661923	/* pi/2 */
    121 #define	M_PI_4		0.78539816339744830962	/* pi/4 */
    122 #define	M_1_PI		0.31830988618379067154	/* 1/pi */
    123 #define	M_2_PI		0.63661977236758134308	/* 2/pi */
    124 #define	M_2_SQRTPI	1.12837916709551257390	/* 2/sqrt(pi) */
    125 #define	M_SQRT2		1.41421356237309504880	/* sqrt(2) */
    126 #define	M_SQRT1_2	0.70710678118654752440	/* 1/sqrt(2) */
    127 
    128 #define	MAXFLOAT	((float)3.40282346638528860e+38)
    129 extern int signgam;
    130 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
    131 
    132 #if defined(_NETBSD_SOURCE)
    133 enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix};
    134 
    135 #define _LIB_VERSION_TYPE enum fdversion
    136 #define _LIB_VERSION _fdlib_version
    137 
    138 /* if global variable _LIB_VERSION is not desirable, one may
    139  * change the following to be a constant by:
    140  *	#define _LIB_VERSION_TYPE const enum version
    141  * In that case, after one initializes the value _LIB_VERSION (see
    142  * s_lib_version.c) during compile time, it cannot be modified
    143  * in the middle of a program
    144  */
    145 extern  _LIB_VERSION_TYPE  _LIB_VERSION;
    146 
    147 #define _IEEE_  fdlibm_ieee
    148 #define _SVID_  fdlibm_svid
    149 #define _XOPEN_ fdlibm_xopen
    150 #define _POSIX_ fdlibm_posix
    151 
    152 #ifndef __cplusplus
    153 struct exception {
    154 	int type;
    155 	const char *name;
    156 	double arg1;
    157 	double arg2;
    158 	double retval;
    159 };
    160 #endif
    161 
    162 #define	HUGE		MAXFLOAT
    163 
    164 /*
    165  * set X_TLOSS = pi*2**52, which is possibly defined in <values.h>
    166  * (one may replace the following line by "#include <values.h>")
    167  */
    168 
    169 #define X_TLOSS		1.41484755040568800000e+16
    170 
    171 #define	DOMAIN		1
    172 #define	SING		2
    173 #define	OVERFLOW	3
    174 #define	UNDERFLOW	4
    175 #define	TLOSS		5
    176 #define	PLOSS		6
    177 
    178 #endif /* _NETBSD_SOURCE */
    179 
    180 __BEGIN_DECLS
    181 /*
    182  * ANSI/POSIX
    183  */
    184 double	acos(double);
    185 double	asin(double);
    186 double	atan(double);
    187 double	atan2(double, double);
    188 double	cos(double);
    189 double	sin(double);
    190 double	tan(double);
    191 
    192 double	cosh(double);
    193 double	sinh(double);
    194 double	tanh(double);
    195 
    196 double	exp(double);
    197 double	exp2(double);
    198 double	frexp(double, int *);
    199 double	ldexp(double, int);
    200 double	log(double);
    201 double	log2(double);
    202 double	log10(double);
    203 double	modf(double, double *);
    204 
    205 double	pow(double, double);
    206 double	sqrt(double);
    207 
    208 double	ceil(double);
    209 double	fabs(double);
    210 double	floor(double);
    211 double	fmod(double, double);
    212 
    213 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
    214 double	erf(double);
    215 double	erfc(double);
    216 double	gamma(double);
    217 double	hypot(double, double);
    218 int	finite(double);
    219 double	j0(double);
    220 double	j1(double);
    221 double	jn(int, double);
    222 double	lgamma(double);
    223 double	y0(double);
    224 double	y1(double);
    225 double	yn(int, double);
    226 
    227 #if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
    228 double	acosh(double);
    229 double	asinh(double);
    230 double	atanh(double);
    231 double	cbrt(double);
    232 double	expm1(double);
    233 int	ilogb(double);
    234 double	log1p(double);
    235 double	logb(double);
    236 double	nextafter(double, double);
    237 double	remainder(double, double);
    238 double	rint(double);
    239 double	scalb(double, double);
    240 #endif /* (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)*/
    241 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
    242 
    243 /*
    244  * ISO C99
    245  */
    246 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
    247     !defined(_XOPEN_SOURCE) || \
    248     ((__STDC_VERSION__ - 0) >= 199901L) || \
    249     ((_POSIX_C_SOURCE - 0) >= 200112L) || \
    250     ((_XOPEN_SOURCE  - 0) >= 600) || \
    251     defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
    252 /* 7.12.3.1 int fpclassify(real-floating x) */
    253 #define	fpclassify(__x)	__fpmacro_unary_floating(fpclassify, __x)
    254 
    255 /* 7.12.3.2 int isfinite(real-floating x) */
    256 #define	isfinite(__x)	__fpmacro_unary_floating(isfinite, __x)
    257 
    258 /* 7.12.3.5 int isnormal(real-floating x) */
    259 #define	isnormal(__x)	(fpclassify(__x) == FP_NORMAL)
    260 
    261 /* 7.12.3.6 int signbit(real-floating x) */
    262 #define	signbit(__x)	__fpmacro_unary_floating(signbit, __x)
    263 
    264 /* 7.12.4 trigonometric */
    265 
    266 float	acosf(float);
    267 float	asinf(float);
    268 float	atanf(float);
    269 float	atan2f(float, float);
    270 float	cosf(float);
    271 float	sinf(float);
    272 float	tanf(float);
    273 
    274 /* 7.12.5 hyperbolic */
    275 
    276 float	acoshf(float);
    277 float	asinhf(float);
    278 float	atanhf(float);
    279 float	coshf(float);
    280 float	sinhf(float);
    281 float	tanhf(float);
    282 
    283 /* 7.12.6 exp / log */
    284 
    285 float	expf(float);
    286 float	exp2f(float);
    287 float	expm1f(float);
    288 float	frexpf(float, int *);
    289 int	ilogbf(float);
    290 float	ldexpf(float, int);
    291 float	logf(float);
    292 float	log2f(float);
    293 float	log10f(float);
    294 float	log1pf(float);
    295 float	logbf(float);
    296 float	modff(float, float *);
    297 float	scalbnf(float, int);
    298 
    299 /* 7.12.7 power / absolute */
    300 
    301 float	cbrtf(float);
    302 float	fabsf(float);
    303 float	hypotf(float, float);
    304 float	powf(float, float);
    305 float	sqrtf(float);
    306 
    307 /* 7.12.8 error / gamma */
    308 
    309 float	erff(float);
    310 float	erfcf(float);
    311 float	lgammaf(float);
    312 
    313 /* 7.12.9 nearest integer */
    314 
    315 float	ceilf(float);
    316 float	floorf(float);
    317 float	rintf(float);
    318 double	round(double);
    319 float	roundf(float);
    320 double	trunc(double);
    321 float	truncf(float);
    322 long int	lrint(double);
    323 long int	lrintf(float);
    324 /* LONGLONG */
    325 long long int	llrint(double);
    326 /* LONGLONG */
    327 long long int	llrintf(float);
    328 long int	lround(double);
    329 long int	lroundf(float);
    330 /* LONGLONG */
    331 long long int	llround(double);
    332 /* LONGLONG */
    333 long long int	llroundf(float);
    334 
    335 /* 7.12.10 remainder */
    336 
    337 float	fmodf(float, float);
    338 float	remainderf(float, float);
    339 
    340 /* 7.12.11 manipulation */
    341 
    342 float	copysignf(float, float);
    343 double	nan(const char *);
    344 float	nanf(const char *);
    345 long double	nanl(const char *);
    346 float	nextafterf(float, float);
    347 
    348 /* 7.12.14 comparision */
    349 
    350 #define isunordered(x, y)	(isnan(x) || isnan(y))
    351 #define isgreater(x, y)		(!isunordered((x), (y)) && (x) > (y))
    352 #define isgreaterequal(x, y)	(!isunordered((x), (y)) && (x) >= (y))
    353 #define isless(x, y)		(!isunordered((x), (y)) && (x) < (y))
    354 #define islessequal(x, y)	(!isunordered((x), (y)) && (x) <= (y))
    355 #define islessgreater(x, y)	(!isunordered((x), (y)) && \
    356 				 ((x) > (y) || (y) > (x)))
    357 double	fdim(double, double);
    358 double	fmax(double, double);
    359 double	fmin(double, double);
    360 float	fdimf(float, float);
    361 float	fmaxf(float, float);
    362 float	fminf(float, float);
    363 long double fdiml(long double, long double);
    364 long double fmaxl(long double, long double);
    365 long double fminl(long double, long double);
    366 
    367 #endif /* !_ANSI_SOURCE && ... */
    368 
    369 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) || \
    370     !defined(_XOPEN_SOURCE) || \
    371     ((__STDC_VERSION__ - 0) >= 199901L) || \
    372     ((_POSIX_C_SOURCE - 0) >= 200112L) || \
    373     defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
    374 /* 7.12.3.3 int isinf(real-floating x) */
    375 #ifdef __isinf
    376 #define	isinf(__x)	__isinf(__x)
    377 #else
    378 #define	isinf(__x)	__fpmacro_unary_floating(isinf, __x)
    379 #endif
    380 
    381 /* 7.12.3.4 int isnan(real-floating x) */
    382 #ifdef __isnan
    383 #define	isnan(__x)	__isnan(__x)
    384 #else
    385 #define	isnan(__x)	__fpmacro_unary_floating(isnan, __x)
    386 #endif
    387 #endif /* !_ANSI_SOURCE && ... */
    388 
    389 #if defined(_NETBSD_SOURCE)
    390 #ifndef __cplusplus
    391 int	matherr(struct exception *);
    392 #endif
    393 
    394 /*
    395  * IEEE Test Vector
    396  */
    397 double	significand(double);
    398 
    399 /*
    400  * Functions callable from C, intended to support IEEE arithmetic.
    401  */
    402 double	copysign(double, double);
    403 double	scalbn(double, int);
    404 
    405 /*
    406  * BSD math library entry points
    407  */
    408 double	drem(double, double);
    409 
    410 #endif /* _NETBSD_SOURCE */
    411 
    412 #if defined(_NETBSD_SOURCE) || defined(_REENTRANT)
    413 /*
    414  * Reentrant version of gamma & lgamma; passes signgam back by reference
    415  * as the second argument; user must allocate space for signgam.
    416  */
    417 double	gamma_r(double, int *);
    418 double	lgamma_r(double, int *);
    419 #endif /* _NETBSD_SOURCE || _REENTRANT */
    420 
    421 
    422 #if defined(_NETBSD_SOURCE)
    423 
    424 /* float versions of ANSI/POSIX functions */
    425 
    426 float	gammaf(float);
    427 int	isinff(float);
    428 int	isnanf(float);
    429 int	finitef(float);
    430 float	j0f(float);
    431 float	j1f(float);
    432 float	jnf(int, float);
    433 float	y0f(float);
    434 float	y1f(float);
    435 float	ynf(int, float);
    436 
    437 float	scalbf(float, float);
    438 
    439 /*
    440  * float version of IEEE Test Vector
    441  */
    442 float	significandf(float);
    443 
    444 /*
    445  * float versions of BSD math library entry points
    446  */
    447 float	dremf(float, float);
    448 #endif /* _NETBSD_SOURCE */
    449 
    450 #if defined(_NETBSD_SOURCE) || defined(_REENTRANT)
    451 /*
    452  * Float versions of reentrant version of gamma & lgamma; passes
    453  * signgam back by reference as the second argument; user must
    454  * allocate space for signgam.
    455  */
    456 float	gammaf_r(float, int *);
    457 float	lgammaf_r(float, int *);
    458 #endif /* !... || _REENTRANT */
    459 
    460 /*
    461  * Library implementation
    462  */
    463 int	__fpclassifyf(float);
    464 int	__fpclassifyd(double);
    465 int	__isfinitef(float);
    466 int	__isfinited(double);
    467 int	__isinff(float);
    468 int	__isinfd(double);
    469 int	__isnanf(float);
    470 int	__isnand(double);
    471 int	__signbitf(float);
    472 int	__signbitd(double);
    473 
    474 #ifdef __HAVE_LONG_DOUBLE
    475 int	__fpclassifyl(long double);
    476 int	__isfinitel(long double);
    477 int	__isinfl(long double);
    478 int	__isnanl(long double);
    479 int	__signbitl(long double);
    480 #endif
    481 __END_DECLS
    482 
    483 #endif /* _MATH_H_ */
    484