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