[444] | 1 | /* Single-precision log function. |
---|
| 2 | Copyright (c) 2017 ARM Ltd. All rights reserved. |
---|
| 3 | |
---|
| 4 | Redistribution and use in source and binary forms, with or without |
---|
| 5 | modification, are permitted provided that the following conditions |
---|
| 6 | are met: |
---|
| 7 | 1. Redistributions of source code must retain the above copyright |
---|
| 8 | notice, this list of conditions and the following disclaimer. |
---|
| 9 | 2. Redistributions in binary form must reproduce the above copyright |
---|
| 10 | notice, this list of conditions and the following disclaimer in the |
---|
| 11 | documentation and/or other materials provided with the distribution. |
---|
| 12 | 3. The name of the company may not be used to endorse or promote |
---|
| 13 | products derived from this software without specific prior written |
---|
| 14 | permission. |
---|
| 15 | |
---|
| 16 | THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS AND ANY EXPRESS OR IMPLIED |
---|
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
---|
| 18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
| 19 | IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
| 21 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
| 26 | |
---|
| 27 | #include "fdlibm.h" |
---|
| 28 | #if !__OBSOLETE_MATH |
---|
| 29 | |
---|
| 30 | #include <math.h> |
---|
| 31 | #include <stdint.h> |
---|
| 32 | #include "math_config.h" |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | LOGF_TABLE_BITS = 4 |
---|
| 36 | LOGF_POLY_ORDER = 4 |
---|
| 37 | |
---|
| 38 | ULP error: 0.818 (nearest rounding.) |
---|
| 39 | Relative error: 1.957 * 2^-26 (before rounding.) |
---|
| 40 | */ |
---|
| 41 | |
---|
| 42 | #define T __logf_data.tab |
---|
| 43 | #define A __logf_data.poly |
---|
| 44 | #define Ln2 __logf_data.ln2 |
---|
| 45 | #define N (1 << LOGF_TABLE_BITS) |
---|
| 46 | #define OFF 0x3f330000 |
---|
| 47 | |
---|
| 48 | float |
---|
| 49 | logf (float x) |
---|
| 50 | { |
---|
| 51 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
---|
| 52 | double_t z, r, r2, y, y0, invc, logc; |
---|
| 53 | uint32_t ix, iz, tmp; |
---|
| 54 | int k, i; |
---|
| 55 | |
---|
| 56 | ix = asuint (x); |
---|
| 57 | #if WANT_ROUNDING |
---|
| 58 | /* Fix sign of zero with downward rounding when x==1. */ |
---|
| 59 | if (__builtin_expect (ix == 0x3f800000, 0)) |
---|
| 60 | return 0; |
---|
| 61 | #endif |
---|
| 62 | if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000, 0)) |
---|
| 63 | { |
---|
| 64 | /* x < 0x1p-126 or inf or nan. */ |
---|
| 65 | if (ix * 2 == 0) |
---|
| 66 | return __math_divzerof (1); |
---|
| 67 | if (ix == 0x7f800000) /* log(inf) == inf. */ |
---|
| 68 | return x; |
---|
| 69 | if ((ix & 0x80000000) || ix * 2 >= 0xff000000) |
---|
| 70 | return __math_invalidf (x); |
---|
| 71 | /* x is subnormal, normalize it. */ |
---|
| 72 | ix = asuint (x * 0x1p23f); |
---|
| 73 | ix -= 23 << 23; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. |
---|
| 77 | The range is split into N subintervals. |
---|
| 78 | The ith subinterval contains z and c is near its center. */ |
---|
| 79 | tmp = ix - OFF; |
---|
| 80 | i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; |
---|
| 81 | k = (int32_t) tmp >> 23; /* arithmetic shift */ |
---|
| 82 | iz = ix - (tmp & 0x1ff << 23); |
---|
| 83 | invc = T[i].invc; |
---|
| 84 | logc = T[i].logc; |
---|
| 85 | z = (double_t) asfloat (iz); |
---|
| 86 | |
---|
| 87 | /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ |
---|
| 88 | r = z * invc - 1; |
---|
| 89 | y0 = logc + (double_t) k * Ln2; |
---|
| 90 | |
---|
| 91 | /* Pipelined polynomial evaluation to approximate log1p(r). */ |
---|
| 92 | r2 = r * r; |
---|
| 93 | y = A[1] * r + A[2]; |
---|
| 94 | y = A[0] * r2 + y; |
---|
| 95 | y = y * r2 + (y0 + r); |
---|
| 96 | return (float) y; |
---|
| 97 | } |
---|
| 98 | #endif /* !__OBSOLETE_MATH */ |
---|