Fraser Cormack | 82912fd | 2025-03-20 11:40:09 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 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 Stellard | d538fdc | 2015-05-13 03:55:07 +0000 | [diff] [blame] | 8 | |
| 9 | #include <clc/clc.h> |
Fraser Cormack | 78b5bb7 | 2025-01-28 14:17:23 +0000 | [diff] [blame] | 10 | #include <clc/math/math.h> |
| 11 | #include <clc/math/tables.h> |
Tom Stellard | d538fdc | 2015-05-13 03:55:07 +0000 | [diff] [blame] | 12 | |
| 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 Cormack | b52977b | 2025-03-28 08:23:24 +0000 | [diff] [blame] | 33 | 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 Stellard | d538fdc | 2015-05-13 03:55:07 +0000 | [diff] [blame] | 36 | |
| 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 |