[SampleProfileLoader] Fix integer overflow in generateMDProfMetadata (#90217)
[llvm-project.git] / libclc / generic / lib / math / exp2.cl
blob1ddccbd3ee653ad445901f625c35fb2d3eb303a1
1 /*
2 * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
23 #include <clc/clc.h>
25 #include "math.h"
26 #include "../clcmacro.h"
28 _CLC_OVERLOAD _CLC_DEF float exp2(float x) {
30 // Reduce x
31 const float ln2HI = 0x1.62e300p-1f;
32 const float ln2LO = 0x1.2fefa2p-17f;
34 float t = rint(x);
35 int p = (int)t;
36 float tt = x - t;
37 float hi = tt * ln2HI;
38 float lo = tt * ln2LO;
40 // Evaluate poly
41 t = hi + lo;
42 tt = t*t;
43 float v = mad(tt,
44 -mad(tt,
45 mad(tt,
46 mad(tt,
47 mad(tt, 0x1.637698p-25f, -0x1.bbd41cp-20f),
48 0x1.1566aap-14f),
49 -0x1.6c16c2p-9f),
50 0x1.555556p-3f),
51 t);
53 float y = 1.0f - (((-lo) - MATH_DIVIDE(t * v, 2.0f - v)) - hi);
55 // Scale by 2^p
56 float r = as_float(as_int(y) + (p << 23));
58 const float ulim = 128.0f;
59 const float llim = -126.0f;
61 r = x < llim ? 0.0f : r;
62 r = x < ulim ? r : as_float(0x7f800000);
63 return isnan(x) ? x : r;
66 _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, exp2, float)
68 #ifdef cl_khr_fp64
70 #include "exp_helper.h"
72 #pragma OPENCL EXTENSION cl_khr_fp64 : enable
74 _CLC_OVERLOAD _CLC_DEF double exp2(double x) {
75 const double R_LN2 = 0x1.62e42fefa39efp-1; // ln(2)
76 const double R_1_BY_64 = 1.0 / 64.0;
78 int n = convert_int(x * 64.0);
79 double r = R_LN2 * fma(-R_1_BY_64, (double)n, x);
80 return __clc_exp_helper(x, -1074.0, 1024.0, r, n);
84 _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, exp2, double)
86 #endif