math.h revision 354596
1/*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4 *
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
8 * is preserved.
9 * ====================================================
10 */
11
12/*
13 * from: @(#)fdlibm.h 5.1 93/09/24
14 * $FreeBSD: stable/10/lib/msun/src/math.h 354596 2019-11-10 17:33:10Z dim $
15 */
16
17#ifndef _MATH_H_
18#define	_MATH_H_
19
20#include <sys/cdefs.h>
21#include <sys/_types.h>
22#include <machine/_limits.h>
23
24/*
25 * ANSI/POSIX
26 */
27extern const union __infinity_un {
28	unsigned char	__uc[8];
29	double		__ud;
30} __infinity;
31
32extern const union __nan_un {
33	unsigned char	__uc[sizeof(float)];
34	float		__uf;
35} __nan;
36
37#if __GNUC_PREREQ__(3, 3) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
38#define	__MATH_BUILTIN_CONSTANTS
39#endif
40
41#if __GNUC_PREREQ__(3, 0) && !defined(__INTEL_COMPILER)
42#define	__MATH_BUILTIN_RELOPS
43#endif
44
45#ifdef __MATH_BUILTIN_CONSTANTS
46#define	HUGE_VAL	__builtin_huge_val()
47#else
48#define	HUGE_VAL	(__infinity.__ud)
49#endif
50
51#if __ISO_C_VISIBLE >= 1999
52#define	FP_ILOGB0	(-__INT_MAX)
53#define	FP_ILOGBNAN	__INT_MAX
54
55#ifdef __MATH_BUILTIN_CONSTANTS
56#define	HUGE_VALF	__builtin_huge_valf()
57#define	HUGE_VALL	__builtin_huge_vall()
58#define	INFINITY	__builtin_inff()
59#define	NAN		__builtin_nanf("")
60#else
61#define	HUGE_VALF	(float)HUGE_VAL
62#define	HUGE_VALL	(long double)HUGE_VAL
63#define	INFINITY	HUGE_VALF
64#define	NAN		(__nan.__uf)
65#endif /* __MATH_BUILTIN_CONSTANTS */
66
67#define	MATH_ERRNO	1
68#define	MATH_ERREXCEPT	2
69#define	math_errhandling	MATH_ERREXCEPT
70
71#define	FP_FAST_FMAF	1
72#ifdef __ia64__
73#define	FP_FAST_FMA	1
74#define	FP_FAST_FMAL	1
75#endif
76
77/* Symbolic constants to classify floating point numbers. */
78#define	FP_INFINITE	0x01
79#define	FP_NAN		0x02
80#define	FP_NORMAL	0x04
81#define	FP_SUBNORMAL	0x08
82#define	FP_ZERO		0x10
83
84#if (__STDC_VERSION__ >= 201112L && defined(__clang__)) || \
85    __has_extension(c_generic_selections)
86#define	__fp_type_select(x, f, d, ld) _Generic((x),			\
87    float: f(x),							\
88    double: d(x),							\
89    long double: ld(x),							\
90    volatile float: f(x),						\
91    volatile double: d(x),						\
92    volatile long double: ld(x),					\
93    volatile const float: f(x),						\
94    volatile const double: d(x),					\
95    volatile const long double: ld(x),					\
96    const float: f(x),							\
97    const double: d(x),							\
98    const long double: ld(x))
99#elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
100#define	__fp_type_select(x, f, d, ld) __builtin_choose_expr(		\
101    __builtin_types_compatible_p(__typeof(x), long double), ld(x),	\
102    __builtin_choose_expr(						\
103    __builtin_types_compatible_p(__typeof(x), double), d(x),		\
104    __builtin_choose_expr(						\
105    __builtin_types_compatible_p(__typeof(x), float), f(x), (void)0)))
106#else
107#define	 __fp_type_select(x, f, d, ld)					\
108    ((sizeof(x) == sizeof(float)) ? f(x)				\
109    : (sizeof(x) == sizeof(double)) ? d(x)				\
110    : ld(x))
111#endif
112
113#define	fpclassify(x) \
114	__fp_type_select(x, __fpclassifyf, __fpclassifyd, __fpclassifyl)
115#define	isfinite(x) __fp_type_select(x, __isfinitef, __isfinite, __isfinitel)
116#define	isinf(x) __fp_type_select(x, __isinff, __isinf, __isinfl)
117#define	isnan(x) \
118	__fp_type_select(x, __inline_isnanf, __inline_isnan, __inline_isnanl)
119#define	isnormal(x) __fp_type_select(x, __isnormalf, __isnormal, __isnormall)
120
121#ifdef __MATH_BUILTIN_RELOPS
122#define	isgreater(x, y)		__builtin_isgreater((x), (y))
123#define	isgreaterequal(x, y)	__builtin_isgreaterequal((x), (y))
124#define	isless(x, y)		__builtin_isless((x), (y))
125#define	islessequal(x, y)	__builtin_islessequal((x), (y))
126#define	islessgreater(x, y)	__builtin_islessgreater((x), (y))
127#define	isunordered(x, y)	__builtin_isunordered((x), (y))
128#else
129#define	isgreater(x, y)		(!isunordered((x), (y)) && (x) > (y))
130#define	isgreaterequal(x, y)	(!isunordered((x), (y)) && (x) >= (y))
131#define	isless(x, y)		(!isunordered((x), (y)) && (x) < (y))
132#define	islessequal(x, y)	(!isunordered((x), (y)) && (x) <= (y))
133#define	islessgreater(x, y)	(!isunordered((x), (y)) && \
134					((x) > (y) || (y) > (x)))
135#define	isunordered(x, y)	(isnan(x) || isnan(y))
136#endif /* __MATH_BUILTIN_RELOPS */
137
138#define	signbit(x) __fp_type_select(x, __signbitf, __signbit, __signbitl)
139
140typedef	__double_t	double_t;
141typedef	__float_t	float_t;
142#endif /* __ISO_C_VISIBLE >= 1999 */
143
144/*
145 * XOPEN/SVID
146 */
147#if __BSD_VISIBLE || __XSI_VISIBLE
148#define	M_E		2.7182818284590452354	/* e */
149#define	M_LOG2E		1.4426950408889634074	/* log 2e */
150#define	M_LOG10E	0.43429448190325182765	/* log 10e */
151#define	M_LN2		0.69314718055994530942	/* log e2 */
152#define	M_LN10		2.30258509299404568402	/* log e10 */
153#define	M_PI		3.14159265358979323846	/* pi */
154#define	M_PI_2		1.57079632679489661923	/* pi/2 */
155#define	M_PI_4		0.78539816339744830962	/* pi/4 */
156#define	M_1_PI		0.31830988618379067154	/* 1/pi */
157#define	M_2_PI		0.63661977236758134308	/* 2/pi */
158#define	M_2_SQRTPI	1.12837916709551257390	/* 2/sqrt(pi) */
159#define	M_SQRT2		1.41421356237309504880	/* sqrt(2) */
160#define	M_SQRT1_2	0.70710678118654752440	/* 1/sqrt(2) */
161
162#define	MAXFLOAT	((float)3.40282346638528860e+38)
163extern int signgam;
164#endif /* __BSD_VISIBLE || __XSI_VISIBLE */
165
166#if __BSD_VISIBLE
167#if 0
168/* Old value from 4.4BSD-Lite math.h; this is probably better. */
169#define	HUGE		HUGE_VAL
170#else
171#define	HUGE		MAXFLOAT
172#endif
173#endif /* __BSD_VISIBLE */
174
175/*
176 * Most of these functions depend on the rounding mode and have the side
177 * effect of raising floating-point exceptions, so they are not declared
178 * as __pure2.  In C99, FENV_ACCESS affects the purity of these functions.
179 */
180__BEGIN_DECLS
181/*
182 * ANSI/POSIX
183 */
184int	__fpclassifyd(double) __pure2;
185int	__fpclassifyf(float) __pure2;
186int	__fpclassifyl(long double) __pure2;
187int	__isfinitef(float) __pure2;
188int	__isfinite(double) __pure2;
189int	__isfinitel(long double) __pure2;
190int	__isinff(float) __pure2;
191int	__isinf(double) __pure2;
192int	__isinfl(long double) __pure2;
193int	__isnormalf(float) __pure2;
194int	__isnormal(double) __pure2;
195int	__isnormall(long double) __pure2;
196int	__signbit(double) __pure2;
197int	__signbitf(float) __pure2;
198int	__signbitl(long double) __pure2;
199
200static __inline int
201__inline_isnan(__const double __x)
202{
203
204	return (__x != __x);
205}
206
207static __inline int
208__inline_isnanf(__const float __x)
209{
210
211	return (__x != __x);
212}
213
214static __inline int
215__inline_isnanl(__const long double __x)
216{
217
218	return (__x != __x);
219}
220
221/*
222 * Define the following aliases, for compatibility with glibc and CUDA.
223 */
224#define __isnan __inline_isnan
225#define __isnanf __inline_isnanf
226
227/*
228 * Version 2 of the Single UNIX Specification (UNIX98) defined isnan() and
229 * isinf() as functions taking double.  C99, and the subsequent POSIX revisions
230 * (SUSv3, POSIX.1-2001, define it as a macro that accepts any real floating
231 * point type.  If we are targeting SUSv2 and C99 or C11 (or C++11) then we
232 * expose the newer definition, assuming that the language spec takes
233 * precedence over the operating system interface spec.
234 */
235#if	__XSI_VISIBLE > 0 && __XSI_VISIBLE < 600 && __ISO_C_VISIBLE < 1999
236#undef isinf
237#undef isnan
238int	isinf(double);
239int	isnan(double);
240#endif
241
242double	acos(double);
243double	asin(double);
244double	atan(double);
245double	atan2(double, double);
246double	cos(double);
247double	sin(double);
248double	tan(double);
249
250double	cosh(double);
251double	sinh(double);
252double	tanh(double);
253
254double	exp(double);
255double	frexp(double, int *);	/* fundamentally !__pure2 */
256double	ldexp(double, int);
257double	log(double);
258double	log10(double);
259double	modf(double, double *);	/* fundamentally !__pure2 */
260
261double	pow(double, double);
262double	sqrt(double);
263
264double	ceil(double);
265double	fabs(double) __pure2;
266double	floor(double);
267double	fmod(double, double);
268
269/*
270 * These functions are not in C90.
271 */
272#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE
273double	acosh(double);
274double	asinh(double);
275double	atanh(double);
276double	cbrt(double);
277double	erf(double);
278double	erfc(double);
279double	exp2(double);
280double	expm1(double);
281double	fma(double, double, double);
282double	hypot(double, double);
283int	ilogb(double) __pure2;
284double	lgamma(double);
285long long llrint(double);
286long long llround(double);
287double	log1p(double);
288double	log2(double);
289double	logb(double);
290long	lrint(double);
291long	lround(double);
292double	nan(const char *) __pure2;
293double	nextafter(double, double);
294double	remainder(double, double);
295double	remquo(double, double, int *);
296double	rint(double);
297#endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */
298
299#if __BSD_VISIBLE || __XSI_VISIBLE
300double	j0(double);
301double	j1(double);
302double	jn(int, double);
303double	y0(double);
304double	y1(double);
305double	yn(int, double);
306
307#if __XSI_VISIBLE <= 500 || __BSD_VISIBLE
308double	gamma(double);
309#endif
310
311#if __XSI_VISIBLE <= 600 || __BSD_VISIBLE
312double	scalb(double, double);
313#endif
314#endif /* __BSD_VISIBLE || __XSI_VISIBLE */
315
316#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999
317double	copysign(double, double) __pure2;
318double	fdim(double, double);
319double	fmax(double, double) __pure2;
320double	fmin(double, double) __pure2;
321double	nearbyint(double);
322double	round(double);
323double	scalbln(double, long);
324double	scalbn(double, int);
325double	tgamma(double);
326double	trunc(double);
327#endif
328
329/*
330 * BSD math library entry points
331 */
332#if __BSD_VISIBLE
333double	drem(double, double);
334int	finite(double) __pure2;
335int	isnanf(float) __pure2;
336
337/*
338 * Reentrant version of gamma & lgamma; passes signgam back by reference
339 * as the second argument; user must allocate space for signgam.
340 */
341double	gamma_r(double, int *);
342double	lgamma_r(double, int *);
343
344/*
345 * IEEE Test Vector
346 */
347double	significand(double);
348#endif /* __BSD_VISIBLE */
349
350/* float versions of ANSI/POSIX functions */
351#if __ISO_C_VISIBLE >= 1999
352float	acosf(float);
353float	asinf(float);
354float	atanf(float);
355float	atan2f(float, float);
356float	cosf(float);
357float	sinf(float);
358float	tanf(float);
359
360float	coshf(float);
361float	sinhf(float);
362float	tanhf(float);
363
364float	exp2f(float);
365float	expf(float);
366float	expm1f(float);
367float	frexpf(float, int *);	/* fundamentally !__pure2 */
368int	ilogbf(float) __pure2;
369float	ldexpf(float, int);
370float	log10f(float);
371float	log1pf(float);
372float	log2f(float);
373float	logf(float);
374float	modff(float, float *);	/* fundamentally !__pure2 */
375
376float	powf(float, float);
377float	sqrtf(float);
378
379float	ceilf(float);
380float	fabsf(float) __pure2;
381float	floorf(float);
382float	fmodf(float, float);
383float	roundf(float);
384
385float	erff(float);
386float	erfcf(float);
387float	hypotf(float, float);
388float	lgammaf(float);
389float	tgammaf(float);
390
391float	acoshf(float);
392float	asinhf(float);
393float	atanhf(float);
394float	cbrtf(float);
395float	logbf(float);
396float	copysignf(float, float) __pure2;
397long long llrintf(float);
398long long llroundf(float);
399long	lrintf(float);
400long	lroundf(float);
401float	nanf(const char *) __pure2;
402float	nearbyintf(float);
403float	nextafterf(float, float);
404float	remainderf(float, float);
405float	remquof(float, float, int *);
406float	rintf(float);
407float	scalblnf(float, long);
408float	scalbnf(float, int);
409float	truncf(float);
410
411float	fdimf(float, float);
412float	fmaf(float, float, float);
413float	fmaxf(float, float) __pure2;
414float	fminf(float, float) __pure2;
415#endif
416
417/*
418 * float versions of BSD math library entry points
419 */
420#if __BSD_VISIBLE
421float	dremf(float, float);
422int	finitef(float) __pure2;
423float	gammaf(float);
424float	j0f(float);
425float	j1f(float);
426float	jnf(int, float);
427float	scalbf(float, float);
428float	y0f(float);
429float	y1f(float);
430float	ynf(int, float);
431
432/*
433 * Float versions of reentrant version of gamma & lgamma; passes
434 * signgam back by reference as the second argument; user must
435 * allocate space for signgam.
436 */
437float	gammaf_r(float, int *);
438float	lgammaf_r(float, int *);
439
440/*
441 * float version of IEEE Test Vector
442 */
443float	significandf(float);
444#endif	/* __BSD_VISIBLE */
445
446/*
447 * long double versions of ISO/POSIX math functions
448 */
449#if __ISO_C_VISIBLE >= 1999
450long double	acoshl(long double);
451long double	acosl(long double);
452long double	asinhl(long double);
453long double	asinl(long double);
454long double	atan2l(long double, long double);
455long double	atanhl(long double);
456long double	atanl(long double);
457long double	cbrtl(long double);
458long double	ceill(long double);
459long double	copysignl(long double, long double) __pure2;
460long double	coshl(long double);
461long double	cosl(long double);
462long double	erfcl(long double);
463long double	erfl(long double);
464long double	exp2l(long double);
465long double	expl(long double);
466long double	expm1l(long double);
467long double	fabsl(long double) __pure2;
468long double	fdiml(long double, long double);
469long double	floorl(long double);
470long double	fmal(long double, long double, long double);
471long double	fmaxl(long double, long double) __pure2;
472long double	fminl(long double, long double) __pure2;
473long double	fmodl(long double, long double);
474long double	frexpl(long double, int *); /* fundamentally !__pure2 */
475long double	hypotl(long double, long double);
476int		ilogbl(long double) __pure2;
477long double	ldexpl(long double, int);
478long double	lgammal(long double);
479long long	llrintl(long double);
480long long	llroundl(long double);
481long double	log10l(long double);
482long double	log1pl(long double);
483long double	log2l(long double);
484long double	logbl(long double);
485long double	logl(long double);
486long		lrintl(long double);
487long		lroundl(long double);
488long double	modfl(long double, long double *); /* fundamentally !__pure2 */
489long double	nanl(const char *) __pure2;
490long double	nearbyintl(long double);
491long double	nextafterl(long double, long double);
492double		nexttoward(double, long double);
493float		nexttowardf(float, long double);
494long double	nexttowardl(long double, long double);
495long double	powl(long double, long double);
496long double	remainderl(long double, long double);
497long double	remquol(long double, long double, int *);
498long double	rintl(long double);
499long double	roundl(long double);
500long double	scalblnl(long double, long);
501long double	scalbnl(long double, int);
502long double	sinhl(long double);
503long double	sinl(long double);
504long double	sqrtl(long double);
505long double	tanhl(long double);
506long double	tanl(long double);
507long double	tgammal(long double);
508long double	truncl(long double);
509#endif /* __ISO_C_VISIBLE >= 1999 */
510
511#if __BSD_VISIBLE
512long double	lgammal_r(long double, int *);
513#endif
514
515__END_DECLS
516
517#endif /* !_MATH_H_ */
518