| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <math.h> |
| 12 |
|
| 13 |
#include "xnnpack/common.h" |
| 14 |
#include "xnnpack/math.h" |
| 15 |
#include "xnnpack/vunary.h" |
| 16 |
|
| 17 |
|
| 18 |
extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_16[16]; |
| 19 |
|
| 20 |
void xnn_f32_velu_ukernel__wasm_rr2_lut16_p3_u6( |
| 21 |
size_t batch, |
| 22 |
const float* input, |
| 23 |
float* output, |
| 24 |
const struct xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS(1)]) |
| 25 |
{ |
| 26 |
assert(batch != 0); |
| 27 |
assert(batch % sizeof(float) == 0); |
| 28 |
assert(input != NULL); |
| 29 |
assert(output != NULL); |
| 30 |
|
| 31 |
const float vsat_cutoff = -0x1.154246p+4f; |
| 32 |
const float vmagic_bias = 0x1.800000p19f; |
| 33 |
const float vlog2e = 0x1.715476p+0f; |
| 34 |
const float vminus_ln2_hi = -0x1.62E400p-1f; |
| 35 |
const float vminus_ln2_lo = -0x1.7F7D1Cp-20f; |
| 36 |
const uint32_t vindex_mask = UINT32_C(0xF); |
| 37 |
const float vc3 = 0x1.55561Cp-3f; |
| 38 |
const float vc2 = 0x1.0001ECp-1f; |
| 39 |
const float vone = 1.0f; |
| 40 |
|
| 41 |
const float vprescale = params->scalar.prescale; |
| 42 |
const float valpha = params->scalar.alpha; |
| 43 |
const float vbeta = params->scalar.beta; |
| 44 |
|
| 45 |
for (; batch >= 6 * sizeof(float); batch -= 6 * sizeof(float)) { |
| 46 |
float vx0 = input[0]; |
| 47 |
float vx1 = input[1]; |
| 48 |
float vx2 = input[2]; |
| 49 |
float vx3 = input[3]; |
| 50 |
float vx4 = input[4]; |
| 51 |
float vx5 = input[5]; |
| 52 |
input += 6; |
| 53 |
|
| 54 |
const float vz0 = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx0 * vprescale, vsat_cutoff), 0.0f); |
| 55 |
const float vz1 = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx1 * vprescale, vsat_cutoff), 0.0f); |
| 56 |
const float vz2 = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx2 * vprescale, vsat_cutoff), 0.0f); |
| 57 |
const float vz3 = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx3 * vprescale, vsat_cutoff), 0.0f); |
| 58 |
const float vz4 = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx4 * vprescale, vsat_cutoff), 0.0f); |
| 59 |
const float vz5 = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx5 * vprescale, vsat_cutoff), 0.0f); |
| 60 |
|
| 61 |
float vn0 = vz0 * vlog2e + vmagic_bias; |
| 62 |
float vn1 = vz1 * vlog2e + vmagic_bias; |
| 63 |
float vn2 = vz2 * vlog2e + vmagic_bias; |
| 64 |
float vn3 = vz3 * vlog2e + vmagic_bias; |
| 65 |
float vn4 = vz4 * vlog2e + vmagic_bias; |
| 66 |
float vn5 = vz5 * vlog2e + vmagic_bias; |
| 67 |
|
| 68 |
const uint32_t ven0 = float_as_uint32(vn0) << 19; |
| 69 |
const uint32_t vidx0 = float_as_uint32(vn0) & vindex_mask; |
| 70 |
vn0 -= vmagic_bias; |
| 71 |
const uint32_t ven1 = float_as_uint32(vn1) << 19; |
| 72 |
const uint32_t vidx1 = float_as_uint32(vn1) & vindex_mask; |
| 73 |
vn1 -= vmagic_bias; |
| 74 |
const uint32_t ven2 = float_as_uint32(vn2) << 19; |
| 75 |
const uint32_t vidx2 = float_as_uint32(vn2) & vindex_mask; |
| 76 |
vn2 -= vmagic_bias; |
| 77 |
const uint32_t ven3 = float_as_uint32(vn3) << 19; |
| 78 |
const uint32_t vidx3 = float_as_uint32(vn3) & vindex_mask; |
| 79 |
vn3 -= vmagic_bias; |
| 80 |
const uint32_t ven4 = float_as_uint32(vn4) << 19; |
| 81 |
const uint32_t vidx4 = float_as_uint32(vn4) & vindex_mask; |
| 82 |
vn4 -= vmagic_bias; |
| 83 |
const uint32_t ven5 = float_as_uint32(vn5) << 19; |
| 84 |
const uint32_t vidx5 = float_as_uint32(vn5) & vindex_mask; |
| 85 |
vn5 -= vmagic_bias; |
| 86 |
|
| 87 |
float vt0 = vn0 * vminus_ln2_hi + vz0; |
| 88 |
float vs0 = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx0] + ven0); |
| 89 |
float vt1 = vn1 * vminus_ln2_hi + vz1; |
| 90 |
float vs1 = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx1] + ven1); |
| 91 |
float vt2 = vn2 * vminus_ln2_hi + vz2; |
| 92 |
float vs2 = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx2] + ven2); |
| 93 |
float vt3 = vn3 * vminus_ln2_hi + vz3; |
| 94 |
float vs3 = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx3] + ven3); |
| 95 |
float vt4 = vn4 * vminus_ln2_hi + vz4; |
| 96 |
float vs4 = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx4] + ven4); |
| 97 |
float vt5 = vn5 * vminus_ln2_hi + vz5; |
| 98 |
float vs5 = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx5] + ven5); |
| 99 |
|
| 100 |
vt0 = vn0 * vminus_ln2_lo + vt0; |
| 101 |
vt1 = vn1 * vminus_ln2_lo + vt1; |
| 102 |
vt2 = vn2 * vminus_ln2_lo + vt2; |
| 103 |
vt3 = vn3 * vminus_ln2_lo + vt3; |
| 104 |
vt4 = vn4 * vminus_ln2_lo + vt4; |
| 105 |
vt5 = vn5 * vminus_ln2_lo + vt5; |
| 106 |
|
| 107 |
float vp0 = vc3 * vt0 + vc2; |
| 108 |
float vp1 = vc3 * vt1 + vc2; |
| 109 |
float vp2 = vc3 * vt2 + vc2; |
| 110 |
float vp3 = vc3 * vt3 + vc2; |
| 111 |
float vp4 = vc3 * vt4 + vc2; |
| 112 |
float vp5 = vc3 * vt5 + vc2; |
| 113 |
|
| 114 |
vp0 *= vt0; |
| 115 |
vp1 *= vt1; |
| 116 |
vp2 *= vt2; |
| 117 |
vp3 *= vt3; |
| 118 |
vp4 *= vt4; |
| 119 |
vp5 *= vt5; |
| 120 |
|
| 121 |
vt0 *= vs0; |
| 122 |
vs0 -= vone; |
| 123 |
vt1 *= vs1; |
| 124 |
vs1 -= vone; |
| 125 |
vt2 *= vs2; |
| 126 |
vs2 -= vone; |
| 127 |
vt3 *= vs3; |
| 128 |
vs3 -= vone; |
| 129 |
vt4 *= vs4; |
| 130 |
vs4 -= vone; |
| 131 |
vt5 *= vs5; |
| 132 |
vs5 -= vone; |
| 133 |
|
| 134 |
vp0 = vp0 * vt0 + vt0; |
| 135 |
vp1 = vp1 * vt1 + vt1; |
| 136 |
vp2 = vp2 * vt2 + vt2; |
| 137 |
vp3 = vp3 * vt3 + vt3; |
| 138 |
vp4 = vp4 * vt4 + vt4; |
| 139 |
vp5 = vp5 * vt5 + vt5; |
| 140 |
|
| 141 |
const float ve0 = (vp0 + vs0) * valpha; |
| 142 |
float vy0 = __builtin_wasm_max_f32(vx0 * vbeta, 0.0f); |
| 143 |
const float ve1 = (vp1 + vs1) * valpha; |
| 144 |
float vy1 = __builtin_wasm_max_f32(vx1 * vbeta, 0.0f); |
| 145 |
const float ve2 = (vp2 + vs2) * valpha; |
| 146 |
float vy2 = __builtin_wasm_max_f32(vx2 * vbeta, 0.0f); |
| 147 |
const float ve3 = (vp3 + vs3) * valpha; |
| 148 |
float vy3 = __builtin_wasm_max_f32(vx3 * vbeta, 0.0f); |
| 149 |
const float ve4 = (vp4 + vs4) * valpha; |
| 150 |
float vy4 = __builtin_wasm_max_f32(vx4 * vbeta, 0.0f); |
| 151 |
const float ve5 = (vp5 + vs5) * valpha; |
| 152 |
float vy5 = __builtin_wasm_max_f32(vx5 * vbeta, 0.0f); |
| 153 |
|
| 154 |
vy0 += __builtin_wasm_min_f32(ve0, 0.0f); |
| 155 |
vy1 += __builtin_wasm_min_f32(ve1, 0.0f); |
| 156 |
vy2 += __builtin_wasm_min_f32(ve2, 0.0f); |
| 157 |
vy3 += __builtin_wasm_min_f32(ve3, 0.0f); |
| 158 |
vy4 += __builtin_wasm_min_f32(ve4, 0.0f); |
| 159 |
vy5 += __builtin_wasm_min_f32(ve5, 0.0f); |
| 160 |
|
| 161 |
output[0] = vy0; |
| 162 |
output[1] = vy1; |
| 163 |
output[2] = vy2; |
| 164 |
output[3] = vy3; |
| 165 |
output[4] = vy4; |
| 166 |
output[5] = vy5; |
| 167 |
output += 6; |
| 168 |
} |
| 169 |
if XNN_UNLIKELY(batch != 0) { |
| 170 |
do { |
| 171 |
float vx = *input++; |
| 172 |
|
| 173 |
const float vz = __builtin_wasm_min_f32(__builtin_wasm_max_f32(vx * vprescale, vsat_cutoff), 0.0f); |
| 174 |
|
| 175 |
float vn = vz * vlog2e + vmagic_bias; |
| 176 |
const uint32_t ven = float_as_uint32(vn) << 19; |
| 177 |
const uint32_t vidx = float_as_uint32(vn) & vindex_mask; |
| 178 |
vn -= vmagic_bias; |
| 179 |
|
| 180 |
float vt = vn * vminus_ln2_hi + vz; |
| 181 |
float vs = uint32_as_float(xnn_table_exp2minus_k_over_16[vidx] + ven); |
| 182 |
|
| 183 |
vt = vn * vminus_ln2_lo + vt; |
| 184 |
|
| 185 |
float vp = vc3 * vt + vc2; |
| 186 |
vp *= vt; |
| 187 |
|
| 188 |
vt *= vs; |
| 189 |
vs -= vone; |
| 190 |
vp = vp * vt + vt; |
| 191 |
const float ve = (vp + vs) * valpha; |
| 192 |
|
| 193 |
float vy = __builtin_wasm_max_f32(vx * vbeta, 0.0f); |
| 194 |
vy += __builtin_wasm_min_f32(ve, 0.0f); |
| 195 |
|
| 196 |
*output++ = vy; |
| 197 |
|
| 198 |
batch -= sizeof(float); |
| 199 |
} while (batch != 0); |
| 200 |
} |
| 201 |
} |