llvm-project
80 строк · 2.0 Кб
1/*
2* Single-precision log function.
3*
4* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5* See https://llvm.org/LICENSE.txt for license information.
6* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*/
8
9#include <math.h>10#include <stdint.h>11#include "math_config.h"12
13/*
14LOGF_TABLE_BITS = 4
15LOGF_POLY_ORDER = 4
16
17ULP error: 0.818 (nearest rounding.)
18Relative error: 1.957 * 2^-26 (before rounding.)
19*/
20
21#define T __logf_data.tab22#define A __logf_data.poly23#define Ln2 __logf_data.ln224#define N (1 << LOGF_TABLE_BITS)25#define OFF 0x3f33000026
27float
28logf (float x)29{
30/* double_t for better performance on targets with FLT_EVAL_METHOD==2. */31double_t z, r, r2, y, y0, invc, logc;32uint32_t ix, iz, tmp;33int k, i;34
35ix = asuint (x);36#if WANT_ROUNDING37/* Fix sign of zero with downward rounding when x==1. */38if (unlikely (ix == 0x3f800000))39return 0;40#endif41if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000))42{43/* x < 0x1p-126 or inf or nan. */44if (ix * 2 == 0)45return __math_divzerof (1);46if (ix == 0x7f800000) /* log(inf) == inf. */47return x;48if ((ix & 0x80000000) || ix * 2 >= 0xff000000)49return __math_invalidf (x);50/* x is subnormal, normalize it. */51ix = asuint (x * 0x1p23f);52ix -= 23 << 23;53}54
55/* x = 2^k z; where z is in range [OFF,2*OFF] and exact.56The range is split into N subintervals.
57The ith subinterval contains z and c is near its center. */
58tmp = ix - OFF;59i = (tmp >> (23 - LOGF_TABLE_BITS)) % N;60k = (int32_t) tmp >> 23; /* arithmetic shift */61iz = ix - (tmp & 0x1ff << 23);62invc = T[i].invc;63logc = T[i].logc;64z = (double_t) asfloat (iz);65
66/* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */67r = z * invc - 1;68y0 = logc + (double_t) k * Ln2;69
70/* Pipelined polynomial evaluation to approximate log1p(r). */71r2 = r * r;72y = A[1] * r + A[2];73y = A[0] * r2 + y;74y = y * r2 + (y0 + r);75return eval_as_float (y);76}
77#if USE_GLIBC_ABI78strong_alias (logf, __logf_finite)79hidden_alias (logf, __ieee754_logf)80#endif81