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