1/* 2 * Copyright (c) 2014 Advanced Micro Devices, Inc. 3 * Copyright (c) 2016 Aaron Watry 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 * THE SOFTWARE. 22 */ 23#if __CLC_FPSIZE == 32 24#ifdef __CLC_SCALAR 25_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fdim(__CLC_GENTYPE x, __CLC_GENTYPE y) { 26 if (__builtin_isnan(x) || __builtin_isnan(y)) 27 return as_float(QNANBITPATT_SP32); 28 return fmax(x - y, 0.0f); 29} 30#define __CLC_FDIM_VEC(width) \ 31_CLC_OVERLOAD _CLC_DEF float##width fdim(float##width x, float##width y) { \ 32 /* Determine if x or y is NaN. */ \ 33 /* Vector true is -1, i.e. all-bits-set, and NaN==NaN is false. */ \ 34 /* If either is NaN, then ~((x==x) & (y==y)) will be 0 (e.g. ~(-1)), as will n. */ \ 35 int##width n = ~((x == x) & (y == y)) & QNANBITPATT_SP32; \ 36 /* Calculate x-y if x>y, otherwise positive 0, again taking */ \ 37 /* advantage of vector true being all-bits-set. */ \ 38 int##width r = (x > y) & as_int##width(x - y); \ 39 return as_float##width(n | r); \ 40} 41__CLC_FDIM_VEC(2) 42__CLC_FDIM_VEC(3) 43__CLC_FDIM_VEC(4) 44__CLC_FDIM_VEC(8) 45__CLC_FDIM_VEC(16) 46#undef __CLC_FDIM_VEC 47#endif 48#endif 49 50#if __CLC_FPSIZE == 64 51#ifdef __CLC_SCALAR 52_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fdim(__CLC_GENTYPE x, private __CLC_GENTYPE y) { 53 long n = -(isnan(x) | isnan(y)) & QNANBITPATT_DP64; 54 long r = -(x > y) & as_long(x - y); 55 return as_double(n | r); 56} 57#define __CLC_FDIM_VEC(width) \ 58_CLC_OVERLOAD _CLC_DEF double##width fdim(double##width x, double##width y) { \ 59 /* See comment in float implementation for explanation. */ \ 60 long##width n = ~((x == x) & (y == y)) & QNANBITPATT_DP64; \ 61 long##width r = (x > y) & as_long##width(x - y); \ 62 return as_double##width(n | r); \ 63} 64__CLC_FDIM_VEC(2) 65__CLC_FDIM_VEC(3) 66__CLC_FDIM_VEC(4) 67__CLC_FDIM_VEC(8) 68__CLC_FDIM_VEC(16) 69#undef __CLC_FDIM_VEC 70#endif 71#endif 72