1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "../imf.h"
#pragma OPENCL FP_CONTRACT OFF
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c7 = {0x37d2d82du};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c6 = {0x3952d831u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c5 = {0x3ab606ceu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c4 = {0x3c08852cu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c3 = {0x3d2aaab0u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c2 = {0x3e2aaab0u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c1 = {0xb07ec280u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_c0 = {0xb09f34cfu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_fL2E = {0x3FB8AA3Bu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_fShifter = {0x4b40007fu};
// log(2) high, low
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_NL2H = {0xbf317218u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ha_NL2L = {0x3102E308u};
__attribute__((always_inline)) inline int
__ocl_svml_internal_sexpm1_ha(float *a, float *pres) {
int nRet = 0;
float xin = *a;
// float expm1f(float xin)
union {
unsigned int w;
float f;
int i;
} xf, fN, xL2E, fS, xa;
union {
unsigned int w;
float f;
int i;
} T, sc, res;
float R, Rh, Rl, A, B, Bh, Th, Tl;
float H, poly, Rhh, Rhl, ThRh, ThRh_l;
float H1, H2, Rhh2, Rhl2, poly0;
xf.f = xin;
xL2E.f =
SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(xf.f, __sexpm1_ha_fL2E.f, 0.0f);
fN.f = SPIRV_OCL_BUILTIN(trunc, _f32, )(xL2E.f);
fS.f = __sexpm1_ha_fShifter.f + fN.f;
// reduced argument
Rh = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(fN.f, __sexpm1_ha_NL2H.f, xf.f);
Rl = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(fN.f, __sexpm1_ha_NL2L.f, 0.0f);
R = Rh + Rl;
// 2^N
T.w = fS.w << 23;
// e^R - 1
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(__sexpm1_ha_c7.f, R,
__sexpm1_ha_c6.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ha_c5.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ha_c4.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ha_c3.f);
poly0 = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ha_c2.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly0, R, __sexpm1_ha_c1.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ha_c0.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, Rl);
// maxabs(T,-1), minabs(T,-1)
A = (xin >= 0.0f) ? T.f : -1.0f;
B = (xin >= 0.0f) ? -1.0f : T.f;
Th = T.f - 1.0f;
Bh = Th - A;
Tl = B - Bh;
// T*Rh
ThRh = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(T.f, Rh, 0.0f);
ThRh_l = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(T.f, Rh, -ThRh);
// Th + Th*Rh
H = ThRh + Th;
// 2*H
H1 = H + H;
// (Th*Rh)_high
Rhh = H - Th;
// (Th*Rh)_low
Rhl = ThRh - Rhh;
Tl = Tl + Rhl + ThRh_l;
// H+H+Rh*Rh
H2 = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(ThRh, Rh, H1);
// Rh^2_high
Rhh2 = H2 - H1;
// Rh^2_low
Rhl2 = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(ThRh, Rh, -Rhh2);
// Tl += 0.5*Rhl2 + Rh*Rl
Tl = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(ThRh, Rl, Tl);
Tl = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(Rhl2, 0.5f, Tl);
// 2^N*poly + Tl
res.f = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(T.f, poly, Tl);
res.f = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(H2, 0.5f, res.f);
// ensure expm1(-0)=-0
xa.f = SPIRV_OCL_BUILTIN(fabs, _f32, )(xf.f);
res.w |= (xf.w ^ xa.w);
if (SPIRV_OCL_BUILTIN(fabs, _f32, )(xf.f) <= 87.0f) {
*pres = res.f;
return nRet;
}
// special case and overflow path
if (xf.f < 0) {
*pres = -1.0f;
return nRet;
}
if (!(xf.f < 128.0f)) {
// +Inf or NaN?
xa.w = xf.w & 0x7fffffff;
if (xa.w > 0x7f800000) {
*pres = xf.f + res.f;
return nRet;
}
// overflow
res.w = 0x7f800000 - 1;
res.f = res.f * res.f; // to set OF flag
//
nRet = 3;
{
*pres = res.f;
return nRet;
}
}
// at or near overflow
// 2^(N-64), N=(int)dN
T.w = (fS.w - 64) << 23;
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly0, R, 0.5f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ha_c1.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, Rl);
poly += Rh;
res.f = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(T.f, poly, T.f);
// final scaling
sc.w = 0x5f800000u;
res.f *= sc.f;
// determine if overflow
if (res.w == 0x7f800000)
nRet = 3;
*pres = res.f;
return nRet;
}
float __ocl_svml_expm1f_ha(float x) {
float r;
__ocl_svml_internal_sexpm1_ha(&x, &r);
return r;
}
|