xref: /openbsd/include/math.h (revision 6c640833)
1 /*	$OpenBSD: math.h,v 1.36 2018/03/10 20:52:58 kettenis Exp $	*/
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 /*
14  * from: @(#)fdlibm.h 5.1 93/09/24
15  */
16 
17 #ifndef _MATH_H_
18 #define _MATH_H_
19 
20 #include <sys/_types.h>
21 #include <sys/limits.h>
22 
23 __BEGIN_DECLS
24 /*
25  * ANSI/POSIX
26  */
27 extern char __infinity[];
28 #if __GNUC_PREREQ__(3, 3)
29 #define HUGE_VAL	__builtin_huge_val()
30 #else /* __GNUC_PREREQ__(3, 3) */
31 #define HUGE_VAL	(*(double *)(void *)__infinity)
32 #endif /* __GNUC_PREREQ__(3, 3) */
33 
34 /*
35  * C99
36  */
37 #if __ISO_C_VISIBLE >= 1999
38 typedef	__double_t	double_t;
39 typedef	__float_t	float_t;
40 
41 #if __GNUC_PREREQ__(3, 3)
42 #define	HUGE_VALF	__builtin_huge_valf()
43 #define	HUGE_VALL	__builtin_huge_vall()
44 #define	INFINITY	__builtin_inff()
45 #define	NAN		__builtin_nanf("")
46 #else /* __GNUC_PREREQ__(3, 3) */
47 #define	HUGE_VALF	((float)HUGE_VAL)
48 #define	HUGE_VALL	((long double)HUGE_VAL)
49 #define	INFINITY	HUGE_VALF
50 extern char __nan[];
51 #define	NAN		(*(float *)(void *)__nan)
52 #endif /* __GNUC_PREREQ__(3, 3) */
53 
54 #define	FP_INFINITE	0x01
55 #define	FP_NAN		0x02
56 #define	FP_NORMAL	0x04
57 #define	FP_SUBNORMAL	0x08
58 #define	FP_ZERO		0x10
59 
60 #define FP_ILOGB0	(-INT_MAX)
61 #define FP_ILOGBNAN	INT_MAX
62 
63 #ifdef	__FP_FAST_FMA
64 #define	FP_FAST_FMA	1
65 #endif	/* __FP_FAST_FMA */
66 
67 #ifdef	__FP_FAST_FMAF
68 #define	FP_FAST_FMAF	1
69 #endif	/* __FP_FAST_FMAF */
70 
71 #ifdef	__FP_FAST_FMAL
72 #define	FP_FAST_FMAL	1
73 #endif	/* __FP_FAST_FMAL */
74 
75 #define	MATH_ERRNO	1
76 #define	MATH_ERREXCEPT	2
77 #define	math_errhandling	MATH_ERREXCEPT
78 
79 #define fpclassify(x) \
80 	((sizeof (x) == sizeof (float)) ? \
81 		__fpclassifyf(x) \
82 	: (sizeof (x) == sizeof (double)) ? \
83 		__fpclassify(x) \
84 	:	__fpclassifyl(x))
85 #define isfinite(x) \
86 	((sizeof (x) == sizeof (float)) ? \
87 		__isfinitef(x) \
88 	: (sizeof (x) == sizeof (double)) ? \
89 		__isfinite(x) \
90 	:	__isfinitel(x))
91 #define isnormal(x) \
92 	((sizeof (x) == sizeof (float)) ? \
93 		__isnormalf(x) \
94 	: (sizeof (x) == sizeof (double)) ? \
95 		__isnormal(x) \
96 	:	__isnormall(x))
97 #define signbit(x) \
98 	((sizeof (x) == sizeof (float)) ? \
99 		__signbitf(x) \
100 	: (sizeof (x) == sizeof (double)) ? \
101 		__signbit(x) \
102 	:	__signbitl(x))
103 
104 #define	isgreater(x, y)		(!isunordered((x), (y)) && (x) > (y))
105 #define	isgreaterequal(x, y)	(!isunordered((x), (y)) && (x) >= (y))
106 #define	isless(x, y)		(!isunordered((x), (y)) && (x) < (y))
107 #define	islessequal(x, y)	(!isunordered((x), (y)) && (x) <= (y))
108 #define	islessgreater(x, y)	(!isunordered((x), (y)) && \
109 					((x) > (y) || (y) > (x)))
110 #define	isunordered(x, y)	(isnan(x) || isnan(y))
111 #endif /* __ISO_C_VISIBLE >= 1999 */
112 
113 #define isinf(x) \
114 	((sizeof (x) == sizeof (float)) ? \
115 		__isinff(x) \
116 	: (sizeof (x) == sizeof (double)) ? \
117 		__isinf(x) \
118 	:	__isinfl(x))
119 #define isnan(x) \
120 	((sizeof (x) == sizeof (float)) ? \
121 		__isnanf(x) \
122 	: (sizeof (x) == sizeof (double)) ? \
123 		__isnan(x) \
124 	:	__isnanl(x))
125 
126 /*
127  * XOPEN/SVID
128  */
129 #if __BSD_VISIBLE || __XPG_VISIBLE
130 #define	M_E		((double)2.7182818284590452354)  /* e */
131 #define	M_LOG2E		((double)1.4426950408889634074)  /* log 2e */
132 #define	M_LOG10E	((double)0.43429448190325182765) /* log 10e */
133 #define	M_LN2		((double)0.69314718055994530942) /* log e2 */
134 #define	M_LN10		((double)2.30258509299404568402) /* log e10 */
135 #define	M_PI		((double)3.14159265358979323846) /* pi */
136 #define	M_PI_2		((double)1.57079632679489661923) /* pi/2 */
137 #define	M_PI_4		((double)0.78539816339744830962) /* pi/4 */
138 #define	M_1_PI		((double)0.31830988618379067154) /* 1/pi */
139 #define	M_2_PI		((double)0.63661977236758134308) /* 2/pi */
140 #define	M_2_SQRTPI	((double)1.12837916709551257390) /* 2/sqrt(pi) */
141 #define	M_SQRT2		((double)1.41421356237309504880) /* sqrt(2) */
142 #define	M_SQRT1_2	((double)0.70710678118654752440) /* 1/sqrt(2) */
143 
144 #define	MAXFLOAT	((float)3.40282346638528860e+38)
145 
146 extern int signgam;
147 #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
148 
149 #if __POSIX_VISIBLE >= 201403
150 #define	M_El		2.718281828459045235360287471352662498L /* e */
151 #define	M_LOG2El	1.442695040888963407359924681001892137L /* log 2e */
152 #define	M_LOG10El	0.434294481903251827651128918916605082L /* log 10e */
153 #define	M_LN2l		0.693147180559945309417232121458176568L /* log e2 */
154 #define	M_LN10l		2.302585092994045684017991454684364208L /* log e10 */
155 #define	M_PIl		3.141592653589793238462643383279502884L /* pi */
156 #define	M_PI_2l		1.570796326794896619231321691639751442L /* pi/2 */
157 #define	M_PI_4l		0.785398163397448309615660845819875721L /* pi/4 */
158 #define	M_1_PIl		0.318309886183790671537767526745028724L /* 1/pi */
159 #define	M_2_PIl		0.636619772367581343075535053490057448L /* 2/pi */
160 #define	M_2_SQRTPIl	1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
161 #define	M_SQRT2l	1.414213562373095048801688724209698079L /* sqrt(2) */
162 #define	M_SQRT1_2l	0.707106781186547524400844362104849039L /* 1/sqrt(2) */
163 #endif /* __POSIX_VISIBLE >= 201403 */
164 
165 #if __BSD_VISIBLE
166 #define	HUGE		MAXFLOAT
167 #endif /* __BSD_VISIBLE */
168 
169 /*
170  * ANSI/POSIX
171  */
172 double acos(double);
173 double asin(double);
174 double atan(double);
175 double atan2(double, double);
176 double cos(double);
177 double sin(double);
178 double tan(double);
179 
180 double cosh(double);
181 double sinh(double);
182 double tanh(double);
183 
184 double exp(double);
185 double frexp(double, int *);
186 double ldexp(double, int);
187 double log(double);
188 double log10(double);
189 double modf(double, double *);
190 
191 double pow(double, double);
192 double sqrt(double);
193 
194 double ceil(double);
195 double fabs(double);
196 double floor(double);
197 double fmod(double, double);
198 
199 /*
200  * C99
201  */
202 #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XPG_VISIBLE
203 double acosh(double);
204 double asinh(double);
205 double atanh(double);
206 
207 double exp2(double);
208 double expm1(double);
209 int ilogb(double);
210 double log1p(double);
211 double log2(double);
212 double logb(double);
213 double scalbn(double, int);
214 double scalbln(double, long int);
215 
216 double cbrt(double);
217 double hypot(double, double);
218 
219 double erf(double);
220 double erfc(double);
221 double lgamma(double);
222 double tgamma(double);
223 
224 double nearbyint(double);
225 double rint(double);
226 long int lrint(double);
227 long long int llrint(double);
228 double round(double);
229 long int lround(double);
230 long long int llround(double);
231 double trunc(double);
232 
233 double remainder(double, double);
234 double remquo(double, double, int *);
235 
236 double copysign(double, double);
237 double nan(const char *);
238 double nextafter(double, double);
239 double nexttoward(double, long double);
240 
241 double fdim(double, double);
242 double fmax(double, double);
243 double fmin(double, double);
244 
245 double fma(double, double, double);
246 #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XPG_VISIBLE */
247 
248 #if __BSD_VISIBLE || __XPG_VISIBLE
249 double j0(double);
250 double j1(double);
251 double jn(int, double);
252 double scalb(double, double);
253 double y0(double);
254 double y1(double);
255 double yn(int, double);
256 #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
257 
258 #if __BSD_VISIBLE || __XPG_VISIBLE <= 500
259 double gamma(double);
260 #endif /* __BSD_VISIBLE || __XPG_VISIBLE <= 500 */
261 
262 /*
263  * BSD math library entry points
264  */
265 #if __BSD_VISIBLE
266 double drem(double, double);
267 int finite(double);
268 
269 /*
270  * Reentrant version of gamma & lgamma; passes signgam back by reference
271  * as the second argument; user must allocate space for signgam.
272  */
273 double gamma_r(double, int *);
274 double lgamma_r(double, int *);
275 
276 void sincos(double, double *, double *);
277 
278 /*
279  * IEEE Test Vector
280  */
281 double significand(double);
282 #endif /* __BSD_VISIBLE */
283 
284 /*
285  * Float versions of C99 functions
286  */
287 #if __ISO_C_VISIBLE >= 1999
288 float acosf(float);
289 float asinf(float);
290 float atanf(float);
291 float atan2f(float, float);
292 float cosf(float);
293 float sinf(float);
294 float tanf(float);
295 
296 float acoshf(float);
297 float asinhf(float);
298 float atanhf(float);
299 float coshf(float);
300 float sinhf(float);
301 float tanhf(float);
302 
303 float expf(float);
304 float exp2f(float);
305 float expm1f(float);
306 float frexpf(float, int *);
307 int ilogbf(float);
308 float ldexpf(float, int);
309 float logf(float);
310 float log10f(float);
311 float log1pf(float);
312 float log2f(float);
313 float logbf(float);
314 float modff(float, float *);
315 float scalbnf(float, int);
316 float scalblnf(float, long int);
317 
318 float cbrtf(float);
319 float fabsf(float);
320 float hypotf(float, float);
321 float powf(float, float);
322 float sqrtf(float);
323 
324 float erff(float);
325 float erfcf(float);
326 float lgammaf(float);
327 float tgammaf(float);
328 
329 float ceilf(float);
330 float floorf(float);
331 float nearbyintf(float);
332 float rintf(float);
333 long int lrintf(float);
334 long long int llrintf(float);
335 float roundf(float);
336 long int lroundf(float);
337 long long int llroundf(float);
338 float truncf(float);
339 
340 float fmodf(float, float);
341 float remainderf(float, float);
342 float remquof(float, float, int *);
343 
344 float copysignf(float, float);
345 float nanf(const char *);
346 float nextafterf(float, float);
347 float nexttowardf(float, long double);
348 
349 float fdimf(float, float);
350 float fmaxf(float, float);
351 float fminf(float, float);
352 
353 float fmaf(float, float, float);
354 #endif /* __ISO_C_VISIBLE >= 1999 */
355 
356 #if __BSD_VISIBLE || __XPG_VISIBLE
357 float j0f(float);
358 float j1f(float);
359 float jnf(int, float);
360 float scalbf(float, float);
361 float y0f(float);
362 float y1f(float);
363 float ynf(int, float);
364 #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
365 
366 #if __BSD_VISIBLE || __XPG_VISIBLE <= 500
367 float gammaf(float);
368 #endif /* __BSD_VISIBLE || __XPG_VISIBLE <= 500 */
369 
370 /*
371  * Float versions of BSD math library entry points
372  */
373 #if __BSD_VISIBLE
374 float dremf(float, float);
375 int finitef(float);
376 int isinff(float);
377 int isnanf(float);
378 
379 /*
380  * Float versions of reentrant version of gamma & lgamma; passes
381  * signgam back by reference as the second argument; user must
382  * allocate space for signgam.
383  */
384 float gammaf_r(float, int *);
385 float lgammaf_r(float, int *);
386 
387 void sincosf(float, float *, float *);
388 
389 /*
390  * Float version of IEEE Test Vector
391  */
392 float significandf(float);
393 #endif /* __BSD_VISIBLE */
394 
395 /*
396  * Long double versions of C99 functions
397  */
398 #if __ISO_C_VISIBLE >= 1999
399 long double acosl(long double);
400 long double asinl(long double);
401 long double atanl(long double);
402 long double atan2l(long double, long double);
403 long double cosl(long double);
404 long double sinl(long double);
405 long double tanl(long double);
406 
407 long double acoshl(long double);
408 long double asinhl(long double);
409 long double atanhl(long double);
410 long double coshl(long double);
411 long double sinhl(long double);
412 long double tanhl(long double);
413 
414 long double expl(long double);
415 long double exp2l(long double);
416 long double expm1l(long double);
417 long double frexpl(long double, int *);
418 int ilogbl(long double);
419 long double ldexpl(long double, int);
420 long double logl(long double);
421 long double log10l(long double);
422 long double log1pl(long double);
423 long double log2l(long double);
424 long double logbl(long double);
425 long double modfl(long double, long double *);
426 long double scalbnl(long double, int);
427 long double scalblnl(long double, long int);
428 
429 long double cbrtl(long double);
430 long double fabsl(long double);
431 long double hypotl(long double, long double);
432 long double powl(long double, long double);
433 long double sqrtl(long double);
434 
435 long double erfl(long double);
436 long double erfcl(long double);
437 long double lgammal(long double);
438 long double tgammal(long double);
439 
440 long double ceill(long double);
441 long double floorl(long double);
442 long double nearbyintl(long double);
443 long double rintl(long double);
444 long int lrintl(long double);
445 long long int llrintl(long double);
446 long double roundl(long double);
447 long int lroundl(long double);
448 long long int llroundl(long double);
449 long double truncl(long double);
450 
451 long double fmodl(long double, long double);
452 long double remainderl(long double, long double);
453 long double remquol(long double, long double, int *);
454 
455 long double copysignl(long double, long double);
456 long double nanl(const char *);
457 long double nextafterl(long double, long double);
458 long double nexttowardl(long double, long double);
459 
460 long double fdiml(long double, long double);
461 long double fmaxl(long double, long double);
462 long double fminl(long double, long double);
463 
464 long double fmal(long double, long double, long double);
465 #endif /* __ISO_C_VISIBLE >= 1999 */
466 
467 /*
468  * Long double versions of BSD math library entry points
469  */
470 #if __BSD_VISIBLE
471 void sincosl(long double, long double *, long double *);
472 #endif
473 
474 /*
475  * Library implementation
476  */
477 int __fpclassify(double);
478 int __fpclassifyf(float);
479 int __fpclassifyl(long double);
480 int __isfinite(double);
481 int __isfinitef(float);
482 int __isfinitel(long double);
483 int __isinf(double);
484 int __isinff(float);
485 int __isinfl(long double);
486 int __isnan(double);
487 int __isnanf(float);
488 int __isnanl(long double);
489 int __isnormal(double);
490 int __isnormalf(float);
491 int __isnormall(long double);
492 int __signbit(double);
493 int __signbitf(float);
494 int __signbitl(long double);
495 __END_DECLS
496 
497 #endif /* !_MATH_H_ */
498