blob: b413228719bfb2f74f706857ba7582ecea74aaed [file] [log] [blame]
Fraser Cormack82912fd2025-03-20 11:40:09 +00001//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
Tom Stellardd538fdc2015-05-13 03:55:07 +00008
9#include <clc/clc.h>
Fraser Cormack78b5bb72025-01-28 14:17:23 +000010#include <clc/math/math.h>
11#include <clc/math/tables.h>
Tom Stellardd538fdc2015-05-13 03:55:07 +000012
13#ifdef cl_khr_fp64
14
15#pragma OPENCL EXTENSION cl_khr_fp64 : enable
16
17_CLC_DEF double __clc_exp_helper(double x, double x_min, double x_max, double r, int n) {
18
19 int j = n & 0x3f;
20 int m = n >> 6;
21
22 // 6 term tail of Taylor expansion of e^r
23 double z2 = r * fma(r,
24 fma(r,
25 fma(r,
26 fma(r,
27 fma(r, 0x1.6c16c16c16c17p-10, 0x1.1111111111111p-7),
28 0x1.5555555555555p-5),
29 0x1.5555555555555p-3),
30 0x1.0000000000000p-1),
31 1.0);
32
Fraser Cormackb52977b2025-03-28 08:23:24 +000033 double tv0 = USE_TABLE(two_to_jby64_ep_tbl_head, j);
34 double tv1 = USE_TABLE(two_to_jby64_ep_tbl_tail, j);
35 z2 = fma(tv0 + tv1, z2, tv1) + tv0;
Tom Stellardd538fdc2015-05-13 03:55:07 +000036
37 int small_value = (m < -1022) || ((m == -1022) && (z2 < 1.0));
38
39 int n1 = m >> 2;
40 int n2 = m-n1;
41 double z3= z2 * as_double(((long)n1 + 1023) << 52);
42 z3 *= as_double(((long)n2 + 1023) << 52);
43
44 z2 = ldexp(z2, m);
45 z2 = small_value ? z3: z2;
46
47 z2 = isnan(x) ? x : z2;
48
49 z2 = x > x_max ? as_double(PINFBITPATT_DP64) : z2;
50 z2 = x < x_min ? 0.0 : z2;
51
52 return z2;
53}
54
55#endif // cl_khr_fp64