| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
|
| 12 |
#include <wasm_simd128.h> |
| 13 |
|
| 14 |
#include "xnnpack/vunary.h" |
| 15 |
#include "xnnpack/common.h" |
| 16 |
|
| 17 |
|
| 18 |
void xnn_f32_velu_ukernel__wasmrelaxedsimd_rr2_p6_u8( |
| 19 |
size_t batch, |
| 20 |
const float* input, |
| 21 |
float* output, |
| 22 |
const struct xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
| 23 |
{ |
| 24 |
assert(batch != 0); |
| 25 |
assert(batch % sizeof(float) == 0); |
| 26 |
assert(input != NULL); |
| 27 |
assert(output != NULL); |
| 28 |
|
| 29 |
const v128_t vsat_cutoff = wasm_f32x4_const_splat(-0x1.154246p+4f); |
| 30 |
const v128_t vmagic_bias = wasm_f32x4_const_splat(0x1.8000FEp23f); |
| 31 |
const v128_t vlog2e = wasm_f32x4_const_splat(0x1.715476p+0f); |
| 32 |
const v128_t vminus_ln2_hi = wasm_f32x4_const_splat(-0x1.62E440p-1f); |
| 33 |
const v128_t vminus_ln2_lo = wasm_f32x4_const_splat(0x1.0105C6p-21f); |
| 34 |
const v128_t vc6 = wasm_f32x4_const_splat(0x1.6b7338p-10f); |
| 35 |
const v128_t vc5 = wasm_f32x4_const_splat(0x1.12278Ep-7f); |
| 36 |
const v128_t vc4 = wasm_f32x4_const_splat(0x1.555716p-5f); |
| 37 |
const v128_t vc3 = wasm_f32x4_const_splat(0x1.5554B0p-3f); |
| 38 |
const v128_t vc2 = wasm_f32x4_const_splat(0x1.FFFFFEp-2f); |
| 39 |
const v128_t vone = wasm_f32x4_const_splat(1.0f); |
| 40 |
|
| 41 |
XNN_FORCE_REALIZATION(vsat_cutoff); |
| 42 |
XNN_FORCE_REALIZATION(vmagic_bias); |
| 43 |
XNN_FORCE_REALIZATION(vlog2e); |
| 44 |
XNN_FORCE_REALIZATION(vminus_ln2_hi); |
| 45 |
XNN_FORCE_REALIZATION(vminus_ln2_lo); |
| 46 |
XNN_FORCE_REALIZATION(vc6); |
| 47 |
XNN_FORCE_REALIZATION(vc5); |
| 48 |
XNN_FORCE_REALIZATION(vc4); |
| 49 |
XNN_FORCE_REALIZATION(vc3); |
| 50 |
XNN_FORCE_REALIZATION(vc2); |
| 51 |
XNN_FORCE_REALIZATION(vone); |
| 52 |
|
| 53 |
const v128_t vprescale = wasm_v128_load32_splat(¶ms->scalar.prescale); |
| 54 |
const v128_t valpha = wasm_v128_load32_splat(¶ms->scalar.alpha); |
| 55 |
const v128_t vbeta = wasm_v128_load32_splat(¶ms->scalar.beta); |
| 56 |
|
| 57 |
for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) { |
| 58 |
v128_t vx0123 = wasm_v128_load(input); |
| 59 |
v128_t vx4567 = wasm_v128_load(input + 4); |
| 60 |
input += 8; |
| 61 |
|
| 62 |
const v128_t vz0123 = wasm_f32x4_relaxed_max(vsat_cutoff, wasm_f32x4_mul(vx0123, vprescale)); |
| 63 |
const v128_t vz4567 = wasm_f32x4_relaxed_max(vsat_cutoff, wasm_f32x4_mul(vx4567, vprescale)); |
| 64 |
|
| 65 |
v128_t vn0123 = wasm_f32x4_add(wasm_f32x4_mul(vz0123, vlog2e), vmagic_bias); |
| 66 |
v128_t vn4567 = wasm_f32x4_add(wasm_f32x4_mul(vz4567, vlog2e), vmagic_bias); |
| 67 |
|
| 68 |
v128_t vs0123 = wasm_i32x4_shl(vn0123, 23); |
| 69 |
v128_t vs4567 = wasm_i32x4_shl(vn4567, 23); |
| 70 |
|
| 71 |
vn0123 = wasm_f32x4_sub(vn0123, vmagic_bias); |
| 72 |
vn4567 = wasm_f32x4_sub(vn4567, vmagic_bias); |
| 73 |
|
| 74 |
v128_t vt0123 = wasm_f32x4_add(wasm_f32x4_mul(vn0123, vminus_ln2_hi), vz0123); |
| 75 |
v128_t vt4567 = wasm_f32x4_add(wasm_f32x4_mul(vn4567, vminus_ln2_hi), vz4567); |
| 76 |
|
| 77 |
vt0123 = wasm_f32x4_add(wasm_f32x4_mul(vn0123, vminus_ln2_lo), vt0123); |
| 78 |
vt4567 = wasm_f32x4_add(wasm_f32x4_mul(vn4567, vminus_ln2_lo), vt4567); |
| 79 |
|
| 80 |
v128_t vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vc6, vt0123), vc5); |
| 81 |
v128_t vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vc6, vt4567), vc5); |
| 82 |
|
| 83 |
vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vp0123, vt0123), vc4); |
| 84 |
vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vp4567, vt4567), vc4); |
| 85 |
|
| 86 |
vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vp0123, vt0123), vc3); |
| 87 |
vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vp4567, vt4567), vc3); |
| 88 |
|
| 89 |
vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vp0123, vt0123), vc2); |
| 90 |
vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vp4567, vt4567), vc2); |
| 91 |
|
| 92 |
vp0123 = wasm_f32x4_mul(vp0123, vt0123); |
| 93 |
vp4567 = wasm_f32x4_mul(vp4567, vt4567); |
| 94 |
|
| 95 |
vt0123 = wasm_f32x4_mul(vt0123, vs0123); |
| 96 |
vs0123 = wasm_f32x4_sub(vs0123, vone); |
| 97 |
vt4567 = wasm_f32x4_mul(vt4567, vs4567); |
| 98 |
vs4567 = wasm_f32x4_sub(vs4567, vone); |
| 99 |
|
| 100 |
vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vp0123, vt0123), vt0123); |
| 101 |
vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vp4567, vt4567), vt4567); |
| 102 |
|
| 103 |
const v128_t ve0123 = wasm_f32x4_mul(wasm_f32x4_add(vp0123, vs0123), valpha); |
| 104 |
const v128_t ve4567 = wasm_f32x4_mul(wasm_f32x4_add(vp4567, vs4567), valpha); |
| 105 |
|
| 106 |
const v128_t vsignm0123 = wasm_i32x4_shr(vx0123, 31); |
| 107 |
vx0123 = wasm_f32x4_mul(vx0123, vbeta); |
| 108 |
const v128_t vsignm4567 = wasm_i32x4_shr(vx4567, 31); |
| 109 |
vx4567 = wasm_f32x4_mul(vx4567, vbeta); |
| 110 |
|
| 111 |
const v128_t vy0123 = wasm_i32x4_relaxed_laneselect(ve0123, vx0123, vsignm0123); |
| 112 |
const v128_t vy4567 = wasm_i32x4_relaxed_laneselect(ve4567, vx4567, vsignm4567); |
| 113 |
|
| 114 |
wasm_v128_store(output, vy0123); |
| 115 |
wasm_v128_store(output + 4, vy4567); |
| 116 |
output += 8; |
| 117 |
} |
| 118 |
for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) { |
| 119 |
v128_t vx = wasm_v128_load(input); |
| 120 |
input += 4; |
| 121 |
|
| 122 |
const v128_t vz = wasm_f32x4_relaxed_max(vsat_cutoff, wasm_f32x4_mul(vx, vprescale)); |
| 123 |
|
| 124 |
v128_t vn = wasm_f32x4_add(wasm_f32x4_mul(vz, vlog2e), vmagic_bias); |
| 125 |
v128_t vs = wasm_i32x4_shl(vn, 23); |
| 126 |
vn = wasm_f32x4_sub(vn, vmagic_bias); |
| 127 |
|
| 128 |
v128_t vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_hi), vz); |
| 129 |
vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_lo), vt); |
| 130 |
|
| 131 |
v128_t vp = wasm_f32x4_add(wasm_f32x4_mul(vc6, vt), vc5); |
| 132 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vc4); |
| 133 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vc3); |
| 134 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vc2); |
| 135 |
vp = wasm_f32x4_mul(vp, vt); |
| 136 |
|
| 137 |
vt = wasm_f32x4_mul(vt, vs); |
| 138 |
vs = wasm_f32x4_sub(vs, vone); |
| 139 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vt); |
| 140 |
const v128_t ve = wasm_f32x4_mul(wasm_f32x4_add(vp, vs), valpha); |
| 141 |
|
| 142 |
const v128_t vsignm = wasm_i32x4_shr(vx, 31); |
| 143 |
vx = wasm_f32x4_mul(vx, vbeta); |
| 144 |
const v128_t vy = wasm_i32x4_relaxed_laneselect(ve, vx, vsignm); |
| 145 |
|
| 146 |
wasm_v128_store(output, vy); |
| 147 |
output += 4; |
| 148 |
} |
| 149 |
if XNN_UNLIKELY(batch != 0) { |
| 150 |
v128_t vx = wasm_v128_load(input); |
| 151 |
|
| 152 |
const v128_t vz = wasm_f32x4_relaxed_max(wasm_f32x4_mul(vx, vprescale), vsat_cutoff); |
| 153 |
|
| 154 |
v128_t vn = wasm_f32x4_add(wasm_f32x4_mul(vz, vlog2e), vmagic_bias); |
| 155 |
v128_t vs = wasm_i32x4_shl(vn, 23); |
| 156 |
vn = wasm_f32x4_sub(vn, vmagic_bias); |
| 157 |
|
| 158 |
v128_t vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_hi), vz); |
| 159 |
vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_lo), vt); |
| 160 |
|
| 161 |
v128_t vp = wasm_f32x4_add(wasm_f32x4_mul(vc6, vt), vc5); |
| 162 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vc4); |
| 163 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vc3); |
| 164 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vc2); |
| 165 |
vp = wasm_f32x4_mul(vp, vt); |
| 166 |
|
| 167 |
vt = wasm_f32x4_mul(vt, vs); |
| 168 |
vs = wasm_f32x4_sub(vs, vone); |
| 169 |
vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vt); |
| 170 |
const v128_t ve = wasm_f32x4_mul(wasm_f32x4_add(vp, vs), valpha); |
| 171 |
|
| 172 |
const v128_t vsignm = wasm_i32x4_shr(vx, 31); |
| 173 |
vx = wasm_f32x4_mul(vx, vbeta); |
| 174 |
v128_t vy = wasm_i32x4_relaxed_laneselect(ve, vx, vsignm); |
| 175 |
|
| 176 |
if (batch & (2 * sizeof(float))) { |
| 177 |
wasm_v128_store64_lane(output, vy, 0); |
| 178 |
vy = wasm_v64x2_shuffle(vy, vy, 1, 1); |
| 179 |
output += 2; |
| 180 |
} |
| 181 |
if (batch & (1 * sizeof(float))) { |
| 182 |
wasm_v128_store32_lane(output, vy, 0); |
| 183 |
} |
| 184 |
} |
| 185 |
} |