1// Copyright 2019 Google LLC 2// 3// This source code is licensed under the BSD-style license found in the 4// LICENSE file in the root directory of this source tree. 5 6$assert BATCH_TILE >= 1 7$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 8#include <assert.h> 9#include <math.h> 10 11#include <xnnpack/common.h> 12#include <xnnpack/math.h> 13#include <xnnpack/vunary.h> 14 15 16void xnn_f32_vsigmoid_ukernel__scalar_rr2_p5_div_x${BATCH_TILE}( 17 size_t n, 18 const float* x, 19 float* y, 20 const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)]) 21{ 22 assert(n % sizeof(float) == 0); 23 24 const float vmagic_bias = params->scalar_rr2_p5.magic_bias; 25 const float vminus_log2e = params->scalar_rr2_p5.minus_log2e; 26 const float vln2_hi = params->scalar_rr2_p5.ln2_hi; 27 const float vln2_lo = params->scalar_rr2_p5.ln2_lo; 28 const float vc5 = params->scalar_rr2_p5.c5; 29 const float vc4 = params->scalar_rr2_p5.c4; 30 const float vc3 = params->scalar_rr2_p5.c3; 31 const float vc2 = params->scalar_rr2_p5.c2; 32 const float vc1 = params->scalar_rr2_p5.c1; 33 const float vone = params->scalar_rr2_p5.one; 34 const float vdenorm_cutoff = params->scalar_rr2_p5.denorm_cutoff; 35 36 $if BATCH_TILE > 1: 37 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 38 $for N in range(BATCH_TILE): 39 const float vx${N} = x[${N}]; 40 x += ${BATCH_TILE}; 41 42 $for N in range(BATCH_TILE): 43 const float vz${N} = fabsf(vx${N}); 44 45 $for N in range(BATCH_TILE): 46 float vn${N} = vz${N} * vminus_log2e + vmagic_bias; 47 48 $for N in range(BATCH_TILE): 49 const float vs${N} = uint32_as_float(float_as_uint32(vn${N}) << 23); 50 51 $for N in range(BATCH_TILE): 52 vn${N} -= vmagic_bias; 53 54 $for N in range(BATCH_TILE): 55 float vt${N} = vn${N} * vln2_hi + vz${N}; 56 57 $for N in range(BATCH_TILE): 58 vt${N} = vn${N} * vln2_lo + vt${N}; 59 60 $for N in range(BATCH_TILE): 61 float vp${N} = vt${N} * vc5 + vc4; 62 63 $for N in range(BATCH_TILE): 64 vp${N} = vt${N} * vp${N} + vc3; 65 66 $for N in range(BATCH_TILE): 67 vp${N} = vt${N} * vp${N} + vc2; 68 69 $for N in range(BATCH_TILE): 70 vp${N} = vt${N} * vp${N} + vc1; 71 72 $for N in range(BATCH_TILE): 73 vt${N} *= vs${N}; 74 75 $for N in range(BATCH_TILE): 76 const float ve${N} = vt${N} * vp${N} + vs${N}; 77 78 $for N in range(BATCH_TILE): 79 const float vd${N} = ve${N} + vone; 80 81 $for N in range(BATCH_TILE): 82 float vf${N} = ve${N} / vd${N}; 83 84 $for N in range(BATCH_TILE): 85 if XNN_UNPREDICTABLE(vz${N} > vdenorm_cutoff) { 86 vf${N} = 0.0f; 87 } 88 89 $for N in range(BATCH_TILE): 90 if XNN_UNPREDICTABLE(vx${N} > 0.0f) { 91 vf${N} = vone - vf${N}; 92 } 93 94 $for N in range(BATCH_TILE): 95 y[${N}] = vf${N}; 96 y += ${BATCH_TILE}; 97 } 98 $if BATCH_TILE == 1: 99 do { 100 const float vx = *x++; 101 102 const float vz = fabsf(vx); 103 104 float vn = vz * vminus_log2e + vmagic_bias; 105 const float vs = uint32_as_float(float_as_uint32(vn) << 23); 106 vn -= vmagic_bias; 107 108 float vt = vn * vln2_hi + vz; 109 vt = vn * vln2_lo + vt; 110 111 float vp = vt * vc5 + vc4; 112 vp = vt * vp + vc3; 113 vp = vt * vp + vc2; 114 vp = vt * vp + vc1; 115 116 vt *= vs; 117 const float ve = vt * vp + vs; 118 const float vd = ve + vone; 119 120 float vf = ve / vd; 121 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) { 122 vf = 0.0f; 123 } 124 if XNN_UNPREDICTABLE(vx > 0.0f) { 125 vf = vone - vf; 126 } 127 128 *y++ = vf; 129 130 n -= sizeof(float); 131 } while (n != 0); 132 $elif BATCH_TILE == 2: 133 if XNN_UNLIKELY(n != 0) { 134 const float vx = *x; 135 136 const float vz = fabsf(vx); 137 138 float vn = vz * vminus_log2e + vmagic_bias; 139 const float vs = uint32_as_float(float_as_uint32(vn) << 23); 140 vn -= vmagic_bias; 141 142 float vt = vn * vln2_hi + vz; 143 vt = vn * vln2_lo + vt; 144 145 float vp = vt * vc5 + vc4; 146 vp = vt * vp + vc3; 147 vp = vt * vp + vc2; 148 vp = vt * vp + vc1; 149 150 vt *= vs; 151 const float ve = vt * vp + vs; 152 const float vd = ve + vone; 153 154 float vf = ve / vd; 155 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) { 156 vf = 0.0f; 157 } 158 if XNN_UNPREDICTABLE(vx > 0.0f) { 159 vf = vone - vf; 160 } 161 162 *y = vf; 163 } 164 $else: 165 if XNN_UNLIKELY(n != 0) { 166 do { 167 const float vx = *x++; 168 169 const float vz = fabsf(vx); 170 171 float vn = vz * vminus_log2e + vmagic_bias; 172 const float vs = uint32_as_float(float_as_uint32(vn) << 23); 173 vn -= vmagic_bias; 174 175 float vt = vn * vln2_hi + vz; 176 vt = vn * vln2_lo + vt; 177 178 float vp = vt * vc5 + vc4; 179 vp = vt * vp + vc3; 180 vp = vt * vp + vc2; 181 vp = vt * vp + vc1; 182 183 vt *= vs; 184 const float ve = vt * vp + vs; 185 const float vd = ve + vone; 186 187 float vf = ve / vd; 188 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) { 189 vf = 0.0f; 190 } 191 if XNN_UNPREDICTABLE(vx > 0.0f) { 192 vf = vone - vf; 193 } 194 195 *y++ = vf; 196 197 n -= sizeof(float); 198 } while (n != 0); 199 } 200} 201