1 /*
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 #ifndef _MATH_PRIVATE_H_
13 #define _MATH_PRIVATE_H_
14 
15 #include <endian.h>
16 #include <sys/types.h>
17 
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.  */
28 
29 /* A union which permits us to convert between a double and two 32 bit
30    ints.  */
31 
32 /*
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.
37  */
38 
39 #if !defined(__MAVERICK__) && ((__BYTE_ORDER == __BIG_ENDIAN) || \
40     (!defined(__VFP_FP__) && (defined(__arm__) || defined(__thumb__))))
41 
42 typedef union
43 {
44   double value;
45   struct
46   {
47     u_int32_t msw;
48     u_int32_t lsw;
49   } parts;
50 } ieee_double_shape_type;
51 
52 #else
53 
54 typedef union
55 {
56   double value;
57   struct
58   {
59     u_int32_t lsw;
60     u_int32_t msw;
61   } parts;
62 } ieee_double_shape_type;
63 
64 #endif
65 
66 /* Get two 32 bit ints from a double.  */
67 
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)
75 
76 /* Get the more significant 32 bit int from a double.  */
77 
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)
84 
85 /* Get the less significant 32 bit int from a double.  */
86 
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)
93 
94 /* Set a double from two 32 bit ints.  */
95 
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)
103 
104 /* Set the more significant 32 bits of a double from an int.  */
105 
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)
113 
114 /* Set the less significant 32 bits of a double from an int.  */
115 
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)
123 
124 /* A union which permits us to convert between a float and a 32 bit
125    int.  */
126 
127 typedef union
128 {
129   float value;
130   u_int32_t word;
131 } ieee_float_shape_type;
132 
133 /* Get a 32 bit int from a float.  */
134 
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)
141 
142 /* Set a float from a 32 bit int.  */
143 
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)
150 
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;
181 
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;
187 extern double __kernel_standard(double x, double y, int type) attribute_hidden;
188 extern float  __kernel_standard_f (float, float, int) attribute_hidden;
189 #ifndef __NO_LONG_DOUBLE_MATH
190 extern long double __kernel_standard_l (long double, long double, int) attribute_hidden;
191 #endif
192 /* wrappers functions for internal use */
193 extern float __lgammaf_r (float, int*);
194 extern double __lgamma_r (double, int*);
195 extern long double __lgammal_r(long double, int*);
196 extern double __ieee754_tgamma(double);
197 
198 /*
199  * math_opt_barrier(x): safely load x, even if it was manipulated
200  * by non-floationg point operations. This macro returns the value of x.
201  * This ensures compiler does not (ab)use its knowledge about x value
202  * and don't optimize future operations. Example:
203  * float x;
204  * SET_FLOAT_WORD(x, 0x80000001); // sets a bit pattern
205  * y = math_opt_barrier(x); // "compiler, do not cheat!"
206  * y = y * y; // compiler can't optimize, must use real multiply insn
207  *
208  * math_force_eval(x): force expression x to be evaluated.
209  * Useful if otherwise compiler may eliminate the expression
210  * as unused. This macro returns no value.
211  * Example: "void fn(float f) { f = f * f; }"
212  *   versus "void fn(float f) { f = f * f; math_force_eval(f); }"
213  *
214  * Currently, math_force_eval(x) stores x into
215  * a floating point register or memory *of the appropriate size*.
216  * There is no guarantee this will not change.
217  */
218 #if defined(__i386__)
219 #define math_opt_barrier(x) ({ \
220 	__typeof(x) __x = (x); \
221 	/* "t": load x into top-of-stack fpreg */ \
222 	__asm__ ("" : "=t" (__x) : "0" (__x)); \
223 	__x; \
224 })
225 #define math_force_eval(x) do {	\
226 	__typeof(x) __x = (x); \
227 	if (sizeof(__x) <= sizeof(double)) \
228 		/* "m": store x into a memory location */ \
229 		__asm__ __volatile__ ("" : : "m" (__x)); \
230 	else /* long double */ \
231 		/* "f": load x into (any) fpreg */ \
232 		__asm__ __volatile__ ("" : : "f" (__x)); \
233 } while (0)
234 #endif
235 
236 #if defined(__x86_64__)
237 #define math_opt_barrier(x) ({ \
238 	__typeof(x) __x = (x); \
239 	if (sizeof(__x) <= sizeof(double)) \
240 		/* "x": load into XMM SSE register */ \
241 		__asm__ ("" : "=x" (__x) : "0" (__x)); \
242 	else /* long double */ \
243 		/* "t": load x into top-of-stack fpreg */ \
244 		__asm__ ("" : "=t" (__x) : "0" (__x)); \
245 	__x; \
246 })
247 #define math_force_eval(x) do { \
248 	__typeof(x) __x = (x); \
249 	if (sizeof(__x) <= sizeof(double)) \
250 		/* "x": load into XMM SSE register */ \
251 		__asm__ __volatile__ ("" : : "x" (__x)); \
252 	else /* long double */ \
253 		/* "f": load x into (any) fpreg */ \
254 		__asm__ __volatile__ ("" : : "f" (__x)); \
255 } while (0)
256 #endif
257 
258 /* Default implementations force store to a memory location */
259 #ifndef math_opt_barrier
260 #define math_opt_barrier(x) ({ __typeof(x) __x = (x); __asm__ ("" : "+m" (__x)); __x; })
261 #endif
262 #ifndef math_force_eval
263 #define math_force_eval(x)  do { __typeof(x) __x = (x); __asm__ __volatile__ ("" : : "m" (__x)); } while (0)
264 #endif
265 
266 
267 #endif /* _MATH_PRIVATE_H_ */
268