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