12116Sjkh/*
22116Sjkh * ====================================================
32116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
42116Sjkh *
52116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business.
62116Sjkh * Permission to use, copy, modify, and distribute this
78870Srgrimes * software is freely granted, provided that this notice
82116Sjkh * is preserved.
92116Sjkh * ====================================================
102116Sjkh */
112116Sjkh
122116Sjkh/*
132116Sjkh * from: @(#)fdlibm.h 5.1 93/09/24
1450476Speter * $FreeBSD$
152116Sjkh */
162116Sjkh
172116Sjkh#ifndef _MATH_H_
1887805Sphantom#define	_MATH_H_
192116Sjkh
20130264Sdas#include <sys/cdefs.h>
21110566Smike#include <sys/_types.h>
22130713Sstefanf#include <machine/_limits.h>
23110566Smike
242116Sjkh/*
252116Sjkh * ANSI/POSIX
262116Sjkh */
27106268Sarchieextern const union __infinity_un {
28106268Sarchie	unsigned char	__uc[8];
29106268Sarchie	double		__ud;
30106268Sarchie} __infinity;
31110566Smike
32110566Smikeextern const union __nan_un {
33110566Smike	unsigned char	__uc[sizeof(float)];
34110566Smike	float		__uf;
35110566Smike} __nan;
36110566Smike
37135360Sdas#if __GNUC_PREREQ__(3, 3) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 800)
38131851Sdas#define	__MATH_BUILTIN_CONSTANTS
39131851Sdas#endif
40131851Sdas
41135360Sdas#if __GNUC_PREREQ__(3, 0) && !defined(__INTEL_COMPILER)
42131851Sdas#define	__MATH_BUILTIN_RELOPS
43131851Sdas#endif
44131851Sdas
45131851Sdas#ifdef __MATH_BUILTIN_CONSTANTS
46131851Sdas#define	HUGE_VAL	__builtin_huge_val()
47131851Sdas#else
48128628Sdas#define	HUGE_VAL	(__infinity.__ud)
49131851Sdas#endif
50128628Sdas
51128628Sdas#if __ISO_C_VISIBLE >= 1999
52130713Sstefanf#define	FP_ILOGB0	(-__INT_MAX)
53130713Sstefanf#define	FP_ILOGBNAN	__INT_MAX
54131851Sdas
55131851Sdas#ifdef __MATH_BUILTIN_CONSTANTS
56131851Sdas#define	HUGE_VALF	__builtin_huge_valf()
57131851Sdas#define	HUGE_VALL	__builtin_huge_vall()
58186886Sdas#define	INFINITY	__builtin_inff()
59186886Sdas#define	NAN		__builtin_nanf("")
60131851Sdas#else
61110566Smike#define	HUGE_VALF	(float)HUGE_VAL
62110566Smike#define	HUGE_VALL	(long double)HUGE_VAL
63110566Smike#define	INFINITY	HUGE_VALF
64110566Smike#define	NAN		(__nan.__uf)
65131851Sdas#endif /* __MATH_BUILTIN_CONSTANTS */
662116Sjkh
67126871Sbde#define	MATH_ERRNO	1
68126871Sbde#define	MATH_ERREXCEPT	2
69140265Sdas#define	math_errhandling	MATH_ERREXCEPT
70126871Sbde
71143221Sdas/* XXX We need a <machine/math.h>. */
72143221Sdas#if defined(__ia64__) || defined(__sparc64__)
73188272Sdas#define	FP_FAST_FMA	1
74140609Sdas#endif
75143221Sdas#ifdef __ia64__
76188272Sdas#define	FP_FAST_FMAL	1
77143221Sdas#endif
78188272Sdas#define	FP_FAST_FMAF	1
79140609Sdas
80110566Smike/* Symbolic constants to classify floating point numbers. */
81110769Smike#define	FP_INFINITE	0x01
82110769Smike#define	FP_NAN		0x02
83110769Smike#define	FP_NORMAL	0x04
84110769Smike#define	FP_SUBNORMAL	0x08
85110769Smike#define	FP_ZERO		0x10
86110566Smike#define	fpclassify(x) \
87110566Smike    ((sizeof (x) == sizeof (float)) ? __fpclassifyf(x) \
88110566Smike    : (sizeof (x) == sizeof (double)) ? __fpclassifyd(x) \
89110566Smike    : __fpclassifyl(x))
90110769Smike
91131852Sdas#define	isfinite(x)					\
92131852Sdas    ((sizeof (x) == sizeof (float)) ? __isfinitef(x)	\
93131852Sdas    : (sizeof (x) == sizeof (double)) ? __isfinite(x)	\
94131852Sdas    : __isfinitel(x))
95131852Sdas#define	isinf(x)					\
96131852Sdas    ((sizeof (x) == sizeof (float)) ? __isinff(x)	\
97131852Sdas    : (sizeof (x) == sizeof (double)) ? isinf(x)	\
98131852Sdas    : __isinfl(x))
99131852Sdas#define	isnan(x)					\
100209110Sdas    ((sizeof (x) == sizeof (float)) ? __isnanf(x)	\
101131852Sdas    : (sizeof (x) == sizeof (double)) ? isnan(x)	\
102131852Sdas    : __isnanl(x))
103131852Sdas#define	isnormal(x)					\
104131852Sdas    ((sizeof (x) == sizeof (float)) ? __isnormalf(x)	\
105131852Sdas    : (sizeof (x) == sizeof (double)) ? __isnormal(x)	\
106131852Sdas    : __isnormall(x))
107110769Smike
108131851Sdas#ifdef __MATH_BUILTIN_RELOPS
109131851Sdas#define	isgreater(x, y)		__builtin_isgreater((x), (y))
110131851Sdas#define	isgreaterequal(x, y)	__builtin_isgreaterequal((x), (y))
111131851Sdas#define	isless(x, y)		__builtin_isless((x), (y))
112131851Sdas#define	islessequal(x, y)	__builtin_islessequal((x), (y))
113131851Sdas#define	islessgreater(x, y)	__builtin_islessgreater((x), (y))
114131851Sdas#define	isunordered(x, y)	__builtin_isunordered((x), (y))
115131851Sdas#else
116110769Smike#define	isgreater(x, y)		(!isunordered((x), (y)) && (x) > (y))
117110769Smike#define	isgreaterequal(x, y)	(!isunordered((x), (y)) && (x) >= (y))
118110769Smike#define	isless(x, y)		(!isunordered((x), (y)) && (x) < (y))
119110769Smike#define	islessequal(x, y)	(!isunordered((x), (y)) && (x) <= (y))
120110769Smike#define	islessgreater(x, y)	(!isunordered((x), (y)) && \
121110769Smike					((x) > (y) || (y) > (x)))
122110769Smike#define	isunordered(x, y)	(isnan(x) || isnan(y))
123131851Sdas#endif /* __MATH_BUILTIN_RELOPS */
124110769Smike
125132382Sdas#define	signbit(x)					\
126132382Sdas    ((sizeof (x) == sizeof (float)) ? __signbitf(x)	\
127132382Sdas    : (sizeof (x) == sizeof (double)) ? __signbit(x)	\
128132382Sdas    : __signbitl(x))
129110566Smike
130110566Smiketypedef	__double_t	double_t;
131110566Smiketypedef	__float_t	float_t;
132128628Sdas#endif /* __ISO_C_VISIBLE >= 1999 */
133110566Smike
1342116Sjkh/*
1352116Sjkh * XOPEN/SVID
1362116Sjkh */
137128628Sdas#if __BSD_VISIBLE || __XSI_VISIBLE
1382116Sjkh#define	M_E		2.7182818284590452354	/* e */
1392116Sjkh#define	M_LOG2E		1.4426950408889634074	/* log 2e */
1402116Sjkh#define	M_LOG10E	0.43429448190325182765	/* log 10e */
1412116Sjkh#define	M_LN2		0.69314718055994530942	/* log e2 */
1422116Sjkh#define	M_LN10		2.30258509299404568402	/* log e10 */
1432116Sjkh#define	M_PI		3.14159265358979323846	/* pi */
1442116Sjkh#define	M_PI_2		1.57079632679489661923	/* pi/2 */
1452116Sjkh#define	M_PI_4		0.78539816339744830962	/* pi/4 */
1462116Sjkh#define	M_1_PI		0.31830988618379067154	/* 1/pi */
1472116Sjkh#define	M_2_PI		0.63661977236758134308	/* 2/pi */
1482116Sjkh#define	M_2_SQRTPI	1.12837916709551257390	/* 2/sqrt(pi) */
1492116Sjkh#define	M_SQRT2		1.41421356237309504880	/* sqrt(2) */
1502116Sjkh#define	M_SQRT1_2	0.70710678118654752440	/* 1/sqrt(2) */
1512116Sjkh
1522116Sjkh#define	MAXFLOAT	((float)3.40282346638528860e+38)
1532116Sjkhextern int signgam;
154128628Sdas#endif /* __BSD_VISIBLE || __XSI_VISIBLE */
1552116Sjkh
156128628Sdas#if __BSD_VISIBLE
157140270Sdas#if 0
158140270Sdas/* Old value from 4.4BSD-Lite math.h; this is probably better. */
159140270Sdas#define	HUGE		HUGE_VAL
160140270Sdas#else
161140270Sdas#define	HUGE		MAXFLOAT
162140270Sdas#endif
163128628Sdas#endif /* __BSD_VISIBLE */
1642116Sjkh
165104280Sbde/*
166140274Sdas * Most of these functions depend on the rounding mode and have the side
167140274Sdas * effect of raising floating-point exceptions, so they are not declared
168140274Sdas * as __pure2.  In C99, FENV_ACCESS affects the purity of these functions.
169104280Sbde */
17093197Sbde__BEGIN_DECLS
1712116Sjkh/*
1722116Sjkh * ANSI/POSIX
1732116Sjkh */
174110769Smikeint	__fpclassifyd(double) __pure2;
175110769Smikeint	__fpclassifyf(float) __pure2;
176110769Smikeint	__fpclassifyl(long double) __pure2;
177131852Sdasint	__isfinitef(float) __pure2;
178131852Sdasint	__isfinite(double) __pure2;
179131852Sdasint	__isfinitel(long double) __pure2;
180131852Sdasint	__isinff(float) __pure2;
181131852Sdasint	__isinfl(long double) __pure2;
182209110Sdasint	__isnanf(float) __pure2;
183131852Sdasint	__isnanl(long double) __pure2;
184131852Sdasint	__isnormalf(float) __pure2;
185131852Sdasint	__isnormal(double) __pure2;
186131852Sdasint	__isnormall(long double) __pure2;
187110769Smikeint	__signbit(double) __pure2;
188132382Sdasint	__signbitf(float) __pure2;
189132382Sdasint	__signbitl(long double) __pure2;
190110566Smike
19192917Sobriendouble	acos(double);
19292917Sobriendouble	asin(double);
19392917Sobriendouble	atan(double);
19492917Sobriendouble	atan2(double, double);
19592917Sobriendouble	cos(double);
19692917Sobriendouble	sin(double);
19792917Sobriendouble	tan(double);
1982116Sjkh
19992917Sobriendouble	cosh(double);
20092917Sobriendouble	sinh(double);
20192917Sobriendouble	tanh(double);
2022116Sjkh
20392917Sobriendouble	exp(double);
204104280Sbdedouble	frexp(double, int *);	/* fundamentally !__pure2 */
20592917Sobriendouble	ldexp(double, int);
20692917Sobriendouble	log(double);
20792917Sobriendouble	log10(double);
208104280Sbdedouble	modf(double, double *);	/* fundamentally !__pure2 */
2092116Sjkh
21092917Sobriendouble	pow(double, double);
21192917Sobriendouble	sqrt(double);
2122116Sjkh
21392917Sobriendouble	ceil(double);
214140274Sdasdouble	fabs(double) __pure2;
21592917Sobriendouble	floor(double);
21692917Sobriendouble	fmod(double, double);
2172116Sjkh
218104280Sbde/*
219140274Sdas * These functions are not in C90.
220104280Sbde */
221128628Sdas#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE
222128628Sdasdouble	acosh(double);
223128628Sdasdouble	asinh(double);
224128628Sdasdouble	atanh(double);
225140274Sdasdouble	cbrt(double);
22692917Sobriendouble	erf(double);
227140274Sdasdouble	erfc(double);
228144648Sdasdouble	exp2(double);
229140274Sdasdouble	expm1(double);
230143777Sdasdouble	fma(double, double, double);
23192917Sobriendouble	hypot(double, double);
232140274Sdasint	ilogb(double) __pure2;
233131852Sdasint	(isinf)(double) __pure2;
234131852Sdasint	(isnan)(double) __pure2;
235128628Sdasdouble	lgamma(double);
236140088Sdaslong long llrint(double);
237140088Sdaslong long llround(double);
238140274Sdasdouble	log1p(double);
239216211Sdasdouble	log2(double);
240140274Sdasdouble	logb(double);
241140088Sdaslong	lrint(double);
242140088Sdaslong	lround(double);
243174684Sdasdouble	nan(const char *) __pure2;
244128628Sdasdouble	nextafter(double, double);
245128628Sdasdouble	remainder(double, double);
246144091Sdasdouble	remquo(double, double, int *);
247140274Sdasdouble	rint(double);
248128628Sdas#endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XSI_VISIBLE */
249128628Sdas
250128628Sdas#if __BSD_VISIBLE || __XSI_VISIBLE
25192917Sobriendouble	j0(double);
25292917Sobriendouble	j1(double);
25392917Sobriendouble	jn(int, double);
25492917Sobriendouble	y0(double);
25592917Sobriendouble	y1(double);
25692917Sobriendouble	yn(int, double);
2572116Sjkh
258128628Sdas#if __XSI_VISIBLE <= 500 || __BSD_VISIBLE
259128628Sdasdouble	gamma(double);
26028971Sbde#endif
261189805Sdas
262189805Sdas#if __XSI_VISIBLE <= 600 || __BSD_VISIBLE
263189805Sdasdouble	scalb(double, double);
264189805Sdas#endif
265128628Sdas#endif /* __BSD_VISIBLE || __XSI_VISIBLE */
2662116Sjkh
267128628Sdas#if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999
268104280Sbdedouble	copysign(double, double) __pure2;
269132292Sstefanfdouble	fdim(double, double);
270132292Sstefanfdouble	fmax(double, double) __pure2;
271132292Sstefanfdouble	fmin(double, double) __pure2;
272140274Sdasdouble	nearbyint(double);
273132292Sstefanfdouble	round(double);
274130768Sdasdouble	scalbln(double, long);
27592917Sobriendouble	scalbn(double, int);
276128628Sdasdouble	tgamma(double);
277132292Sstefanfdouble	trunc(double);
278128628Sdas#endif
2792116Sjkh
2802116Sjkh/*
2812116Sjkh * BSD math library entry points
2822116Sjkh */
283128628Sdas#if __BSD_VISIBLE
28492917Sobriendouble	drem(double, double);
285128628Sdasint	finite(double) __pure2;
286131852Sdasint	isnanf(float) __pure2;
2872116Sjkh
2882116Sjkh/*
2892116Sjkh * Reentrant version of gamma & lgamma; passes signgam back by reference
2902116Sjkh * as the second argument; user must allocate space for signgam.
2912116Sjkh */
29292917Sobriendouble	gamma_r(double, int *);
29392917Sobriendouble	lgamma_r(double, int *);
294128628Sdas
295128628Sdas/*
296128628Sdas * IEEE Test Vector
297128628Sdas */
298128628Sdasdouble	significand(double);
299111546Simp#endif /* __BSD_VISIBLE */
3002116Sjkh
3012116Sjkh/* float versions of ANSI/POSIX functions */
302128628Sdas#if __ISO_C_VISIBLE >= 1999
30392917Sobrienfloat	acosf(float);
30492917Sobrienfloat	asinf(float);
30592917Sobrienfloat	atanf(float);
30692917Sobrienfloat	atan2f(float, float);
30792917Sobrienfloat	cosf(float);
30892917Sobrienfloat	sinf(float);
30992917Sobrienfloat	tanf(float);
3102116Sjkh
31192917Sobrienfloat	coshf(float);
31292917Sobrienfloat	sinhf(float);
31392917Sobrienfloat	tanhf(float);
3142116Sjkh
315144648Sdasfloat	exp2f(float);
31692917Sobrienfloat	expf(float);
317140274Sdasfloat	expm1f(float);
318104280Sbdefloat	frexpf(float, int *);	/* fundamentally !__pure2 */
319140274Sdasint	ilogbf(float) __pure2;
32092917Sobrienfloat	ldexpf(float, int);
321128628Sdasfloat	log10f(float);
322140274Sdasfloat	log1pf(float);
323216211Sdasfloat	log2f(float);
32492917Sobrienfloat	logf(float);
325104280Sbdefloat	modff(float, float *);	/* fundamentally !__pure2 */
3262116Sjkh
32792917Sobrienfloat	powf(float, float);
32892917Sobrienfloat	sqrtf(float);
3292116Sjkh
33092917Sobrienfloat	ceilf(float);
331140274Sdasfloat	fabsf(float) __pure2;
33292917Sobrienfloat	floorf(float);
33392917Sobrienfloat	fmodf(float, float);
334130179Sdasfloat	roundf(float);
3352116Sjkh
33692917Sobrienfloat	erff(float);
337140274Sdasfloat	erfcf(float);
338140274Sdasfloat	hypotf(float, float);
33992917Sobrienfloat	lgammaf(float);
340176388Sdasfloat	tgammaf(float);
3412116Sjkh
34292917Sobrienfloat	acoshf(float);
34392917Sobrienfloat	asinhf(float);
34492917Sobrienfloat	atanhf(float);
345140274Sdasfloat	cbrtf(float);
346140274Sdasfloat	logbf(float);
347128628Sdasfloat	copysignf(float, float) __pure2;
348140088Sdaslong long llrintf(float);
349140088Sdaslong long llroundf(float);
350140088Sdaslong	lrintf(float);
351140088Sdaslong	lroundf(float);
352174684Sdasfloat	nanf(const char *) __pure2;
353140274Sdasfloat	nearbyintf(float);
35492917Sobrienfloat	nextafterf(float, float);
35592917Sobrienfloat	remainderf(float, float);
356144091Sdasfloat	remquof(float, float, int *);
35792917Sobrienfloat	rintf(float);
358130768Sdasfloat	scalblnf(float, long);
35992917Sobrienfloat	scalbnf(float, int);
360130768Sdasfloat	truncf(float);
361131320Sdas
362131320Sdasfloat	fdimf(float, float);
363143777Sdasfloat	fmaf(float, float, float);
364131320Sdasfloat	fmaxf(float, float) __pure2;
365131320Sdasfloat	fminf(float, float) __pure2;
366128628Sdas#endif
3672116Sjkh
3682116Sjkh/*
3692116Sjkh * float versions of BSD math library entry points
3702116Sjkh */
371128628Sdas#if __BSD_VISIBLE
37292917Sobrienfloat	dremf(float, float);
373128628Sdasint	finitef(float) __pure2;
374128628Sdasfloat	gammaf(float);
375128628Sdasfloat	j0f(float);
376128628Sdasfloat	j1f(float);
377128628Sdasfloat	jnf(int, float);
378128628Sdasfloat	scalbf(float, float);
379128628Sdasfloat	y0f(float);
380128628Sdasfloat	y1f(float);
381128628Sdasfloat	ynf(int, float);
3822116Sjkh
3832116Sjkh/*
3842116Sjkh * Float versions of reentrant version of gamma & lgamma; passes
3852116Sjkh * signgam back by reference as the second argument; user must
3862116Sjkh * allocate space for signgam.
3872116Sjkh */
38892917Sobrienfloat	gammaf_r(float, int *);
38992917Sobrienfloat	lgammaf_r(float, int *);
390128628Sdas
391128628Sdas/*
392128628Sdas * float version of IEEE Test Vector
393128628Sdas */
394128628Sdasfloat	significandf(float);
395111546Simp#endif	/* __BSD_VISIBLE */
3962116Sjkh
397121418Sdes/*
398121418Sdes * long double versions of ISO/POSIX math functions
399121418Sdes */
400128628Sdas#if __ISO_C_VISIBLE >= 1999
401121418Sdeslong double	acosl(long double);
402121418Sdeslong double	asinl(long double);
403121418Sdeslong double	atan2l(long double, long double);
404121418Sdeslong double	atanl(long double);
405121418Sdeslong double	cbrtl(long double);
406121418Sdeslong double	ceill(long double);
407140274Sdaslong double	copysignl(long double, long double) __pure2;
408121418Sdeslong double	cosl(long double);
409121418Sdeslong double	exp2l(long double);
410140274Sdaslong double	fabsl(long double) __pure2;
411131320Sdaslong double	fdiml(long double, long double);
412140142Sstefanflong double	floorl(long double);
413121418Sdeslong double	fmal(long double, long double, long double);
414131320Sdaslong double	fmaxl(long double, long double) __pure2;
415131320Sdaslong double	fminl(long double, long double) __pure2;
416121418Sdeslong double	fmodl(long double, long double);
417140274Sdaslong double	frexpl(long double value, int *); /* fundamentally !__pure2 */
418121418Sdeslong double	hypotl(long double, long double);
419140274Sdasint		ilogbl(long double) __pure2;
420143221Sdaslong double	ldexpl(long double, int);
421121418Sdeslong long	llrintl(long double);
422121418Sdeslong long	llroundl(long double);
423121418Sdeslong double	logbl(long double);
424121418Sdeslong		lrintl(long double);
425121418Sdeslong		lroundl(long double);
426165855Sdaslong double	modfl(long double, long double *); /* fundamentally !__pure2 */
427174684Sdaslong double	nanl(const char *) __pure2;
428121418Sdeslong double	nearbyintl(long double);
429121418Sdeslong double	nextafterl(long double, long double);
430121418Sdesdouble		nexttoward(double, long double);
431121418Sdesfloat		nexttowardf(float, long double);
432121418Sdeslong double	nexttowardl(long double, long double);
433121418Sdeslong double	remainderl(long double, long double);
434121418Sdeslong double	remquol(long double, long double, int *);
435121418Sdeslong double	rintl(long double);
436121418Sdeslong double	roundl(long double);
437121418Sdeslong double	scalblnl(long double, long);
438121418Sdeslong double	scalbnl(long double, int);
439121418Sdeslong double	sinl(long double);
440176720Sdaslong double	sqrtl(long double);
441121418Sdeslong double	tanl(long double);
442121418Sdeslong double	truncl(long double);
443121418Sdes
444128628Sdas#endif /* __ISO_C_VISIBLE >= 1999 */
4452116Sjkh__END_DECLS
4462116Sjkh
44787805Sphantom#endif /* !_MATH_H_ */
448236612Stheraven
449236612Stheraven/* separate header for cmath */
450236612Stheraven#ifndef _MATH_EXTRA_H_
451236612Stheraven#if __ISO_C_VISIBLE >= 1999
452236612Stheraven#if _DECLARE_C99_LDBL_MATH
453236612Stheraven
454236612Stheraven#define _MATH_EXTRA_H_
455236612Stheraven
456236612Stheraven/*
457236612Stheraven * extra long double versions of math functions for C99 and cmath
458236612Stheraven */
459236612Stheraven__BEGIN_DECLS
460236612Stheraven
461236612Stheravenlong double	acoshl(long double);
462236612Stheravenlong double	asinhl(long double);
463236612Stheravenlong double	atanhl(long double);
464236612Stheravenlong double	coshl(long double);
465236612Stheravenlong double	erfcl(long double);
466236612Stheravenlong double	erfl(long double);
467236612Stheravenlong double	expl(long double);
468236612Stheravenlong double	expm1l(long double);
469236612Stheravenlong double	lgammal(long double);
470236612Stheravenlong double	log10l(long double);
471236612Stheravenlong double	log1pl(long double);
472236612Stheravenlong double	log2l(long double);
473236612Stheravenlong double	logl(long double);
474236612Stheravenlong double	powl(long double, long double);
475236612Stheravenlong double	sinhl(long double);
476236612Stheravenlong double	tanhl(long double);
477236612Stheravenlong double	tgammal(long double);
478236612Stheraven
479236612Stheraven__END_DECLS
480236612Stheraven
481236612Stheraven#endif /* !_DECLARE_C99_LDBL_MATH */
482236612Stheraven#endif /* __ISO_C_VISIBLE >= 1999 */
483236612Stheraven#endif /* !_MATH_EXTRA_H_ */
484