blob: 9b815f118089ef772767cb38aa09c5f4cff1eef6 [file] [log] [blame]
Dave Hylandsf14b92b2014-03-12 18:06:26 -07001#include <stdint.h>
Damien Georgee90eefc2014-04-02 19:55:08 +01002#include <math.h>
3
Damien George5a166582014-03-23 00:34:49 +00004typedef float float_t;
5typedef union {
6 float f;
7 struct {
8 uint64_t m : 23;
9 uint64_t e : 8;
10 uint64_t s : 1;
11 };
12} float_s_t;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070013
Damien George5a166582014-03-23 00:34:49 +000014typedef union {
15 double d;
16 struct {
17 uint64_t m : 52;
18 uint64_t e : 11;
19 uint64_t s : 1;
20 };
21} double_s_t;
22
23double __attribute__((pcs("aapcs"))) __aeabi_f2d(float x) {
24 float_s_t fx={0};
25 double_s_t dx={0};
26
27 fx.f = x;
28 dx.s = (fx.s);
29 dx.e = (fx.e-127+1023) & 0x7FF;
30 dx.m = fx.m;
31 dx.m <<=(52-23); // left justify
32 return dx.d;
Dave Hylandsf14b92b2014-03-12 18:06:26 -070033}
34
Damien George5a166582014-03-23 00:34:49 +000035float __attribute__((pcs("aapcs"))) __aeabi_d2f(double x) {
36 float_s_t fx={0};
37 double_s_t dx={0};
Dave Hylandsf14b92b2014-03-12 18:06:26 -070038
Damien George5a166582014-03-23 00:34:49 +000039 dx.d = x;
40 fx.s = (dx.s);
41 fx.e = (dx.e-1023+127) & 0xFF;
42 fx.m = (dx.m>>(52-23)); // right justify
43 return fx.f;
44}
45double __aeabi_dmul(double x , double y) {
46 return 0.0;
47
48}
Dave Hylandsf14b92b2014-03-12 18:06:26 -070049
50float sqrtf(float x) {
51 asm volatile (
52 "vsqrt.f32 %[r], %[x]\n"
53 : [r] "=t" (x)
54 : [x] "t" (x));
55 return x;
56}
57
Damien Georgeaba9f512014-04-03 21:16:37 +010058// some compilers define log2f in terms of logf
59#ifdef log2f
60#undef log2f
61#endif
Damien George769b23a2014-04-03 22:11:28 +010062float log2f(float x) { return logf(x) / (float)_M_LN2; }
63
64static const float _M_LN10 = 2.30258509299404; // 0x40135d8e
65float log10f(float x) { return logf(x) / (float)_M_LN10; }
66
67float tanhf(float x) { return sinhf(x) / coshf(x); }
68
69// TODO we need import these functions from some library (eg musl or newlib)
Dave Hylandsf14b92b2014-03-12 18:06:26 -070070float acoshf(float x) { return 0.0; }
71float asinhf(float x) { return 0.0; }
72float atanhf(float x) { return 0.0; }
73float cosf(float x) { return 0.0; }
74float sinf(float x) { return 0.0; }
75float tanf(float x) { return 0.0; }
76float acosf(float x) { return 0.0; }
77float asinf(float x) { return 0.0; }
78float atanf(float x) { return 0.0; }
79float atan2f(float x, float y) { return 0.0; }
Damien Georgec070ff22014-03-21 20:52:54 +000080float ceilf(float x) { return 0.0; }
81float floorf(float x) { return 0.0; }
82float truncf(float x) { return 0.0; }
83float fmodf(float x, float y) { return 0.0; }
Damien George90834b92014-03-23 14:00:02 +000084float tgammaf(float x) { return 0.0; }
Damien George81382052014-03-22 20:44:43 +000085float lgammaf(float x) { return 0.0; }
86float erff(float x) { return 0.0; }
87float erfcf(float x) { return 0.0; }
88float modff(float x, float *y) { return 0.0; }
89float frexpf(float x, int *exp) { return 0.0; }
90float ldexpf(float x, int exp) { return 0.0; }
Dave Hylandsf14b92b2014-03-12 18:06:26 -070091
92/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +010093/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -070094// from musl-0.9.15 libm.h
Damien George769b23a2014-04-03 22:11:28 +010095/*****************************************************************************/
96/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -070097
98/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
99/*
100 * ====================================================
101 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
102 *
103 * Developed at SunPro, a Sun Microsystems, Inc. business.
104 * Permission to use, copy, modify, and distribute this
105 * software is freely granted, provided that this notice
106 * is preserved.
107 * ====================================================
108 */
109
110#define FORCE_EVAL(x) do { \
111 if (sizeof(x) == sizeof(float)) { \
112 volatile float __x; \
113 __x = (x); \
114 (void)__x; \
115 } else if (sizeof(x) == sizeof(double)) { \
116 volatile double __x; \
117 __x = (x); \
118 (void)__x; \
119 } else { \
120 volatile long double __x; \
121 __x = (x); \
122 (void)__x; \
123 } \
124} while(0)
125
126/* Get a 32 bit int from a float. */
127#define GET_FLOAT_WORD(w,d) \
128do { \
129 union {float f; uint32_t i;} __u; \
130 __u.f = (d); \
131 (w) = __u.i; \
132} while (0)
133
134/* Set a float from a 32 bit int. */
135#define SET_FLOAT_WORD(d,w) \
136do { \
137 union {float f; uint32_t i;} __u; \
138 __u.i = (w); \
139 (d) = __u.f; \
140} while (0)
141
142/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100143/*****************************************************************************/
Damien Georgee90eefc2014-04-02 19:55:08 +0100144// __fpclassifyf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100145/*****************************************************************************/
146/*****************************************************************************/
Damien Georgee90eefc2014-04-02 19:55:08 +0100147
148int __fpclassifyf(float x)
149{
150 union {float f; uint32_t i;} u = {x};
151 int e = u.i>>23 & 0xff;
152 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
153 if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
154 return FP_NORMAL;
155}
156
157/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100158/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700159// scalbnf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100160/*****************************************************************************/
161/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700162
163float scalbnf(float x, int n)
164{
165 union {float f; uint32_t i;} u;
166 float_t y = x;
167
168 if (n > 127) {
169 y *= 0x1p127f;
170 n -= 127;
171 if (n > 127) {
172 y *= 0x1p127f;
173 n -= 127;
174 if (n > 127)
175 n = 127;
176 }
177 } else if (n < -126) {
178 y *= 0x1p-126f;
179 n += 126;
180 if (n < -126) {
181 y *= 0x1p-126f;
182 n += 126;
183 if (n < -126)
184 n = -126;
185 }
186 }
187 u.i = (uint32_t)(0x7f+n)<<23;
188 x = y * u.f;
189 return x;
190}
191
192/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100193/*****************************************************************************/
194// powf from musl-0.9.15
195/*****************************************************************************/
196/*****************************************************************************/
197
198/* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
199/*
200 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
201 */
202/*
203 * ====================================================
204 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
205 *
206 * Developed at SunPro, a Sun Microsystems, Inc. business.
207 * Permission to use, copy, modify, and distribute this
208 * software is freely granted, provided that this notice
209 * is preserved.
210 * ====================================================
211 */
212
213static const float
214bp[] = {1.0, 1.5,},
215dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */
216dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */
217two24 = 16777216.0, /* 0x4b800000 */
218huge = 1.0e30,
219tiny = 1.0e-30,
220/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
221L1 = 6.0000002384e-01, /* 0x3f19999a */
222L2 = 4.2857143283e-01, /* 0x3edb6db7 */
223L3 = 3.3333334327e-01, /* 0x3eaaaaab */
224L4 = 2.7272811532e-01, /* 0x3e8ba305 */
225L5 = 2.3066075146e-01, /* 0x3e6c3255 */
226L6 = 2.0697501302e-01, /* 0x3e53f142 */
227P1 = 1.6666667163e-01, /* 0x3e2aaaab */
228P2 = -2.7777778450e-03, /* 0xbb360b61 */
229P3 = 6.6137559770e-05, /* 0x388ab355 */
230P4 = -1.6533901999e-06, /* 0xb5ddea0e */
231P5 = 4.1381369442e-08, /* 0x3331bb4c */
232lg2 = 6.9314718246e-01, /* 0x3f317218 */
233lg2_h = 6.93145752e-01, /* 0x3f317200 */
234lg2_l = 1.42860654e-06, /* 0x35bfbe8c */
235ovt = 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */
236cp = 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */
237cp_h = 9.6191406250e-01, /* 0x3f764000 =12b cp */
238cp_l = -1.1736857402e-04, /* 0xb8f623c6 =tail of cp_h */
239ivln2 = 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */
240ivln2_h = 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/
241ivln2_l = 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/
242
243float powf(float x, float y)
244{
245 float z,ax,z_h,z_l,p_h,p_l;
246 float y1,t1,t2,r,s,sn,t,u,v,w;
247 int32_t i,j,k,yisint,n;
248 int32_t hx,hy,ix,iy,is;
249
250 GET_FLOAT_WORD(hx, x);
251 GET_FLOAT_WORD(hy, y);
252 ix = hx & 0x7fffffff;
253 iy = hy & 0x7fffffff;
254
255 /* x**0 = 1, even if x is NaN */
256 if (iy == 0)
257 return 1.0f;
258 /* 1**y = 1, even if y is NaN */
259 if (hx == 0x3f800000)
260 return 1.0f;
261 /* NaN if either arg is NaN */
262 if (ix > 0x7f800000 || iy > 0x7f800000)
263 return x + y;
264
265 /* determine if y is an odd int when x < 0
266 * yisint = 0 ... y is not an integer
267 * yisint = 1 ... y is an odd int
268 * yisint = 2 ... y is an even int
269 */
270 yisint = 0;
271 if (hx < 0) {
272 if (iy >= 0x4b800000)
273 yisint = 2; /* even integer y */
274 else if (iy >= 0x3f800000) {
275 k = (iy>>23) - 0x7f; /* exponent */
276 j = iy>>(23-k);
277 if ((j<<(23-k)) == iy)
278 yisint = 2 - (j & 1);
279 }
280 }
281
282 /* special value of y */
283 if (iy == 0x7f800000) { /* y is +-inf */
284 if (ix == 0x3f800000) /* (-1)**+-inf is 1 */
285 return 1.0f;
286 else if (ix > 0x3f800000) /* (|x|>1)**+-inf = inf,0 */
287 return hy >= 0 ? y : 0.0f;
288 else if (ix != 0) /* (|x|<1)**+-inf = 0,inf if x!=0 */
289 return hy >= 0 ? 0.0f: -y;
290 }
291 if (iy == 0x3f800000) /* y is +-1 */
292 return hy >= 0 ? x : 1.0f/x;
293 if (hy == 0x40000000) /* y is 2 */
294 return x*x;
295 if (hy == 0x3f000000) { /* y is 0.5 */
296 if (hx >= 0) /* x >= +0 */
297 return sqrtf(x);
298 }
299
300 ax = fabsf(x);
301 /* special value of x */
302 if (ix == 0x7f800000 || ix == 0 || ix == 0x3f800000) { /* x is +-0,+-inf,+-1 */
303 z = ax;
304 if (hy < 0) /* z = (1/|x|) */
305 z = 1.0f/z;
306 if (hx < 0) {
307 if (((ix-0x3f800000)|yisint) == 0) {
308 z = (z-z)/(z-z); /* (-1)**non-int is NaN */
309 } else if (yisint == 1)
310 z = -z; /* (x<0)**odd = -(|x|**odd) */
311 }
312 return z;
313 }
314
315 sn = 1.0f; /* sign of result */
316 if (hx < 0) {
317 if (yisint == 0) /* (x<0)**(non-int) is NaN */
318 return (x-x)/(x-x);
319 if (yisint == 1) /* (x<0)**(odd int) */
320 sn = -1.0f;
321 }
322
323 /* |y| is huge */
324 if (iy > 0x4d000000) { /* if |y| > 2**27 */
325 /* over/underflow if x is not close to one */
326 if (ix < 0x3f7ffff8)
327 return hy < 0 ? sn*huge*huge : sn*tiny*tiny;
328 if (ix > 0x3f800007)
329 return hy > 0 ? sn*huge*huge : sn*tiny*tiny;
330 /* now |1-x| is tiny <= 2**-20, suffice to compute
331 log(x) by x-x^2/2+x^3/3-x^4/4 */
332 t = ax - 1; /* t has 20 trailing zeros */
333 w = (t*t)*(0.5f - t*(0.333333333333f - t*0.25f));
334 u = ivln2_h*t; /* ivln2_h has 16 sig. bits */
335 v = t*ivln2_l - w*ivln2;
336 t1 = u + v;
337 GET_FLOAT_WORD(is, t1);
338 SET_FLOAT_WORD(t1, is & 0xfffff000);
339 t2 = v - (t1-u);
340 } else {
341 float s2,s_h,s_l,t_h,t_l;
342 n = 0;
343 /* take care subnormal number */
344 if (ix < 0x00800000) {
345 ax *= two24;
346 n -= 24;
347 GET_FLOAT_WORD(ix, ax);
348 }
349 n += ((ix)>>23) - 0x7f;
350 j = ix & 0x007fffff;
351 /* determine interval */
352 ix = j | 0x3f800000; /* normalize ix */
353 if (j <= 0x1cc471) /* |x|<sqrt(3/2) */
354 k = 0;
355 else if (j < 0x5db3d7) /* |x|<sqrt(3) */
356 k = 1;
357 else {
358 k = 0;
359 n += 1;
360 ix -= 0x00800000;
361 }
362 SET_FLOAT_WORD(ax, ix);
363
364 /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
365 u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
366 v = 1.0f/(ax+bp[k]);
367 s = u*v;
368 s_h = s;
369 GET_FLOAT_WORD(is, s_h);
370 SET_FLOAT_WORD(s_h, is & 0xfffff000);
371 /* t_h=ax+bp[k] High */
372 is = ((ix>>1) & 0xfffff000) | 0x20000000;
373 SET_FLOAT_WORD(t_h, is + 0x00400000 + (k<<21));
374 t_l = ax - (t_h - bp[k]);
375 s_l = v*((u - s_h*t_h) - s_h*t_l);
376 /* compute log(ax) */
377 s2 = s*s;
378 r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
379 r += s_l*(s_h+s);
380 s2 = s_h*s_h;
381 t_h = 3.0f + s2 + r;
382 GET_FLOAT_WORD(is, t_h);
383 SET_FLOAT_WORD(t_h, is & 0xfffff000);
384 t_l = r - ((t_h - 3.0f) - s2);
385 /* u+v = s*(1+...) */
386 u = s_h*t_h;
387 v = s_l*t_h + t_l*s;
388 /* 2/(3log2)*(s+...) */
389 p_h = u + v;
390 GET_FLOAT_WORD(is, p_h);
391 SET_FLOAT_WORD(p_h, is & 0xfffff000);
392 p_l = v - (p_h - u);
393 z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */
394 z_l = cp_l*p_h + p_l*cp+dp_l[k];
395 /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
396 t = (float)n;
397 t1 = (((z_h + z_l) + dp_h[k]) + t);
398 GET_FLOAT_WORD(is, t1);
399 SET_FLOAT_WORD(t1, is & 0xfffff000);
400 t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
401 }
402
403 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
404 GET_FLOAT_WORD(is, y);
405 SET_FLOAT_WORD(y1, is & 0xfffff000);
406 p_l = (y-y1)*t1 + y*t2;
407 p_h = y1*t1;
408 z = p_l + p_h;
409 GET_FLOAT_WORD(j, z);
410 if (j > 0x43000000) /* if z > 128 */
411 return sn*huge*huge; /* overflow */
412 else if (j == 0x43000000) { /* if z == 128 */
413 if (p_l + ovt > z - p_h)
414 return sn*huge*huge; /* overflow */
415 } else if ((j&0x7fffffff) > 0x43160000) /* z < -150 */ // FIXME: check should be (uint32_t)j > 0xc3160000
416 return sn*tiny*tiny; /* underflow */
417 else if (j == 0xc3160000) { /* z == -150 */
418 if (p_l <= z-p_h)
419 return sn*tiny*tiny; /* underflow */
420 }
421 /*
422 * compute 2**(p_h+p_l)
423 */
424 i = j & 0x7fffffff;
425 k = (i>>23) - 0x7f;
426 n = 0;
427 if (i > 0x3f000000) { /* if |z| > 0.5, set n = [z+0.5] */
428 n = j + (0x00800000>>(k+1));
429 k = ((n&0x7fffffff)>>23) - 0x7f; /* new k for n */
430 SET_FLOAT_WORD(t, n & ~(0x007fffff>>k));
431 n = ((n&0x007fffff)|0x00800000)>>(23-k);
432 if (j < 0)
433 n = -n;
434 p_h -= t;
435 }
436 t = p_l + p_h;
437 GET_FLOAT_WORD(is, t);
438 SET_FLOAT_WORD(t, is & 0xffff8000);
439 u = t*lg2_h;
440 v = (p_l-(t-p_h))*lg2 + t*lg2_l;
441 z = u + v;
442 w = v - (z - u);
443 t = z*z;
444 t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
445 r = (z*t1)/(t1-2.0f) - (w+z*w);
446 z = 1.0f - (r - z);
447 GET_FLOAT_WORD(j, z);
448 j += n<<23;
449 if ((j>>23) <= 0) /* subnormal output */
450 z = scalbnf(z, n);
451 else
452 SET_FLOAT_WORD(z, j);
453 return sn*z;
454}
455
456/*****************************************************************************/
457/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700458// expf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100459/*****************************************************************************/
460/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700461
462/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
463/*
464 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
465 */
466/*
467 * ====================================================
468 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
469 *
470 * Developed at SunPro, a Sun Microsystems, Inc. business.
471 * Permission to use, copy, modify, and distribute this
472 * software is freely granted, provided that this notice
473 * is preserved.
474 * ====================================================
475 */
476
477static const float
478half[2] = {0.5,-0.5},
479ln2hi = 6.9314575195e-1f, /* 0x3f317200 */
480ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */
481invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */
482/*
483 * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
484 * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
485 */
Damien George769b23a2014-04-03 22:11:28 +0100486expf_P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */
487expf_P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700488
489float expf(float x)
490{
491 float_t hi, lo, c, xx, y;
492 int k, sign;
493 uint32_t hx;
494
495 GET_FLOAT_WORD(hx, x);
496 sign = hx >> 31; /* sign bit of x */
497 hx &= 0x7fffffff; /* high word of |x| */
498
499 /* special cases */
500 if (hx >= 0x42aeac50) { /* if |x| >= -87.33655f or NaN */
501 if (hx >= 0x42b17218 && !sign) { /* x >= 88.722839f */
502 /* overflow */
503 x *= 0x1p127f;
504 return x;
505 }
506 if (sign) {
507 /* underflow */
508 FORCE_EVAL(-0x1p-149f/x);
509 if (hx >= 0x42cff1b5) /* x <= -103.972084f */
510 return 0;
511 }
512 }
513
514 /* argument reduction */
515 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
516 if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
517 k = invln2*x + half[sign];
518 else
519 k = 1 - sign - sign;
520 hi = x - k*ln2hi; /* k*ln2hi is exact here */
521 lo = k*ln2lo;
522 x = hi - lo;
523 } else if (hx > 0x39000000) { /* |x| > 2**-14 */
524 k = 0;
525 hi = x;
526 lo = 0;
527 } else {
528 /* raise inexact */
529 FORCE_EVAL(0x1p127f + x);
530 return 1 + x;
531 }
532
533 /* x is now in primary range */
534 xx = x*x;
Damien George769b23a2014-04-03 22:11:28 +0100535 c = x - xx*(expf_P1+xx*expf_P2);
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700536 y = 1 + (x*c/(2-c) - lo + hi);
537 if (k == 0)
538 return y;
539 return scalbnf(y, k);
540}
541
542/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100543/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700544// expm1f from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100545/*****************************************************************************/
546/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700547
548/* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */
549/*
550 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
551 */
552/*
553 * ====================================================
554 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
555 *
556 * Developed at SunPro, a Sun Microsystems, Inc. business.
557 * Permission to use, copy, modify, and distribute this
558 * software is freely granted, provided that this notice
559 * is preserved.
560 * ====================================================
561 */
562
563static const float
564o_threshold = 8.8721679688e+01, /* 0x42b17180 */
565ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
566ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
567//invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
568/*
569 * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
570 * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
571 * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
572 */
573Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
574Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */
575
576float expm1f(float x)
577{
578 float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
579 union {float f; uint32_t i;} u = {x};
580 uint32_t hx = u.i & 0x7fffffff;
581 int k, sign = u.i >> 31;
582
583 /* filter out huge and non-finite argument */
584 if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */
585 if (hx > 0x7f800000) /* NaN */
586 return x;
587 if (sign)
588 return -1;
589 if (x > o_threshold) {
590 x *= 0x1p127f;
591 return x;
592 }
593 }
594
595 /* argument reduction */
596 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
597 if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
598 if (!sign) {
599 hi = x - ln2_hi;
600 lo = ln2_lo;
601 k = 1;
602 } else {
603 hi = x + ln2_hi;
604 lo = -ln2_lo;
605 k = -1;
606 }
607 } else {
608 k = invln2*x + (sign ? -0.5f : 0.5f);
609 t = k;
610 hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
611 lo = t*ln2_lo;
612 }
613 x = hi-lo;
614 c = (hi-x)-lo;
615 } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */
616 if (hx < 0x00800000)
617 FORCE_EVAL(x*x);
618 return x;
619 } else
620 k = 0;
621
622 /* x is now in primary range */
623 hfx = 0.5f*x;
624 hxs = x*hfx;
625 r1 = 1.0f+hxs*(Q1+hxs*Q2);
626 t = 3.0f - r1*hfx;
627 e = hxs*((r1-t)/(6.0f - x*t));
628 if (k == 0) /* c is 0 */
629 return x - (x*e-hxs);
630 e = x*(e-c) - c;
631 e -= hxs;
632 /* exp(x) ~ 2^k (x_reduced - e + 1) */
633 if (k == -1)
634 return 0.5f*(x-e) - 0.5f;
635 if (k == 1) {
636 if (x < -0.25f)
637 return -2.0f*(e-(x+0.5f));
638 return 1.0f + 2.0f*(x-e);
639 }
640 u.i = (0x7f+k)<<23; /* 2^k */
641 twopk = u.f;
642 if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */
643 y = x - e + 1.0f;
644 if (k == 128)
645 y = y*2.0f*0x1p127f;
646 else
647 y = y*twopk;
648 return y - 1.0f;
649 }
650 u.i = (0x7f-k)<<23; /* 2^-k */
651 if (k < 23)
652 y = (x-e+(1-u.f))*twopk;
653 else
654 y = (x-(e+u.f)+1)*twopk;
655 return y;
656}
657
658/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100659/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700660// __expo2f from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100661/*****************************************************************************/
662/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700663
664/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
665static const int k = 235;
666static const float kln2 = 0x1.45c778p+7f;
667
668/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
669float __expo2f(float x)
670{
671 float scale;
672
673 /* note that k is odd and scale*scale overflows */
674 SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23);
675 /* exp(x - k ln2) * 2**(k-1) */
676 return expf(x - kln2) * scale * scale;
677}
678
679/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100680/*****************************************************************************/
681// logf from musl-0.9.15
682/*****************************************************************************/
683/*****************************************************************************/
684
685/* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */
686/*
687 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
688 */
689/*
690 * ====================================================
691 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
692 *
693 * Developed at SunPro, a Sun Microsystems, Inc. business.
694 * Permission to use, copy, modify, and distribute this
695 * software is freely granted, provided that this notice
696 * is preserved.
697 * ====================================================
698 */
699
700static const float
701/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
702Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
703Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
704Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
705Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
706
707float logf(float x)
708{
709 union {float f; uint32_t i;} u = {x};
710 float_t hfsq,f,s,z,R,w,t1,t2,dk;
711 uint32_t ix;
712 int k;
713
714 ix = u.i;
715 k = 0;
716 if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */
717 if (ix<<1 == 0)
718 return -1/(x*x); /* log(+-0)=-inf */
719 if (ix>>31)
720 return (x-x)/0.0f; /* log(-#) = NaN */
721 /* subnormal number, scale up x */
722 k -= 25;
723 x *= 0x1p25f;
724 u.f = x;
725 ix = u.i;
726 } else if (ix >= 0x7f800000) {
727 return x;
728 } else if (ix == 0x3f800000)
729 return 0;
730
731 /* reduce x into [sqrt(2)/2, sqrt(2)] */
732 ix += 0x3f800000 - 0x3f3504f3;
733 k += (int)(ix>>23) - 0x7f;
734 ix = (ix&0x007fffff) + 0x3f3504f3;
735 u.i = ix;
736 x = u.f;
737
738 f = x - 1.0f;
739 s = f/(2.0f + f);
740 z = s*s;
741 w = z*z;
742 t1= w*(Lg2+w*Lg4);
743 t2= z*(Lg1+w*Lg3);
744 R = t2 + t1;
745 hfsq = 0.5f*f*f;
746 dk = k;
747 return s*(hfsq+R) + dk*ln2_lo - hfsq + f + dk*ln2_hi;
748}
749
750/*****************************************************************************/
751/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700752// coshf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100753/*****************************************************************************/
754/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700755
756float coshf(float x)
757{
758 union {float f; uint32_t i;} u = {.f = x};
759 uint32_t w;
760 float t;
761
762 /* |x| */
763 u.i &= 0x7fffffff;
764 x = u.f;
765 w = u.i;
766
767 /* |x| < log(2) */
768 if (w < 0x3f317217) {
769 if (w < 0x3f800000 - (12<<23)) {
770 FORCE_EVAL(x + 0x1p120f);
771 return 1;
772 }
773 t = expm1f(x);
774 return 1 + t*t/(2*(1+t));
775 }
776
777 /* |x| < log(FLT_MAX) */
778 if (w < 0x42b17217) {
779 t = expf(x);
780 return 0.5f*(t + 1/t);
781 }
782
783 /* |x| > log(FLT_MAX) or nan */
784 t = __expo2f(x);
785 return t;
786}
787
788/*****************************************************************************/
Damien George769b23a2014-04-03 22:11:28 +0100789/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700790// sinhf from musl-0.9.15
Damien George769b23a2014-04-03 22:11:28 +0100791/*****************************************************************************/
792/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700793
794float sinhf(float x)
795{
796 union {float f; uint32_t i;} u = {.f = x};
797 uint32_t w;
798 float t, h, absx;
799
800 h = 0.5;
801 if (u.i >> 31)
802 h = -h;
803 /* |x| */
804 u.i &= 0x7fffffff;
805 absx = u.f;
806 w = u.i;
807
808 /* |x| < log(FLT_MAX) */
809 if (w < 0x42b17217) {
810 t = expm1f(absx);
811 if (w < 0x3f800000) {
812 if (w < 0x3f800000 - (12<<23))
813 return x;
814 return h*(2*t - t*t/(t+1));
815 }
816 return h*(t + t/(t+1));
817 }
818
819 /* |x| > logf(FLT_MAX) or nan */
820 t = 2*h*__expo2f(absx);
821 return t;
822}