1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * This file is adapted from from newlib-nano-2, the newlib/libm/common/fdlib.h,
5  * available from https://github.com/32bitmicro/newlib-nano-2.  The main change
6  * is removal of anything to do with double precision.
7  *
8  * Appropriate copyright headers are reproduced below.
9  */
10 
11 /* @(#)fdlibm.h 5.1 93/09/24 */
12 /*
13  * ====================================================
14  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
15  *
16  * Developed at SunPro, a Sun Microsystems, Inc. business.
17  * Permission to use, copy, modify, and distribute this
18  * software is freely granted, provided that this notice
19  * is preserved.
20  * ====================================================
21  */
22 
23 #include <math.h>
24 
25 /* Default to XOPEN_MODE.  */
26 #define _XOPEN_MODE
27 
28 /* Most routines need to check whether a float is finite, infinite, or not a
29    number, and many need to know whether the result of an operation will
30    overflow.  These conditions depend on whether the largest exponent is
31    used for NaNs & infinities, or whether it's used for finite numbers.  The
32    macros below wrap up that kind of information:
33 
34    FLT_UWORD_IS_FINITE(X)
35 	True if a positive float with bitmask X is finite.
36 
37    FLT_UWORD_IS_NAN(X)
38 	True if a positive float with bitmask X is not a number.
39 
40    FLT_UWORD_IS_INFINITE(X)
41 	True if a positive float with bitmask X is +infinity.
42 
43    FLT_UWORD_MAX
44 	The bitmask of FLT_MAX.
45 
46    FLT_UWORD_HALF_MAX
47 	The bitmask of FLT_MAX/2.
48 
49    FLT_UWORD_EXP_MAX
50 	The bitmask of the largest finite exponent (129 if the largest
51 	exponent is used for finite numbers, 128 otherwise).
52 
53    FLT_UWORD_LOG_MAX
54 	The bitmask of log(FLT_MAX), rounded down.  This value is the largest
55 	input that can be passed to exp() without producing overflow.
56 
57    FLT_UWORD_LOG_2MAX
58 	The bitmask of log(2*FLT_MAX), rounded down.  This value is the
59 	largest input than can be passed to cosh() without producing
60 	overflow.
61 
62    FLT_LARGEST_EXP
63 	The largest biased exponent that can be used for finite numbers
64 	(255 if the largest exponent is used for finite numbers, 254
65 	otherwise) */
66 
67 #ifdef _FLT_LARGEST_EXPONENT_IS_NORMAL
68 #define FLT_UWORD_IS_FINITE(x) 1
69 #define FLT_UWORD_IS_NAN(x) 0
70 #define FLT_UWORD_IS_INFINITE(x) 0
71 #define FLT_UWORD_MAX 0x7fffffff
72 #define FLT_UWORD_EXP_MAX 0x43010000
73 #define FLT_UWORD_LOG_MAX 0x42b2d4fc
74 #define FLT_UWORD_LOG_2MAX 0x42b437e0
75 #define HUGE ((float)0X1.FFFFFEP128)
76 #else
77 #define FLT_UWORD_IS_FINITE(x) ((x)<0x7f800000L)
78 #define FLT_UWORD_IS_NAN(x) ((x)>0x7f800000L)
79 #define FLT_UWORD_IS_INFINITE(x) ((x)==0x7f800000L)
80 #define FLT_UWORD_MAX 0x7f7fffffL
81 #define FLT_UWORD_EXP_MAX 0x43000000
82 #define FLT_UWORD_LOG_MAX 0x42b17217
83 #define FLT_UWORD_LOG_2MAX 0x42b2d4fc
84 #define HUGE ((float)3.40282346638528860e+38)
85 #endif
86 #define FLT_UWORD_HALF_MAX (FLT_UWORD_MAX-(1L<<23))
87 #define FLT_LARGEST_EXP (FLT_UWORD_MAX>>23)
88 
89 /* Many routines check for zero and subnormal numbers.  Such things depend
90    on whether the target supports denormals or not:
91 
92    FLT_UWORD_IS_ZERO(X)
93 	True if a positive float with bitmask X is +0.	Without denormals,
94 	any float with a zero exponent is a +0 representation.	With
95 	denormals, the only +0 representation is a 0 bitmask.
96 
97    FLT_UWORD_IS_SUBNORMAL(X)
98 	True if a non-zero positive float with bitmask X is subnormal.
99 	(Routines should check for zeros first.)
100 
101    FLT_UWORD_MIN
102 	The bitmask of the smallest float above +0.  Call this number
103 	REAL_FLT_MIN...
104 
105    FLT_UWORD_EXP_MIN
106 	The bitmask of the float representation of REAL_FLT_MIN's exponent.
107 
108    FLT_UWORD_LOG_MIN
109 	The bitmask of |log(REAL_FLT_MIN)|, rounding down.
110 
111    FLT_SMALLEST_EXP
112 	REAL_FLT_MIN's exponent - EXP_BIAS (1 if denormals are not supported,
113 	-22 if they are).
114 */
115 
116 #ifdef _FLT_NO_DENORMALS
117 #define FLT_UWORD_IS_ZERO(x) ((x)<0x00800000L)
118 #define FLT_UWORD_IS_SUBNORMAL(x) 0
119 #define FLT_UWORD_MIN 0x00800000
120 #define FLT_UWORD_EXP_MIN 0x42fc0000
121 #define FLT_UWORD_LOG_MIN 0x42aeac50
122 #define FLT_SMALLEST_EXP 1
123 #else
124 #define FLT_UWORD_IS_ZERO(x) ((x)==0)
125 #define FLT_UWORD_IS_SUBNORMAL(x) ((x)<0x00800000L)
126 #define FLT_UWORD_MIN 0x00000001
127 #define FLT_UWORD_EXP_MIN 0x43160000
128 #define FLT_UWORD_LOG_MIN 0x42cff1b5
129 #define FLT_SMALLEST_EXP -22
130 #endif
131 
132 #ifdef __STDC__
133 #undef __P
134 #define	__P(p)	p
135 #else
136 #define	__P(p)	()
137 #endif
138 
139 /*
140  * set X_TLOSS = pi*2**52, which is possibly defined in <values.h>
141  * (one may replace the following line by "#include <values.h>")
142  */
143 
144 #define X_TLOSS		1.41484755040568800000e+16
145 
146 /* Functions that are not documented, and are not in <math.h>.  */
147 
148 /* Undocumented float functions.  */
149 #ifdef _SCALB_INT
150 extern float scalbf __P((float, int));
151 #else
152 extern float scalbf __P((float, float));
153 #endif
154 extern float significandf __P((float));
155 
156 /* ieee style elementary float functions */
157 extern float __ieee754_sqrtf __P((float));
158 extern float __ieee754_acosf __P((float));
159 extern float __ieee754_acoshf __P((float));
160 extern float __ieee754_logf __P((float));
161 extern float __ieee754_atanhf __P((float));
162 extern float __ieee754_asinf __P((float));
163 extern float __ieee754_atan2f __P((float,float));
164 extern float __ieee754_expf __P((float));
165 extern float __ieee754_coshf __P((float));
166 extern float __ieee754_fmodf __P((float,float));
167 extern float __ieee754_powf __P((float,float));
168 extern float __ieee754_lgammaf_r __P((float,int *));
169 extern float __ieee754_gammaf_r __P((float,int *));
170 extern float __ieee754_log10f __P((float));
171 extern float __ieee754_sinhf __P((float));
172 extern float __ieee754_hypotf __P((float,float));
173 extern float __ieee754_j0f __P((float));
174 extern float __ieee754_j1f __P((float));
175 extern float __ieee754_y0f __P((float));
176 extern float __ieee754_y1f __P((float));
177 extern float __ieee754_jnf __P((int,float));
178 extern float __ieee754_ynf __P((int,float));
179 extern float __ieee754_remainderf __P((float,float));
180 extern __int32_t __ieee754_rem_pio2f __P((float,float*));
181 #ifdef _SCALB_INT
182 extern float __ieee754_scalbf __P((float,int));
183 #else
184 extern float __ieee754_scalbf __P((float,float));
185 #endif
186 
187 /* float versions of fdlibm kernel functions */
188 extern float __kernel_sinf __P((float,float,int));
189 extern float __kernel_cosf __P((float,float));
190 extern float __kernel_tanf __P((float,float,int));
191 extern int   __kernel_rem_pio2f __P((float*,float*,int,int,int,const __uint8_t*));
192 
193 /* A union which permits us to convert between a float and a 32 bit
194    int.  */
195 
196 typedef union
197 {
198   float value;
199   __uint32_t word;
200 } ieee_float_shape_type;
201 
202 /* Get a 32 bit int from a float.  */
203 
204 #define GET_FLOAT_WORD(i,d)					\
205 do {								\
206   ieee_float_shape_type gf_u;					\
207   gf_u.value = (d);						\
208   (i) = gf_u.word;						\
209 } while (0)
210 
211 /* Set a float from a 32 bit int.  */
212 
213 #define SET_FLOAT_WORD(d,i)					\
214 do {								\
215   ieee_float_shape_type sf_u;					\
216   sf_u.word = (i);						\
217   (d) = sf_u.value;						\
218 } while (0)
219 
220 /* Macros to avoid undefined behaviour that can arise if the amount
221    of a shift is exactly equal to the size of the shifted operand.  */
222 
223 #define SAFE_LEFT_SHIFT(op,amt)					\
224   (((amt) < 8 * sizeof(op)) ? ((op) << (amt)) : 0)
225 
226 #define SAFE_RIGHT_SHIFT(op,amt)				\
227   (((amt) < 8 * sizeof(op)) ? ((op) >> (amt)) : 0)
228