2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2016 Aaron Watry
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
23 #if __CLC_FPSIZE == 32
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);
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); \
50 #if __CLC_FPSIZE == 64
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);
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); \
73 #if __CLC_FPSIZE == 16
75 #define QNANBITPATT_FP16 ((short)0x7e00)
76 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fdim(__CLC_GENTYPE x,
77 private __CLC_GENTYPE y) {
78 short n = -(isnan(x) | isnan(y)) & QNANBITPATT_FP16;
79 short r = -(x > y) & as_short(x - y);
80 return as_half((short)(n | r));
82 #define __CLC_FDIM_VEC(width) \
83 _CLC_OVERLOAD _CLC_DEF half##width fdim(half##width x, half##width y) { \
84 /* See comment in float implementation for explanation. */ \
85 short##width n = ~((x == x) & (y == y)) & QNANBITPATT_FP16; \
86 short##width r = (x > y) & as_short##width(x - y); \
87 return as_half##width(n | r); \