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