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