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