xref: /haiku/headers/posix/math.h (revision 4b7e219688450694efc9d1890f83f816758c16d3)
1 /*
2  * Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _MATH_H_
6 #define _MATH_H_
7 
8 /* (float|double)_t typedefs */
9 
10 #ifdef __FLT_EVAL_METHOD__
11 	#define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
12 #else
13 	#define FLT_EVAL_METHOD 0
14 #endif
15 
16 #if FLT_EVAL_METHOD == 1
17 	typedef double float_t;
18 	typedef double double_t;
19 #elif FLT_EVAL_METHOD == 2
20 	typedef long double float_t;
21 	typedef long double double_t;
22 #else
23 	typedef float float_t;
24 	typedef double double_t;
25 #endif
26 
27 #define M_E				2.7182818284590452354	/* e */
28 #define M_LOG2E			1.4426950408889634074	/* log 2e */
29 #define M_LOG10E		0.43429448190325182765	/* log 10e */
30 #define M_LN2			0.69314718055994530942	/* log e2 */
31 #define M_LN10			2.30258509299404568402	/* log e10 */
32 #define M_PI			3.14159265358979323846	/* pi */
33 #define M_PI_2			1.57079632679489661923	/* pi/2 */
34 #define M_PI_4			0.78539816339744830962	/* pi/4 */
35 #define M_1_PI			0.31830988618379067154	/* 1/pi */
36 #define M_2_PI			0.63661977236758134308	/* 2/pi */
37 #define M_2_SQRTPI		1.12837916709551257390	/* 2/sqrt(pi) */
38 #define M_SQRT2			1.41421356237309504880	/* sqrt(2) */
39 #define M_SQRT1_2		0.70710678118654752440	/* 1/sqrt(2) */
40 
41 /* platform independent IEEE floating point special values */
42 #if __GNUC__ >= 4
43 	/* gcc4 and clang definitions */
44 #	ifndef HUGE_VAL
45 #		define HUGE_VAL     __builtin_huge_val()
46 #	endif
47 
48 #	define HUGE_VALF        __builtin_huge_valf()
49 #	define HUGE_VALL		__builtin_huge_vall()
50 #	define NAN              __builtin_nanf("")
51 #	define INFINITY			__builtin_inff()
52 #else
53 	/* gcc2 definitions */
54 #	define	__HUGE_VAL_v	0x7ff0000000000000LL
55 #	define __huge_val_t	union { unsigned char __c[8]; long long __ll; double __d; }
56 #	ifndef HUGE_VAL
57 #		define HUGE_VAL		(((__huge_val_t) { __ll: __HUGE_VAL_v }).__d)
58 #	endif
59 
60 #	define __HUGE_VALF_v	0x7f800000L
61 #	define __huge_valf_t	union { unsigned char __c[4]; long __l; float __f; }
62 #	define HUGE_VALF		(((__huge_valf_t) { __l: __HUGE_VALF_v }).__f)
63 
64 	/* TODO: define HUGE_VALL for long doubles */
65 
66 #	define __NAN_VALF_v	0x7fc00000L
67 #	define NAN				(((__huge_valf_t) { __l: __NAN_VALF_v }).__f)
68 #	define INFINITY			HUGE_VALF
69 #endif
70 
71 #include <limits.h>
72 #define MAXFLOAT FLT_MAX
73 
74 /* floating-point categories */
75 #define FP_NAN			0
76 #define FP_INFINITE		1
77 #define FP_ZERO			2
78 #define FP_SUBNORMAL	3
79 #define FP_NORMAL		4
80 
81 #ifdef __FP_FAST_FMA
82 	#define FP_FAST_FMA		__FP_FAST_FMA
83 #else
84 	#define FP_FAST_FMA		0
85 #endif
86 #ifdef __FP_FAST_FMAF
87 	#define FP_FAST_FMAF	__FP_FAST_FMAF
88 #else
89 	#define FP_FAST_FMAF	0
90 #endif
91 #ifdef __FP_FAST_FMAL
92 	#define FP_FAST_FMAL 	__FP_FAST_FMAL
93 #else
94 	#define FP_FAST_FMAL	0
95 #endif
96 
97 #define FP_ILOGB0		INT_MAX
98 #define FP_ILOGBNAN		INT_MAX
99 
100 #ifdef __cplusplus
101 struct __exception;
102 extern "C" int matherr(struct __exception *);
103 struct __exception {
104 #else
105 struct exception;
106 extern int matherr(struct exception *);
107 struct exception {
108 #endif
109 	int		type;
110 	char	*name;
111 	double	arg1;
112 	double	arg2;
113 	double	retval;
114 };
115 
116 #define MATH_ERRNO			1
117 #define MATH_ERREXCEPT		2
118 #define math_errhandling	0
119 
120 #define DOMAIN		1
121 #define SING		2
122 #define OVERFLOW	3
123 #define UNDERFLOW	4
124 #define TLOSS		5
125 #define PLOSS		6
126 
127 extern int signgam;
128 
129 
130 #ifdef __cplusplus
131 extern "C" {
132 #endif
133 
134 /* float math functions */
135 extern float		acosf(float x);
136 extern float		acoshf(float x);
137 extern float		asinf(float x);
138 extern float		asinhf(float x);
139 extern float		atan2f(float y, float x);
140 extern float		atanf(float x);
141 extern float		atanhf(float x);
142 extern float		cbrtf(float x);
143 extern float		ceilf(float x);
144 extern float		copysignf(float x, float y);
145 extern float		cosf(float x);
146 extern float		coshf(float x);
147 extern float		erfcf(float x);
148 extern float		erff(float x);
149 extern float		expf(float x);
150 extern float		exp2f(float x);
151 extern float		expm1f(float x);
152 extern float		fabsf(float x);
153 extern float		fdimf(float x, float y);
154 extern float		floorf(float x);
155 extern float		fmaf(float x, float y, float z);
156 extern float		fmaxf(float x, float y);
157 extern float		fminf(float x, float y);
158 extern float		fmodf(float x, float y);
159 extern float		frexpf(float x, int *_exponent);
160 extern float		gammaf(float x);
161 extern float		hypotf(float x, float y);
162 extern int			ilogbf(float x);
163 extern float		ldexpf(float x, int exponent);
164 extern float		lgammaf(float x);
165 extern long long	llrintf(float x);
166 extern long long	llroundf(float x);
167 extern float		log10f(float x);
168 extern float		log1pf(float x);
169 extern float		log2f(float x);
170 extern float		logbf(float x);
171 extern float		logf(float x);
172 extern long			lrintf(float x);
173 extern long			lroundf(float x);
174 extern float		modff(float x, float *y);
175 extern float		nanf(const char *tagp);
176 extern float		nearbyintf(float x);
177 extern float		nextafterf(float x, float y);
178 extern float		nexttowardf(float x, long double y);
179 extern float		powf(float x, float y);
180 extern float		remainderf(float x, float y);
181 extern float		remquof(float x, float y, int *quo);
182 extern float		rintf(float x);
183 extern float		roundf(float x);
184 extern float		scalbf(float x, float n);
185 extern float		scalblnf(float x, long n);
186 extern float		scalbnf(float x, int n);
187 extern float		sinf(float x);
188 extern float		sinhf(float x);
189 extern float		sqrtf(float x);
190 extern float		tanf(float x);
191 extern float		tanhf(float x);
192 extern float		tgammaf(float x);
193 extern float		truncf(float x);
194 
195 /* double math functions */
196 extern double		acos(double x);
197 extern double		acosh(double x);
198 extern double		asin(double x);
199 extern double		asinh(double x);
200 extern double		atan(double x);
201 extern double		atan2(double x, double y);
202 extern double		atanh(double x);
203 extern double		cbrt(double x);
204 extern double		ceil(double x);
205 extern double		copysign(double x, double y);
206 extern double		cos(double x);
207 extern double		cosh(double x);
208 extern double		erf(double x);
209 extern double		erfc(double x);
210 extern double		exp(double x);
211 extern double		expm1(double x);
212 extern double		exp2(double x);
213 extern double		fabs(double x);
214 extern double		fdim(double x, double y);
215 extern double		floor(double x);
216 extern double		fma(double x, double y, double z);
217 extern double		fmax(double x, double y);
218 extern double		fmin(double x, double y);
219 extern double		fmod(double x, double y);
220 extern double		frexp(double x, int *_exponent);
221 extern double		gamma(double x);
222 extern double		hypot(double x, double y);
223 extern int			ilogb(double x);
224 extern double		ldexp(double x, int exponent);
225 extern double		lgamma(double x);
226 extern long long	llrint(double x);
227 extern long long	llround(double x);
228 extern double		log(double x);
229 extern double		log10(double x);
230 extern double		log1p(double x);
231 extern double		log2(double x);
232 extern double		logb(double x);
233 extern long			lrint(double x);
234 extern long			lround(double x);
235 extern double		modf(double x, double *y);
236 extern double		nan(const char* tagp);
237 extern double		nearbyint(double x);
238 extern double		nextafter(double x, double y);
239 extern double		nexttoward(double x, long double y);
240 extern double		pow(double x, double y);
241 extern double		remainder(double x, double y);
242 extern double		remquo(double x, double y, int *quo);
243 extern double		rint(double x);
244 extern double		round(double x);
245 extern double		scalb(double x, double y);
246 extern double 		scalbln(double x, long n);
247 extern double		scalbn(double x, int y);
248 extern double		sin(double x);
249 extern double		sinh(double x);
250 extern double		sqrt(double x);
251 extern double		tan(double x);
252 extern double		tanh(double x);
253 extern double		tgamma(double x);
254 extern double		trunc(double x);
255 
256 /* long double math functions */
257 extern long double	acoshl(long double x);
258 extern long double	acosl(long double x);
259 extern long double	asinhl(long double x);
260 extern long double	asinl(long double x);
261 extern long double	atanhl(long double x);
262 extern long double	atanl(long double x);
263 extern long double	atan2l(long double y, long double x);
264 extern long double	cbrtl(long double x);
265 extern long double	ceill(long double x);
266 extern long double	copysignl(long double x, long double y);
267 extern long double	cosl(long double x);
268 extern long double	coshl(long double x);
269 extern long double	erfcl(long double x);
270 extern long double	erfl(long double x);
271 extern long double	expl(long double x);
272 extern long double	expm1l(long double x);
273 extern long double	exp2l(long double x);
274 extern long double	fabsl(long double x);
275 extern long double	fdiml(long double x, long double y);
276 extern long double	floorl(long double x);
277 extern long double	fmal(long double x, long double y, long double z);
278 extern long double	fmaxl(long double x, long double y);
279 extern long double	fminl(long double x, long double y);
280 extern long double	fmodl(long double x, long double y);
281 extern long double	frexpl(long double num, int *_exponent);
282 extern long double	hypotl(long double x, long double y);
283 extern int			ilogbl(long double x);
284 extern long double	ldexpl(long double x, int exponent);
285 extern long double	lgammal(long double x);
286 extern long long	llrintl(long double x);
287 extern long long	llroundl(long double x);
288 extern long double	logbl(long double x);
289 extern long double	logl(long double x);
290 extern long double	log1pl(long double x);
291 extern long double	log10l(long double x);
292 extern long double	log2l(long double x);
293 extern long			lrintl(long double x);
294 extern long			lroundl(long double x);
295 extern long double	modfl(long double value, long double *iptr);
296 extern long double	nanl(const char* tagp);
297 extern long double	nearbyintl(long double x);
298 extern long double	nextafterl(long double x, long double y);
299 extern long double	nexttowardl(long double x, long double y);
300 extern long double	powl(long double x, long double y);
301 extern long double	remainderl(long double x, long double y);
302 extern long double	remquol(long double x, long double y, int *quo);
303 extern long double	rintl(long double x);
304 extern long double	roundl(long double x);
305 extern long double	scalbnl(long double x, int n);
306 extern long double	scalblnl(long double x, long n);
307 extern long double	sinl(long double x);
308 extern long double	sinhl(long double x);
309 extern long double	sqrtl(long double x);
310 extern long double	tanl(long double x);
311 extern long double	tanhl(long double x);
312 extern long double	tgammal(long double x);
313 extern long double	truncl(long double x);
314 
315 /* some BSD non-ANSI or POSIX math functions */
316 extern double		j0(double x);
317 extern double		j1(double x);
318 extern double		jn(int x, double y);
319 extern double		lgamma_r(double x, int *y);
320 extern double		y0(double x);
321 extern double		y1(double x);
322 extern double		yn(int x, double y);
323 
324 /* other stuff as defined in BeOS */
325 extern int			isinff(float value);
326 extern int			finitef(float value);
327 extern float		dremf(float x, float y);
328 extern float		significandf(float x);
329 extern int			isnanf(float value);
330 extern double		significand(double x);
331 extern double		drem(double x, double y);
332 extern int			finite(double x);
333 extern float		j0f(float x);
334 extern float		j1f(float x);
335 extern float		jnf(int x, float y);
336 extern float		y0f(float x);
337 extern float		y1f(float x);
338 extern float		ynf(int x, float y);
339 extern float		lgammaf_r(float x, int *y);
340 
341 
342 /* prototypes for functions used in the macros below */
343 extern int			__fpclassifyf(float value);
344 extern int			__signbitf(float value);
345 extern int			__finitef(float value);
346 extern int			__isnanf(float value);
347 extern int			__isinff(float value);
348 
349 extern int			__fpclassifyl(long double value);
350 extern int			__signbitl(long double value);
351 extern int			__finitel(long double value);
352 extern int			__isnanl(long double value);
353 extern int			__isinfl(long double value);
354 
355 extern int			__fpclassify(double value);
356 extern int			__signbit(double value);
357 extern int			__finite(double value);
358 extern int			__isnan(double value);
359 extern int			__isinf(double value);
360 
361 /* returns number of classification appropriate for 'value' */
362 #define fpclassify(value) \
363 	(sizeof(value) == sizeof(float) ? __fpclassifyf(value)		\
364 		: sizeof(value) == sizeof(double) ? __fpclassify(value)	\
365 		: __fpclassifyl(value))
366 
367 /* returns non-zero if 'value' is negative */
368 # define signbit(value) \
369 	(sizeof(value) == sizeof(float) ? __signbitf(value)			\
370 		: sizeof(value) == sizeof(double) ? __signbit(value)	\
371 		: __signbitl(value))
372 
373 /* returns non-zero if 'value' is not Inf or NaN */
374 # define isfinite(value) \
375 	(sizeof(value) == sizeof(float) ? __finitef(value)			\
376 		: sizeof(value) == sizeof(double) ? __finite(value)		\
377 		: __finitel(value))
378 
379 /* returns non-zero if 'value' is neither zero, sub-normal, Inf, nor NaN */
380 # define isnormal(value) \
381 	(fpclassify(value) == FP_NORMAL)
382 
383 /* returns non-zero if 'value' is NaN */
384 # define isnan(value) \
385 	(sizeof(value) == sizeof(float) ? __isnanf(value)			\
386 		: sizeof(value) == sizeof(double) ? __isnan(value)		\
387 		: __isnanl(value))
388 
389 /* returns non-zero if 'value is Inf */
390 # define isinf(value) \
391 	(sizeof(value) == sizeof(float) ? __isinff(value)			\
392 		: sizeof(value) == sizeof(double) ? __isinf(value)		\
393 		: __isinfl(value))
394 
395 #if __GNUC__ >= 4
396 	#define isgreater(x, y)			__builtin_isgreater((x), (y))
397 	#define isgreaterequal(x, y)	__builtin_isgreaterequal((x), (y))
398 	#define isless(x, y)			__builtin_isless((x), (y))
399 	#define islessequal(x, y)		__builtin_islessequal((x), (y))
400 	#define islessgreater(x, y)		__builtin_islessgreater((x), (y))
401 	#define isunordered(x, y)		__builtin_isunordered((x), (y))
402 #else
403 	/* these are only for GCC2, and require GNUC statement expressions */
404 	#define _wrap_expr_typeof(x, y, body) ({	\
405 		__typeof(x) X = (x);					\
406 		__typeof(y) Y = (y);					\
407 		body;})
408 	#define isgreater(x, y)	\
409 		_wrap_expr_typeof(x, y, !isnan(X) && !isnan(Y) && X > Y)
410 	#define isgreaterequal(x, y) \
411 		_wrap_expr_typeof(x, y, !isnan(X) && !isnan(Y) && X >= Y)
412 	#define isless(x, y) \
413 		_wrap_expr_typeof(x, y, !isnan(X) && !isnan(Y) && X < Y)
414 	#define islessequal(x, y) \
415 		_wrap_expr_typeof(x, y, !isnan(X) && !isnan(Y) && X <= Y)
416 	#define islessgreater(x, y)	\
417 		_wrap_expr_typeof(x, y, X < Y || Y < X)
418 	#define isunordered(x, y) \
419 		_wrap_expr_typeof(x, y, isnan(X) || isnan(Y))
420 #endif
421 
422 #ifdef __cplusplus
423 }
424 #endif
425 
426 #endif	/* _MATH_H_ */
427