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
|
/*========================== 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_ep_c4 = {0x3c0b4a77u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_c3 = {0x3d2e1d2cu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_c2 = {0x3e2aa0ccu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_c1 = {0x3efff2b6u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_c0 = {0x35bb1a40u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_fL2E = {0x3FB8AA3Bu};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_fShifter = {0x4b40007fu};
// log(2) high, low
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_NL2H = {0xbf317218u};
static __constant union {
unsigned int w;
float f;
int i;
} __sexpm1_ep_NL2L = {0x3102E308u};
__attribute__((always_inline)) inline int
__ocl_svml_internal_sexpm1_ep(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, poly, Th;
xf.f = xin;
xL2E.f =
SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(xf.f, __sexpm1_ep_fL2E.f, 0.0f);
fN.f = SPIRV_OCL_BUILTIN(trunc, _f32, )(xL2E.f);
fS.f = __sexpm1_ep_fShifter.f + fN.f;
// reduced argument
R = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(fN.f, __sexpm1_ep_NL2H.f, xf.f);
// R = SP_FMA(fN.f, _VSTATIC(NL2L).f, R);
// 2^N
T.w = fS.w << 23;
// e^R - 1
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(__sexpm1_ep_c4.f, R,
__sexpm1_ep_c3.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ep_c2.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ep_c1.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, __sexpm1_ep_c0.f);
poly = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(poly, R, R);
// maxabs(T,-1), minabs(T,-1)
Th = T.f - 1.0f;
// 2^N*poly + 2^N - 1
res.f = SPIRV_OCL_BUILTIN(fma, _f32_f32_f32, )(T.f, poly, Th);
// 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.0f) {
*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;
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_ep(float x) {
float r;
__ocl_svml_internal_sexpm1_ep(&x, &r);
return r;
}
|