OSDN Git Service

math: add fp_arch.h with fp_barrier and fp_force_eval
[android-x86/external-musl-libc.git] / src / internal / libm.h
index 67c42b9..5669c04 100644 (file)
-/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
-/*
- * ====================================================
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
- *
- * Developed at SunPro, a Sun Microsystems, Inc. business.
- * Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
- * is preserved.
- * ====================================================
- */
-
 #ifndef _LIBM_H
 #define _LIBM_H
 
 #include <stdint.h>
 #include <float.h>
 #include <math.h>
-#include <complex.h>
+#include <endian.h>
+#include "fp_arch.h"
+
+#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
+#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
+union ldshape {
+       long double f;
+       struct {
+               uint64_t m;
+               uint16_t se;
+       } i;
+};
+#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
+/* This is the m68k variant of 80-bit long double, and this definition only works
+ * on archs where the alignment requirement of uint64_t is <= 4. */
+union ldshape {
+       long double f;
+       struct {
+               uint16_t se;
+               uint16_t pad;
+               uint64_t m;
+       } i;
+};
+#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
+union ldshape {
+       long double f;
+       struct {
+               uint64_t lo;
+               uint32_t mid;
+               uint16_t top;
+               uint16_t se;
+       } i;
+       struct {
+               uint64_t lo;
+               uint64_t hi;
+       } i2;
+};
+#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
+union ldshape {
+       long double f;
+       struct {
+               uint16_t se;
+               uint16_t top;
+               uint32_t mid;
+               uint64_t lo;
+       } i;
+       struct {
+               uint64_t hi;
+               uint64_t lo;
+       } i2;
+};
+#else
+#error Unsupported long double representation
+#endif
 
-#include "longdbl.h"
+/* fp_barrier returns its input, but limits code transformations
+   as if it had a side-effect (e.g. observable io) and returned
+   an arbitrary value.  */
+
+#ifndef fp_barrierf
+#define fp_barrierf fp_barrierf
+static inline float fp_barrierf(float x)
+{
+       volatile float y = x;
+       return y;
+}
+#endif
 
-#include "libc.h"
+#ifndef fp_barrier
+#define fp_barrier fp_barrier
+static inline double fp_barrier(double x)
+{
+       volatile double y = x;
+       return y;
+}
+#endif
 
-union fshape {
-       float value;
-       uint32_t bits;
-};
+#ifndef fp_barrierl
+#define fp_barrierl fp_barrierl
+static inline long double fp_barrierl(long double x)
+{
+       volatile long double y = x;
+       return y;
+}
+#endif
 
-union dshape {
-       double value;
-       uint64_t bits;
-};
+/* fp_force_eval ensures that the input value is computed when that's
+   otherwise unused.  To prevent the constant folding of the input
+   expression, an additional fp_barrier may be needed or a compilation
+   mode that does so (e.g. -frounding-math in gcc). Then it can be
+   used to evaluate an expression for its fenv side-effects only.   */
+
+#ifndef fp_force_evalf
+#define fp_force_evalf fp_force_evalf
+static inline void fp_force_evalf(float x)
+{
+       volatile float y = x;
+}
+#endif
 
-/* Get two 32 bit ints from a double.  */
-#define EXTRACT_WORDS(hi,lo,d)                                  \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.value = (d);                                              \
-  (hi) = __u.bits >> 32;                                        \
-  (lo) = (uint32_t)__u.bits;                                    \
-} while (0)
+#ifndef fp_force_eval
+#define fp_force_eval fp_force_eval
+static inline void fp_force_eval(double x)
+{
+       volatile double y = x;
+}
+#endif
 
-/* Get a 64 bit int from a double.  */
-#define EXTRACT_WORD64(i,d)                                     \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.value = (d);                                              \
-  (i) = __u.bits;                                               \
-} while (0)
+#ifndef fp_force_evall
+#define fp_force_evall fp_force_evall
+static inline void fp_force_evall(long double x)
+{
+       volatile long double y = x;
+}
+#endif
 
-/* Get the more significant 32 bit int from a double.  */
-#define GET_HIGH_WORD(i,d)                                      \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.value = (d);                                              \
-  (i) = __u.bits >> 32;                                         \
+#define FORCE_EVAL(x) do {                        \
+       if (sizeof(x) == sizeof(float)) {         \
+               fp_force_evalf(x);                \
+       } else if (sizeof(x) == sizeof(double)) { \
+               fp_force_eval(x);                 \
+       } else {                                  \
+               fp_force_evall(x);                \
+       }                                         \
+} while(0)
+
+#define asuint(f) ((union{float _f; uint32_t _i;}){f})._i
+#define asfloat(i) ((union{uint32_t _i; float _f;}){i})._f
+#define asuint64(f) ((union{double _f; uint64_t _i;}){f})._i
+#define asdouble(i) ((union{uint64_t _i; double _f;}){i})._f
+
+#define EXTRACT_WORDS(hi,lo,d)                    \
+do {                                              \
+  uint64_t __u = asuint64(d);                     \
+  (hi) = __u >> 32;                               \
+  (lo) = (uint32_t)__u;                           \
 } while (0)
 
-/* Get the less significant 32 bit int from a double.  */
-#define GET_LOW_WORD(i,d)                                       \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.value = (d);                                              \
-  (i) = (uint32_t)__u.bits;                                     \
+#define GET_HIGH_WORD(hi,d)                       \
+do {                                              \
+  (hi) = asuint64(d) >> 32;                       \
 } while (0)
 
