[clang] Avoid linking libdl unless needed
[llvm-project.git] / libc / AOR_v20.02 / math / log2f.c
bloba43814f9827c0a8aba48064bb1a056a095f8f918
1 /*
2 * Single-precision log2 function.
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 */
9 #include <math.h>
10 #include <stdint.h>
11 #include "math_config.h"
14 LOG2F_TABLE_BITS = 4
15 LOG2F_POLY_ORDER = 4
17 ULP error: 0.752 (nearest rounding.)
18 Relative error: 1.9 * 2^-26 (before rounding.)
21 #define N (1 << LOG2F_TABLE_BITS)
22 #define T __log2f_data.tab
23 #define A __log2f_data.poly
24 #define OFF 0x3f330000
26 float
27 log2f (float x)
29 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
30 double_t z, r, r2, p, y, y0, invc, logc;
31 uint32_t ix, iz, top, tmp;
32 int k, i;
34 ix = asuint (x);
35 #if WANT_ROUNDING
36 /* Fix sign of zero with downward rounding when x==1. */
37 if (unlikely (ix == 0x3f800000))
38 return 0;
39 #endif
40 if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000))
42 /* x < 0x1p-126 or inf or nan. */
43 if (ix * 2 == 0)
44 return __math_divzerof (1);
45 if (ix == 0x7f800000) /* log2(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;
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 - LOG2F_TABLE_BITS)) % N;
59 top = tmp & 0xff800000;
60 iz = ix - top;
61 k = (int32_t) tmp >> 23; /* arithmetic shift */
62 invc = T[i].invc;
63 logc = T[i].logc;
64 z = (double_t) asfloat (iz);
66 /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
67 r = z * invc - 1;
68 y0 = logc + (double_t) k;
70 /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */
71 r2 = r * r;
72 y = A[1] * r + A[2];
73 y = A[0] * r2 + y;
74 p = A[3] * r + y0;
75 y = y * r2 + p;
76 return eval_as_float (y);
78 #if USE_GLIBC_ABI
79 strong_alias (log2f, __log2f_finite)
80 hidden_alias (log2f, __ieee754_log2f)
81 #endif