blob: 534389df519c45ac15a322744a7641a298fea88a [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/*
50double sqrt(double x) {
51 // TODO
52 return 0.0;
53}
54*/
55
56float sqrtf(float x) {
57 asm volatile (
58 "vsqrt.f32 %[r], %[x]\n"
59 : [r] "=t" (x)
60 : [x] "t" (x));
61 return x;
62}
63
64// TODO we need import these functions from some library (eg musl or newlib)
65float powf(float x, float y) { return 0.0; }
66float logf(float x) { return 0.0; }
67float log2f(float x) { return 0.0; }
68float log10f(float x) { return 0.0; }
69float tanhf(float x) { return 0.0; }
70float 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/*****************************************************************************/
93// from musl-0.9.15 libm.h
94
95/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
96/*
97 * ====================================================
98 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
99 *
100 * Developed at SunPro, a Sun Microsystems, Inc. business.
101 * Permission to use, copy, modify, and distribute this
102 * software is freely granted, provided that this notice
103 * is preserved.
104 * ====================================================
105 */
106
107#define FORCE_EVAL(x) do { \
108 if (sizeof(x) == sizeof(float)) { \
109 volatile float __x; \
110 __x = (x); \
111 (void)__x; \
112 } else if (sizeof(x) == sizeof(double)) { \
113 volatile double __x; \
114 __x = (x); \
115 (void)__x; \
116 } else { \
117 volatile long double __x; \
118 __x = (x); \
119 (void)__x; \
120 } \
121} while(0)
122
123/* Get a 32 bit int from a float. */
124#define GET_FLOAT_WORD(w,d) \
125do { \
126 union {float f; uint32_t i;} __u; \
127 __u.f = (d); \
128 (w) = __u.i; \
129} while (0)
130
131/* Set a float from a 32 bit int. */
132#define SET_FLOAT_WORD(d,w) \
133do { \
134 union {float f; uint32_t i;} __u; \
135 __u.i = (w); \
136 (d) = __u.f; \
137} while (0)
138
139/*****************************************************************************/
Damien Georgee90eefc2014-04-02 19:55:08 +0100140// __fpclassifyf from musl-0.9.15
141
142int __fpclassifyf(float x)
143{
144 union {float f; uint32_t i;} u = {x};
145 int e = u.i>>23 & 0xff;
146 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
147 if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
148 return FP_NORMAL;
149}
150
151/*****************************************************************************/
Dave Hylandsf14b92b2014-03-12 18:06:26 -0700152// scalbnf from musl-0.9.15
153
154float scalbnf(float x, int n)
155{
156 union {float f; uint32_t i;} u;
157 float_t y = x;
158
159 if (n > 127) {
160 y *= 0x1p127f;
161 n -= 127;
162 if (n > 127) {
163 y *= 0x1p127f;
164 n -= 127;
165 if (n > 127)
166 n = 127;
167 }
168 } else if (n < -126) {
169 y *= 0x1p-126f;
170 n += 126;
171 if (n < -126) {
172 y *= 0x1p-126f;
173 n += 126;
174 if (n < -126)
175 n = -126;
176 }
177 }
178 u.i = (uint32_t)(0x7f+n)<<23;
179 x = y * u.f;
180 return x;
181}
182
183/*****************************************************************************/
184// expf from musl-0.9.15
185
186/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
187/*
188 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
189 */
190/*
191 * ====================================================
192 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
193 *
194 * Developed at SunPro, a Sun Microsystems, Inc. business.
195 * Permission to use, copy, modify, and distribute this
196 * software is freely granted, provided that this notice
197 * is preserved.
198 * ====================================================
199 */
200
201static const float
202half[2] = {0.5,-0.5},
203ln2hi = 6.9314575195e-1f, /* 0x3f317200 */
204ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */
205invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */
206/*
207 * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
208 * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
209 */
210P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */
211P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */
212
213float expf(float x)
214{
215 float_t hi, lo, c, xx, y;
216 int k, sign;
217 uint32_t hx;
218
219 GET_FLOAT_WORD(hx, x);
220 sign = hx >> 31; /* sign bit of x */
221 hx &= 0x7fffffff; /* high word of |x| */
222
223 /* special cases */
224 if (hx >= 0x42aeac50) { /* if |x| >= -87.33655f or NaN */
225 if (hx >= 0x42b17218 && !sign) { /* x >= 88.722839f */
226 /* overflow */
227 x *= 0x1p127f;
228 return x;
229 }
230 if (sign) {
231 /* underflow */
232 FORCE_EVAL(-0x1p-149f/x);
233 if (hx >= 0x42cff1b5) /* x <= -103.972084f */
234 return 0;
235 }
236 }
237
238 /* argument reduction */
239 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
240 if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
241 k = invln2*x + half[sign];
242 else
243 k = 1 - sign - sign;
244 hi = x - k*ln2hi; /* k*ln2hi is exact here */
245 lo = k*ln2lo;
246 x = hi - lo;
247 } else if (hx > 0x39000000) { /* |x| > 2**-14 */
248 k = 0;
249 hi = x;
250 lo = 0;
251 } else {
252 /* raise inexact */
253 FORCE_EVAL(0x1p127f + x);
254 return 1 + x;
255 }
256
257 /* x is now in primary range */
258 xx = x*x;
259 c = x - xx*(P1+xx*P2);
260 y = 1 + (x*c/(2-c) - lo + hi);
261 if (k == 0)
262 return y;
263 return scalbnf(y, k);
264}
265
266/*****************************************************************************/
267// expm1f from musl-0.9.15
268
269/* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */
270/*
271 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
272 */
273/*
274 * ====================================================
275 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
276 *
277 * Developed at SunPro, a Sun Microsystems, Inc. business.
278 * Permission to use, copy, modify, and distribute this
279 * software is freely granted, provided that this notice
280 * is preserved.
281 * ====================================================
282 */
283
284static const float
285o_threshold = 8.8721679688e+01, /* 0x42b17180 */
286ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
287ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
288//invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
289/*
290 * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
291 * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
292 * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
293 */
294Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
295Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */
296
297float expm1f(float x)
298{
299 float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
300 union {float f; uint32_t i;} u = {x};
301 uint32_t hx = u.i & 0x7fffffff;
302 int k, sign = u.i >> 31;
303
304 /* filter out huge and non-finite argument */
305 if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */
306 if (hx > 0x7f800000) /* NaN */
307 return x;
308 if (sign)
309 return -1;
310 if (x > o_threshold) {
311 x *= 0x1p127f;
312 return x;
313 }
314 }
315
316 /* argument reduction */
317 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
318 if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
319 if (!sign) {
320 hi = x - ln2_hi;
321 lo = ln2_lo;
322 k = 1;
323 } else {
324 hi = x + ln2_hi;
325 lo = -ln2_lo;
326 k = -1;
327 }
328 } else {
329 k = invln2*x + (sign ? -0.5f : 0.5f);
330 t = k;
331 hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
332 lo = t*ln2_lo;
333 }
334 x = hi-lo;
335 c = (hi-x)-lo;
336 } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */
337 if (hx < 0x00800000)
338 FORCE_EVAL(x*x);
339 return x;
340 } else
341 k = 0;
342
343 /* x is now in primary range */
344 hfx = 0.5f*x;
345 hxs = x*hfx;
346 r1 = 1.0f+hxs*(Q1+hxs*Q2);
347 t = 3.0f - r1*hfx;
348 e = hxs*((r1-t)/(6.0f - x*t));
349 if (k == 0) /* c is 0 */
350 return x - (x*e-hxs);
351 e = x*(e-c) - c;
352 e -= hxs;
353 /* exp(x) ~ 2^k (x_reduced - e + 1) */
354 if (k == -1)
355 return 0.5f*(x-e) - 0.5f;
356 if (k == 1) {
357 if (x < -0.25f)
358 return -2.0f*(e-(x+0.5f));
359 return 1.0f + 2.0f*(x-e);
360 }
361 u.i = (0x7f+k)<<23; /* 2^k */
362 twopk = u.f;
363 if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */
364 y = x - e + 1.0f;
365 if (k == 128)
366 y = y*2.0f*0x1p127f;
367 else
368 y = y*twopk;
369 return y - 1.0f;
370 }
371 u.i = (0x7f-k)<<23; /* 2^-k */
372 if (k < 23)
373 y = (x-e+(1-u.f))*twopk;
374 else
375 y = (x-(e+u.f)+1)*twopk;
376 return y;
377}
378
379/*****************************************************************************/
380// __expo2f from musl-0.9.15
381
382/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
383static const int k = 235;
384static const float kln2 = 0x1.45c778p+7f;
385
386/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
387float __expo2f(float x)
388{
389 float scale;
390
391 /* note that k is odd and scale*scale overflows */
392 SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23);
393 /* exp(x - k ln2) * 2**(k-1) */
394 return expf(x - kln2) * scale * scale;
395}
396
397/*****************************************************************************/
398// coshf from musl-0.9.15
399
400float coshf(float x)
401{
402 union {float f; uint32_t i;} u = {.f = x};
403 uint32_t w;
404 float t;
405
406 /* |x| */
407 u.i &= 0x7fffffff;
408 x = u.f;
409 w = u.i;
410
411 /* |x| < log(2) */
412 if (w < 0x3f317217) {
413 if (w < 0x3f800000 - (12<<23)) {
414 FORCE_EVAL(x + 0x1p120f);
415 return 1;
416 }
417 t = expm1f(x);
418 return 1 + t*t/(2*(1+t));
419 }
420
421 /* |x| < log(FLT_MAX) */
422 if (w < 0x42b17217) {
423 t = expf(x);
424 return 0.5f*(t + 1/t);
425 }
426
427 /* |x| > log(FLT_MAX) or nan */
428 t = __expo2f(x);
429 return t;
430}
431
432/*****************************************************************************/
433// sinhf from musl-0.9.15
434
435float sinhf(float x)
436{
437 union {float f; uint32_t i;} u = {.f = x};
438 uint32_t w;
439 float t, h, absx;
440
441 h = 0.5;
442 if (u.i >> 31)
443 h = -h;
444 /* |x| */
445 u.i &= 0x7fffffff;
446 absx = u.f;
447 w = u.i;
448
449 /* |x| < log(FLT_MAX) */
450 if (w < 0x42b17217) {
451 t = expm1f(absx);
452 if (w < 0x3f800000) {
453 if (w < 0x3f800000 - (12<<23))
454 return x;
455 return h*(2*t - t*t/(t+1));
456 }
457 return h*(t + t/(t+1));
458 }
459
460 /* |x| > logf(FLT_MAX) or nan */
461 t = 2*h*__expo2f(absx);
462 return t;
463}