xref: /aosp_15_r20/external/XNNPACK/src/qs8-requantization/rndna-scalar-unsigned64.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_rndna__scalar_unsigned64(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_rndna__scalar_unsigned64(
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   const uint32_t scale_bits = float_as_uint32(scale);
31   const uint32_t multiplier = (scale_bits & UINT32_C(0x007FFFFF)) | UINT32_C(0x00800000);
32   const uint32_t shift = 127 + 23 - (scale_bits >> 23);
33   assert(shift >= 24);
34   assert(shift < 56);
35 
36   const uint64_t rounding = UINT64_C(1) << (shift - 1);
37   const int32_t smin = (int32_t) qmin - (int32_t) zero_point;
38   const int32_t smax = (int32_t) qmax - (int32_t) zero_point;
39   for (; n != 0; n -= 4) {
40     const int32_t x = input[0];
41     const int32_t y = input[1];
42     const int32_t z = input[2];
43     const int32_t w = input[3];
44     input += 4;
45 
46     // Compute absolute value of input as unsigned 32-bit int.
47     // All further computations will work with unsigned values to avoid undefined behaviour on signed operations.
48     const uint32_t x_abs = (x >= 0) ? (uint32_t) x : -(uint32_t) x;
49     const uint32_t y_abs = (y >= 0) ? (uint32_t) y : -(uint32_t) y;
50     const uint32_t z_abs = (z >= 0) ? (uint32_t) z : -(uint32_t) z;
51     const uint32_t w_abs = (w >= 0) ? (uint32_t) w : -(uint32_t) w;
52 
53     // Compute full 64-bit product of 32-bit factors.
54     const uint64_t x_product = (uint64_t) x_abs * (uint64_t) multiplier;
55     const uint64_t y_product = (uint64_t) y_abs * (uint64_t) multiplier;
56     const uint64_t z_product = (uint64_t) z_abs * (uint64_t) multiplier;
57     const uint64_t w_product = (uint64_t) w_abs * (uint64_t) multiplier;
58 
59     // Shift the full 64-bit product right with rounding.
60     // Rounding is performed towards closest integer, with midpoints rounded up (same as away from zero).
61     //
62     // Note that although rounding is precomputed, it is dependent on shift value, and on processors with 64-bit
63     // "right shift with rounding" instruction each line below can be represented by just one such instruction
64     // (e.g. VRSHL.U64 on ARM NEON, URSHL in ARM64 Advanced SIMD).
65     const uint32_t x_abs_scaled = (uint32_t) ((x_product + rounding) >> shift);
66     const uint32_t y_abs_scaled = (uint32_t) ((y_product + rounding) >> shift);
67     const uint32_t z_abs_scaled = (uint32_t) ((z_product + rounding) >> shift);
68     const uint32_t w_abs_scaled = (uint32_t) ((w_product + rounding) >> shift);
69 
70     // Copy the sign of input to scaled absolute input value.
71     //
72     // On x86 processors with SSSE3 instruction set, this operation nicely maps to PSIGND instruction.
73     const int32_t x_scaled = (int32_t) (x >= 0 ? x_abs_scaled : -x_abs_scaled);
74     const int32_t y_scaled = (int32_t) (y >= 0 ? y_abs_scaled : -y_abs_scaled);
75     const int32_t z_scaled = (int32_t) (z >= 0 ? z_abs_scaled : -z_abs_scaled);
76     const int32_t w_scaled = (int32_t) (w >= 0 ? w_abs_scaled : -w_abs_scaled);
77 
78     // Clamp scaled value with zero point between (qmin - zero point) and (qmax - zero point).
79     const int32_t x_clamped = math_min_s32(math_max_s32(x_scaled, smin), smax);
80     const int32_t y_clamped = math_min_s32(math_max_s32(y_scaled, smin), smax);
81     const int32_t z_clamped = math_min_s32(math_max_s32(z_scaled, smin), smax);
82     const int32_t w_clamped = math_min_s32(math_max_s32(w_scaled, smin), smax);
83 
84     // Add zero point to clamped value.
85     // The result is guaranteed to be in [qmin, qmax] range.
86     //
87     // This addition can not be safely done before clamping, because scaled values are in [-2147483520, 2147483519]
88     // range, so addition of zero point (which can be up to 127) can overflow signed 32-bit integer.
89     const int32_t x_biased = x_clamped + zero_point;
90     const int32_t y_biased = y_clamped + zero_point;
91     const int32_t z_biased = z_clamped + zero_point;
92     const int32_t w_biased = w_clamped + zero_point;
93 
94     output[0] = (int8_t) x_biased;
95     output[1] = (int8_t) y_biased;
96     output[2] = (int8_t) z_biased;
97     output[3] = (int8_t) w_biased;
98     output += 4;
99   }
100 }
101