George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 1 | /* |
Szabolcs Nagy | 1b94597 | 2018-05-14 14:46:40 +0100 | [diff] [blame] | 2 | * Single-precision log function. |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 3 | * |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 4 | * Copyright (c) 2017-2018, Arm Limited. |
| 5 | * SPDX-License-Identifier: MIT |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 8 | #include <math.h> |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include "math_config.h" |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 11 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 12 | /* |
| 13 | LOGF_TABLE_BITS = 4 |
| 14 | LOGF_POLY_ORDER = 4 |
| 15 | |
| 16 | ULP error: 0.818 (nearest rounding.) |
| 17 | Relative error: 1.957 * 2^-26 (before rounding.) |
| 18 | */ |
| 19 | |
| 20 | #define T __logf_data.tab |
| 21 | #define A __logf_data.poly |
| 22 | #define Ln2 __logf_data.ln2 |
| 23 | #define N (1 << LOGF_TABLE_BITS) |
| 24 | #define OFF 0x3f330000 |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 25 | |
| 26 | float |
Szabolcs Nagy | 0d51c04 | 2018-04-25 13:26:22 +0100 | [diff] [blame] | 27 | logf (float x) |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 28 | { |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 29 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 30 | double_t z, r, r2, y, y0, invc, logc; |
| 31 | uint32_t ix, iz, tmp; |
| 32 | int k, i; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 33 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 34 | ix = asuint (x); |
| 35 | #if WANT_ROUNDING |
| 36 | /* Fix sign of zero with downward rounding when x==1. */ |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 37 | if (unlikely (ix == 0x3f800000)) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 38 | return 0; |
| 39 | #endif |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 40 | if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000)) |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 41 | { |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 42 | /* x < 0x1p-126 or inf or nan. */ |
| 43 | if (ix * 2 == 0) |
| 44 | return __math_divzerof (1); |
| 45 | if (ix == 0x7f800000) /* log(inf) == inf. */ |
| 46 | return x; |
| 47 | if ((ix & 0x80000000) || ix * 2 >= 0xff000000) |
| 48 | return __math_invalidf (x); |
| 49 | /* x is subnormal, normalize it. */ |
| 50 | ix = asuint (x * 0x1p23f); |
| 51 | ix -= 23 << 23; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 54 | /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. |
| 55 | The range is split into N subintervals. |
| 56 | The ith subinterval contains z and c is near its center. */ |
| 57 | tmp = ix - OFF; |
| 58 | i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; |
| 59 | k = (int32_t) tmp >> 23; /* arithmetic shift */ |
| 60 | iz = ix - (tmp & 0x1ff << 23); |
| 61 | invc = T[i].invc; |
| 62 | logc = T[i].logc; |
| 63 | z = (double_t) asfloat (iz); |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 64 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 65 | /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ |
| 66 | r = z * invc - 1; |
| 67 | y0 = logc + (double_t) k * Ln2; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 68 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 69 | /* Pipelined polynomial evaluation to approximate log1p(r). */ |
| 70 | r2 = r * r; |
| 71 | y = A[1] * r + A[2]; |
| 72 | y = A[0] * r2 + y; |
| 73 | y = y * r2 + (y0 + r); |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 74 | return eval_as_float (y); |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 75 | } |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 76 | #if USE_GLIBC_ABI |
| 77 | strong_alias (logf, __logf_finite) |
| 78 | hidden_alias (logf, __ieee754_logf) |
| 79 | #endif |