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