xref: /aosp_15_r20/external/XNNPACK/src/f32-raddstoreexpminusmax/scalar-rr2-lut64-p2.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1*4bdc9457SAndroid Build Coastguard Worker// Copyright 2020 Google LLC
2*4bdc9457SAndroid Build Coastguard Worker//
3*4bdc9457SAndroid Build Coastguard Worker// This source code is licensed under the BSD-style license found in the
4*4bdc9457SAndroid Build Coastguard Worker// LICENSE file in the root directory of this source tree.
5*4bdc9457SAndroid Build Coastguard Worker
6*4bdc9457SAndroid Build Coastguard Worker$assert ELEMENTS_TILE >= 1
7*4bdc9457SAndroid Build Coastguard Worker$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8*4bdc9457SAndroid Build Coastguard Worker#include <assert.h>
9*4bdc9457SAndroid Build Coastguard Worker
10*4bdc9457SAndroid Build Coastguard Worker#include <xnnpack/common.h>
11*4bdc9457SAndroid Build Coastguard Worker#include <xnnpack/math.h>
12*4bdc9457SAndroid Build Coastguard Worker#include <xnnpack/raddstoreexpminusmax.h>
13*4bdc9457SAndroid Build Coastguard Worker
14*4bdc9457SAndroid Build Coastguard Worker
15*4bdc9457SAndroid Build Coastguard Worker// Note redefine as uint32[] to avoid redundant bitcasts.
16*4bdc9457SAndroid Build Coastguard Workerextern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_64[64];
17*4bdc9457SAndroid Build Coastguard Worker
18*4bdc9457SAndroid Build Coastguard Workervoid xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_lut64_p2_x${ELEMENTS_TILE}${"" if ACCUMULATORS == 1 else "_acc%d" % ACCUMULATORS}(
19*4bdc9457SAndroid Build Coastguard Worker    size_t elements,
20*4bdc9457SAndroid Build Coastguard Worker    const float* input,
21*4bdc9457SAndroid Build Coastguard Worker    const float* max,
22*4bdc9457SAndroid Build Coastguard Worker    float* output,
23*4bdc9457SAndroid Build Coastguard Worker    float* sum,
24*4bdc9457SAndroid Build Coastguard Worker    const union xnn_f32_expminus_params params[restrict XNN_MIN_ELEMENTS(1)])
25*4bdc9457SAndroid Build Coastguard Worker{
26*4bdc9457SAndroid Build Coastguard Worker  assert(elements % sizeof(float) == 0);
27*4bdc9457SAndroid Build Coastguard Worker
28*4bdc9457SAndroid Build Coastguard Worker  const float vi_max = *max;
29*4bdc9457SAndroid Build Coastguard Worker  const float vlog2e = params->scalar_rr2_lut64_p2.log2e;
30*4bdc9457SAndroid Build Coastguard Worker  const float vmagic_bias = params->scalar_rr2_lut64_p2.magic_bias;
31*4bdc9457SAndroid Build Coastguard Worker  const uint32_t vindex_mask = UINT32_C(0x3F);
32*4bdc9457SAndroid Build Coastguard Worker  const float vminus_ln2_hi = params->scalar_rr2_lut64_p2.minus_ln2_hi;
33*4bdc9457SAndroid Build Coastguard Worker  const float vminus_ln2_lo = params->scalar_rr2_lut64_p2.minus_ln2_lo;
34*4bdc9457SAndroid Build Coastguard Worker  const float vc2 = params->scalar_rr2_lut64_p2.c2;
35*4bdc9457SAndroid Build Coastguard Worker  const float vdenorm_cutoff = params->scalar_rr2_lut64_p2.denorm_cutoff;
36*4bdc9457SAndroid Build Coastguard Worker
37*4bdc9457SAndroid Build Coastguard Worker  $if ELEMENTS_TILE > 1:
38*4bdc9457SAndroid Build Coastguard Worker    $for K in range(ACCUMULATORS):
39*4bdc9457SAndroid Build Coastguard Worker      float vacc${K} = 0.0f;
40*4bdc9457SAndroid Build Coastguard Worker    for (; elements >= ${ELEMENTS_TILE} * sizeof(float); elements -= ${ELEMENTS_TILE} * sizeof(float)) {
41*4bdc9457SAndroid Build Coastguard Worker      // Load ${ELEMENTS_TILE} inputs at a time.
42*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
43*4bdc9457SAndroid Build Coastguard Worker        const float vi${N} = input[${N}];
44*4bdc9457SAndroid Build Coastguard Worker      input += ${ELEMENTS_TILE};
45*4bdc9457SAndroid Build Coastguard Worker
46*4bdc9457SAndroid Build Coastguard Worker      // Subtract maximum input x := i - i_max. This implies x <= 0.
47*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
48*4bdc9457SAndroid Build Coastguard Worker        const float vx${N} = vi${N} - vi_max;
49*4bdc9457SAndroid Build Coastguard Worker
50*4bdc9457SAndroid Build Coastguard Worker      // Compute reduced argument n := round(x * 64 / log(2)).
51*4bdc9457SAndroid Build Coastguard Worker      // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
52*4bdc9457SAndroid Build Coastguard Worker      // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
53*4bdc9457SAndroid Build Coastguard Worker      // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
54*4bdc9457SAndroid Build Coastguard Worker      // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
55*4bdc9457SAndroid Build Coastguard Worker      // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
56*4bdc9457SAndroid Build Coastguard Worker      // algorithm.
57*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
58*4bdc9457SAndroid Build Coastguard Worker        float vn${N} = vx${N} * vlog2e + vmagic_bias;
59*4bdc9457SAndroid Build Coastguard Worker
60*4bdc9457SAndroid Build Coastguard Worker      // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
61*4bdc9457SAndroid Build Coastguard Worker      // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
62*4bdc9457SAndroid Build Coastguard Worker      // e := int(n / 64). We create s in two steps:
63*4bdc9457SAndroid Build Coastguard Worker      // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
64*4bdc9457SAndroid Build Coastguard Worker      //    fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
65*4bdc9457SAndroid Build Coastguard Worker      // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
66*4bdc9457SAndroid Build Coastguard Worker      //    number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
67*4bdc9457SAndroid Build Coastguard Worker      //    and thus the adjusted exponent is not lower than -126.
68*4bdc9457SAndroid Build Coastguard Worker      //
69*4bdc9457SAndroid Build Coastguard Worker      // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
70*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
71*4bdc9457SAndroid Build Coastguard Worker        const uint32_t ve${N} = (float_as_uint32(vn${N}) & UINT32_C(0xFFFFFFC0)) << 17;
72*4bdc9457SAndroid Build Coastguard Worker
73*4bdc9457SAndroid Build Coastguard Worker      // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
74*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
75*4bdc9457SAndroid Build Coastguard Worker        const uint32_t vidx${N} = float_as_uint32(vn${N}) & vindex_mask;
76*4bdc9457SAndroid Build Coastguard Worker      // Adjust exponent of the value l fetched from the table to get the final s value.
77*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
78*4bdc9457SAndroid Build Coastguard Worker        const float vs${N} = uint32_as_float(xnn_table_exp2_k_over_64[vidx${N}] + ve${N});
79*4bdc9457SAndroid Build Coastguard Worker
80*4bdc9457SAndroid Build Coastguard Worker      // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
81*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
82*4bdc9457SAndroid Build Coastguard Worker        vn${N} -= vmagic_bias;
83*4bdc9457SAndroid Build Coastguard Worker
84*4bdc9457SAndroid Build Coastguard Worker      // Compute reduced argument t := x - n * log(2) / 64.
85*4bdc9457SAndroid Build Coastguard Worker      // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
86*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
87*4bdc9457SAndroid Build Coastguard Worker        float vt${N} = vn${N} * vminus_ln2_hi + vx${N};
88*4bdc9457SAndroid Build Coastguard Worker
89*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
90*4bdc9457SAndroid Build Coastguard Worker        vt${N} = vn${N} * vminus_ln2_lo + vt${N};
91*4bdc9457SAndroid Build Coastguard Worker
92*4bdc9457SAndroid Build Coastguard Worker      // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
93*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
94*4bdc9457SAndroid Build Coastguard Worker        float vp${N} = vt${N} * vc2;
95*4bdc9457SAndroid Build Coastguard Worker
96*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
97*4bdc9457SAndroid Build Coastguard Worker        vp${N} = vp${N} * vt${N} + vt${N};
98*4bdc9457SAndroid Build Coastguard Worker
99*4bdc9457SAndroid Build Coastguard Worker      // Reconstruct the final f value:
100*4bdc9457SAndroid Build Coastguard Worker      //   f = s * (1 + t * (1 + t * c2))
101*4bdc9457SAndroid Build Coastguard Worker      //     = s * (1 + t + t * (t * c2))
102*4bdc9457SAndroid Build Coastguard Worker      //     = s + s * (t + t * (t * c2))
103*4bdc9457SAndroid Build Coastguard Worker      //     = s + s * p
104*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
105*4bdc9457SAndroid Build Coastguard Worker        float vf${N} = vp${N} * vs${N} + vs${N};
106*4bdc9457SAndroid Build Coastguard Worker
107*4bdc9457SAndroid Build Coastguard Worker      // For inputs below denormal cutoff, replace output with +0.0f.
108*4bdc9457SAndroid Build Coastguard Worker      // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
109*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
110*4bdc9457SAndroid Build Coastguard Worker        if XNN_UNPREDICTABLE(vx${N} < vdenorm_cutoff) {
111*4bdc9457SAndroid Build Coastguard Worker          vf${N} = 0.0f;
112*4bdc9457SAndroid Build Coastguard Worker        }
113*4bdc9457SAndroid Build Coastguard Worker
114*4bdc9457SAndroid Build Coastguard Worker      // Store ${ELEMENTS_TILE} outputs at a time.
115*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
116*4bdc9457SAndroid Build Coastguard Worker        output[${N}] = vf${N};
117*4bdc9457SAndroid Build Coastguard Worker      output += ${ELEMENTS_TILE};
118*4bdc9457SAndroid Build Coastguard Worker
119*4bdc9457SAndroid Build Coastguard Worker      // Accumulate computed exponents.
120*4bdc9457SAndroid Build Coastguard Worker      $for N in range(ELEMENTS_TILE):
121*4bdc9457SAndroid Build Coastguard Worker        vacc${N % ACCUMULATORS} += vf${N};
122*4bdc9457SAndroid Build Coastguard Worker    }
123*4bdc9457SAndroid Build Coastguard Worker    $if ACCUMULATORS > 1:
124*4bdc9457SAndroid Build Coastguard Worker      // Add up all accumulators to vacc0
125*4bdc9457SAndroid Build Coastguard Worker      $ACC_SLICE = 1
126*4bdc9457SAndroid Build Coastguard Worker      $while ACC_SLICE < ACCUMULATORS:
127*4bdc9457SAndroid Build Coastguard Worker        $for A in range(0, ACCUMULATORS, ACC_SLICE * 2):
128*4bdc9457SAndroid Build Coastguard Worker          $if A + ACC_SLICE < ACCUMULATORS:
129*4bdc9457SAndroid Build Coastguard Worker            vacc${A} += vacc${A + ACC_SLICE};
130*4bdc9457SAndroid Build Coastguard Worker        $ACC_SLICE *= 2
131*4bdc9457SAndroid Build Coastguard Worker
132*4bdc9457SAndroid Build Coastguard Worker    float vacc = vacc0;
133*4bdc9457SAndroid Build Coastguard Worker  $else:
134*4bdc9457SAndroid Build Coastguard Worker    float vacc = 0.0f;
135*4bdc9457SAndroid Build Coastguard Worker  for (; elements >= sizeof(float); elements -= sizeof(float)) {
136*4bdc9457SAndroid Build Coastguard Worker    // Load 1 input at a time.
137*4bdc9457SAndroid Build Coastguard Worker    const float vi = *input++;
138*4bdc9457SAndroid Build Coastguard Worker
139*4bdc9457SAndroid Build Coastguard Worker    // Subtract maximum input x := i - i_max. This implies x <= 0.
140*4bdc9457SAndroid Build Coastguard Worker    const float vx = vi - vi_max;
141*4bdc9457SAndroid Build Coastguard Worker
142*4bdc9457SAndroid Build Coastguard Worker    // Compute reduced argument n := round(x * 64 / log(2)).
143*4bdc9457SAndroid Build Coastguard Worker    // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
144*4bdc9457SAndroid Build Coastguard Worker    // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
145*4bdc9457SAndroid Build Coastguard Worker    // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
146*4bdc9457SAndroid Build Coastguard Worker    // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
147*4bdc9457SAndroid Build Coastguard Worker    // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
148*4bdc9457SAndroid Build Coastguard Worker    // algorithm.
149*4bdc9457SAndroid Build Coastguard Worker    float vn = vx * vlog2e + vmagic_bias;
150*4bdc9457SAndroid Build Coastguard Worker
151*4bdc9457SAndroid Build Coastguard Worker    // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
152*4bdc9457SAndroid Build Coastguard Worker    // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
153*4bdc9457SAndroid Build Coastguard Worker    // e := int(n / 64). We create s in two steps:
154*4bdc9457SAndroid Build Coastguard Worker    // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
155*4bdc9457SAndroid Build Coastguard Worker    //    fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
156*4bdc9457SAndroid Build Coastguard Worker    // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
157*4bdc9457SAndroid Build Coastguard Worker    //    number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
158*4bdc9457SAndroid Build Coastguard Worker    //    and thus the adjusted exponent is not lower than -126.
159*4bdc9457SAndroid Build Coastguard Worker    //
160*4bdc9457SAndroid Build Coastguard Worker    // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
161*4bdc9457SAndroid Build Coastguard Worker    const uint32_t ve = (float_as_uint32(vn) & UINT32_C(0xFFFFFFC0)) << 17;
162*4bdc9457SAndroid Build Coastguard Worker
163*4bdc9457SAndroid Build Coastguard Worker    // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
164*4bdc9457SAndroid Build Coastguard Worker    const uint32_t vidx = float_as_uint32(vn) & vindex_mask;
165*4bdc9457SAndroid Build Coastguard Worker    // Adjust exponent of the value l fetched from the table to get the final s value.
166*4bdc9457SAndroid Build Coastguard Worker    const float vs = uint32_as_float(xnn_table_exp2_k_over_64[vidx] + ve);
167*4bdc9457SAndroid Build Coastguard Worker
168*4bdc9457SAndroid Build Coastguard Worker    // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
169*4bdc9457SAndroid Build Coastguard Worker    vn -= vmagic_bias;
170*4bdc9457SAndroid Build Coastguard Worker
171*4bdc9457SAndroid Build Coastguard Worker    // Compute reduced argument t := x - n * log(2) / 64.
172*4bdc9457SAndroid Build Coastguard Worker    // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
173*4bdc9457SAndroid Build Coastguard Worker    float vt = vn * vminus_ln2_hi + vx;
174*4bdc9457SAndroid Build Coastguard Worker    vt = vn * vminus_ln2_lo + vt;
175*4bdc9457SAndroid Build Coastguard Worker
176*4bdc9457SAndroid Build Coastguard Worker    // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
177*4bdc9457SAndroid Build Coastguard Worker    float vp = vt * vc2;
178*4bdc9457SAndroid Build Coastguard Worker    vp = vp * vt + vt;
179*4bdc9457SAndroid Build Coastguard Worker
180*4bdc9457SAndroid Build Coastguard Worker    // Reconstruct the final f value:
181*4bdc9457SAndroid Build Coastguard Worker    //   f = s * (1 + t * (1 + t * c2))
182*4bdc9457SAndroid Build Coastguard Worker    //     = s * (1 + t + t * (t * c2))
183*4bdc9457SAndroid Build Coastguard Worker    //     = s + s * (t + t * (t * c2))
184*4bdc9457SAndroid Build Coastguard Worker    //     = s + s * p
185*4bdc9457SAndroid Build Coastguard Worker    float vf = vp * vs + vs;
186*4bdc9457SAndroid Build Coastguard Worker
187*4bdc9457SAndroid Build Coastguard Worker    // For inputs below denormal cutoff, replace output with +0.0f.
188*4bdc9457SAndroid Build Coastguard Worker    // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
189*4bdc9457SAndroid Build Coastguard Worker    if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
190*4bdc9457SAndroid Build Coastguard Worker      vf = 0.0f;
191*4bdc9457SAndroid Build Coastguard Worker    }
192*4bdc9457SAndroid Build Coastguard Worker
193*4bdc9457SAndroid Build Coastguard Worker    // Store 1 output at a time.
194*4bdc9457SAndroid Build Coastguard Worker    *output++ = vf;
195*4bdc9457SAndroid Build Coastguard Worker
196*4bdc9457SAndroid Build Coastguard Worker    // Accumulate computed exponents.
197*4bdc9457SAndroid Build Coastguard Worker    vacc += vf;
198*4bdc9457SAndroid Build Coastguard Worker  }
199*4bdc9457SAndroid Build Coastguard Worker  *sum = vacc;
200*4bdc9457SAndroid Build Coastguard Worker}
201