1*4bdc9457SAndroid Build Coastguard Worker // Copyright (c) Facebook, Inc. and its affiliates.
2*4bdc9457SAndroid Build Coastguard Worker // All rights reserved.
3*4bdc9457SAndroid Build Coastguard Worker //
4*4bdc9457SAndroid Build Coastguard Worker // Copyright 2019 Google LLC
5*4bdc9457SAndroid Build Coastguard Worker //
6*4bdc9457SAndroid Build Coastguard Worker // This source code is licensed under the BSD-style license found in the
7*4bdc9457SAndroid Build Coastguard Worker // LICENSE file in the root directory of this source tree.
8*4bdc9457SAndroid Build Coastguard Worker
9*4bdc9457SAndroid Build Coastguard Worker #include <assert.h>
10*4bdc9457SAndroid Build Coastguard Worker #include <stdint.h>
11*4bdc9457SAndroid Build Coastguard Worker #include <stddef.h>
12*4bdc9457SAndroid Build Coastguard Worker
13*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/math.h>
14*4bdc9457SAndroid Build Coastguard Worker #include <xnnpack/requantization-stubs.h>
15*4bdc9457SAndroid Build Coastguard Worker
16*4bdc9457SAndroid Build Coastguard Worker
xnn_qs8_requantize_gemmlowp__scalar(size_t n,const int32_t * input,float scale,int8_t zero_point,int8_t qmin,int8_t qmax,int8_t * output)17*4bdc9457SAndroid Build Coastguard Worker void xnn_qs8_requantize_gemmlowp__scalar(
18*4bdc9457SAndroid Build Coastguard Worker size_t n,
19*4bdc9457SAndroid Build Coastguard Worker const int32_t* input,
20*4bdc9457SAndroid Build Coastguard Worker float scale,
21*4bdc9457SAndroid Build Coastguard Worker int8_t zero_point,
22*4bdc9457SAndroid Build Coastguard Worker int8_t qmin,
23*4bdc9457SAndroid Build Coastguard Worker int8_t qmax,
24*4bdc9457SAndroid Build Coastguard Worker int8_t* output)
25*4bdc9457SAndroid Build Coastguard Worker {
26*4bdc9457SAndroid Build Coastguard Worker assert(n % 4 == 0);
27*4bdc9457SAndroid Build Coastguard Worker assert(scale < 1.0f);
28*4bdc9457SAndroid Build Coastguard Worker assert(scale >= 0x1.0p-32f);
29*4bdc9457SAndroid Build Coastguard Worker
30*4bdc9457SAndroid Build Coastguard Worker // Compute requantization parameters.
31*4bdc9457SAndroid Build Coastguard Worker const uint32_t scale_bits = float_as_uint32(scale);
32*4bdc9457SAndroid Build Coastguard Worker
33*4bdc9457SAndroid Build Coastguard Worker // Multiplier is in [0x40000000, 0x7FFFFF80] range.
34*4bdc9457SAndroid Build Coastguard Worker const int32_t multiplier = (int32_t)(((scale_bits & UINT32_C(0x007FFFFF)) | UINT32_C(0x00800000)) << 7);
35*4bdc9457SAndroid Build Coastguard Worker assert(multiplier >= INT32_C(0x40000000));
36*4bdc9457SAndroid Build Coastguard Worker assert(multiplier <= INT32_C(0x7FFFFF80));
37*4bdc9457SAndroid Build Coastguard Worker
38*4bdc9457SAndroid Build Coastguard Worker // Shift is in [0, 31] range.
39*4bdc9457SAndroid Build Coastguard Worker const int32_t shift = 127 + 31 - 32 - (float_as_uint32(scale) >> 23);
40*4bdc9457SAndroid Build Coastguard Worker assert(shift >= 0);
41*4bdc9457SAndroid Build Coastguard Worker assert(shift < 32);
42*4bdc9457SAndroid Build Coastguard Worker
43*4bdc9457SAndroid Build Coastguard Worker const int64_t q31rounding = INT64_C(0x40000000);
44*4bdc9457SAndroid Build Coastguard Worker const int32_t remainder_mask = (int32_t)((UINT32_C(1) << shift) - UINT32_C(1));
45*4bdc9457SAndroid Build Coastguard Worker const int32_t threshold = (int32_t)((uint32_t) remainder_mask >> 1);
46*4bdc9457SAndroid Build Coastguard Worker const int32_t smin = (int32_t) qmin - (int32_t) zero_point;
47*4bdc9457SAndroid Build Coastguard Worker const int32_t smax = (int32_t) qmax - (int32_t) zero_point;
48*4bdc9457SAndroid Build Coastguard Worker for (; n != 0; n -= 4) {
49*4bdc9457SAndroid Build Coastguard Worker const int32_t x = input[0];
50*4bdc9457SAndroid Build Coastguard Worker const int32_t y = input[1];
51*4bdc9457SAndroid Build Coastguard Worker const int32_t z = input[2];
52*4bdc9457SAndroid Build Coastguard Worker const int32_t w = input[3];
53*4bdc9457SAndroid Build Coastguard Worker input += 4;
54*4bdc9457SAndroid Build Coastguard Worker
55*4bdc9457SAndroid Build Coastguard Worker // Compute full 64-bit product of signed 32-bit factors.
56*4bdc9457SAndroid Build Coastguard Worker //
57*4bdc9457SAndroid Build Coastguard Worker // Note: multiplier can be treated as either signed or unsigned.
58*4bdc9457SAndroid Build Coastguard Worker const int64_t x_product = (int64_t) x * (int64_t) multiplier;
59*4bdc9457SAndroid Build Coastguard Worker const int64_t y_product = (int64_t) y * (int64_t) multiplier;
60*4bdc9457SAndroid Build Coastguard Worker const int64_t z_product = (int64_t) z * (int64_t) multiplier;
61*4bdc9457SAndroid Build Coastguard Worker const int64_t w_product = (int64_t) w * (int64_t) multiplier;
62*4bdc9457SAndroid Build Coastguard Worker
63*4bdc9457SAndroid Build Coastguard Worker // Get the Q31 multiplication result by extracting bits 31-62 of the product, with rounding up.
64*4bdc9457SAndroid Build Coastguard Worker // Add rounding value (0x40000000) and then shift right by 31 bits and extract the low 32-bit word.
65*4bdc9457SAndroid Build Coastguard Worker // Note: casts to unsigned types are needed to avoid undefined behavior.
66*4bdc9457SAndroid Build Coastguard Worker // Given the multiplier range, the result of Q31 multiplication is in [-2147483520, 2147483519] range.
67*4bdc9457SAndroid Build Coastguard Worker const int32_t x_q31product = (int32_t) (uint32_t) ((uint64_t) (x_product + q31rounding) >> 31);
68*4bdc9457SAndroid Build Coastguard Worker const int32_t y_q31product = (int32_t) (uint32_t) ((uint64_t) (y_product + q31rounding) >> 31);
69*4bdc9457SAndroid Build Coastguard Worker const int32_t z_q31product = (int32_t) (uint32_t) ((uint64_t) (z_product + q31rounding) >> 31);
70*4bdc9457SAndroid Build Coastguard Worker const int32_t w_q31product = (int32_t) (uint32_t) ((uint64_t) (w_product + q31rounding) >> 31);
71*4bdc9457SAndroid Build Coastguard Worker
72*4bdc9457SAndroid Build Coastguard Worker // Arithmetically shift the adjusted product right with rounding.
73*4bdc9457SAndroid Build Coastguard Worker // Rounding is performed towards closest integer, with midpoints rounded away from zero.
74*4bdc9457SAndroid Build Coastguard Worker //
75*4bdc9457SAndroid Build Coastguard Worker // Shift with correct rounding could be efficiently implemented by pre-adding rounding constant, but with input in
76*4bdc9457SAndroid Build Coastguard Worker // [-2147483520, 2147483519] range and rounding constant up to 2**30 we can't rule out overflow. This limitation
77*4bdc9457SAndroid Build Coastguard Worker // leaves us with 3 options:
78*4bdc9457SAndroid Build Coastguard Worker // 1. Extend input to 64-bit signed integer, perform addition and shift on 64-bit integers, then truncate result
79*4bdc9457SAndroid Build Coastguard Worker // to 32 bits.
80*4bdc9457SAndroid Build Coastguard Worker // 2. Detect overflow and handle this situation separately. Note that overflow is possible only when input is
81*4bdc9457SAndroid Build Coastguard Worker // positive, and even when addition of a rounding constant overflows 32-bit signed integer, it still doesn't
82*4bdc9457SAndroid Build Coastguard Worker // overflow 32-bit unsigned integer. Thus, in case of signed overflow, we can compute the result using unsigned
83*4bdc9457SAndroid Build Coastguard Worker // arithmetics, specifically using logical shift right instead of arithmetic shift right.
84*4bdc9457SAndroid Build Coastguard Worker // 3. Performs arithmetic shift as is, which will produce division result rounded down. Then compute remainder of
85*4bdc9457SAndroid Build Coastguard Worker // this division by a power of 2, and adjust the result. Result needs adjustment (increment by 1) when
86*4bdc9457SAndroid Build Coastguard Worker // - input is positive, shift is non-zero, and remainder >= 2**(shift - 1), e.g. 10 >> 2 needs adjustment
87*4bdc9457SAndroid Build Coastguard Worker // - input is negative, shift is non-zero, and remainder > 2**(shift - 1), e.g. -10 >> 2 doesn't need adjustment
88*4bdc9457SAndroid Build Coastguard Worker // These conditions can be generalized as
89*4bdc9457SAndroid Build Coastguard Worker // remainder + (input <= 0) > 2**(shift - 1)
90*4bdc9457SAndroid Build Coastguard Worker // or equivalently
91*4bdc9457SAndroid Build Coastguard Worker // remainder - (input < 0) > ((2**shift - 1) >> 1)
92*4bdc9457SAndroid Build Coastguard Worker // When shift is 0, remainder is 0 as well, the last condition is always false, and no adjustment is done.
93*4bdc9457SAndroid Build Coastguard Worker //
94*4bdc9457SAndroid Build Coastguard Worker // Among these options, option 3 is the most performant across the board, although option 1 is promising for 64-bit
95*4bdc9457SAndroid Build Coastguard Worker // instruction sets.
96*4bdc9457SAndroid Build Coastguard Worker const int32_t x_remainder = (x_q31product & remainder_mask) - (int32_t) (x_q31product < 0);
97*4bdc9457SAndroid Build Coastguard Worker const int32_t y_remainder = (y_q31product & remainder_mask) - (int32_t) (y_q31product < 0);
98*4bdc9457SAndroid Build Coastguard Worker const int32_t z_remainder = (z_q31product & remainder_mask) - (int32_t) (z_q31product < 0);
99*4bdc9457SAndroid Build Coastguard Worker const int32_t w_remainder = (w_q31product & remainder_mask) - (int32_t) (w_q31product < 0);
100*4bdc9457SAndroid Build Coastguard Worker
101*4bdc9457SAndroid Build Coastguard Worker const int32_t x_scaled = math_asr_s32(x_q31product, shift) + (int32_t) (x_remainder > threshold);
102*4bdc9457SAndroid Build Coastguard Worker const int32_t y_scaled = math_asr_s32(y_q31product, shift) + (int32_t) (y_remainder > threshold);
103*4bdc9457SAndroid Build Coastguard Worker const int32_t z_scaled = math_asr_s32(z_q31product, shift) + (int32_t) (z_remainder > threshold);
104*4bdc9457SAndroid Build Coastguard Worker const int32_t w_scaled = math_asr_s32(w_q31product, shift) + (int32_t) (w_remainder > threshold);
105*4bdc9457SAndroid Build Coastguard Worker
106*4bdc9457SAndroid Build Coastguard Worker // Clamp scaled value with zero point between (qmin - zero point) and (qmax - zero point).
107*4bdc9457SAndroid Build Coastguard Worker const int32_t x_clamped = math_min_s32(math_max_s32(x_scaled, smin), smax);
108*4bdc9457SAndroid Build Coastguard Worker const int32_t y_clamped = math_min_s32(math_max_s32(y_scaled, smin), smax);
109*4bdc9457SAndroid Build Coastguard Worker const int32_t z_clamped = math_min_s32(math_max_s32(z_scaled, smin), smax);
110*4bdc9457SAndroid Build Coastguard Worker const int32_t w_clamped = math_min_s32(math_max_s32(w_scaled, smin), smax);
111*4bdc9457SAndroid Build Coastguard Worker
112*4bdc9457SAndroid Build Coastguard Worker // Add zero point to clamped value.
113*4bdc9457SAndroid Build Coastguard Worker // The result is guaranteed to be in [qmin, qmax] range.
114*4bdc9457SAndroid Build Coastguard Worker //
115*4bdc9457SAndroid Build Coastguard Worker // This addition can be safely done before clamping, because scaled values are in [-2147483520, 2147483519]
116*4bdc9457SAndroid Build Coastguard Worker // range, so addition of zero point (which is in [-128, 127] range) can not overflow signed 32-bit integer.
117*4bdc9457SAndroid Build Coastguard Worker const int32_t x_biased = x_clamped + zero_point;
118*4bdc9457SAndroid Build Coastguard Worker const int32_t y_biased = y_clamped + zero_point;
119*4bdc9457SAndroid Build Coastguard Worker const int32_t z_biased = z_clamped + zero_point;
120*4bdc9457SAndroid Build Coastguard Worker const int32_t w_biased = w_clamped + zero_point;
121*4bdc9457SAndroid Build Coastguard Worker
122*4bdc9457SAndroid Build Coastguard Worker output[0] = (int8_t) x_biased;
123*4bdc9457SAndroid Build Coastguard Worker output[1] = (int8_t) y_biased;
124*4bdc9457SAndroid Build Coastguard Worker output[2] = (int8_t) z_biased;
125*4bdc9457SAndroid Build Coastguard Worker output[3] = (int8_t) w_biased;
126*4bdc9457SAndroid Build Coastguard Worker output += 4;
127*4bdc9457SAndroid Build Coastguard Worker }
128*4bdc9457SAndroid Build Coastguard Worker }
129