blob: 637f447cf1f8fd801850fca2758c3d2c2b088974 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Dave Hylandsf14b92b2014-03-12 18:06:26 -070027#include <stdint.h>
Damien Georgee90eefc2014-04-02 19:55:08 +010028#include <math.h>
29
Damien George5a166582014-03-23 00:34:49 +000030typedef float float_t;
31typedef union {
32 float f;
33 struct {
34 uint64_t m : 23;
35 uint64_t e : 8;
36 uint64_t s : 1;
37 };
38} float_s_t;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070039
Damien George5a166582014-03-23 00:34:49 +000040typedef union {
41 double d;
42 struct {
43 uint64_t m : 52;
44 uint64_t e : 11;
45 uint64_t s : 1;
46 };
47} double_s_t;
48
Damien George38ae0142014-04-18 23:28:56 +010049double __attribute__((pcs("aapcs"))) __aeabi_i2d(int32_t x) {
50 return (float)x;
51}
52
Damien George5a166582014-03-23 00:34:49 +000053double __attribute__((pcs("aapcs"))) __aeabi_f2d(float x) {
54 float_s_t fx={0};
55 double_s_t dx={0};
56
57 fx.f = x;
58 dx.s = (fx.s);
59 dx.e = (fx.e-127+1023) & 0x7FF;
60 dx.m = fx.m;
61 dx.m <<=(52-23); // left justify
62 return dx.d;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070063}
64
Damien George5a166582014-03-23 00:34:49 +000065float __attribute__((pcs("aapcs"))) __aeabi_d2f(double x) {
66 float_s_t fx={0};
67 double_s_t dx={0};
Dave Hylandsf14b92b2014-03-12 18:06:26 -070068
Damien George5a166582014-03-23 00:34:49 +000069 dx.d = x;
70 fx.s = (dx.s);
71 fx.e = (dx.e-1023+127) & 0xFF;
72 fx.m = (dx.m>>(52-23)); // right justify
73 return fx.f;
74}
75double __aeabi_dmul(double x , double y) {
76 return 0.0;
77
78}
Dave Hylandsf14b92b2014-03-12 18:06:26 -070079
80float sqrtf(float x) {
81 asm volatile (
82 "vsqrt.f32 %[r], %[x]\n"
83 : [r] "=t" (x)
84 : [x] "t" (x));
85 return x;
86}
87
Damien Georgeaba9f512014-04-03 21:16:37 +010088// some compilers define log2f in terms of logf
89#ifdef log2f
90#undef log2f
91#endif
Damien George769b23a2014-04-03 22:11:28 +010092float log2f(float x) { return logf(x) / (float)_M_LN2; }
93
94static const float _M_LN10 = 2.30258509299404; // 0x40135d8e
95float log10f(float x) { return logf(x) / (float)_M_LN10; }
96
97float tanhf(float x) { return sinhf(x) / coshf(x); }
98
99// TODO we need import these functions from some library (eg musl or newlib)
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700100float acoshf(float x) { return 0.0; }
101float asinhf(float x) { return 0.0; }
102float atanhf(float x) { return 0.0; }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700103float tanf(float x) { return 0.0; }
104float acosf(float x) { return 0.0; }
105float asinf(float x) { return 0.0; }
106float atanf(float x) { return 0.0; }
107float atan2f(float x, float y) { return 0.0; }
Damien Georgec070ff22014-03-21 20:52:54 +0000108float fmodf(float x, float y) { return 0.0; }
Damien George90834b92014-03-23 14:00:02 +0000109float tgammaf(float x) { return 0.0; }
Damien George81382052014-03-22 20:44:43 +0000110float lgammaf(float x) { return 0.0; }
111float erff(float x) { return 0.0; }
112float erfcf(float x) { return 0.0; }
113float modff(float x, float *y) { return 0.0; }
114float frexpf(float x, int *exp) { return 0.0; }
115float ldexpf(float x, int exp) { return 0.0; }
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700116
117/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100118/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700119// from musl-0.9.15 libm.h
Damien George769b23a2014-04-03 22:11:28 +0100120/*****************************************************************************/
121/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700122
123/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
124/*
125 * ====================================================
126 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
127 *
128 * Developed at SunPro, a Sun Microsystems, Inc. business.
129 * Permission to use, copy, modify, and distribute this
130 * software is freely granted, provided that this notice
131 * is preserved.
132 * ====================================================
133 */
134
135#define FORCE_EVAL(x) do { \
136 if (sizeof(x) == sizeof(float)) { \
137 volatile float __x; \
138 __x = (x); \
139 (void)__x; \
140 } else if (sizeof(x) == sizeof(double)) { \
141 volatile double __x; \
142 __x = (x); \
143 (void)__x; \
144 } else { \
145 volatile long double __x; \
146 __x = (x); \
147 (void)__x; \
148 } \
149} while(0)
150
151/* Get a 32 bit int from a float. */
152#define GET_FLOAT_WORD(w,d) \
153do { \
154 union {float f; uint32_t i;} __u; \
155 __u.f = (d); \
156 (w) = __u.i; \
157} while (0)
158
159/* Set a float from a 32 bit int. */
160#define SET_FLOAT_WORD(d,w) \
161do { \
162 union {float f; uint32_t i;} __u; \
163 __u.i = (w); \
164 (d) = __u.f; \
165} while (0)
166
167/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100168/*****************************************************************************/
Damien Georgee90eefc2014-04-02 19:55:08 +0100169// __fpclassifyf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100170/*****************************************************************************/
171/*****************************************************************************/
Damien Georgee90eefc2014-04-02 19:55:08 +0100172
173int __fpclassifyf(float x)
174{
175 union {float f; uint32_t i;} u = {x};
176 int e = u.i>>23 & 0xff;
177 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
178 if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
179 return FP_NORMAL;
180}
181
182/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100183/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700184// scalbnf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100185/*****************************************************************************/
186/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700187
188float scalbnf(float x, int n)
189{
190 union {float f; uint32_t i;} u;
191 float_t y = x;
192
193 if (n > 127) {
194 y *= 0x1p127f;
195 n -= 127;
196 if (n > 127) {
197 y *= 0x1p127f;
198 n -= 127;
199 if (n > 127)
200 n = 127;
201 }
202 } else if (n < -126) {
203 y *= 0x1p-126f;
204 n += 126;
205 if (n < -126) {
206 y *= 0x1p-126f;
207 n += 126;
208 if (n < -126)
209 n = -126;
210 }
211 }
212 u.i = (uint32_t)(0x7f+n)<<23;
213 x = y * u.f;
214 return x;
215}
216
217/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100218/*****************************************************************************/
219// powf from musl-0.9.15
220/*****************************************************************************/
221/*****************************************************************************/
222
223/* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
224/*
225 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
226 */
227/*
228 * ====================================================
229 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
230 *
231 * Developed at SunPro, a Sun Microsystems, Inc. business.
232 * Permission to use, copy, modify, and distribute this
233 * software is freely granted, provided that this notice
234 * is preserved.
235 * ====================================================
236 */
237
238static const float
239bp[] = {1.0, 1.5,},
240dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */
241dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */
242two24 = 16777216.0, /* 0x4b800000 */
243huge = 1.0e30,
244tiny = 1.0e-30,
245/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
246L1 = 6.0000002384e-01, /* 0x3f19999a */
247L2 = 4.2857143283e-01, /* 0x3edb6db7 */
248L3 = 3.3333334327e-01, /* 0x3eaaaaab */
249L4 = 2.7272811532e-01, /* 0x3e8ba305 */
250L5 = 2.3066075146e-01, /* 0x3e6c3255 */
251L6 = 2.0697501302e-01, /* 0x3e53f142 */
252P1 = 1.6666667163e-01, /* 0x3e2aaaab */
253P2 = -2.7777778450e-03, /* 0xbb360b61 */
254P3 = 6.6137559770e-05, /* 0x388ab355 */
255P4 = -1.6533901999e-06, /* 0xb5ddea0e */
256P5 = 4.1381369442e-08, /* 0x3331bb4c */
257lg2 = 6.9314718246e-01, /* 0x3f317218 */
258lg2_h = 6.93145752e-01, /* 0x3f317200 */
259lg2_l = 1.42860654e-06, /* 0x35bfbe8c */
260ovt = 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */
261cp = 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */
262cp_h = 9.6191406250e-01, /* 0x3f764000 =12b cp */
263cp_l = -1.1736857402e-04, /* 0xb8f623c6 =tail of cp_h */
264ivln2 = 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */
265ivln2_h = 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/
266ivln2_l = 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/
267
268float powf(float x, float y)
269{
270 float z,ax,z_h,z_l,p_h,p_l;
271 float y1,t1,t2,r,s,sn,t,u,v,w;
272 int32_t i,j,k,yisint,n;
273 int32_t hx,hy,ix,iy,is;
274
275 GET_FLOAT_WORD(hx, x);
276 GET_FLOAT_WORD(hy, y);
277 ix = hx & 0x7fffffff;
278 iy = hy & 0x7fffffff;
279
280 /* x**0 = 1, even if x is NaN */
281 if (iy == 0)
282 return 1.0f;
283 /* 1**y = 1, even if y is NaN */
284 if (hx == 0x3f800000)
285 return 1.0f;
286 /* NaN if either arg is NaN */
287 if (ix > 0x7f800000 || iy > 0x7f800000)
288 return x + y;
289
290 /* determine if y is an odd int when x < 0
291 * yisint = 0 ... y is not an integer
292 * yisint = 1 ... y is an odd int
293 * yisint = 2 ... y is an even int
294 */
295 yisint = 0;
296 if (hx < 0) {
297 if (iy >= 0x4b800000)
298 yisint = 2; /* even integer y */
299 else if (iy >= 0x3f800000) {
300 k = (iy>>23) - 0x7f; /* exponent */
301 j = iy>>(23-k);
302 if ((j<<(23-k)) == iy)
303 yisint = 2 - (j & 1);
304 }
305 }
306
307 /* special value of y */
308 if (iy == 0x7f800000) { /* y is +-inf */
309 if (ix == 0x3f800000) /* (-1)**+-inf is 1 */
310 return 1.0f;
311 else if (ix > 0x3f800000) /* (|x|>1)**+-inf = inf,0 */
312 return hy >= 0 ? y : 0.0f;
313 else if (ix != 0) /* (|x|<1)**+-inf = 0,inf if x!=0 */
314 return hy >= 0 ? 0.0f: -y;
315 }
316 if (iy == 0x3f800000) /* y is +-1 */
317 return hy >= 0 ? x : 1.0f/x;
318 if (hy == 0x40000000) /* y is 2 */
319 return x*x;
320 if (hy == 0x3f000000) { /* y is 0.5 */
321 if (hx >= 0) /* x >= +0 */
322 return sqrtf(x);
323 }
324
325 ax = fabsf(x);
326 /* special value of x */
327 if (ix == 0x7f800000 || ix == 0 || ix == 0x3f800000) { /* x is +-0,+-inf,+-1 */
328 z = ax;
329 if (hy < 0) /* z = (1/|x|) */
330 z = 1.0f/z;
331 if (hx < 0) {
332 if (((ix-0x3f800000)|yisint) == 0) {
333 z = (z-z)/(z-z); /* (-1)**non-int is NaN */
334 } else if (yisint == 1)
335 z = -z; /* (x<0)**odd = -(|x|**odd) */
336 }
337 return z;
338 }
339
340 sn = 1.0f; /* sign of result */
341 if (hx < 0) {
342 if (yisint == 0) /* (x<0)**(non-int) is NaN */
343 return (x-x)/(x-x);
344 if (yisint == 1) /* (x<0)**(odd int) */
345 sn = -1.0f;
346 }
347
348 /* |y| is huge */
349 if (iy > 0x4d000000) { /* if |y| > 2**27 */
350 /* over/underflow if x is not close to one */
351 if (ix < 0x3f7ffff8)
352 return hy < 0 ? sn*huge*huge : sn*tiny*tiny;
353 if (ix > 0x3f800007)
354 return hy > 0 ? sn*huge*huge : sn*tiny*tiny;
355 /* now |1-x| is tiny <= 2**-20, suffice to compute
356 log(x) by x-x^2/2+x^3/3-x^4/4 */
357 t = ax - 1; /* t has 20 trailing zeros */
358 w = (t*t)*(0.5f - t*(0.333333333333f - t*0.25f));
359 u = ivln2_h*t; /* ivln2_h has 16 sig. bits */
360 v = t*ivln2_l - w*ivln2;
361 t1 = u + v;
362 GET_FLOAT_WORD(is, t1);
363 SET_FLOAT_WORD(t1, is & 0xfffff000);
364 t2 = v - (t1-u);
365 } else {
366 float s2,s_h,s_l,t_h,t_l;
367 n = 0;
368 /* take care subnormal number */
369 if (ix < 0x00800000) {
370 ax *= two24;
371 n -= 24;
372 GET_FLOAT_WORD(ix, ax);
373 }
374 n += ((ix)>>23) - 0x7f;
375 j = ix & 0x007fffff;
376 /* determine interval */
377 ix = j | 0x3f800000; /* normalize ix */
378 if (j <= 0x1cc471) /* |x|<sqrt(3/2) */
379 k = 0;
380 else if (j < 0x5db3d7) /* |x|<sqrt(3) */
381 k = 1;
382 else {
383 k = 0;
384 n += 1;
385 ix -= 0x00800000;
386 }
387 SET_FLOAT_WORD(ax, ix);
388
389 /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
390 u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
391 v = 1.0f/(ax+bp[k]);
392 s = u*v;
393 s_h = s;
394 GET_FLOAT_WORD(is, s_h);
395 SET_FLOAT_WORD(s_h, is & 0xfffff000);
396 /* t_h=ax+bp[k] High */
397 is = ((ix>>1) & 0xfffff000) | 0x20000000;
398 SET_FLOAT_WORD(t_h, is + 0x00400000 + (k<<21));
399 t_l = ax - (t_h - bp[k]);
400 s_l = v*((u - s_h*t_h) - s_h*t_l);
401 /* compute log(ax) */
402 s2 = s*s;
403 r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
404 r += s_l*(s_h+s);
405 s2 = s_h*s_h;
406 t_h = 3.0f + s2 + r;
407 GET_FLOAT_WORD(is, t_h);
408 SET_FLOAT_WORD(t_h, is & 0xfffff000);
409 t_l = r - ((t_h - 3.0f) - s2);
410 /* u+v = s*(1+...) */
411 u = s_h*t_h;
412 v = s_l*t_h + t_l*s;
413 /* 2/(3log2)*(s+...) */
414 p_h = u + v;
415 GET_FLOAT_WORD(is, p_h);
416 SET_FLOAT_WORD(p_h, is & 0xfffff000);
417 p_l = v - (p_h - u);
418 z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */
419 z_l = cp_l*p_h + p_l*cp+dp_l[k];
420 /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
421 t = (float)n;
422 t1 = (((z_h + z_l) + dp_h[k]) + t);
423 GET_FLOAT_WORD(is, t1);
424 SET_FLOAT_WORD(t1, is & 0xfffff000);
425 t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
426 }
427
428 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
429 GET_FLOAT_WORD(is, y);
430 SET_FLOAT_WORD(y1, is & 0xfffff000);
431 p_l = (y-y1)*t1 + y*t2;
432 p_h = y1*t1;
433 z = p_l + p_h;
434 GET_FLOAT_WORD(j, z);
435 if (j > 0x43000000) /* if z > 128 */
436 return sn*huge*huge; /* overflow */
437 else if (j == 0x43000000) { /* if z == 128 */
438 if (p_l + ovt > z - p_h)
439 return sn*huge*huge; /* overflow */
440 } else if ((j&0x7fffffff) > 0x43160000) /* z < -150 */ // FIXME: check should be (uint32_t)j > 0xc3160000
441 return sn*tiny*tiny; /* underflow */
442 else if (j == 0xc3160000) { /* z == -150 */
443 if (p_l <= z-p_h)
444 return sn*tiny*tiny; /* underflow */
445 }
446 /*
447 * compute 2**(p_h+p_l)
448 */
449 i = j & 0x7fffffff;
450 k = (i>>23) - 0x7f;
451 n = 0;
452 if (i > 0x3f000000) { /* if |z| > 0.5, set n = [z+0.5] */
453 n = j + (0x00800000>>(k+1));
454 k = ((n&0x7fffffff)>>23) - 0x7f; /* new k for n */
455 SET_FLOAT_WORD(t, n & ~(0x007fffff>>k));
456 n = ((n&0x007fffff)|0x00800000)>>(23-k);
457 if (j < 0)
458 n = -n;
459 p_h -= t;
460 }
461 t = p_l + p_h;
462 GET_FLOAT_WORD(is, t);
463 SET_FLOAT_WORD(t, is & 0xffff8000);
464 u = t*lg2_h;
465 v = (p_l-(t-p_h))*lg2 + t*lg2_l;
466 z = u + v;
467 w = v - (z - u);
468 t = z*z;
469 t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
470 r = (z*t1)/(t1-2.0f) - (w+z*w);
471 z = 1.0f - (r - z);
472 GET_FLOAT_WORD(j, z);
473 j += n<<23;
474 if ((j>>23) <= 0) /* subnormal output */
475 z = scalbnf(z, n);
476 else
477 SET_FLOAT_WORD(z, j);
478 return sn*z;
479}
480
481/*****************************************************************************/
482/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700483// expf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100484/*****************************************************************************/
485/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700486
487/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
488/*
489 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
490 */
491/*
492 * ====================================================
493 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
494 *
495 * Developed at SunPro, a Sun Microsystems, Inc. business.
496 * Permission to use, copy, modify, and distribute this
497 * software is freely granted, provided that this notice
498 * is preserved.
499 * ====================================================
500 */
501
502static const float
503half[2] = {0.5,-0.5},
504ln2hi = 6.9314575195e-1f, /* 0x3f317200 */
505ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */
506invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */
507/*
508 * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
509 * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
510 */
Damien George769b23a2014-04-03 22:11:28 +0100511expf_P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */
512expf_P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700513
514float expf(float x)
515{
516 float_t hi, lo, c, xx, y;
517 int k, sign;
518 uint32_t hx;
519
520 GET_FLOAT_WORD(hx, x);
521 sign = hx >> 31; /* sign bit of x */
522 hx &= 0x7fffffff; /* high word of |x| */
523
524 /* special cases */
525 if (hx >= 0x42aeac50) { /* if |x| >= -87.33655f or NaN */
526 if (hx >= 0x42b17218 && !sign) { /* x >= 88.722839f */
527 /* overflow */
528 x *= 0x1p127f;
529 return x;
530 }
531 if (sign) {
532 /* underflow */
533 FORCE_EVAL(-0x1p-149f/x);
534 if (hx >= 0x42cff1b5) /* x <= -103.972084f */
535 return 0;
536 }
537 }
538
539 /* argument reduction */
540 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
541 if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
542 k = invln2*x + half[sign];
543 else
544 k = 1 - sign - sign;
545 hi = x - k*ln2hi; /* k*ln2hi is exact here */
546 lo = k*ln2lo;
547 x = hi - lo;
548 } else if (hx > 0x39000000) { /* |x| > 2**-14 */
549 k = 0;
550 hi = x;
551 lo = 0;
552 } else {
553 /* raise inexact */
554 FORCE_EVAL(0x1p127f + x);
555 return 1 + x;
556 }
557
558 /* x is now in primary range */
559 xx = x*x;
Damien George769b23a2014-04-03 22:11:28 +0100560 c = x - xx*(expf_P1+xx*expf_P2);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700561 y = 1 + (x*c/(2-c) - lo + hi);
562 if (k == 0)
563 return y;
564 return scalbnf(y, k);
565}
566
567/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100568/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700569// expm1f from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100570/*****************************************************************************/
571/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700572
573/* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */
574/*
575 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
576 */
577/*
578 * ====================================================
579 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
580 *
581 * Developed at SunPro, a Sun Microsystems, Inc. business.
582 * Permission to use, copy, modify, and distribute this
583 * software is freely granted, provided that this notice
584 * is preserved.
585 * ====================================================
586 */
587
588static const float
589o_threshold = 8.8721679688e+01, /* 0x42b17180 */
590ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
591ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
592//invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
593/*
594 * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
595 * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
596 * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
597 */
598Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
599Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */
600
601float expm1f(float x)
602{
603 float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
604 union {float f; uint32_t i;} u = {x};
605 uint32_t hx = u.i & 0x7fffffff;
606 int k, sign = u.i >> 31;
607
608 /* filter out huge and non-finite argument */
609 if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */
610 if (hx > 0x7f800000) /* NaN */
611 return x;
612 if (sign)
613 return -1;
614 if (x > o_threshold) {
615 x *= 0x1p127f;
616 return x;
617 }
618 }
619
620 /* argument reduction */
621 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
622 if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
623 if (!sign) {
624 hi = x - ln2_hi;
625 lo = ln2_lo;
626 k = 1;
627 } else {
628 hi = x + ln2_hi;
629 lo = -ln2_lo;
630 k = -1;
631 }
632 } else {
633 k = invln2*x + (sign ? -0.5f : 0.5f);
634 t = k;
635 hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
636 lo = t*ln2_lo;
637 }
638 x = hi-lo;
639 c = (hi-x)-lo;
640 } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */
641 if (hx < 0x00800000)
642 FORCE_EVAL(x*x);
643 return x;
644 } else
645 k = 0;
646
647 /* x is now in primary range */
648 hfx = 0.5f*x;
649 hxs = x*hfx;
650 r1 = 1.0f+hxs*(Q1+hxs*Q2);
651 t = 3.0f - r1*hfx;
652 e = hxs*((r1-t)/(6.0f - x*t));
653 if (k == 0) /* c is 0 */
654 return x - (x*e-hxs);
655 e = x*(e-c) - c;
656 e -= hxs;
657 /* exp(x) ~ 2^k (x_reduced - e + 1) */
658 if (k == -1)
659 return 0.5f*(x-e) - 0.5f;
660 if (k == 1) {
661 if (x < -0.25f)
662 return -2.0f*(e-(x+0.5f));
663 return 1.0f + 2.0f*(x-e);
664 }
665 u.i = (0x7f+k)<<23; /* 2^k */
666 twopk = u.f;
667 if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */
668 y = x - e + 1.0f;
669 if (k == 128)
670 y = y*2.0f*0x1p127f;
671 else
672 y = y*twopk;
673 return y - 1.0f;
674 }
675 u.i = (0x7f-k)<<23; /* 2^-k */
676 if (k < 23)
677 y = (x-e+(1-u.f))*twopk;
678 else
679 y = (x-(e+u.f)+1)*twopk;
680 return y;
681}
682
683/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100684/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700685// __expo2f from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100686/*****************************************************************************/
687/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700688
689/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
690static const int k = 235;
691static const float kln2 = 0x1.45c778p+7f;
692
693/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
694float __expo2f(float x)
695{
696 float scale;
697
698 /* note that k is odd and scale*scale overflows */
699 SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23);
700 /* exp(x - k ln2) * 2**(k-1) */
701 return expf(x - kln2) * scale * scale;
702}
703
704/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100705/*****************************************************************************/
706// logf from musl-0.9.15
707/*****************************************************************************/
708/*****************************************************************************/
709
710/* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */
711/*
712 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
713 */
714/*
715 * ====================================================
716 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
717 *
718 * Developed at SunPro, a Sun Microsystems, Inc. business.
719 * Permission to use, copy, modify, and distribute this
720 * software is freely granted, provided that this notice
721 * is preserved.
722 * ====================================================
723 */
724
725static const float
726/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
727Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
728Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
729Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
730Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
731
732float logf(float x)
733{
734 union {float f; uint32_t i;} u = {x};
735 float_t hfsq,f,s,z,R,w,t1,t2,dk;
736 uint32_t ix;
737 int k;
738
739 ix = u.i;
740 k = 0;
741 if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */
742 if (ix<<1 == 0)
743 return -1/(x*x); /* log(+-0)=-inf */
744 if (ix>>31)
745 return (x-x)/0.0f; /* log(-#) = NaN */
746 /* subnormal number, scale up x */
747 k -= 25;
748 x *= 0x1p25f;
749 u.f = x;
750 ix = u.i;
751 } else if (ix >= 0x7f800000) {
752 return x;
753 } else if (ix == 0x3f800000)
754 return 0;
755
756 /* reduce x into [sqrt(2)/2, sqrt(2)] */
757 ix += 0x3f800000 - 0x3f3504f3;
758 k += (int)(ix>>23) - 0x7f;
759 ix = (ix&0x007fffff) + 0x3f3504f3;
760 u.i = ix;
761 x = u.f;
762
763 f = x - 1.0f;
764 s = f/(2.0f + f);
765 z = s*s;
766 w = z*z;
767 t1= w*(Lg2+w*Lg4);
768 t2= z*(Lg1+w*Lg3);
769 R = t2 + t1;
770 hfsq = 0.5f*f*f;
771 dk = k;
772 return s*(hfsq+R) + dk*ln2_lo - hfsq + f + dk*ln2_hi;
773}
774
775/*****************************************************************************/
776/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700777// coshf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100778/*****************************************************************************/
779/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700780
781float coshf(float x)
782{
783 union {float f; uint32_t i;} u = {.f = x};
784 uint32_t w;
785 float t;
786
787 /* |x| */
788 u.i &= 0x7fffffff;
789 x = u.f;
790 w = u.i;
791
792 /* |x| < log(2) */
793 if (w < 0x3f317217) {
794 if (w < 0x3f800000 - (12<<23)) {
795 FORCE_EVAL(x + 0x1p120f);
796 return 1;
797 }
798 t = expm1f(x);
799 return 1 + t*t/(2*(1+t));
800 }
801
802 /* |x| < log(FLT_MAX) */
803 if (w < 0x42b17217) {
804 t = expf(x);
805 return 0.5f*(t + 1/t);
806 }
807
808 /* |x| > log(FLT_MAX) or nan */
809 t = __expo2f(x);
810 return t;
811}
812
813/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100814/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700815// sinhf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100816/*****************************************************************************/
817/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700818
819float sinhf(float x)
820{
821 union {float f; uint32_t i;} u = {.f = x};
822 uint32_t w;
823 float t, h, absx;
824
825 h = 0.5;
826 if (u.i >> 31)
827 h = -h;
828 /* |x| */
829 u.i &= 0x7fffffff;
830 absx = u.f;
831 w = u.i;
832
833 /* |x| < log(FLT_MAX) */
834 if (w < 0x42b17217) {
835 t = expm1f(absx);
836 if (w < 0x3f800000) {
837 if (w < 0x3f800000 - (12<<23))
838 return x;
839 return h*(2*t - t*t/(t+1));
840 }
841 return h*(t + t/(t+1));
842 }
843
844 /* |x| > logf(FLT_MAX) or nan */
845 t = 2*h*__expo2f(absx);
846 return t;
847}
Damien George89831d02014-04-17 00:13:13 +0100848
849/*****************************************************************************/
850/*****************************************************************************/
851// ceilf, floorf and truncf from musl-0.9.15
852/*****************************************************************************/
853/*****************************************************************************/
854
855float ceilf(float x)
856{
857 union {float f; uint32_t i;} u = {x};
858 int e = (int)(u.i >> 23 & 0xff) - 0x7f;
859 uint32_t m;
860
861 if (e >= 23)
862 return x;
863 if (e >= 0) {
864 m = 0x007fffff >> e;
865 if ((u.i & m) == 0)
866 return x;
867 FORCE_EVAL(x + 0x1p120f);
868 if (u.i >> 31 == 0)
869 u.i += m;
870 u.i &= ~m;
871 } else {
872 FORCE_EVAL(x + 0x1p120f);
873 if (u.i >> 31)
874 u.f = -0.0;
875 else if (u.i << 1)
876 u.f = 1.0;
877 }
878 return u.f;
879}
880
881float floorf(float x)
882{
883 union {float f; uint32_t i;} u = {x};
884 int e = (int)(u.i >> 23 & 0xff) - 0x7f;
885 uint32_t m;
886
887 if (e >= 23)
888 return x;
889 if (e >= 0) {
890 m = 0x007fffff >> e;
891 if ((u.i & m) == 0)
892 return x;
893 FORCE_EVAL(x + 0x1p120f);
894 if (u.i >> 31)
895 u.i += m;
896 u.i &= ~m;
897 } else {
898 FORCE_EVAL(x + 0x1p120f);
899 if (u.i >> 31 == 0)
900 u.i = 0;
901 else if (u.i << 1)
902 u.f = -1.0;
903 }
904 return u.f;
905}
906
907float truncf(float x)
908{
909 union {float f; uint32_t i;} u = {x};
910 int e = (int)(u.i >> 23 & 0xff) - 0x7f + 9;
911 uint32_t m;
912
913 if (e >= 23 + 9)
914 return x;
915 if (e < 9)
916 e = 1;
917 m = -1U >> e;
918 if ((u.i & m) == 0)
919 return x;
920 FORCE_EVAL(x + 0x1p120f);
921 u.i &= ~m;
922 return u.f;
923}