bump for release
[uclibc-ng.git] / libm / math_private.h
blob620ce9a44dd7dc87f54ef2d66afe88f5721d4546
1 /*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
12 #ifndef _MATH_PRIVATE_H_
13 #define _MATH_PRIVATE_H_
15 #include <endian.h>
16 #include <sys/types.h>
18 /* The original fdlibm code used statements like:
19 n0 = ((*(int*)&one)>>29)^1; * index of high word *
20 ix0 = *(n0+(int*)&x); * high word of x *
21 ix1 = *((1-n0)+(int*)&x); * low word of x *
22 to dig two 32 bit words out of the 64 bit IEEE floating point
23 value. That is non-ANSI, and, moreover, the gcc instruction
24 scheduler gets it wrong. We instead use the following macros.
25 Unlike the original code, we determine the endianness at compile
26 time, not at run time; I don't see much benefit to selecting
27 endianness at run time. */
29 /* A union which permits us to convert between a double and two 32 bit
30 ints. */
33 * Math on arm is special (read: stupid):
34 * For FPA, float words are always big-endian.
35 * For VFP, float words follow the memory system mode.
36 * For Maverick, float words are always little-endian.
39 #if !defined(__MAVERICK__) && ((__BYTE_ORDER == __BIG_ENDIAN) || \
40 (!defined(__VFP_FP__) && (defined(__arm__) || defined(__thumb__))))
42 typedef union
44 double value;
45 struct
47 u_int32_t msw;
48 u_int32_t lsw;
49 } parts;
50 } ieee_double_shape_type;
52 #else
54 typedef union
56 double value;
57 struct
59 u_int32_t lsw;
60 u_int32_t msw;
61 } parts;
62 } ieee_double_shape_type;
64 #endif
66 /* Get two 32 bit ints from a double. */
68 #define EXTRACT_WORDS(ix0,ix1,d) \
69 do { \
70 ieee_double_shape_type ew_u; \
71 ew_u.value = (d); \
72 (ix0) = ew_u.parts.msw; \
73 (ix1) = ew_u.parts.lsw; \
74 } while (0)
76 /* Get the more significant 32 bit int from a double. */
78 #define GET_HIGH_WORD(i,d) \
79 do { \
80 ieee_double_shape_type gh_u; \
81 gh_u.value = (d); \
82 (i) = gh_u.parts.msw; \
83 } while (0)
85 /* Get the less significant 32 bit int from a double. */
87 #define GET_LOW_WORD(i,d) \
88 do { \
89 ieee_double_shape_type gl_u; \
90 gl_u.value = (d); \
91 (i) = gl_u.parts.lsw; \
92 } while (0)
94 /* Set a double from two 32 bit ints. */
96 #define INSERT_WORDS(d,ix0,ix1) \
97 do { \
98 ieee_double_shape_type iw_u; \
99 iw_u.parts.msw = (ix0); \
100 iw_u.parts.lsw = (ix1); \
101 (d) = iw_u.value; \
102 } while (0)
104 /* Set the more significant 32 bits of a double from an int. */
106 #define SET_HIGH_WORD(d,v) \
107 do { \
108 ieee_double_shape_type sh_u; \
109 sh_u.value = (d); \
110 sh_u.parts.msw = (v); \
111 (d) = sh_u.value; \
112 } while (0)
114 /* Set the less significant 32 bits of a double from an int. */
116 #define SET_LOW_WORD(d,v) \
117 do { \
118 ieee_double_shape_type sl_u; \
119 sl_u.value = (d); \
120 sl_u.parts.lsw = (v); \
121 (d) = sl_u.value; \
122 } while (0)
124 /* A union which permits us to convert between a float and a 32 bit
125 int. */
127 typedef union
129 float value;
130 u_int32_t word;
131 } ieee_float_shape_type;
133 /* Get a 32 bit int from a float. */
135 #define GET_FLOAT_WORD(i,d) \
136 do { \
137 ieee_float_shape_type gf_u; \
138 gf_u.value = (d); \
139 (i) = gf_u.word; \
140 } while (0)
142 /* Set a float from a 32 bit int. */
144 #define SET_FLOAT_WORD(d,i) \
145 do { \
146 ieee_float_shape_type sf_u; \
147 sf_u.word = (i); \
148 (d) = sf_u.value; \
149 } while (0)
151 /* ieee style elementary functions */
152 extern double __ieee754_sqrt (double) attribute_hidden;
153 extern double __ieee754_acos (double) attribute_hidden;
154 extern double __ieee754_acosh (double) attribute_hidden;
155 extern double __ieee754_log (double) attribute_hidden;
156 extern double __ieee754_log2 (double) attribute_hidden;
157 extern double __ieee754_atanh (double) attribute_hidden;
158 extern double __ieee754_asin (double) attribute_hidden;
159 extern double __ieee754_atan2 (double,double) attribute_hidden;
160 extern double __ieee754_exp (double) attribute_hidden;
161 extern double __ieee754_exp10 (double) attribute_hidden;
162 extern double __ieee754_cosh (double) attribute_hidden;
163 extern double __ieee754_fmod (double,double) attribute_hidden;
164 extern double __ieee754_pow (double,double) attribute_hidden;
165 extern double __ieee754_lgamma_r (double,int *) attribute_hidden;
166 /*extern double __ieee754_gamma_r (double,int *) attribute_hidden;*/
167 extern double __ieee754_lgamma (double) attribute_hidden;
168 /*extern double __ieee754_gamma (double) attribute_hidden;*/
169 extern double __ieee754_log10 (double) attribute_hidden;
170 extern double __ieee754_sinh (double) attribute_hidden;
171 extern double __ieee754_hypot (double,double) attribute_hidden;
172 extern double __ieee754_j0 (double) attribute_hidden;
173 extern double __ieee754_j1 (double) attribute_hidden;
174 extern double __ieee754_y0 (double) attribute_hidden;
175 extern double __ieee754_y1 (double) attribute_hidden;
176 extern double __ieee754_jn (int,double) attribute_hidden;
177 extern double __ieee754_yn (int,double) attribute_hidden;
178 extern double __ieee754_remainder (double,double) attribute_hidden;
179 extern int __ieee754_rem_pio2 (double,double*) attribute_hidden;
180 extern double __ieee754_scalb (double,double) attribute_hidden;
182 /* fdlibm kernel function */
183 extern double __kernel_sin (double,double,int) attribute_hidden;
184 extern double __kernel_cos (double,double) attribute_hidden;
185 extern double __kernel_tan (double,double,int) attribute_hidden;
186 extern int __kernel_rem_pio2 (double*,double*,int,int,int,const int*) attribute_hidden;
189 * math_opt_barrier(x): safely load x, even if it was manipulated
190 * by non-floationg point operations. This macro returns the value of x.
191 * This ensures compiler does not (ab)use its knowledge about x value
192 * and don't optimize future operations. Example:
193 * float x;
194 * SET_FLOAT_WORD(x, 0x80000001); // sets a bit pattern
195 * y = math_opt_barrier(x); // "compiler, do not cheat!"
196 * y = y * y; // compiler can't optimize, must use real multiply insn
198 * math_force_eval(x): force expression x to be evaluated.
199 * Useful if otherwise compiler may eliminate the expression
200 * as unused. This macro returns no value.
201 * Example: "void fn(float f) { f = f * f; }"
202 * versus "void fn(float f) { f = f * f; math_force_eval(f); }"
204 * Currently, math_force_eval(x) stores x into
205 * a floating point register or memory *of the appropriate size*.
206 * There is no guarantee this will not change.
208 #if defined(__i386__)
209 #define math_opt_barrier(x) ({ \
210 __typeof(x) __x = (x); \
211 /* "t": load x into top-of-stack fpreg */ \
212 __asm__ ("" : "=t" (__x) : "0" (__x)); \
213 __x; \
215 #define math_force_eval(x) do { \
216 __typeof(x) __x = (x); \
217 if (sizeof(__x) <= sizeof(double)) \
218 /* "m": store x into a memory location */ \
219 __asm__ __volatile__ ("" : : "m" (__x)); \
220 else /* long double */ \
221 /* "f": load x into (any) fpreg */ \
222 __asm__ __volatile__ ("" : : "f" (__x)); \
223 } while (0)
224 #endif
226 #if defined(__x86_64__)
227 #define math_opt_barrier(x) ({ \
228 __typeof(x) __x = (x); \
229 if (sizeof(__x) <= sizeof(double)) \
230 /* "x": load into XMM SSE register */ \
231 __asm__ ("" : "=x" (__x) : "0" (__x)); \
232 else /* long double */ \
233 /* "t": load x into top-of-stack fpreg */ \
234 __asm__ ("" : "=t" (__x) : "0" (__x)); \
235 __x; \
237 #define math_force_eval(x) do { \
238 __typeof(x) __x = (x); \
239 if (sizeof(__x) <= sizeof(double)) \
240 /* "x": load into XMM SSE register */ \
241 __asm__ __volatile__ ("" : : "x" (__x)); \
242 else /* long double */ \
243 /* "f": load x into (any) fpreg */ \
244 __asm__ __volatile__ ("" : : "f" (__x)); \
245 } while (0)
246 #endif
248 /* Default implementations force store to a memory location */
249 #ifndef math_opt_barrier
250 #define math_opt_barrier(x) ({ __typeof(x) __x = (x); __asm__ ("" : "+m" (__x)); __x; })
251 #endif
252 #ifndef math_force_eval
253 #define math_force_eval(x) do { __typeof(x) __x = (x); __asm__ __volatile__ ("" : : "m" (__x)); } while (0)
254 #endif
257 #endif /* _MATH_PRIVATE_H_ */