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