-/* Set a double from two 32 bit ints.  */
-#define INSERT_WORDS(d,hi,lo)                                   \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.bits = ((uint64_t)(hi) << 32) | (uint32_t)(lo);           \
-  (d) = __u.value;                                              \
+#define GET_LOW_WORD(lo,d)                        \
+do {                                              \
+  (lo) = (uint32_t)asuint64(d);                   \
 } while (0)
 
-/* Set a double from a 64 bit int.  */
-#define INSERT_WORD64(d,i)                                      \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.bits = (i);                                               \
-  (d) = __u.value;                                              \
+#define INSERT_WORDS(d,hi,lo)                     \
+do {                                              \
+  (d) = asdouble(((uint64_t)(hi)<<32) | (uint32_t)(lo)); \
 } while (0)
 
-/* Set the more significant 32 bits of a double from an int.  */
-#define SET_HIGH_WORD(d,hi)                                     \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.value = (d);                                              \
-  __u.bits &= 0xffffffff;                                       \
-  __u.bits |= (uint64_t)(hi) << 32;                             \
-  (d) = __u.value;                                              \
-} while (0)
+#define SET_HIGH_WORD(d,hi)                       \
+  INSERT_WORDS(d, hi, (uint32_t)asuint64(d))
 
-/* Set the less significant 32 bits of a double from an int.  */
-#define SET_LOW_WORD(d,lo)                                      \
-do {                                                            \
-  union dshape __u;                                             \
-  __u.value = (d);                                              \
-  __u.bits &= 0xffffffff00000000ull;                            \
-  __u.bits |= (uint32_t)(lo);                                   \
-  (d) = __u.value;                                              \
-} while (0)
+#define SET_LOW_WORD(d,lo)                        \
+  INSERT_WORDS(d, asuint64(d)>>32, lo)
 
-/* Get a 32 bit int from a float.  */
-#define GET_FLOAT_WORD(i,d)                                     \
-do {                                                            \
-  union fshape __u;                                             \
-  __u.value = (d);                                              \
-  (i) = __u.bits;                                               \
+#define GET_FLOAT_WORD(w,d)                       \
+do {                                              \
+  (w) = asuint(d);                                \
 } while (0)
 
-/* Set a float from a 32 bit int.  */
-#define SET_FLOAT_WORD(d,i)                                     \
-do {                                                            \
-  union fshape __u;                                             \
-  __u.bits = (i);                                               \
-  (d) = __u.value;                                              \
+#define SET_FLOAT_WORD(d,w)                       \
+do {                                              \
+  (d) = asfloat(w);                               \
 } while (0)
 
-/* fdlibm kernel functions */
-
-int    __rem_pio2_large(double*,double*,int,int,int);
-
-int    __rem_pio2(double,double*);
-double __sin(double,double,int);
-double __cos(double,double);
-double __tan(double,double,int);
-double __expo2(double);
-double complex __ldexp_cexp(double complex,int);
-
-int    __rem_pio2f(float,double*);
-float  __sindf(double);
-float  __cosdf(double);
-float  __tandf(double,int);
-float  __expo2f(float);
-float complex __ldexp_cexpf(float complex,int);
-
-int __rem_pio2l(long double, long double *);
-long double __sinl(long double, long double, int);
-long double __cosl(long double, long double);
-long double __tanl(long double, long double, int);
-
-/* polynomial evaluation */
-long double __polevll(long double, const long double *, int);
-long double __p1evll(long double, const long double *, int);
-
-// FIXME: not needed when -fexcess-precision=standard is supported (>=gcc4.5)
-/*
- * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
- */
-#if 1
-#define STRICT_ASSIGN(type, lval, rval) do {    \
-        volatile type __v = (rval);             \
-        (lval) = __v;                           \
-} while (0)
-#else
-#define STRICT_ASSIGN(type, lval, rval) ((lval) = (type)(rval))
-#endif
+hidden int    __rem_pio2_large(double*,double*,int,int,int);
 
+hidden int    __rem_pio2(double,double*);
+hidden double __sin(double,double,int);
+hidden double __cos(double,double);
+hidden double __tan(double,double,int);
+hidden double __expo2(double);
 
-/* complex */
+hidden int    __rem_pio2f(float,double*);
+hidden float  __sindf(double);
+hidden float  __cosdf(double);
+hidden float  __tandf(double,int);
+hidden float  __expo2f(float);
 
-union dcomplex {
-       double complex z;
-       double a[2];
-};
-union fcomplex {
-       float complex z;
-       float a[2];
-};
-union lcomplex {
-       long double complex z;
-       long double a[2];
-};
+hidden int __rem_pio2l(long double, long double *);
+hidden long double __sinl(long double, long double, int);
+hidden long double __cosl(long double, long double);
+hidden long double __tanl(long double, long double, int);
+
+hidden long double __polevll(long double, const long double *, int);
+hidden long double __p1evll(long double, const long double *, int);
 
-// FIXME: move to complex.h ?
-#define creal(z) ((double)(z))
-#define crealf(z) ((float)(z))
-#define creall(z) ((long double)(z))
-#define cimag(z) ((union dcomplex){(z)}.a[1])
-#define cimagf(z) ((union fcomplex){(z)}.a[1])
-#define cimagl(z) ((union lcomplex){(z)}.a[1])
-
-/* x + y*I is not supported properly by gcc */
-#define cpack(x,y) ((union dcomplex){.a={(x),(y)}}.z)
-#define cpackf(x,y) ((union fcomplex){.a={(x),(y)}}.z)
-#define cpackl(x,y) ((union lcomplex){.a={(x),(y)}}.z)
+extern int __signgam;
+hidden double __lgamma_r(double, int *);
+hidden float __lgammaf_r(float, int *);
 
 #endif