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