Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 1 | #include <stdint.h> |
Damien George | e90eefc | 2014-04-02 19:55:08 +0100 | [diff] [blame^] | 2 | #include <math.h> |
| 3 | |
Damien George | 5a16658 | 2014-03-23 00:34:49 +0000 | [diff] [blame] | 4 | typedef float float_t; |
| 5 | typedef 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 Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 13 | |
Damien George | 5a16658 | 2014-03-23 00:34:49 +0000 | [diff] [blame] | 14 | typedef 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 | |
| 23 | double __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 Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Damien George | 5a16658 | 2014-03-23 00:34:49 +0000 | [diff] [blame] | 35 | float __attribute__((pcs("aapcs"))) __aeabi_d2f(double x) { |
| 36 | float_s_t fx={0}; |
| 37 | double_s_t dx={0}; |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 38 | |
Damien George | 5a16658 | 2014-03-23 00:34:49 +0000 | [diff] [blame] | 39 | 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 | } |
| 45 | double __aeabi_dmul(double x , double y) { |
| 46 | return 0.0; |
| 47 | |
| 48 | } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 49 | /* |
| 50 | double sqrt(double x) { |
| 51 | // TODO |
| 52 | return 0.0; |
| 53 | } |
| 54 | */ |
| 55 | |
| 56 | float 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) |
| 65 | float powf(float x, float y) { return 0.0; } |
| 66 | float logf(float x) { return 0.0; } |
| 67 | float log2f(float x) { return 0.0; } |
| 68 | float log10f(float x) { return 0.0; } |
| 69 | float tanhf(float x) { return 0.0; } |
| 70 | float acoshf(float x) { return 0.0; } |
| 71 | float asinhf(float x) { return 0.0; } |
| 72 | float atanhf(float x) { return 0.0; } |
| 73 | float cosf(float x) { return 0.0; } |
| 74 | float sinf(float x) { return 0.0; } |
| 75 | float tanf(float x) { return 0.0; } |
| 76 | float acosf(float x) { return 0.0; } |
| 77 | float asinf(float x) { return 0.0; } |
| 78 | float atanf(float x) { return 0.0; } |
| 79 | float atan2f(float x, float y) { return 0.0; } |
Damien George | c070ff2 | 2014-03-21 20:52:54 +0000 | [diff] [blame] | 80 | float ceilf(float x) { return 0.0; } |
| 81 | float floorf(float x) { return 0.0; } |
| 82 | float truncf(float x) { return 0.0; } |
| 83 | float fmodf(float x, float y) { return 0.0; } |
Damien George | 90834b9 | 2014-03-23 14:00:02 +0000 | [diff] [blame] | 84 | float tgammaf(float x) { return 0.0; } |
Damien George | 8138205 | 2014-03-22 20:44:43 +0000 | [diff] [blame] | 85 | float lgammaf(float x) { return 0.0; } |
| 86 | float erff(float x) { return 0.0; } |
| 87 | float erfcf(float x) { return 0.0; } |
| 88 | float modff(float x, float *y) { return 0.0; } |
| 89 | float frexpf(float x, int *exp) { return 0.0; } |
| 90 | float ldexpf(float x, int exp) { return 0.0; } |
Dave Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 91 | |
| 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) \ |
| 125 | do { \ |
| 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) \ |
| 133 | do { \ |
| 134 | union {float f; uint32_t i;} __u; \ |
| 135 | __u.i = (w); \ |
| 136 | (d) = __u.f; \ |
| 137 | } while (0) |
| 138 | |
| 139 | /*****************************************************************************/ |
Damien George | e90eefc | 2014-04-02 19:55:08 +0100 | [diff] [blame^] | 140 | // __fpclassifyf from musl-0.9.15 |
| 141 | |
| 142 | int __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 Hylands | f14b92b | 2014-03-12 18:06:26 -0700 | [diff] [blame] | 152 | // scalbnf from musl-0.9.15 |
| 153 | |
| 154 | float 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 | |
| 201 | static const float |
| 202 | half[2] = {0.5,-0.5}, |
| 203 | ln2hi = 6.9314575195e-1f, /* 0x3f317200 */ |
| 204 | ln2lo = 1.4286067653e-6f, /* 0x35bfbe8e */ |
| 205 | invln2 = 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 | */ |
| 210 | P1 = 1.6666625440e-1f, /* 0xaaaa8f.0p-26 */ |
| 211 | P2 = -2.7667332906e-3f; /* -0xb55215.0p-32 */ |
| 212 | |
| 213 | float 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 | |
| 284 | static const float |
| 285 | o_threshold = 8.8721679688e+01, /* 0x42b17180 */ |
| 286 | ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ |
| 287 | ln2_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 | */ |
| 294 | Q1 = -3.3333212137e-2, /* -0x888868.0p-28 */ |
| 295 | Q2 = 1.5807170421e-3; /* 0xcf3010.0p-33 */ |
| 296 | |
| 297 | float 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) */ |
| 383 | static const int k = 235; |
| 384 | static 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) */ |
| 387 | float __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 | |
| 400 | float 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 | |
| 435 | float 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 | } |