1 //===-- Single-precision 2^x function -------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "math_utils.h"
12 #include "include/math.h"
13 #include "src/__support/common.h"
17 #define T exp2f_data.tab
18 #define C exp2f_data.poly
19 #define SHIFT exp2f_data.shift_scaled
21 namespace __llvm_libc
{
23 float LLVM_LIBC_ENTRYPOINT(exp2f
)(float x
) {
26 // double_t for better performance on targets with FLT_EVAL_METHOD==2.
27 double_t kd
, xd
, z
, r
, r2
, y
, s
;
29 xd
= static_cast<double_t
>(x
);
30 abstop
= top12_bits(x
) & 0x7ff;
31 if (unlikely(abstop
>= top12_bits(128.0f
))) {
32 // |x| >= 128 or x is nan.
33 if (as_uint32_bits(x
) == as_uint32_bits(-INFINITY
))
35 if (abstop
>= top12_bits(INFINITY
))
38 return overflow
<float>(0);
40 return underflow
<float>(0);
42 return may_underflow
<float>(0);
45 // x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.
46 kd
= static_cast<double>(xd
+ SHIFT
);
47 ki
= as_uint64_bits(kd
);
48 kd
-= SHIFT
; // k/N for int k.
51 // exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1)
53 t
+= ki
<< (52 - EXP2F_TABLE_BITS
);
60 return static_cast<float>(y
);
63 } // namespace __llvm_libc