aboutsummaryrefslogtreecommitdiff
path: root/src/f32-sigmoid/scalar-lut64-p2-div.c.in
blob: 7cc5a40534b3a7590e15273199a730dd92cd960e (plain)
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
191
192
193
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

$assert BATCH_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>
#include <math.h>

#include <xnnpack/common.h>
#include <xnnpack/vunary.h>

#include <fp16/bitcasts.h>


// Note redefine as uint32[] to avoid redundant bitcasts.
extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_64[64];

void xnn_f32_sigmoid_ukernel__scalar_lut64_p2_div_x${BATCH_TILE}(
    size_t n,
    const float* x,
    float* y,
    const void* params)
{
  assert(n % sizeof(float) == 0);

  const float vmagic_bias = 0x1.800000p17f;
  const float vminus_log2e = -0x1.715476p0f;
  const uint32_t vindex_mask = UINT32_C(0x3F);
  const float vln2_hi =  0x1.630000p-1f;
  const float vln2_lo = -0x1.BD0106p-13f;
  const float vc2 = 0x1.FFFF0Ap-2f;
  const float vone = 1.0f;
  const float vdenorm_cutoff = 0x1.5D589Ep+6f;

  $if BATCH_TILE > 1:
    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
      $for N in range(BATCH_TILE):
        const float vx${N} = x[${N}];
      x += ${BATCH_TILE};

      $for N in range(BATCH_TILE):
        const float vz${N} = fabsf(vx${N});

      $for N in range(BATCH_TILE):
        float vn${N} = vz${N} * vminus_log2e + vmagic_bias;

      $for N in range(BATCH_TILE):
        const uint32_t ve${N} = fp32_to_bits(vn${N}) << 17;

      $for N in range(BATCH_TILE):
        const uint32_t vidx${N} = fp32_to_bits(vn${N}) & vindex_mask;
        const float vs${N} = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx${N}] + ve${N});

      $for N in range(BATCH_TILE):
        vn${N} -= vmagic_bias;

      $for N in range(BATCH_TILE):
        float vt${N} = vn${N} * vln2_hi + vz${N};

      $for N in range(BATCH_TILE):
        vt${N} = vn${N} * vln2_lo + vt${N};

      $for N in range(BATCH_TILE):
        float vp${N} = vt${N} * vc2;

      $for N in range(BATCH_TILE):
        vp${N} = vt${N} - vp${N} * vt${N};

      $for N in range(BATCH_TILE):
        const float vy${N} = vs${N} - vs${N} * vp${N};

      $for N in range(BATCH_TILE):
        const float vd${N} = vy${N} + vone;

      $for N in range(BATCH_TILE):
        float vf${N} = vy${N} / vd${N};

      $for N in range(BATCH_TILE):
        if XNN_UNPREDICTABLE(vz${N} > vdenorm_cutoff) {
          vf${N} = 0.0f;
        }

      $for N in range(BATCH_TILE):
        if XNN_UNPREDICTABLE(vx${N} > 0.0f) {
          vf${N} = vone - vf${N};
        }

      $for N in range(BATCH_TILE):
        y[${N}] = vf${N};
      y += ${BATCH_TILE};
    }
  $if BATCH_TILE == 1:
    do {
      const float vx = *x++;

      const float vz = fabsf(vx);

      float vn = vz * vminus_log2e + vmagic_bias;
      const uint32_t ve = fp32_to_bits(vn) << 17;
      const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
      const float vs = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx] + ve);
      vn -= vmagic_bias;

      float vt = vn * vln2_hi + vz;
      vt = vn * vln2_lo + vt;

      float vp = vt * vc2;
      vp = vt - vp * vt;

      const float vy = vs - vs * vp;
      const float vd = vy + vone;

      float vf = vy / vd;
      if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
        vf = 0.0f;
      }
      if XNN_UNPREDICTABLE(vx > 0.0f) {
        vf = vone - vf;
      }

      *y++ = vf;

      n -= sizeof(float);
    } while (n != 0);
  $elif BATCH_TILE == 2:
    if XNN_UNLIKELY(n != 0) {
      const float vx = *x;

      const float vz = fabsf(vx);

      float vn = vz * vminus_log2e + vmagic_bias;
      const uint32_t ve = fp32_to_bits(vn) << 17;
      const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
      const float vs = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx] + ve);
      vn -= vmagic_bias;

      float vt = vn * vln2_hi + vz;
      vt = vn * vln2_lo + vt;

      float vp = vt * vc2;
      vp = vt - vp * vt;

      const float vy = vs - vs * vp;
      const float vd = vy + vone;

      float vf = vy / vd;
      if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
        vf = 0.0f;
      }
      if XNN_UNPREDICTABLE(vx > 0.0f) {
        vf = vone - vf;
      }

      *y = vf;
    }
  $else:
    if XNN_UNLIKELY(n != 0) {
      do {
        const float vx = *x++;

        const float vz = fabsf(vx);

        float vn = vz * vminus_log2e + vmagic_bias;
        const uint32_t ve = fp32_to_bits(vn) << 17;
        const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
        const float vs = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx] + ve);
        vn -= vmagic_bias;

        float vt = vn * vln2_hi + vz;
        vt = vn * vln2_lo + vt;

        float vp = vt * vc2;
        vp = vt - vp * vt;

        const float vy = vs - vs * vp;
        const float vd = vy + vone;

        float vf = vy / vd;
        if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
          vf = 0.0f;
        }
        if XNN_UNPREDICTABLE(vx > 0.0f) {
          vf = vone - vf;
        }

        *y++ = vf;

        n -= sizeof(float);
      } while (n != 0);
    }
}