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_qu8_requantize_rndna__scalar_unsigned32(size_t n,const int32_t * input,float scale,uint8_t zero_point,uint8_t qmin,uint8_t qmax,uint8_t * output)17*4bdc9457SAndroid Build Coastguard Worker void xnn_qu8_requantize_rndna__scalar_unsigned32(
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 uint8_t zero_point,
22*4bdc9457SAndroid Build Coastguard Worker uint8_t qmin,
23*4bdc9457SAndroid Build Coastguard Worker uint8_t qmax,
24*4bdc9457SAndroid Build Coastguard Worker uint8_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 const uint32_t scale_bits = float_as_uint32(scale);
31*4bdc9457SAndroid Build Coastguard Worker const uint32_t multiplier = (scale_bits << 8) | UINT32_C(0x80000000);
32*4bdc9457SAndroid Build Coastguard Worker const uint32_t shift = 127 + 31 - (scale_bits >> 23);
33*4bdc9457SAndroid Build Coastguard Worker assert(shift >= 32);
34*4bdc9457SAndroid Build Coastguard Worker assert(shift < 64);
35*4bdc9457SAndroid Build Coastguard Worker
36*4bdc9457SAndroid Build Coastguard Worker const uint64_t rounding = UINT64_C(1) << (shift - 1);
37*4bdc9457SAndroid Build Coastguard Worker const uint32_t rounding_hi = (uint32_t) (rounding >> 32);
38*4bdc9457SAndroid Build Coastguard Worker const uint32_t rounding_lo = (uint32_t) rounding;
39*4bdc9457SAndroid Build Coastguard Worker const uint32_t shift_minus_32 = shift - 32;
40*4bdc9457SAndroid Build Coastguard Worker const int32_t smin = (int32_t) (uint32_t) qmin - (int32_t) (uint32_t) zero_point;
41*4bdc9457SAndroid Build Coastguard Worker const int32_t smax = (int32_t) (uint32_t) qmax - (int32_t) (uint32_t) zero_point;
42*4bdc9457SAndroid Build Coastguard Worker for (; n != 0; n -= 4) {
43*4bdc9457SAndroid Build Coastguard Worker const int32_t x = input[0];
44*4bdc9457SAndroid Build Coastguard Worker const int32_t y = input[1];
45*4bdc9457SAndroid Build Coastguard Worker const int32_t z = input[2];
46*4bdc9457SAndroid Build Coastguard Worker const int32_t w = input[3];
47*4bdc9457SAndroid Build Coastguard Worker input += 4;
48*4bdc9457SAndroid Build Coastguard Worker
49*4bdc9457SAndroid Build Coastguard Worker // Compute absolute value of input as unsigned 32-bit int.
50*4bdc9457SAndroid Build Coastguard Worker // All further computations will work with unsigned values to avoid undefined behaviour on signed operations.
51*4bdc9457SAndroid Build Coastguard Worker const uint32_t x_abs = (x >= 0) ? (uint32_t) x : -(uint32_t) x;
52*4bdc9457SAndroid Build Coastguard Worker const uint32_t y_abs = (y >= 0) ? (uint32_t) y : -(uint32_t) y;
53*4bdc9457SAndroid Build Coastguard Worker const uint32_t z_abs = (z >= 0) ? (uint32_t) z : -(uint32_t) z;
54*4bdc9457SAndroid Build Coastguard Worker const uint32_t w_abs = (w >= 0) ? (uint32_t) w : -(uint32_t) w;
55*4bdc9457SAndroid Build Coastguard Worker
56*4bdc9457SAndroid Build Coastguard Worker // Compute full 64-bit product of 32-bit factors.
57*4bdc9457SAndroid Build Coastguard Worker const uint64_t x_product = (uint64_t) x_abs * (uint64_t) multiplier;
58*4bdc9457SAndroid Build Coastguard Worker const uint64_t y_product = (uint64_t) y_abs * (uint64_t) multiplier;
59*4bdc9457SAndroid Build Coastguard Worker const uint64_t z_product = (uint64_t) z_abs * (uint64_t) multiplier;
60*4bdc9457SAndroid Build Coastguard Worker const uint64_t w_product = (uint64_t) w_abs * (uint64_t) multiplier;
61*4bdc9457SAndroid Build Coastguard Worker
62*4bdc9457SAndroid Build Coastguard Worker // Shift the full 64-bit product right with rounding.
63*4bdc9457SAndroid Build Coastguard Worker // Rounding is performed towards closest integer, with midpoints rounded up (same as away from zero).
64*4bdc9457SAndroid Build Coastguard Worker //
65*4bdc9457SAndroid Build Coastguard Worker // Generally, this operation requires both 64-bit addition and 64-bit shift, but we use two tricks to replace
66*4bdc9457SAndroid Build Coastguard Worker // 64-bit operations with 32-bit operations.
67*4bdc9457SAndroid Build Coastguard Worker //
68*4bdc9457SAndroid Build Coastguard Worker // To avoid full 64-bit addition we make use of three facts:
69*4bdc9457SAndroid Build Coastguard Worker // - 64-bit rounding value added before the shift is a power of 2, and thus has only one bit set.
70*4bdc9457SAndroid Build Coastguard Worker // - When 0x1.0p-32f <= scale < 0x1.0p-31f, then the non-zero bit in rounding is in the low 32 bits, and
71*4bdc9457SAndroid Build Coastguard Worker // rounding is exactly 0x80000000 (2**31), because rounding is 2**(scale-1) and scale >= 32. In this case,
72*4bdc9457SAndroid Build Coastguard Worker // addition of rounding can affect high 32 bits of the product only through overflow, which happens if
73*4bdc9457SAndroid Build Coastguard Worker // low 32-bit part of the product equals or exceeds 0x80000000. We can reformulate the latter condition
74*4bdc9457SAndroid Build Coastguard Worker // as low 32-bit part of the product has the bit 31 set, and then overflow happens if both the low 32-bit part
75*4bdc9457SAndroid Build Coastguard Worker // of the product and the low 32-bit part of the rounding value have bit 31 set. Since 32-bit numbers with the
76*4bdc9457SAndroid Build Coastguard Worker // bit 31 set are negative when interpreted as signed integers, we can check the overflow condition as
77*4bdc9457SAndroid Build Coastguard Worker // (int32_t) (LOW(product) & LOW(rounding)) < 0
78*4bdc9457SAndroid Build Coastguard Worker // - When 0x1.0p-31f <= scale < 1.0f, then the non-zero bit is in the high 32 bits of rounding. We just need
79*4bdc9457SAndroid Build Coastguard Worker // to do 32-bit addition of high 32 bits of rounding and high 32 bits of product. This addition never
80*4bdc9457SAndroid Build Coastguard Worker // overflows because product <= 0x80000000 * 0xFFFFFF00 < 2**63 and rounding = 2**(scale-1) <= 2**62.
81*4bdc9457SAndroid Build Coastguard Worker //
82*4bdc9457SAndroid Build Coastguard Worker // To avoid full 64-bit shift, we leverage the fact that shift >= 32, and do it in two steps:
83*4bdc9457SAndroid Build Coastguard Worker // - Shift by 32, which can be implemented by extacting the high 32-bit word on 32-bit systems.
84*4bdc9457SAndroid Build Coastguard Worker // - Shift by (shift - 32), which can be implemented as a 32-bit shift of high word of addition result.
85*4bdc9457SAndroid Build Coastguard Worker const uint32_t x_carry_lo = (uint32_t) ((int32_t)((uint32_t) x_product & rounding_lo) < 0);
86*4bdc9457SAndroid Build Coastguard Worker const uint32_t y_carry_lo = (uint32_t) ((int32_t)((uint32_t) y_product & rounding_lo) < 0);
87*4bdc9457SAndroid Build Coastguard Worker const uint32_t z_carry_lo = (uint32_t) ((int32_t)((uint32_t) z_product & rounding_lo) < 0);
88*4bdc9457SAndroid Build Coastguard Worker const uint32_t w_carry_lo = (uint32_t) ((int32_t)((uint32_t) w_product & rounding_lo) < 0);
89*4bdc9457SAndroid Build Coastguard Worker
90*4bdc9457SAndroid Build Coastguard Worker const uint32_t x_product_hi = (uint32_t) (x_product >> 32);
91*4bdc9457SAndroid Build Coastguard Worker const uint32_t y_product_hi = (uint32_t) (y_product >> 32);
92*4bdc9457SAndroid Build Coastguard Worker const uint32_t z_product_hi = (uint32_t) (z_product >> 32);
93*4bdc9457SAndroid Build Coastguard Worker const uint32_t w_product_hi = (uint32_t) (w_product >> 32);
94*4bdc9457SAndroid Build Coastguard Worker
95*4bdc9457SAndroid Build Coastguard Worker const uint32_t x_abs_scaled = (uint32_t) (x_product_hi + rounding_hi + x_carry_lo) >> shift_minus_32;
96*4bdc9457SAndroid Build Coastguard Worker const uint32_t y_abs_scaled = (uint32_t) (y_product_hi + rounding_hi + y_carry_lo) >> shift_minus_32;
97*4bdc9457SAndroid Build Coastguard Worker const uint32_t z_abs_scaled = (uint32_t) (z_product_hi + rounding_hi + z_carry_lo) >> shift_minus_32;
98*4bdc9457SAndroid Build Coastguard Worker const uint32_t w_abs_scaled = (uint32_t) (w_product_hi + rounding_hi + w_carry_lo) >> shift_minus_32;
99*4bdc9457SAndroid Build Coastguard Worker
100*4bdc9457SAndroid Build Coastguard Worker // Copy the sign of input to scaled absolute input value.
101*4bdc9457SAndroid Build Coastguard Worker const int32_t x_scaled = (int32_t) (x >= 0 ? x_abs_scaled : -x_abs_scaled);
102*4bdc9457SAndroid Build Coastguard Worker const int32_t y_scaled = (int32_t) (y >= 0 ? y_abs_scaled : -y_abs_scaled);
103*4bdc9457SAndroid Build Coastguard Worker const int32_t z_scaled = (int32_t) (z >= 0 ? z_abs_scaled : -z_abs_scaled);
104*4bdc9457SAndroid Build Coastguard Worker const int32_t w_scaled = (int32_t) (w >= 0 ? w_abs_scaled : -w_abs_scaled);
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 not be safely done before clamping, because scaled values are in [-2147483520, 2147483519]
116*4bdc9457SAndroid Build Coastguard Worker // range, so addition of zero point (which can be up to 255) can 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] = (uint8_t) x_biased;
123*4bdc9457SAndroid Build Coastguard Worker output[1] = (uint8_t) y_biased;
124*4bdc9457SAndroid Build Coastguard Worker output[2] = (uint8_t) z_biased;
125*4bdc9457SAndroid Build Coastguard Worker output[3] = (uint8_t) w_biased;
126*4bdc9457SAndroid Build Coastguard Worker output += 4;
127*4bdc9457SAndroid Build Coastguard Worker }
128*4bdc9457SAndroid Build Coastguard Worker }
129