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