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 33 34_CLC_OVERLOAD _CLC_DEF double sqrt(double x) { 35 36 uint vcc = x < 0x1p-767; 37 uint exp0 = vcc ? 0x100 : 0; 38 unsigned exp1 = vcc ? 0xffffff80 : 0; 39 40 double v01 = ldexp(x, exp0); 41 double v23 = __builtin_amdgpu_rsq(v01); 42 double v45 = v01 * v23; 43 v23 = v23 * 0.5; 44 45 double v67 = fma(-v23, v45, 0.5); 46 v45 = fma(v45, v67, v45); 47 double v89 = fma(-v45, v45, v01); 48 v23 = fma(v23, v67, v23); 49 v45 = fma(v89, v23, v45); 50 v67 = fma(-v45, v45, v01); 51 v23 = fma(v67, v23, v45); 52 53 v23 = ldexp(v23, exp1); 54 return ((x == __builtin_inf()) || (x == 0.0)) ? v01 : v23; 55} 56 57_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sqrt, double); 58 59#endif 60