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