blob: cdb55a8adaf597b74fab317d7054bd405d3db3c6 [file] [log] [blame]
Dave Hylandsf14b92b2014-03-12 18:06:26 -07001#include <stdint.h>
2#include <math.h>
3
4// these 2 functions seem to actually work... no idea why
5// replacing with libgcc does not work (probably due to wrong calling conventions)
6double __aeabi_f2d(float x) {
7 // TODO
8 return 0.0;
9}
10
11float __aeabi_d2f(double x) {
12 // TODO
13 return 0.0;
14}
15
16/*
17double sqrt(double x) {
18 // TODO
19 return 0.0;
20}
21*/
22
23float sqrtf(float x) {
24 asm volatile (
25 "vsqrt.f32 %[r], %[x]\n"
26 : [r] "=t" (x)
27 : [x] "t" (x));
28 return x;
29}
30
31// TODO we need import these functions from some library (eg musl or newlib)
32float powf(float x, float y) { return 0.0; }
33float logf(float x) { return 0.0; }
34float log2f(float x) { return 0.0; }
35float log10f(float x) { return 0.0; }
36float tanhf(float x) { return 0.0; }
37float acoshf(float x) { return 0.0; }
38float asinhf(float x) { return 0.0; }
39float atanhf(float x) { return 0.0; }
40float cosf(float x) { return 0.0; }
41float sinf(float x) { return 0.0; }
42float tanf(float x) { return 0.0; }
43float acosf(float x) { return 0.0; }
44float asinf(float x) { return 0.0; }
45float atanf(float x) { return 0.0; }
46float atan2f(float x, float y) { return 0.0; }
Damien Georgec070ff22014-03-21 20:52:54 +000047float ceilf(float x) { return 0.0; }
48float floorf(float x) { return 0.0; }
49float truncf(float x) { return 0.0; }
50float fmodf(float x, float y) { return 0.0; }
Damien George81382052014-03-22 20:44:43 +000051float gammaf(float x) { return 0.0; }
52float lgammaf(float x) { return 0.0; }
53float erff(float x) { return 0.0; }
54float erfcf(float x) { return 0.0; }
55float modff(float x, float *y) { return 0.0; }
56float frexpf(float x, int *exp) { return 0.0; }
57float ldexpf(float x, int exp) { return 0.0; }
58int __fpclassifyf(float x) { return 0; }
Dave Hylandsf14b92b2014-03-12 18:06:26 -070059
60/*****************************************************************************/
61// from musl-0.9.15 libm.h
62
63/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
64/*
65 * ====================================================
66 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
67 *
68 * Developed at SunPro, a Sun Microsystems, Inc. business.
69 * Permission to use, copy, modify, and distribute this
70 * software is freely granted, provided that this notice
71 * is preserved.
72 * ====================================================
73 */
74
75#define FORCE_EVAL(x) do { \
76 if (sizeof(x) == sizeof(float)) { \
77 volatile float __x; \
78 __x = (x); \
79 (void)__x; \
80 } else if (sizeof(x) == sizeof(double)) { \
81 volatile double __x; \
82 __x = (x); \
83 (void)__x; \
84 } else { \
85 volatile long double __x; \
86 __x = (x); \
87 (void)__x; \
88 } \
89} while(0)
90
91/* Get a 32 bit int from a float. */
92#define GET_FLOAT_WORD(w,d) \
93do { \
94 union {float f; uint32_t i;} __u; \
95 __u.f = (d); \
96 (w) = __u.i; \
97} while (0)
98
99/* Set a float from a 32 bit int. */
100#define SET_FLOAT_WORD(d,w) \
101do { \
102 union {float f; uint32_t i;} __u; \
103 __u.i = (w); \
104 (d) = __u.f; \
105} while (0)
106
107/*****************************************************************************/
108// scalbnf from musl-0.9.15
109
110float scalbnf(float x, int n)
111{
112 union {float f; uint32_t i;} u;
113 float_t y = x;
114
115 if (n > 127) {
116 y *= 0x1p127f;
117 n -= 127;
118 if (n > 127) {
119 y *= 0x1p127f;
120 n -= 127;
121 if (n > 127)
122 n = 127;
123 }
124 } else if (n < -126) {
125 y *= 0x1p-126f;
126 n += 126;
127 if (n < -126) {
128 y *= 0x1p-126f;
129 n += 126;
130 if (n < -126)
131 n = -126;
132 }
133 }
134 u.i = (uint32_t)(0x7f+n)<<23;
135 x = y * u.f;
136 return x;
137}
138
139/*****************************************************************************/
140// expf from musl-0.9.15
141
142/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
143/*
144 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
145 */
146/*
147 * ====================================================
148 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
149 *
150 * Developed at SunPro, a Sun Microsystems, Inc. business.
151 * Permission to use, copy, modify, and distribute this
152 * software is freely granted, provided that this notice
153 * is preserved.
154 * ====================================================
155 */
156
157static const float
158half[2] = {0.5,-0.5},
159ln2hi = 6.9314575195e-1f, /* 0x3f317200 */
160ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */
161invln2 = 1.4426950216e+0f, /* 0x3fb8aa3b */
162/*
163 * Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
164 * |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
165 */
166P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */
167P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */
168
169float expf(float x)
170{
171 float_t hi, lo, c, xx, y;
172 int k, sign;
173 uint32_t hx;
174
175 GET_FLOAT_WORD(hx, x);
176 sign = hx >> 31; /* sign bit of x */
177 hx &= 0x7fffffff; /* high word of |x| */
178
179 /* special cases */
180 if (hx >= 0x42aeac50) { /* if |x| >= -87.33655f or NaN */
181 if (hx >= 0x42b17218 && !sign) { /* x >= 88.722839f */
182 /* overflow */
183 x *= 0x1p127f;
184 return x;
185 }
186 if (sign) {
187 /* underflow */
188 FORCE_EVAL(-0x1p-149f/x);
189 if (hx >= 0x42cff1b5) /* x <= -103.972084f */
190 return 0;
191 }
192 }
193
194 /* argument reduction */
195 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
196 if (hx > 0x3f851592) /* if |x| > 1.5 ln2 */
197 k = invln2*x + half[sign];
198 else
199 k = 1 - sign - sign;
200 hi = x - k*ln2hi; /* k*ln2hi is exact here */
201 lo = k*ln2lo;
202 x = hi - lo;
203 } else if (hx > 0x39000000) { /* |x| > 2**-14 */
204 k = 0;
205 hi = x;
206 lo = 0;
207 } else {
208 /* raise inexact */
209 FORCE_EVAL(0x1p127f + x);
210 return 1 + x;
211 }
212
213 /* x is now in primary range */
214 xx = x*x;
215 c = x - xx*(P1+xx*P2);
216 y = 1 + (x*c/(2-c) - lo + hi);
217 if (k == 0)
218 return y;
219 return scalbnf(y, k);
220}
221
222/*****************************************************************************/
223// expm1f from musl-0.9.15
224
225/* origin: FreeBSD /usr/src/lib/msun/src/s_expm1f.c */
226/*
227 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
228 */
229/*
230 * ====================================================
231 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
232 *
233 * Developed at SunPro, a Sun Microsystems, Inc. business.
234 * Permission to use, copy, modify, and distribute this
235 * software is freely granted, provided that this notice
236 * is preserved.
237 * ====================================================
238 */
239
240static const float
241o_threshold = 8.8721679688e+01, /* 0x42b17180 */
242ln2_hi = 6.9313812256e-01, /* 0x3f317180 */
243ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */
244//invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */
245/*
246 * Domain [-0.34568, 0.34568], range ~[-6.694e-10, 6.696e-10]:
247 * |6 / x * (1 + 2 * (1 / (exp(x) - 1) - 1 / x)) - q(x)| < 2**-30.04
248 * Scaled coefficients: Qn_here = 2**n * Qn_for_q (see s_expm1.c):
249 */
250Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */
251Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */
252
253float expm1f(float x)
254{
255 float_t y,hi,lo,c,t,e,hxs,hfx,r1,twopk;
256 union {float f; uint32_t i;} u = {x};
257 uint32_t hx = u.i & 0x7fffffff;
258 int k, sign = u.i >> 31;
259
260 /* filter out huge and non-finite argument */
261 if (hx >= 0x4195b844) { /* if |x|>=27*ln2 */
262 if (hx > 0x7f800000) /* NaN */
263 return x;
264 if (sign)
265 return -1;
266 if (x > o_threshold) {
267 x *= 0x1p127f;
268 return x;
269 }
270 }
271
272 /* argument reduction */
273 if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */
274 if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */
275 if (!sign) {
276 hi = x - ln2_hi;
277 lo = ln2_lo;
278 k = 1;
279 } else {
280 hi = x + ln2_hi;
281 lo = -ln2_lo;
282 k = -1;
283 }
284 } else {
285 k = invln2*x + (sign ? -0.5f : 0.5f);
286 t = k;
287 hi = x - t*ln2_hi; /* t*ln2_hi is exact here */
288 lo = t*ln2_lo;
289 }
290 x = hi-lo;
291 c = (hi-x)-lo;
292 } else if (hx < 0x33000000) { /* when |x|<2**-25, return x */
293 if (hx < 0x00800000)
294 FORCE_EVAL(x*x);
295 return x;
296 } else
297 k = 0;
298
299 /* x is now in primary range */
300 hfx = 0.5f*x;
301 hxs = x*hfx;
302 r1 = 1.0f+hxs*(Q1+hxs*Q2);
303 t = 3.0f - r1*hfx;
304 e = hxs*((r1-t)/(6.0f - x*t));
305 if (k == 0) /* c is 0 */
306 return x - (x*e-hxs);
307 e = x*(e-c) - c;
308 e -= hxs;
309 /* exp(x) ~ 2^k (x_reduced - e + 1) */
310 if (k == -1)
311 return 0.5f*(x-e) - 0.5f;
312 if (k == 1) {
313 if (x < -0.25f)
314 return -2.0f*(e-(x+0.5f));
315 return 1.0f + 2.0f*(x-e);
316 }
317 u.i = (0x7f+k)<<23; /* 2^k */
318 twopk = u.f;
319 if (k < 0 || k > 56) { /* suffice to return exp(x)-1 */
320 y = x - e + 1.0f;
321 if (k == 128)
322 y = y*2.0f*0x1p127f;
323 else
324 y = y*twopk;
325 return y - 1.0f;
326 }
327 u.i = (0x7f-k)<<23; /* 2^-k */
328 if (k < 23)
329 y = (x-e+(1-u.f))*twopk;
330 else
331 y = (x-(e+u.f)+1)*twopk;
332 return y;
333}
334
335/*****************************************************************************/
336// __expo2f from musl-0.9.15
337
338/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
339static const int k = 235;
340static const float kln2 = 0x1.45c778p+7f;
341
342/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
343float __expo2f(float x)
344{
345 float scale;
346
347 /* note that k is odd and scale*scale overflows */
348 SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23);
349 /* exp(x - k ln2) * 2**(k-1) */
350 return expf(x - kln2) * scale * scale;
351}
352
353/*****************************************************************************/
354// coshf from musl-0.9.15
355
356float coshf(float x)
357{
358 union {float f; uint32_t i;} u = {.f = x};
359 uint32_t w;
360 float t;
361
362 /* |x| */
363 u.i &= 0x7fffffff;
364 x = u.f;
365 w = u.i;
366
367 /* |x| < log(2) */
368 if (w < 0x3f317217) {
369 if (w < 0x3f800000 - (12<<23)) {
370 FORCE_EVAL(x + 0x1p120f);
371 return 1;
372 }
373 t = expm1f(x);
374 return 1 + t*t/(2*(1+t));
375 }
376
377 /* |x| < log(FLT_MAX) */
378 if (w < 0x42b17217) {
379 t = expf(x);
380 return 0.5f*(t + 1/t);
381 }
382
383 /* |x| > log(FLT_MAX) or nan */
384 t = __expo2f(x);
385 return t;
386}
387
388/*****************************************************************************/
389// sinhf from musl-0.9.15
390
391float sinhf(float x)
392{
393 union {float f; uint32_t i;} u = {.f = x};
394 uint32_t w;
395 float t, h, absx;
396
397 h = 0.5;
398 if (u.i >> 31)
399 h = -h;
400 /* |x| */
401 u.i &= 0x7fffffff;
402 absx = u.f;
403 w = u.i;
404
405 /* |x| < log(FLT_MAX) */
406 if (w < 0x42b17217) {
407 t = expm1f(absx);
408 if (w < 0x3f800000) {
409 if (w < 0x3f800000 - (12<<23))
410 return x;
411 return h*(2*t - t*t/(t+1));
412 }
413 return h*(t + t/(t+1));
414 }
415
416 /* |x| > logf(FLT_MAX) or nan */
417 t = 2*h*__expo2f(absx);
418 return t;
419}