source: trunk/libs/newlib/src/newlib/libc/include/math.h @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 19.8 KB
Line 
1#ifndef  _MATH_H_
2
3#define  _MATH_H_
4
5#include <sys/reent.h>
6#include <sys/cdefs.h>
7#include <machine/ieeefp.h>
8#include "_ansi.h"
9
10_BEGIN_STD_C
11
12/* Natural log of 2 */
13#define _M_LN2        0.693147180559945309417
14
15#if __GNUC_PREREQ (3, 3)
16 /* gcc >= 3.3 implicitly defines builtins for HUGE_VALx values.  */
17
18# ifndef HUGE_VAL
19#  define HUGE_VAL (__builtin_huge_val())
20# endif
21
22# ifndef HUGE_VALF
23#  define HUGE_VALF (__builtin_huge_valf())
24# endif
25
26# ifndef HUGE_VALL
27#  define HUGE_VALL (__builtin_huge_vall())
28# endif
29
30# ifndef INFINITY
31#  define INFINITY (__builtin_inff())
32# endif
33
34# ifndef NAN
35#  define NAN (__builtin_nanf(""))
36# endif
37
38#else /* !gcc >= 3.3  */
39
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.  */
51
52 #ifndef HUGE_VAL
53  #define HUGE_VAL (1.0e999999999)
54 #endif
55
56 #ifndef HUGE_VALF
57  #define HUGE_VALF (1.0e999999999F)
58 #endif
59
60 #if !defined(HUGE_VALL)  &&  defined(_HAVE_LONG_DOUBLE)
61  #define HUGE_VALL (1.0e999999999L)
62 #endif
63
64 #if !defined(INFINITY)
65  #define INFINITY (HUGE_VALF)
66 #endif
67
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
80
81#endif /* !gcc >= 3.3  */
82
83/* Reentrant ANSI C functions.  */
84
85#ifndef __math_68881
86extern double atan (double);
87extern double cos (double);
88extern double sin (double);
89extern double tan (double);
90extern double tanh (double);
91extern double frexp (double, int *);
92extern double modf (double, double *);
93extern double ceil (double);
94extern double fabs (double);
95extern double floor (double);
96#endif /* ! defined (__math_68881) */
97
98/* Non reentrant ANSI C functions.  */
99
100#ifndef _REENT_ONLY
101#ifndef __math_68881
102extern double acos (double);
103extern double asin (double);
104extern double atan2 (double, double);
105extern double cosh (double);
106extern double sinh (double);
107extern double exp (double);
108extern double ldexp (double, int);
109extern double log (double);
110extern double log10 (double);
111extern double pow (double, double);
112extern double sqrt (double);
113extern double fmod (double, double);
114#endif /* ! defined (__math_68881) */
115#endif /* ! defined (_REENT_ONLY) */
116
117#if __MISC_VISIBLE
118extern int finite (double);
119extern int finitef (float);
120extern int finitel (long double);
121extern int isinff (float);
122extern int isnanf (float);
123#ifdef __CYGWIN__ /* not implemented in newlib yet */
124extern int isinfl (long double);
125extern int isnanl (long double);
126#endif
127#if !defined(__cplusplus) || __cplusplus < 201103L
128extern int isinf (double);
129#endif
130#endif /* __MISC_VISIBLE */
131#if (__MISC_VISIBLE || (__XSI_VISIBLE && __XSI_VISIBLE < 600)) \
132  && (!defined(__cplusplus) || __cplusplus < 201103L)
133extern int isnan (double);
134#endif
135
136#if __ISO_C_VISIBLE >= 1999
137/* ISO C99 types and macros. */
138
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  #if FLT_EVAL_METHOD == 0
150    typedef float  float_t;
151    typedef double double_t;
152   #elif FLT_EVAL_METHOD == 1
153    typedef double float_t;
154    typedef double double_t;
155   #elif FLT_EVAL_METHOD == 2
156    typedef long double float_t;
157    typedef long double double_t;
158   #else
159    /* Implementation-defined.  Assume float_t and double_t have been
160     * defined previously for this configuration (e.g. config.h). */
161  #endif
162#else
163    /* Assume basic definitions.  */
164    typedef float  float_t;
165    typedef double double_t;
166#endif
167#if defined(__TMP_FLT_EVAL_METHOD)
168  #undef FLT_EVAL_METHOD
169#endif
170
171#define FP_NAN         0
172#define FP_INFINITE    1
173#define FP_ZERO        2
174#define FP_SUBNORMAL   3
175#define FP_NORMAL      4
176
177#ifndef FP_ILOGB0
178# define FP_ILOGB0 (-__INT_MAX__)
179#endif
180#ifndef FP_ILOGBNAN
181# define FP_ILOGBNAN __INT_MAX__
182#endif
183
184#ifndef MATH_ERRNO
185# define MATH_ERRNO 1
186#endif
187#ifndef MATH_ERREXCEPT
188# define MATH_ERREXCEPT 2
189#endif
190#ifndef math_errhandling
191# define math_errhandling MATH_ERRNO
192#endif
193
194extern int __isinff (float x);
195extern int __isinfd (double x);
196extern int __isnanf (float x);
197extern int __isnand (double x);
198extern int __fpclassifyf (float x);
199extern int __fpclassifyd (double x);
200extern int __signbitf (float x);
201extern int __signbitd (double x);
202
203/* Note: isinf and isnan were once functions in newlib that took double
204 *       arguments.  C99 specifies that these names are reserved for macros
205 *       supporting multiple floating point types.  Thus, they are
206 *       now defined as macros.  Implementations of the old functions
207 *       taking double arguments still exist for compatibility purposes
208 *       (prototypes for them are earlier in this header).  */
209
210#if __GNUC_PREREQ (4, 4)
211  #define fpclassify(__x) (__builtin_fpclassify (FP_NAN, FP_INFINITE, \
212                                                 FP_NORMAL, FP_SUBNORMAL, \
213                                                 FP_ZERO, __x))
214  #ifndef isfinite
215    #define isfinite(__x)       (__builtin_isfinite (__x))
216  #endif
217  #ifndef isinf
218    #define isinf(__x) (__builtin_isinf_sign (__x))
219  #endif
220  #ifndef isnan
221    #define isnan(__x) (__builtin_isnan (__x))
222  #endif
223  #define isnormal(__x) (__builtin_isnormal (__x))
224#else
225  #define fpclassify(__x) \
226          ((sizeof(__x) == sizeof(float))  ? __fpclassifyf(__x) : \
227          __fpclassifyd(__x))
228  #ifndef isfinite
229    #define isfinite(__y) \
230            (__extension__ ({int __cy = fpclassify(__y); \
231                             __cy != FP_INFINITE && __cy != FP_NAN;}))
232  #endif
233  #ifndef isinf
234    #define isinf(__x) (fpclassify(__x) == FP_INFINITE)
235  #endif
236  #ifndef isnan
237    #define isnan(__x) (fpclassify(__x) == FP_NAN)
238  #endif
239  #define isnormal(__x) (fpclassify(__x) == FP_NORMAL)
240#endif
241
242#if __GNUC_PREREQ (4, 0)
243  #if defined(_HAVE_LONG_DOUBLE)
244    #define signbit(__x) \
245            ((sizeof(__x) == sizeof(float))  ? __builtin_signbitf(__x) : \
246             (sizeof(__x) == sizeof(double)) ? __builtin_signbit (__x) : \
247                                               __builtin_signbitl(__x))
248  #else
249    #define signbit(__x) \
250            ((sizeof(__x) == sizeof(float))  ? __builtin_signbitf(__x) : \
251                                               __builtin_signbit (__x))
252  #endif
253#else
254  #define signbit(__x) \
255          ((sizeof(__x) == sizeof(float))  ?  __signbitf(__x) : \
256                  __signbitd(__x))
257#endif
258
259#if __GNUC_PREREQ (2, 97)
260#define isgreater(__x,__y)      (__builtin_isgreater (__x, __y))
261#define isgreaterequal(__x,__y) (__builtin_isgreaterequal (__x, __y))
262#define isless(__x,__y)         (__builtin_isless (__x, __y))
263#define islessequal(__x,__y)    (__builtin_islessequal (__x, __y))
264#define islessgreater(__x,__y)  (__builtin_islessgreater (__x, __y))
265#define isunordered(__x,__y)    (__builtin_isunordered (__x, __y))
266#else
267#define isgreater(x,y) \
268          (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
269                           !isunordered(__x,__y) && (__x > __y);}))
270#define isgreaterequal(x,y) \
271          (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
272                           !isunordered(__x,__y) && (__x >= __y);}))
273#define isless(x,y) \
274          (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
275                           !isunordered(__x,__y) && (__x < __y);}))
276#define islessequal(x,y) \
277          (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
278                           !isunordered(__x,__y) && (__x <= __y);}))
279#define islessgreater(x,y) \
280          (__extension__ ({__typeof__(x) __x = (x); __typeof__(y) __y = (y); \
281                           !isunordered(__x,__y) && (__x < __y || __x > __y);}))
282
283#define isunordered(a,b) \
284          (__extension__ ({__typeof__(a) __a = (a); __typeof__(b) __b = (b); \
285                           fpclassify(__a) == FP_NAN || fpclassify(__b) == FP_NAN;}))
286#endif
287
288/* Non ANSI double precision functions.  */
289
290extern double infinity (void);
291extern double nan (const char *);
292extern double copysign (double, double);
293extern double logb (double);
294extern int ilogb (double);
295
296extern double asinh (double);
297extern double cbrt (double);
298extern double nextafter (double, double);
299extern double rint (double);
300extern double scalbn (double, int);
301
302extern double exp2 (double);
303extern double scalbln (double, long int);
304extern double tgamma (double);
305extern double nearbyint (double);
306extern long int lrint (double);
307extern long long int llrint (double);
308extern double round (double);
309extern long int lround (double);
310extern long long int llround (double);
311extern double trunc (double);
312extern double remquo (double, double, int *);
313extern double fdim (double, double);
314extern double fmax (double, double);
315extern double fmin (double, double);
316extern double fma (double, double, double);
317
318#ifndef __math_68881
319extern double log1p (double);
320extern double expm1 (double);
321#endif /* ! defined (__math_68881) */
322
323#ifndef _REENT_ONLY
324extern double acosh (double);
325extern double atanh (double);
326extern double remainder (double, double);
327extern double gamma (double);
328extern double lgamma (double);
329extern double erf (double);
330extern double erfc (double);
331extern double log2 (double);
332#if !defined(__cplusplus)
333#define log2(x) (log (x) / _M_LN2)
334#endif
335
336#ifndef __math_68881
337extern double hypot (double, double);
338#endif
339
340#endif /* ! defined (_REENT_ONLY) */
341
342/* Single precision versions of ANSI functions.  */
343
344extern float atanf (float);
345extern float cosf (float);
346extern float sinf (float);
347extern float tanf (float);
348extern float tanhf (float);
349extern float frexpf (float, int *);
350extern float modff (float, float *);
351extern float ceilf (float);
352extern float fabsf (float);
353extern float floorf (float);
354
355#ifndef _REENT_ONLY
356extern float acosf (float);
357extern float asinf (float);
358extern float atan2f (float, float);
359extern float coshf (float);
360extern float sinhf (float);
361extern float expf (float);
362extern float ldexpf (float, int);
363extern float logf (float);
364extern float log10f (float);
365extern float powf (float, float);
366extern float sqrtf (float);
367extern float fmodf (float, float);
368#endif /* ! defined (_REENT_ONLY) */
369
370/* Other single precision functions.  */
371
372extern float exp2f (float);
373extern float scalblnf (float, long int);
374extern float tgammaf (float);
375extern float nearbyintf (float);
376extern long int lrintf (float);
377extern long long int llrintf (float);
378extern float roundf (float);
379extern long int lroundf (float);
380extern long long int llroundf (float);
381extern float truncf (float);
382extern float remquof (float, float, int *);
383extern float fdimf (float, float);
384extern float fmaxf (float, float);
385extern float fminf (float, float);
386extern float fmaf (float, float, float);
387
388extern float infinityf (void);
389extern float nanf (const char *);
390extern float copysignf (float, float);
391extern float logbf (float);
392extern int ilogbf (float);
393
394extern float asinhf (float);
395extern float cbrtf (float);
396extern float nextafterf (float, float);
397extern float rintf (float);
398extern float scalbnf (float, int);
399extern float log1pf (float);
400extern float expm1f (float);
401
402#ifndef _REENT_ONLY
403extern float acoshf (float);
404extern float atanhf (float);
405extern float remainderf (float, float);
406extern float gammaf (float);
407extern float lgammaf (float);
408extern float erff (float);
409extern float erfcf (float);
410extern float log2f (float);
411extern float hypotf (float, float);
412#endif /* ! defined (_REENT_ONLY) */
413
414/* Newlib doesn't fully support long double math functions so far.
415   On platforms where long double equals double the long double functions
416   simply call the double functions.  On Cygwin the long double functions
417   are implemented independently from newlib to be able to use optimized
418   assembler functions despite using the Microsoft x86_64 ABI. */
419#if defined (_LDBL_EQ_DBL) || defined (__CYGWIN__)
420/* Reentrant ANSI C functions.  */
421#ifndef __math_68881
422extern long double atanl (long double);
423extern long double cosl (long double);
424extern long double sinl (long double);
425extern long double tanl (long double);
426extern long double tanhl (long double);
427extern long double frexpl (long double, int *);
428extern long double modfl (long double, long double *);
429extern long double ceill (long double);
430extern long double fabsl (long double);
431extern long double floorl (long double);
432extern long double log1pl (long double);
433extern long double expm1l (long double);
434#endif /* ! defined (__math_68881) */
435/* Non reentrant ANSI C functions.  */
436#ifndef _REENT_ONLY
437#ifndef __math_68881
438extern long double acosl (long double);
439extern long double asinl (long double);
440extern long double atan2l (long double, long double);
441extern long double coshl (long double);
442extern long double sinhl (long double);
443extern long double expl (long double);
444extern long double ldexpl (long double, int);
445extern long double logl (long double);
446extern long double log10l (long double);
447extern long double powl (long double, long double);
448extern long double sqrtl (long double);
449extern long double fmodl (long double, long double);
450extern long double hypotl (long double, long double);
451#endif /* ! defined (__math_68881) */
452#endif /* ! defined (_REENT_ONLY) */
453extern long double copysignl (long double, long double);
454extern long double nanl (const char *);
455extern int ilogbl (long double);
456extern long double asinhl (long double);
457extern long double cbrtl (long double);
458extern long double nextafterl (long double, long double);
459extern float nexttowardf (float, long double);
460extern double nexttoward (double, long double);
461extern long double nexttowardl (long double, long double);
462extern long double logbl (long double);
463extern long double log2l (long double);
464extern long double rintl (long double);
465extern long double scalbnl (long double, int);
466extern long double exp2l (long double);
467extern long double scalblnl (long double, long);
468extern long double tgammal (long double);
469extern long double nearbyintl (long double);
470extern long int lrintl (long double);
471extern long long int llrintl (long double);
472extern long double roundl (long double);
473extern long lroundl (long double);
474extern long long int llroundl (long double);
475extern long double truncl (long double);
476extern long double remquol (long double, long double, int *);
477extern long double fdiml (long double, long double);
478extern long double fmaxl (long double, long double);
479extern long double fminl (long double, long double);
480extern long double fmal (long double, long double, long double);
481#ifndef _REENT_ONLY
482extern long double acoshl (long double);
483extern long double atanhl (long double);
484extern long double remainderl (long double, long double);
485extern long double lgammal (long double);
486extern long double erfl (long double);
487extern long double erfcl (long double);
488#endif /* ! defined (_REENT_ONLY) */
489#else /* !_LDBL_EQ_DBL && !__CYGWIN__ */
490extern long double hypotl (long double, long double);
491extern long double sqrtl (long double);
492#ifdef __i386__
493/* Other long double precision functions.  */
494extern _LONG_DOUBLE rintl (_LONG_DOUBLE);
495extern long int lrintl (_LONG_DOUBLE);
496extern long long int llrintl (_LONG_DOUBLE);
497#endif /* __i386__ */
498#endif /* !_LDBL_EQ_DBL && !__CYGWIN__ */
499
500#endif /* __ISO_C_VISIBLE >= 1999 */
501
502#if __MISC_VISIBLE
503extern double drem (double, double);
504extern float dremf (float, float);
505#ifdef __CYGWIN__
506extern float dreml (long double, long double);
507#endif /* __CYGWIN__ */
508extern double gamma_r (double, int *);
509extern double lgamma_r (double, int *);
510extern float gammaf_r (float, int *);
511extern float lgammaf_r (float, int *);
512#endif
513
514#if __MISC_VISIBLE || __XSI_VISIBLE
515extern double y0 (double);
516extern double y1 (double);
517extern double yn (int, double);
518extern double j0 (double);
519extern double j1 (double);
520extern double jn (int, double);
521#endif
522
523#if __MISC_VISIBLE || __XSI_VISIBLE >= 600
524extern float y0f (float);
525extern float y1f (float);
526extern float ynf (int, float);
527extern float j0f (float);
528extern float j1f (float);
529extern float jnf (int, float);
530#endif
531
532/* GNU extensions */
533#if __GNU_VISIBLE
534extern void sincos (double, double *, double *);
535extern void sincosf (float, float *, float *);
536#ifdef __CYGWIN__
537extern void sincosl (long double, long double *, long double *);
538#endif /* __CYGWIN__ */
539# ifndef exp10
540extern double exp10 (double);
541# endif
542# ifndef pow10
543extern double pow10 (double);
544# endif
545# ifndef exp10f
546extern float exp10f (float);
547# endif
548# ifndef pow10f
549extern float pow10f (float);
550# endif
551#ifdef __CYGWIN__
552# ifndef exp10l
553extern float exp10l (float);
554# endif
555# ifndef pow10l
556extern float pow10l (float);
557# endif
558#endif /* __CYGWIN__ */
559#endif /* __GNU_VISIBLE */
560
561#if __MISC_VISIBLE || __XSI_VISIBLE
562/* The gamma functions use a global variable, signgam.  */
563#ifndef _REENT_ONLY
564#define signgam (*__signgam())
565extern int *__signgam (void);
566#endif /* ! defined (_REENT_ONLY) */
567
568#define __signgam_r(ptr) _REENT_SIGNGAM(ptr)
569#endif /* __MISC_VISIBLE || __XSI_VISIBLE */
570
571#if __SVID_VISIBLE
572/* The exception structure passed to the matherr routine.  */
573/* We have a problem when using C++ since `exception' is a reserved
574   name in C++.  */
575#ifdef __cplusplus
576struct __exception
577#else
578struct exception
579#endif
580{
581  int type;
582  char *name;
583  double arg1;
584  double arg2;
585  double retval;
586  int err;
587};
588
589#ifdef __cplusplus
590extern int matherr (struct __exception *e);
591#else
592extern int matherr (struct exception *e);
593#endif
594
595/* Values for the type field of struct exception.  */
596
597#define DOMAIN 1
598#define SING 2
599#define OVERFLOW 3
600#define UNDERFLOW 4
601#define TLOSS 5
602#define PLOSS 6
603
604#endif /* __SVID_VISIBLE */
605
606/* Useful constants.  */
607
608#if __BSD_VISIBLE || __XSI_VISIBLE
609
610#define MAXFLOAT        3.40282347e+38F
611
612#define M_E             2.7182818284590452354
613#define M_LOG2E         1.4426950408889634074
614#define M_LOG10E        0.43429448190325182765
615#define M_LN2           _M_LN2
616#define M_LN10          2.30258509299404568402
617#define M_PI            3.14159265358979323846
618#define M_PI_2          1.57079632679489661923
619#define M_PI_4          0.78539816339744830962
620#define M_1_PI          0.31830988618379067154
621#define M_2_PI          0.63661977236758134308
622#define M_2_SQRTPI      1.12837916709551257390
623#define M_SQRT2         1.41421356237309504880
624#define M_SQRT1_2       0.70710678118654752440
625
626#endif
627
628#if __BSD_VISIBLE
629
630#define M_TWOPI         (M_PI * 2.0)
631#define M_3PI_4         2.3561944901923448370E0
632#define M_SQRTPI        1.77245385090551602792981
633#define M_LN2LO         1.9082149292705877000E-10
634#define M_LN2HI         6.9314718036912381649E-1
635#define M_SQRT3 1.73205080756887719000
636#define M_IVLN10        0.43429448190325182765 /* 1 / log(10) */
637#define M_LOG2_E        _M_LN2
638#define M_INVLN2        1.4426950408889633870E0  /* 1 / log(2) */
639
640/* Global control over fdlibm error handling.  */
641
642enum __fdlibm_version
643{
644  __fdlibm_ieee = -1,
645  __fdlibm_svid,
646  __fdlibm_xopen,
647  __fdlibm_posix
648};
649
650#define _LIB_VERSION_TYPE enum __fdlibm_version
651#define _LIB_VERSION __fdlib_version
652
653extern __IMPORT _LIB_VERSION_TYPE _LIB_VERSION;
654
655#define _IEEE_  __fdlibm_ieee
656#define _SVID_  __fdlibm_svid
657#define _XOPEN_ __fdlibm_xopen
658#define _POSIX_ __fdlibm_posix
659
660#endif /* __BSD_VISIBLE */
661
662_END_STD_C
663
664#ifdef __FAST_MATH__
665#include <machine/fastmath.h>
666#endif
667
668#endif /* _MATH_H_ */
Note: See TracBrowser for help on using the repository browser.