Tom Stellard | 4f8d262 | 2015-07-10 13:37:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Advanced Micro Devices, Inc. |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #include <clc/clc.h> |
| 24 | #include "../../../generic/lib/clcmacro.h" |
| 25 | #include "math/clc_sqrt.h" |
| 26 | |
| 27 | _CLC_DEFINE_UNARY_BUILTIN(float, sqrt, __clc_sqrt, float) |
| 28 | |
| 29 | #ifdef cl_khr_fp64 |
| 30 | |
| 31 | #pragma OPENCL EXTENSION cl_khr_fp64 : enable |
| 32 | |
Matt Arsenault | 633d749 | 2016-07-19 19:02:01 +0000 | [diff] [blame] | 33 | #ifdef __AMDGCN__ |
| 34 | #define __clc_builtin_rsq __builtin_amdgcn_rsq |
| 35 | #else |
| 36 | #define __clc_builtin_rsq __builtin_r600_recipsqrt_ieee |
| 37 | #endif |
Tom Stellard | 4f8d262 | 2015-07-10 13:37:08 +0000 | [diff] [blame] | 38 | |
| 39 | _CLC_OVERLOAD _CLC_DEF double sqrt(double x) { |
| 40 | |
| 41 | uint vcc = x < 0x1p-767; |
| 42 | uint exp0 = vcc ? 0x100 : 0; |
| 43 | unsigned exp1 = vcc ? 0xffffff80 : 0; |
| 44 | |
| 45 | double v01 = ldexp(x, exp0); |
Matt Arsenault | 633d749 | 2016-07-19 19:02:01 +0000 | [diff] [blame] | 46 | double v23 = __clc_builtin_rsq(v01); |
Tom Stellard | 4f8d262 | 2015-07-10 13:37:08 +0000 | [diff] [blame] | 47 | double v45 = v01 * v23; |
| 48 | v23 = v23 * 0.5; |
| 49 | |
| 50 | double v67 = fma(-v23, v45, 0.5); |
| 51 | v45 = fma(v45, v67, v45); |
| 52 | double v89 = fma(-v45, v45, v01); |
| 53 | v23 = fma(v23, v67, v23); |
| 54 | v45 = fma(v89, v23, v45); |
| 55 | v67 = fma(-v45, v45, v01); |
| 56 | v23 = fma(v67, v23, v45); |
| 57 | |
| 58 | v23 = ldexp(v23, exp1); |
| 59 | return ((x == __builtin_inf()) || (x == 0.0)) ? v01 : v23; |
| 60 | } |
| 61 | |
| 62 | _CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sqrt, double); |
| 63 | |
| 64 | #endif |