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