blob: 3d0ec496b30ba0bb0ba033f5fdbd26efa6644169 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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
Elliott Hughesa0ee0782013-01-30 19:06:37 -080014 * $FreeBSD$
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015 */
16
17#ifndef _MATH_H_
Elliott Hughesb6622802015-08-14 14:04:30 -070018#define _MATH_H_
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080019
20#include <sys/cdefs.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080021#include <limits.h>
22
Elliott Hughesde9ac712014-05-19 16:58:52 -070023__BEGIN_DECLS
Elliott Hughesde9ac712014-05-19 16:58:52 -070024
Elliott Hughes50cda382017-09-14 15:30:08 -070025/* C11. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080026
Elliott Hughes50cda382017-09-14 15:30:08 -070027typedef double __double_t;
28typedef __double_t double_t;
29typedef float __float_t;
30typedef __float_t float_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
Elliott Hughes50cda382017-09-14 15:30:08 -070032#define HUGE_VAL __builtin_huge_val()
33#define HUGE_VALF __builtin_huge_valf()
34#define HUGE_VALL __builtin_huge_vall()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Elliott Hughes50cda382017-09-14 15:30:08 -070036#define INFINITY __builtin_inff()
37
38#define NAN __builtin_nanf("")
39
40#define FP_INFINITE 0x01
41#define FP_NAN 0x02
42#define FP_NORMAL 0x04
43#define FP_SUBNORMAL 0x08
44#define FP_ZERO 0x10
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045
Elliott Hughesb6622802015-08-14 14:04:30 -070046#if defined(__FP_FAST_FMA)
47#define FP_FAST_FMA 1
48#endif
49#if defined(__FP_FAST_FMAF)
50#define FP_FAST_FMAF 1
51#endif
52#if defined(__FP_FAST_FMAL)
53#define FP_FAST_FMAL 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
Elliott Hughes50cda382017-09-14 15:30:08 -070056#define FP_ILOGB0 (-INT_MAX)
57#define FP_ILOGBNAN INT_MAX
58
59#define MATH_ERRNO 1
60#define MATH_ERREXCEPT 2
61#define math_errhandling MATH_ERREXCEPT
62
63#define fpclassify(x) __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Elliott Hughesb6622802015-08-14 14:04:30 -070065#define isfinite(x) __builtin_isfinite(x)
Elliott Hughes50cda382017-09-14 15:30:08 -070066
Elliott Hughesb6622802015-08-14 14:04:30 -070067#define isinf(x) __builtin_isinf(x)
Elliott Hughes50cda382017-09-14 15:30:08 -070068
Elliott Hughesb6622802015-08-14 14:04:30 -070069#define isnan(x) __builtin_isnan(x)
Elliott Hughes50cda382017-09-14 15:30:08 -070070
Elliott Hughesb6622802015-08-14 14:04:30 -070071#define isnormal(x) __builtin_isnormal(x)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
Elliott Hughes50cda382017-09-14 15:30:08 -070073#define signbit(x) \
74 ((sizeof(x) == sizeof(float)) ? __builtin_signbitf(x) \
75 : (sizeof(x) == sizeof(double)) ? __builtin_signbit(x) \
76 : __builtin_signbitl(x))
77
78double acos(double __x);
79float acosf(float __x);
80long double acosl(long double __x) __RENAME_LDBL(acos, 3, 21);
81
82double asin(double __x);
83float asinf(float __x);
84long double asinl(long double __x) __RENAME_LDBL(asin, 3, 21);
85
86double atan(double __x);
87float atanf(float __x);
88long double atanl(long double __x) __RENAME_LDBL(atan, 3, 21);
89
90double atan2(double __y, double __x);
91float atan2f(float __y, float __x);
92long double atan2l(long double __y, long double __x) __RENAME_LDBL(atan2, 3, 21);
93
94double cos(double __x);
95float cosf(float __x);
96long double cosl(long double __x) __RENAME_LDBL(cos, 3, 21);
97
98double sin(double __x);
99float sinf(float __x);
100long double sinl(long double __x) __RENAME_LDBL(sin, 3, 21);
101
102double tan(double __x);
103float tanf(float __x);
104long double tanl(long double __x) __RENAME_LDBL(tan, 3, 21);
105
106double acosh(double __x);
107float acoshf(float __x);
108long double acoshl(long double __x) __RENAME_LDBL(acosh, 3, 21);
109
110double asinh(double __x);
111float asinhf(float __x);
112long double asinhl(long double __x) __RENAME_LDBL(asinh, 3, 21);
113
114double atanh(double __x);
115float atanhf(float __x);
116long double atanhl(long double __x) __RENAME_LDBL(atanh, 3, 21);
117
118double cosh(double __x);
119float coshf(float __x);
120long double coshl(long double __x) __RENAME_LDBL(cosh, 3, 21);
121
122double sinh(double __x);
123float sinhf(float __x);
124long double sinhl(long double __x) __RENAME_LDBL(sinh, 3, 21);
125
126double tanh(double __x);
127float tanhf(float __x);
128long double tanhl(long double __x) __RENAME_LDBL(tanh, 3, 21);
129
130double exp(double __x);
131float expf(float __x);
132long double expl(long double __x) __RENAME_LDBL(exp, 3, 21);
133
134double exp2(double __x);
135float exp2f(float __x);
136long double exp2l(long double __x) __RENAME_LDBL(exp2, 3, 21);
137
138double expm1(double __x);
139float expm1f(float __x);
140long double expm1l(long double __x) __RENAME_LDBL(expm1, 3, 21);
141
142double frexp(double __x, int* __exponent);
143float frexpf(float __x, int* __exponent);
144long double frexpl(long double __x, int* __exponent) __RENAME_LDBL(frexp, 3, 21);
145
146int ilogb(double __x) __attribute_const__;
147int ilogbf(float __x) __attribute_const__;
148int ilogbl(long double __x) __attribute_const__ __RENAME_LDBL(ilogb, 3, 3);
149
150double ldexp(double __x, int __exponent);
151float ldexpf(float __x, int __exponent);
152long double ldexpl(long double __x, int __exponent) __RENAME_LDBL(ldexp, 3, 3);
153
154double log(double __x);
155float logf(float __x);
156long double logl(long double __x) __RENAME_LDBL(log, 3, 21);
157
158double log10(double __x);
159float log10f(float __x);
160long double log10l(long double __x) __RENAME_LDBL(log10, 3, 21);
161
162double log1p(double __x);
163float log1pf(float __x);
164long double log1pl(long double __x) __RENAME_LDBL(log1p, 3, 21);
165
166double log2(double __x) __INTRODUCED_IN(18);
167float log2f(float __x) __INTRODUCED_IN(18);
168long double log2l(long double __x) __RENAME_LDBL(log2, 18, 18);
169
170double logb(double __x);
171float logbf(float __x);
172long double logbl(long double __x) __RENAME_LDBL(logb, 3, 18);
173
174double modf(double __x, double* __integral_part);
175float modff(float __x, float* __integral_part);
176long double modfl(long double __x, long double* __integral_part) __RENAME_LDBL(modf, 3, 21);
177
178double scalbn(double __x, int __exponent);
179float scalbnf(float __x, int __exponent);
180long double scalbnl(long double __x, int __exponent) __RENAME_LDBL(scalbn, 3, 3);
181
182/* TODO: once the NDK only supports >= 18, use __RENAME_LDBL here too. */
183double scalbln(double __x, long __exponent) __INTRODUCED_IN_X86(18) __VERSIONER_NO_GUARD;
184float scalblnf(float __x, long __exponent) __INTRODUCED_IN_X86(18) __VERSIONER_NO_GUARD;
185long double scalblnl(long double __x, long __exponent) __INTRODUCED_IN_X86(18) __VERSIONER_NO_GUARD;
186
187double cbrt(double __x);
188float cbrtf(float __x);
189long double cbrtl(long double __x) __RENAME_LDBL(cbrt, 3, 21);
190
191double fabs(double __x) __attribute_const__;
192float fabsf(float __x) __attribute_const__;
193long double fabsl(long double __x) __attribute_const__ __RENAME_LDBL(fabs, 3, 3);
194
195double hypot(double __x, double __y);
196float hypotf(float __x, float __y);
197long double hypotl(long double __x, long double __y) __RENAME_LDBL(hypot, 3, 21);
198
199double pow(double __x, double __y);
200float powf(float __x, float __y);
201long double powl(long double __x, long double __y) __RENAME_LDBL(pow, 3, 21);
202
203double sqrt(double __x);
204float sqrtf(float __x);
205long double sqrtl(long double __x) __RENAME_LDBL(sqrt, 3, 21);
206
207double erf(double __x);
208float erff(float __x);
209long double erfl(long double __x) __RENAME_LDBL(erf, 3, 21);
210
211double erfc(double __x);
212float erfcf(float __x);
213long double erfcl(long double __x) __RENAME_LDBL(erfc, 3, 21);
214
215double lgamma(double __x);
216float lgammaf(float __x);
217long double lgammal(long double __x) __RENAME_LDBL(lgamma, 3, 21);
218
219double tgamma(double __x);
220float tgammaf(float __x) __INTRODUCED_IN_ARM(13) __INTRODUCED_IN_MIPS(13) __INTRODUCED_IN_X86(9);
221long double tgammal(long double __x) __RENAME_LDBL(tgamma, 3, 21);
222
223double ceil(double __x);
224float ceilf(float __x);
225long double ceill(long double __x) __RENAME_LDBL(ceil, 3, 3);
226
227double floor(double __x);
228float floorf(float __x);
229long double floorl(long double __x) __RENAME_LDBL(floor, 3, 3);
230
231double nearbyint(double __x);
232float nearbyintf(float __x);
233long double nearbyintl(long double __x) __RENAME_LDBL(nearbyint, 3, 21);
234
235double rint(double __x);
236float rintf(float __x);
237long double rintl(long double __x) __RENAME_LDBL(rint, 3, 21);
238
239long lrint(double __x);
240long lrintf(float __x);
241long lrintl(long double __x) __RENAME_LDBL(lrint, 3, 21);
242
243long long llrint(double __x);
244long long llrintf(float __x);
245long long llrintl(long double __x) __RENAME_LDBL(llrint, 3, 21);
246
247double round(double __x);
248float roundf(float __x);
249long double roundl(long double __x) __RENAME_LDBL(roundl, 3, 3);
250
251long lround(double __x);
252long lroundf(float __x);
253long lroundl(long double __x) __RENAME_LDBL(lround, 3, 3);
254
255long long llround(double __x);
256long long llroundf(float __x);
257long long llroundl(long double __x) __RENAME_LDBL(llround, 3, 3);
258
259double trunc(double __x);
260float truncf(float __x);
261long double truncl(long double __x) __RENAME_LDBL(trunc, 3, 3);
262
263double fmod(double __x, double __y);
264float fmodf(float __x, float __y);
265long double fmodl(long double __x, long double __y) __RENAME_LDBL(fmod, 3, 21);
266
267double remainder(double __x, double __y);
268float remainderf(float __x, float __y);
269long double remainderl(long double __x, long double __y) __RENAME_LDBL(remainder, 3, 21);
270
271double remquo(double __x, double __y, int* __quotient_bits);
272float remquof(float __x, float __y, int* __quotient_bits);
273long double remquol(long double __x, long double __y, int* __quotient_bits) __RENAME_LDBL(remquo, 3, 21);
274
275double copysign(double __value, double __sign) __attribute_const__;
276float copysignf(float __value, float __sign) __attribute_const__;
277long double copysignl(long double __value, long double __sign) __attribute_const__ __RENAME_LDBL(copysign, 3, 3);
278
279double nan(const char* __kind) __attribute_const__ __INTRODUCED_IN_ARM(13) __INTRODUCED_IN_MIPS(13) __INTRODUCED_IN_X86(9);
280float nanf(const char* __kind) __attribute_const__ __INTRODUCED_IN_ARM(13) __INTRODUCED_IN_MIPS(13) __INTRODUCED_IN_X86(9);
281long double nanl(const char* __kind) __attribute_const__ __RENAME_LDBL(nan, 13, 13);
282
283double nextafter(double __x, double __y);
284float nextafterf(float __x, float __y);
285long double nextafterl(long double __x, long double __y) __RENAME_LDBL(nextafter, 3, 21) __VERSIONER_NO_GUARD;
286
287double nexttoward(double __x, long double __y) __INTRODUCED_IN(18) __VERSIONER_NO_GUARD;
288float nexttowardf(float __x, long double __y);
289long double nexttowardl(long double __x, long double __y) __RENAME_LDBL(nexttoward, 18, 18) __VERSIONER_NO_GUARD;
290
291double fdim(double __x, double __y);
292float fdimf(float __x, float __y);
293long double fdiml(long double __x, long double __y) __RENAME_LDBL(fdim, 3, 3);
294
295double fmax(double __x, double __y) __attribute_const__;
296float fmaxf(float __x, float __y) __attribute_const__;
297long double fmaxl(long double __x, long double __y) __attribute_const__ __RENAME_LDBL(fmax, 3, 3);
298
299double fmin(double __x, double __y) __attribute_const__;
300float fminf(float __x, float __y) __attribute_const__;
301long double fminl(long double __x, long double __y) __attribute_const__ __RENAME_LDBL(fmin, 3, 3);
302
303double fma(double __x, double __y, double __z);
304float fmaf(float __x, float __y, float __z);
305long double fmal(long double __x, long double __y, long double __z) __RENAME_LDBL(fma, 3, 21) __VERSIONER_NO_GUARD;
306
Elliott Hughesb6622802015-08-14 14:04:30 -0700307#define isgreater(x, y) __builtin_isgreater((x), (y))
308#define isgreaterequal(x, y) __builtin_isgreaterequal((x), (y))
309#define isless(x, y) __builtin_isless((x), (y))
310#define islessequal(x, y) __builtin_islessequal((x), (y))
311#define islessgreater(x, y) __builtin_islessgreater((x), (y))
312#define isunordered(x, y) __builtin_isunordered((x), (y))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800313
Dan Albert284c8f12017-03-30 16:34:27 -0700314/*
315 * https://code.google.com/p/android/issues/detail?id=271629
316 * To be fully compliant with C++, we need to not define these (C doesn't
317 * specify them either). Exposing these means that isinf and isnan will have a
318 * return type of int in C++ rather than bool like they're supposed to be.
319 *
320 * GNU libstdc++ 4.9 isn't able to handle a standard compliant C library. Its
321 * <cmath> will `#undef isnan` from math.h and only adds the function overloads
322 * to the std namespace, making it impossible to use both <cmath> (which gets
323 * included by a lot of other standard headers) and ::isnan.
324 */
Elliott Hughes684c31a2017-08-18 15:07:41 -0700325int (isinf)(double __x) __attribute_const__ __INTRODUCED_IN(21);
326int (isnan)(double __x) __attribute_const__;
Dan Albert284c8f12017-03-30 16:34:27 -0700327
Elliott Hughes50cda382017-09-14 15:30:08 -0700328/* POSIX extensions. */
Josh Gao46b44162016-05-27 11:14:16 -0700329
Elliott Hughes50cda382017-09-14 15:30:08 -0700330extern int signgam;
Elliott Hughesb4f2f282014-05-08 21:19:12 -0700331
Elliott Hughes684c31a2017-08-18 15:07:41 -0700332double j0(double __x);
333double j1(double __x);
334double jn(int __n, double __x);
335double y0(double __x);
336double y1(double __x);
337double yn(int __n, double __x);
Elliott Hughesc1929e42016-08-12 16:18:03 -0700338
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700339#define M_E 2.7182818284590452354 /* e */
340#define M_LOG2E 1.4426950408889634074 /* log 2e */
341#define M_LOG10E 0.43429448190325182765 /* log 10e */
342#define M_LN2 0.69314718055994530942 /* log e2 */
343#define M_LN10 2.30258509299404568402 /* log e10 */
344#define M_PI 3.14159265358979323846 /* pi */
345#define M_PI_2 1.57079632679489661923 /* pi/2 */
346#define M_PI_4 0.78539816339744830962 /* pi/4 */
347#define M_1_PI 0.31830988618379067154 /* 1/pi */
348#define M_2_PI 0.63661977236758134308 /* 2/pi */
349#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
350#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
351#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700352
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700353#define MAXFLOAT ((float)3.40282346638528860e+38)
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700354
Elliott Hughes50cda382017-09-14 15:30:08 -0700355/* Legacy cruft from before we had builtin implementations of the standard macros. */
356
357int __fpclassifyd(double __x) __attribute_const__;
358int __fpclassifyf(float __x) __attribute_const__;
359int __fpclassifyl(long double __x) __attribute_const__;
360
361int __isfinitef(float __x) __attribute_const__;
362int __isfinite(double __x) __attribute_const__;
363int __isfinitel(long double __x) __attribute_const__;
364
365int __isinff(float __x) __attribute_const__;
366int __isinfl(long double __x) __attribute_const__;
367
368int __isnanf(float __x) __attribute_const__ __INTRODUCED_IN(21);
369int __isnanl(long double __x) __attribute_const__;
370
371int __isnormal(double __x) __attribute_const__;
372int __isnormalf(float __x) __attribute_const__;
373int __isnormall(long double __x) __attribute_const__;
374
375int __signbit(double __x) __attribute_const__;
376int __signbitf(float __x) __attribute_const__;
377int __signbitl(long double __x) __attribute_const__;
378
379/* BSD extensions. */
380
381#if defined(__USE_BSD)
382#define HUGE MAXFLOAT
383#endif
384
385/* Extensions in both BSD and GNU. */
386
Elliott Hughes9c8d7112016-06-13 13:23:42 -0700387#if defined(__USE_BSD) || defined(__USE_GNU)
Elliott Hughes684c31a2017-08-18 15:07:41 -0700388double gamma(double __x);
389double scalb(double __x, double __exponent);
390double drem(double __x, double __y);
391int finite(double __x) __attribute_const__;
392int isnanf(float __x) __attribute_const__;
393double gamma_r(double __x, int* __sign);
394double lgamma_r(double __x, int* __sign);
395double significand(double __x);
396long double lgammal_r(long double __x, int* __sign) __INTRODUCED_IN(23);
397long double significandl(long double __x) __INTRODUCED_IN(21);
398float dremf(float __x, float __y);
399int finitef(float __x) __attribute_const__;
400float gammaf(float __x);
401float j0f(float __x);
402float j1f(float __x);
403float jnf(int __n, float __x);
404float scalbf(float __x, float __exponent);
405float y0f(float __x);
406float y1f(float __x);
407float ynf(int __n, float __x);
408float gammaf_r(float __x, int* __sign);
409float lgammaf_r(float __x, int* __sign);
410float significandf(float __x);
Elliott Hughes75531852014-09-18 11:23:58 -0700411#endif
412
Elliott Hughes50cda382017-09-14 15:30:08 -0700413/* GNU extensions. */
414
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700415#if defined(__USE_GNU)
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700416#define M_El 2.718281828459045235360287471352662498L /* e */
417#define M_LOG2El 1.442695040888963407359924681001892137L /* log 2e */
418#define M_LOG10El 0.434294481903251827651128918916605082L /* log 10e */
419#define M_LN2l 0.693147180559945309417232121458176568L /* log e2 */
420#define M_LN10l 2.302585092994045684017991454684364208L /* log e10 */
421#define M_PIl 3.141592653589793238462643383279502884L /* pi */
422#define M_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
423#define M_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
424#define M_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
425#define M_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
426#define M_2_SQRTPIl 1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
427#define M_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
428#define M_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
Elliott Hughes684c31a2017-08-18 15:07:41 -0700429void sincos(double __x, double* __sin, double* __cos);
430void sincosf(float __x, float* __sin, float* __cos);
431void sincosl(long double __x, long double* __sin, long double* __cos);
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700432#endif
Elliott Hughesb4f2f282014-05-08 21:19:12 -0700433
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434__END_DECLS
435
Elliott Hughes684c31a2017-08-18 15:07:41 -0700436#endif