Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / include / math.h
blob5e92d2662da145cabad51e5946051aa34e9092b6
1 #ifndef _MATH_H_
3 #define _MATH_H_
5 #include <sys/reent.h>
6 #include <sys/cdefs.h>
7 #include <machine/ieeefp.h>
8 #include "_ansi.h"
10 _BEGIN_STD_C
12 /* Natural log of 2 */
13 #define _M_LN2 0.693147180559945309417
15 #if __GNUC_PREREQ (3, 3)
16 /* gcc >= 3.3 implicitly defines builtins for HUGE_VALx values. */
18 # ifndef HUGE_VAL
19 # define HUGE_VAL (__builtin_huge_val())
20 # endif
22 # ifndef HUGE_VALF
23 # define HUGE_VALF (__builtin_huge_valf())
24 # endif
26 # ifndef HUGE_VALL
27 # define HUGE_VALL (__builtin_huge_vall())
28 # endif
30 # ifndef INFINITY
31 # define INFINITY (__builtin_inff())
32 # endif
34 # ifndef NAN
35 # define NAN (__builtin_nanf(""))
36 # endif
38 #else /* !gcc >= 3.3 */
40 /* No builtins. Use fixed defines instead. (All 3 HUGE plus the INFINITY
41 * and NAN macros are required to be constant expressions. Using a variable--
42 * even a static const--does not meet this requirement, as it cannot be
43 * evaluated at translation time.)
44 * The infinities are done using numbers that are far in excess of
45 * something that would be expected to be encountered in a floating-point
46 * implementation. (A more certain way uses values from float.h, but that is
47 * avoided because system includes are not supposed to include each other.)
48 * This method might produce warnings from some compilers. (It does in
49 * newer GCCs, but not for ones that would hit this #else.) If this happens,
50 * please report details to the Newlib mailing list. */
52 #ifndef HUGE_VAL
53 #define HUGE_VAL (1.0e999999999)
54 #endif
56 #ifndef HUGE_VALF
57 #define HUGE_VALF (1.0e999999999F)
58 #endif
60 #if !defined(HUGE_VALL) && defined(_HAVE_LONG_DOUBLE)
61 #define HUGE_VALL (1.0e999999999L)
62 #endif
64 #if !defined(INFINITY)
65 #define INFINITY (HUGE_VALF)
66 #endif
68 #if !defined(NAN)
69 #if defined(__GNUC__) && defined(__cplusplus)
70 /* Exception: older g++ versions warn about the divide by 0 used in the
71 * normal case (even though older gccs do not). This trick suppresses the
72 * warning, but causes errors for plain gcc, so is only used in the one
73 * special case. */
74 static const union { __ULong __i[1]; float __d; } __Nanf = {0x7FC00000};
75 #define NAN (__Nanf.__d)
76 #else
77 #define NAN (0.0F/0.0F)
78 #endif
79 #endif
81 #endif /* !gcc >= 3.3 */
83 /* Reentrant ANSI C functions. */
85 #ifndef __math_68881
86 extern double atan (double);
87 extern double cos (double);
88 extern double sin (double);
89 extern double tan (double);
90 extern double tanh (double);
91 extern double frexp (double, int *);
92 extern double modf (double, double *);
93 extern double ceil (double);
94 extern double fabs (double);
95 extern double floor (double);
96 #endif /* ! defined (__math_68881) */
98 /* Non reentrant ANSI C functions. */
100 #ifndef _REENT_ONLY
101 #ifndef __math_68881
102 extern double acos (double);
103 extern double asin (double);
104 extern double atan2 (double, double);
105 extern double cosh (double);
106 extern double sinh (double);
107 extern double exp (double);
108 extern double ldexp (double, int);
109 extern double log (double);
110 extern double log10 (double);
111 extern double pow (double, double);
112 extern double sqrt (double);
113 extern double fmod (double, double);
114 #endif /* ! defined (__math_68881) */
115 #endif /* ! defined (_REENT_ONLY) */
117 #if __MISC_VISIBLE
118 extern int finite (double);
119 extern int finitef (float);
120 extern int finitel (long double);
121 extern int isinff (float);
122 extern int isnanf (float);
123 #ifdef __CYGWIN__ /* not implemented in newlib yet */
124 extern int isinfl (long double);
125 extern int isnanl (long double);
126 #endif
127 #if !defined(__cplusplus) || __cplusplus < 201103L
128 extern int isinf (double);
129 #endif
130 #endif /* __MISC_VISIBLE */
131 #if (__MISC_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)) \
132 && (!defined(__cplusplus) || __cplusplus < 201103L)
133 extern int isnan (double);
134 #endif
136 #if __ISO_C_VISIBLE >= 1999
137 /* ISO C99 types and macros. */
139 /* FIXME: FLT_EVAL_METHOD should somehow be gotten from float.h (which is hard,
140 * considering that the standard says the includes it defines should not
141 * include other includes that it defines) and that value used. (This can be
142 * solved, but autoconf has a bug which makes the solution more difficult, so
143 * it has been skipped for now.) */
144 #if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__)
145 #define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
146 #define __TMP_FLT_EVAL_METHOD
147 #endif /* FLT_EVAL_METHOD */
148 #if defined FLT_EVAL_METHOD
149 /* FLT_EVAL_METHOD == 16 has meaning as defined in ISO/IEC TS 18661-3,
150 * which provides non-compliant extensions to C and POSIX (by adding
151 * additional positive values for FLT_EVAL_METHOD). It effectively has
152 * same meaning as the C99 and C11 definitions for value 0, while also
153 * serving as a flag that the _Float16 (float16_t) type exists.
155 * FLT_EVAL_METHOD could be any number of bits of supported floating point
156 * format (e.g. 32, 64, 128), but currently only AArch64 and few other targets
157 * might define that as 16. */
158 #if (FLT_EVAL_METHOD == 0) \
159 || (FLT_EVAL_METHOD == 16)
160 typedef float float_t;
161 typedef double double_t;
162 #elif FLT_EVAL_METHOD == 1
163 typedef double float_t;
164 typedef double double_t;
165 #elif FLT_EVAL_METHOD == 2
166 typedef long double float_t;
167 typedef long double double_t;
168 #else
169 /* Implementation-defined. Assume float_t and double_t have been
170 * defined previously for this configuration (e.g. config.h). */
172 /* If __DOUBLE_TYPE is defined (__FLOAT_TYPE is then supposed to be
173 defined as well) float_t and double_t definition is suggested by
174 an arch specific header. */
175 #ifdef __DOUBLE_TYPE
176 typedef __DOUBLE_TYPE double_t;
177 typedef __FLOAT_TYPE float_t;
178 #endif
179 /* Assume config.h has provided these types. */
180 #endif
181 #else
182 /* Assume basic definitions. */
183 typedef float float_t;
184 typedef double double_t;
185 #endif
186 #if defined(__TMP_FLT_EVAL_METHOD)
187 #undef FLT_EVAL_METHOD
188 #endif
190 #define FP_NAN 0
191 #define FP_INFINITE 1
192 #define FP_ZERO 2
193 #define FP_SUBNORMAL 3
194 #define FP_NORMAL 4
196 #ifndef FP_ILOGB0
197 # define FP_ILOGB0 (-__INT_MAX__)
198 #endif
199 #ifndef FP_ILOGBNAN
200 # define FP_ILOGBNAN __INT_MAX__
201 #endif
203 #ifndef MATH_ERRNO
204 # define MATH_ERRNO 1
205 #endif
206 #ifndef MATH_ERREXCEPT
207 # define MATH_ERREXCEPT 2
208 #endif
209 #ifndef math_errhandling
210 # ifdef _IEEE_LIBM
211 # define _MATH_ERRHANDLING_ERRNO 0
212 # else
213 # define _MATH_ERRHANDLING_ERRNO MATH_ERRNO
214 # endif
215 # ifdef _SUPPORTS_ERREXCEPT
216 # define _MATH_ERRHANDLING_ERREXCEPT MATH_ERREXCEPT
217 # else
218 # define _MATH_ERRHANDLING_ERREXCEPT 0
219 # endif
220 # define math_errhandling (_MATH_ERRHANDLING_ERRNO | _MATH_ERRHANDLING_ERREXCEPT)
221 #endif
223 extern int __isinff (float);
224 extern int __isinfd (double);
225 extern int __isnanf (float);
226 extern int __isnand (double);
227 extern int __fpclassifyf (float);
228 extern int __fpclassifyd (double);
229 extern int __signbitf (float);
230 extern int __signbitd (double);
232 /* Note: isinf and isnan were once functions in newlib that took double
233 * arguments. C99 specifies that these names are reserved for macros
234 * supporting multiple floating point types. Thus, they are
235 * now defined as macros. Implementations of the old functions
236 * taking double arguments still exist for compatibility purposes
237 * (prototypes for them are earlier in this header). */
239 #if __GNUC_PREREQ (4, 4)
240 #define fpclassify(__x) (__builtin_fpclassify (FP_NAN, FP_INFINITE, \
241 FP_NORMAL, FP_SUBNORMAL, \
242 FP_ZERO, __x))
243 #ifndef isfinite
244 #define isfinite(__x) (__builtin_isfinite (__x))
245 #endif
246 #ifndef isinf
247 #define isinf(__x) (__builtin_isinf_sign (__x))
248 #endif
249 #ifndef isnan
250 #define isnan(__x) (__builtin_isnan (__x))
251 #endif
252 #define isnormal(__x) (__builtin_isnormal (__x))
253 #else
254 #define fpclassify(__x) \
255 ((sizeof(__x) == sizeof(float)) ? __fpclassifyf(__x) : \
256 __fpclassifyd(__x))
257 #ifndef isfinite
258 #define isfinite(__y) \
259 (__extension__ ({int __cy = fpclassify(__y); \
260 __cy != FP_INFINITE && __cy != FP_NAN;}))
261 #endif
262 #ifndef isinf
263 #define isinf(__x) (fpclassify(__x) == FP_INFINITE)
264 #endif
265 #ifndef isnan
266 #define isnan(__x) (fpclassify(__x) == FP_NAN)
267 #endif
268 #define isnormal(__x) (fpclassify(__x) == FP_NORMAL)
269 #endif
271 #if __GNUC_PREREQ (4, 0)
272 #if defined(_HAVE_LONG_DOUBLE)
273 #define signbit(__x) \
274 ((sizeof(__x) == sizeof(float)) ? __builtin_signbitf(__x) : \
275 (sizeof(__x) == sizeof(double)) ? __builtin_signbit (__x) : \
276 __builtin_signbitl(__x))
277 #else
278 #define signbit(__x) \
279 ((sizeof(__x) == sizeof(float)) ? __builtin_signbitf(__x) : \
280 __builtin_signbit (__x))
281 #endif
282 #else
283 #define signbit(__x) \
284 ((sizeof(__x) == sizeof(float)) ? __signbitf(__x) : \
285 __signbitd(__x))
286 #endif
288 #if __GNUC_PREREQ (2, 97)
289 #define isgreater(__x,__y) (__builtin_isgreater (__x, __y))
290 #define isgreaterequal(__x,__y) (__builtin_isgreaterequal (__x, __y))
291 #define isless(__x,__y) (__builtin_isless (__x, __y))
292 #define islessequal(__x,__y) (__builtin_islessequal (__x, __y))
293 #define islessgreater(__x,__y) (__builtin_islessgreater (__x, __y))
294 #define isunordered(__x,__y) (__builtin_isunordered (__x, __y))
295 #else
296 #define isgreater(x,y) \
297 (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
298 !isunordered(__x,__y) && (__x > __y);}))
299 #define isgreaterequal(x,y) \
300 (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
301 !isunordered(__x,__y) && (__x >= __y);}))
302 #define isless(x,y) \
303 (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
304 !isunordered(__x,__y) && (__x < __y);}))
305 #define islessequal(x,y) \
306 (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
307 !isunordered(__x,__y) && (__x <= __y);}))
308 #define islessgreater(x,y) \
309 (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
310 !isunordered(__x,__y) && (__x < __y || __x > __y);}))
312 #define isunordered(a,b) \
313 (__extension__ ({__typeof__(a) __a = (a); __typeof__(b) __b = (b); \
314 fpclassify(__a) == FP_NAN || fpclassify(__b) == FP_NAN;}))
315 #endif
317 /* Non ANSI double precision functions. */
319 extern double infinity (void);
320 extern double nan (const char *);
321 extern double copysign (double, double);
322 extern double logb (double);
323 extern int ilogb (double);
325 extern double asinh (double);
326 extern double cbrt (double);
327 extern double nextafter (double, double);
328 extern double rint (double);
329 extern double scalbn (double, int);
331 extern double exp2 (double);
332 extern double scalbln (double, long int);
333 extern double tgamma (double);
334 extern double nearbyint (double);
335 extern long int lrint (double);
336 extern long long int llrint (double);
337 extern double round (double);
338 extern long int lround (double);
339 extern long long int llround (double);
340 extern double trunc (double);
341 extern double remquo (double, double, int *);
342 extern double fdim (double, double);
343 extern double fmax (double, double);
344 extern double fmin (double, double);
345 extern double fma (double, double, double);
347 #ifndef __math_68881
348 extern double log1p (double);
349 extern double expm1 (double);
350 #endif /* ! defined (__math_68881) */
352 #ifndef _REENT_ONLY
353 extern double acosh (double);
354 extern double atanh (double);
355 extern double remainder (double, double);
356 extern double gamma (double);
357 extern double lgamma (double);
358 extern double erf (double);
359 extern double erfc (double);
360 extern double log2 (double);
361 #if !defined(__cplusplus)
362 #define log2(x) (log (x) / _M_LN2)
363 #endif
365 #ifndef __math_68881
366 extern double hypot (double, double);
367 #endif
369 #endif /* ! defined (_REENT_ONLY) */
371 /* Single precision versions of ANSI functions. */
373 extern float atanf (float);
374 extern float cosf (float);
375 extern float sinf (float);
376 extern float tanf (float);
377 extern float tanhf (float);
378 extern float frexpf (float, int *);
379 extern float modff (float, float *);
380 extern float ceilf (float);
381 extern float fabsf (float);
382 extern float floorf (float);
384 #ifndef _REENT_ONLY
385 extern float acosf (float);
386 extern float asinf (float);
387 extern float atan2f (float, float);
388 extern float coshf (float);
389 extern float sinhf (float);
390 extern float expf (float);
391 extern float ldexpf (float, int);
392 extern float logf (float);
393 extern float log10f (float);
394 extern float powf (float, float);
395 extern float sqrtf (float);
396 extern float fmodf (float, float);
397 #endif /* ! defined (_REENT_ONLY) */
399 /* Other single precision functions. */
401 extern float exp2f (float);
402 extern float scalblnf (float, long int);
403 extern float tgammaf (float);
404 extern float nearbyintf (float);
405 extern long int lrintf (float);
406 extern long long int llrintf (float);
407 extern float roundf (float);
408 extern long int lroundf (float);
409 extern long long int llroundf (float);
410 extern float truncf (float);
411 extern float remquof (float, float, int *);
412 extern float fdimf (float, float);
413 extern float fmaxf (float, float);
414 extern float fminf (float, float);
415 extern float fmaf (float, float, float);
417 extern float infinityf (void);
418 extern float nanf (const char *);
419 extern float copysignf (float, float);
420 extern float logbf (float);
421 extern int ilogbf (float);
423 extern float asinhf (float);
424 extern float cbrtf (float);
425 extern float nextafterf (float, float);
426 extern float rintf (float);
427 extern float scalbnf (float, int);
428 extern float log1pf (float);
429 extern float expm1f (float);
431 #ifndef _REENT_ONLY
432 extern float acoshf (float);
433 extern float atanhf (float);
434 extern float remainderf (float, float);
435 extern float gammaf (float);
436 extern float lgammaf (float);
437 extern float erff (float);
438 extern float erfcf (float);
439 extern float log2f (float);
440 extern float hypotf (float, float);
441 #endif /* ! defined (_REENT_ONLY) */
443 /* Newlib doesn't fully support long double math functions so far.
444 On platforms where long double equals double the long double functions
445 simply call the double functions. On Cygwin the long double functions
446 are implemented independently from newlib to be able to use optimized
447 assembler functions despite using the Microsoft x86_64 ABI. */
448 #if defined (_LDBL_EQ_DBL) || defined (__CYGWIN__) || \
449 defined(__aarch64__) || defined(__i386__) || defined(__x86_64__) || \
450 defined(__riscv)
451 /* Reentrant ANSI C functions. */
452 #ifndef __math_68881
453 extern long double atanl (long double);
454 extern long double cosl (long double);
455 extern long double sinl (long double);
456 extern long double tanl (long double);
457 extern long double tanhl (long double);
458 extern long double frexpl (long double, int *);
459 extern long double modfl (long double, long double *);
460 extern long double ceill (long double);
461 extern long double fabsl (long double);
462 extern long double floorl (long double);
463 extern long double log1pl (long double);
464 extern long double expm1l (long double);
465 #endif /* ! defined (__math_68881) */
466 /* Non reentrant ANSI C functions. */
467 #ifndef _REENT_ONLY
468 #ifndef __math_68881
469 extern long double acosl (long double);
470 extern long double asinl (long double);
471 extern long double atan2l (long double, long double);
472 extern long double coshl (long double);
473 extern long double sinhl (long double);
474 extern long double expl (long double);
475 extern long double ldexpl (long double, int);
476 extern long double logl (long double);
477 extern long double log10l (long double);
478 extern long double powl (long double, long double);
479 extern long double sqrtl (long double);
480 extern long double fmodl (long double, long double);
481 extern long double hypotl (long double, long double);
482 #endif /* ! defined (__math_68881) */
483 #endif /* ! defined (_REENT_ONLY) */
484 extern long double copysignl (long double, long double);
485 extern long double nanl (const char *);
486 extern int ilogbl (long double);
487 extern long double asinhl (long double);
488 extern long double cbrtl (long double);
489 extern long double nextafterl (long double, long double);
490 extern float nexttowardf (float, long double);
491 extern double nexttoward (double, long double);
492 extern long double nexttowardl (long double, long double);
493 extern long double logbl (long double);
494 extern long double log2l (long double);
495 extern long double rintl (long double);
496 extern long double scalbnl (long double, int);
497 extern long double exp2l (long double);
498 extern long double scalblnl (long double, long);
499 extern long double tgammal (long double);
500 extern long double nearbyintl (long double);
501 extern long int lrintl (long double);
502 extern long long int llrintl (long double);
503 extern long double roundl (long double);
504 extern long lroundl (long double);
505 extern long long int llroundl (long double);
506 extern long double truncl (long double);
507 extern long double remquol (long double, long double, int *);
508 extern long double fdiml (long double, long double);
509 extern long double fmaxl (long double, long double);
510 extern long double fminl (long double, long double);
511 extern long double fmal (long double, long double, long double);
512 #ifndef _REENT_ONLY
513 extern long double acoshl (long double);
514 extern long double atanhl (long double);
515 extern long double remainderl (long double, long double);
516 extern long double lgammal (long double);
517 extern long double erfl (long double);
518 extern long double erfcl (long double);
519 #endif /* ! defined (_REENT_ONLY) */
520 #else /* !_LDBL_EQ_DBL && !__CYGWIN__ */
521 extern long double hypotl (long double, long double);
522 extern long double sqrtl (long double);
523 extern long double frexpl (long double, int *);
524 #ifdef __i386__
525 /* Other long double precision functions. */
526 extern _LONG_DOUBLE rintl (_LONG_DOUBLE);
527 extern long int lrintl (_LONG_DOUBLE);
528 extern long long int llrintl (_LONG_DOUBLE);
529 #endif /* __i386__ */
530 #endif /* !_LDBL_EQ_DBL && !__CYGWIN__ */
532 #endif /* __ISO_C_VISIBLE >= 1999 */
534 #if __MISC_VISIBLE
535 extern double drem (double, double);
536 extern float dremf (float, float);
537 #ifdef __CYGWIN__
538 extern float dreml (long double, long double);
539 #endif /* __CYGWIN__ */
540 extern double gamma_r (double, int *);
541 extern double lgamma_r (double, int *);
542 extern float gammaf_r (float, int *);
543 extern float lgammaf_r (float, int *);
544 #endif
546 #if __MISC_VISIBLE || __XSI_VISIBLE
547 extern double y0 (double);
548 extern double y1 (double);
549 extern double yn (int, double);
550 extern double j0 (double);
551 extern double j1 (double);
552 extern double jn (int, double);
553 #endif
555 #if __MISC_VISIBLE || __XSI_VISIBLE >= 600
556 extern float y0f (float);
557 extern float y1f (float);
558 extern float ynf (int, float);
559 extern float j0f (float);
560 extern float j1f (float);
561 extern float jnf (int, float);
562 #endif
564 /* GNU extensions */
565 #if __GNU_VISIBLE
566 extern void sincos (double, double *, double *);
567 extern void sincosf (float, float *, float *);
568 #ifdef __CYGWIN__
569 extern void sincosl (long double, long double *, long double *);
570 #endif /* __CYGWIN__ */
571 # ifndef exp10
572 extern double exp10 (double);
573 # endif
574 # ifndef pow10
575 extern double pow10 (double);
576 # endif
577 # ifndef exp10f
578 extern float exp10f (float);
579 # endif
580 # ifndef pow10f
581 extern float pow10f (float);
582 # endif
583 #ifdef __CYGWIN__
584 # ifndef exp10l
585 extern float exp10l (float);
586 # endif
587 # ifndef pow10l
588 extern float pow10l (float);
589 # endif
590 #endif /* __CYGWIN__ */
591 #endif /* __GNU_VISIBLE */
593 #if __MISC_VISIBLE || __XSI_VISIBLE
594 /* The gamma functions use a global variable, signgam. */
595 #ifndef _REENT_ONLY
596 #define signgam (*__signgam())
597 extern int *__signgam (void);
598 #endif /* ! defined (_REENT_ONLY) */
600 #define __signgam_r(ptr) _REENT_SIGNGAM(ptr)
601 #endif /* __MISC_VISIBLE || __XSI_VISIBLE */
603 /* Useful constants. */
605 #if __BSD_VISIBLE || __XSI_VISIBLE
607 #define MAXFLOAT 3.40282347e+38F
609 #define M_E 2.7182818284590452354
610 #define M_LOG2E 1.4426950408889634074
611 #define M_LOG10E 0.43429448190325182765
612 #define M_LN2 _M_LN2
613 #define M_LN10 2.30258509299404568402
614 #define M_PI 3.14159265358979323846
615 #define M_PI_2 1.57079632679489661923
616 #define M_PI_4 0.78539816339744830962
617 #define M_1_PI 0.31830988618379067154
618 #define M_2_PI 0.63661977236758134308
619 #define M_2_SQRTPI 1.12837916709551257390
620 #define M_SQRT2 1.41421356237309504880
621 #define M_SQRT1_2 0.70710678118654752440
623 #endif
625 #if __BSD_VISIBLE
627 #define M_TWOPI (M_PI * 2.0)
628 #define M_3PI_4 2.3561944901923448370E0
629 #define M_SQRTPI 1.77245385090551602792981
630 #define M_LN2LO 1.9082149292705877000E-10
631 #define M_LN2HI 6.9314718036912381649E-1
632 #define M_SQRT3 1.73205080756887719000
633 #define M_IVLN10 0.43429448190325182765 /* 1 / log(10) */
634 #define M_LOG2_E _M_LN2
635 #define M_INVLN2 1.4426950408889633870E0 /* 1 / log(2) */
637 #endif /* __BSD_VISIBLE */
639 _END_STD_C
641 #ifdef __FAST_MATH__
642 #include <machine/fastmath.h>
643 #endif
645 #endif /* _MATH_H_ */