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