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