1/* 2 * Copyright (c) 2014 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 <math/clc_hypot.h> 25 26#include "config.h" 27#include "math.h" 28#include "../clcmacro.h" 29 30// Returns sqrt(x*x + y*y) with no overflow or underflow unless the result warrants it 31_CLC_DEF _CLC_OVERLOAD float __clc_hypot(float x, float y) 32{ 33 uint ux = as_uint(x); 34 uint aux = ux & EXSIGNBIT_SP32; 35 uint uy = as_uint(y); 36 uint auy = uy & EXSIGNBIT_SP32; 37 float retval; 38 int c = aux > auy; 39 ux = c ? aux : auy; 40 uy = c ? auy : aux; 41 42 int xexp = clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126); 43 float fx_exp = as_float((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32); 44 float fi_exp = as_float((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32); 45 float fx = as_float(ux) * fi_exp; 46 float fy = as_float(uy) * fi_exp; 47 retval = sqrt(mad(fx, fx, fy*fy)) * fx_exp; 48 49 retval = ux > PINFBITPATT_SP32 | uy == 0 ? as_float(ux) : retval; 50 retval = ux == PINFBITPATT_SP32 | uy == PINFBITPATT_SP32 ? as_float(PINFBITPATT_SP32) : retval; 51 return retval; 52} 53_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_hypot, float, float) 54 55#ifdef cl_khr_fp64 56_CLC_DEF _CLC_OVERLOAD double __clc_hypot(double x, double y) 57{ 58 ulong ux = as_ulong(x) & ~SIGNBIT_DP64; 59 int xexp = ux >> EXPSHIFTBITS_DP64; 60 x = as_double(ux); 61 62 ulong uy = as_ulong(y) & ~SIGNBIT_DP64; 63 int yexp = uy >> EXPSHIFTBITS_DP64; 64 y = as_double(uy); 65 66 int c = xexp > EXPBIAS_DP64 + 500 | yexp > EXPBIAS_DP64 + 500; 67 double preadjust = c ? 0x1.0p-600 : 1.0; 68 double postadjust = c ? 0x1.0p+600 : 1.0; 69 70 c = xexp < EXPBIAS_DP64 - 500 | yexp < EXPBIAS_DP64 - 500; 71 preadjust = c ? 0x1.0p+600 : preadjust; 72 postadjust = c ? 0x1.0p-600 : postadjust; 73 74 double ax = x * preadjust; 75 double ay = y * preadjust; 76 77 // The post adjust may overflow, but this can't be avoided in any case 78 double r = sqrt(fma(ax, ax, ay*ay)) * postadjust; 79 80 // If the difference in exponents between x and y is large 81 double s = x + y; 82 c = abs(xexp - yexp) > MANTLENGTH_DP64 + 1; 83 r = c ? s : r; 84 85 // Check for NaN 86 //c = x != x | y != y; 87 c = isnan(x) | isnan(y); 88 r = c ? as_double(QNANBITPATT_DP64) : r; 89 90 // If either is Inf, we must return Inf 91 c = x == as_double(PINFBITPATT_DP64) | y == as_double(PINFBITPATT_DP64); 92 r = c ? as_double(PINFBITPATT_DP64) : r; 93 94 return r; 95} 96 97_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_hypot, double, double) 98#endif 99