• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #include <fp16/bitcasts.h>
13 
14 #include <xnnpack/scalar-utils.h>
15 #include <xnnpack/requantization-stubs.h>
16 
17 
xnn_requantize_precise__scalar_unsigned32(size_t n,const int32_t * input,float scale,uint8_t zero_point,uint8_t qmin,uint8_t qmax,uint8_t * output)18 void xnn_requantize_precise__scalar_unsigned32(
19     size_t n,
20     const int32_t* input,
21     float scale,
22     uint8_t zero_point,
23     uint8_t qmin,
24     uint8_t qmax,
25     uint8_t* output)
26 {
27   assert(n % 4 == 0);
28   assert(scale < 1.0f);
29   assert(scale >= 0x1.0p-32f);
30 
31   const uint32_t scale_bits = fp32_to_bits(scale);
32   const uint32_t multiplier = (scale_bits << 8) | UINT32_C(0x80000000);
33   const uint32_t shift = 127 + 31 - (scale_bits >> 23);
34   assert(shift >= 32);
35   assert(shift < 64);
36 
37   const uint64_t rounding = UINT64_C(1) << (shift - 1);
38   const uint32_t rounding_hi = (uint32_t)(rounding >> 32);
39   const uint32_t rounding_lo = (uint32_t) rounding;
40   const uint32_t shift_minus_32 = shift - 32;
41   const int32_t smin = (int32_t)(uint32_t) qmin - (int32_t)(uint32_t) zero_point;
42   const int32_t smax = (int32_t)(uint32_t) qmax - (int32_t)(uint32_t) zero_point;
43   for (; n != 0; n -= 4) {
44     const int32_t x = input[0];
45     const int32_t y = input[1];
46     const int32_t z = input[2];
47     const int32_t w = input[3];
48     input += 4;
49 
50     // Compute absolute value of input as unsigned 32-bit int.
51     // All further computations will work with unsigned values to avoid undefined behaviour on signed operations.
52     const uint32_t x_abs = (x >= 0) ? (uint32_t) x : -(uint32_t) x;
53     const uint32_t y_abs = (y >= 0) ? (uint32_t) y : -(uint32_t) y;
54     const uint32_t z_abs = (z >= 0) ? (uint32_t) z : -(uint32_t) z;
55     const uint32_t w_abs = (w >= 0) ? (uint32_t) w : -(uint32_t) w;
56 
57     // Compute full 64-bit product of 32-bit factors.
58     const uint64_t x_product = (uint64_t) x_abs * (uint64_t) multiplier;
59     const uint64_t y_product = (uint64_t) y_abs * (uint64_t) multiplier;
60     const uint64_t z_product = (uint64_t) z_abs * (uint64_t) multiplier;
61     const uint64_t w_product = (uint64_t) w_abs * (uint64_t) multiplier;
62 
63     // Shift the full 64-bit product right with rounding.
64     // Rounding is performed towards closest integer, with midpoints rounded up (same as away from zero).
65     //
66     // Generally, this operation requires both 64-bit addition and 64-bit shift, but we use two tricks to replace
67     // 64-bit operations with 32-bit operations.
68     //
69     // To avoid full 64-bit addition we make use of three facts:
70     // - 64-bit rounding value added before the shift is a power of 2, and thus has only one bit set.
71     // - When 0x1.0p-32f <= scale < 0x1.0p-31f, then the non-zero bit in rounding is in the low 32 bits, and
72     //   rounding is exactly 0x80000000 (2**31), because rounding is 2**(scale-1) and scale >= 32. In this case,
73     //   addition of rounding can affect high 32 bits of the product only through overflow, which happens if
74     //   low 32-bit part of the product equals or exceeds 0x80000000. We can reformulate the latter condition
75     //   as low 32-bit part of the product has the bit 31 set, and then overflow happens if both the low 32-bit part
76     //   of the product and the low 32-bit part of the rounding value have bit 31 set. Since 32-bit numbers with the
77     //   bit 31 set are negative when interpreted as signed integers, we can check the overflow condition as
78     //      (int32_t) (LOW(product) & LOW(rounding)) < 0
79     // - When 0x1.0p-31f <= scale < 1.0f, then the non-zero bit is in the high 32 bits of rounding. We just need
80     //   to do 32-bit addition of high 32 bits of rounding and high 32 bits of product. This addition never
81     //   overflows because product <= 0x80000000 * 0xFFFFFF00 < 2**63 and rounding = 2**(scale-1) <= 2**62.
82     //
83     // To avoid full 64-bit shift, we leverage the fact that shift >= 32, and do it in two steps:
84     // - Shift by 32, which can be implemented by extacting the high 32-bit word on 32-bit systems.
85     // - Shift by (shift - 32), which can be implemented as a 32-bit shift of high word of addition result.
86     const uint32_t x_carry_lo = (uint32_t)((int32_t)((uint32_t) x_product & rounding_lo) < 0);
87     const uint32_t y_carry_lo = (uint32_t)((int32_t)((uint32_t) y_product & rounding_lo) < 0);
88     const uint32_t z_carry_lo = (uint32_t)((int32_t)((uint32_t) z_product & rounding_lo) < 0);
89     const uint32_t w_carry_lo = (uint32_t)((int32_t)((uint32_t) w_product & rounding_lo) < 0);
90 
91     const uint32_t x_product_hi = (uint32_t)(x_product >> 32);
92     const uint32_t y_product_hi = (uint32_t)(y_product >> 32);
93     const uint32_t z_product_hi = (uint32_t)(z_product >> 32);
94     const uint32_t w_product_hi = (uint32_t)(w_product >> 32);
95 
96     const uint32_t x_abs_scaled = (uint32_t)(x_product_hi + rounding_hi + x_carry_lo) >> shift_minus_32;
97     const uint32_t y_abs_scaled = (uint32_t)(y_product_hi + rounding_hi + y_carry_lo) >> shift_minus_32;
98     const uint32_t z_abs_scaled = (uint32_t)(z_product_hi + rounding_hi + z_carry_lo) >> shift_minus_32;
99     const uint32_t w_abs_scaled = (uint32_t)(w_product_hi + rounding_hi + w_carry_lo) >> shift_minus_32;
100 
101     // Copy the sign of input to scaled absolute input value.
102     const int32_t x_scaled = (int32_t)(x >= 0 ? x_abs_scaled : -x_abs_scaled);
103     const int32_t y_scaled = (int32_t)(y >= 0 ? y_abs_scaled : -y_abs_scaled);
104     const int32_t z_scaled = (int32_t)(z >= 0 ? z_abs_scaled : -z_abs_scaled);
105     const int32_t w_scaled = (int32_t)(w >= 0 ? w_abs_scaled : -w_abs_scaled);
106 
107     // Clamp scaled value with zero point between (qmin - zero point) and (qmax - zero point).
108     const int32_t x_clamped = x_scaled < smin ? smin : x_scaled > smax ? smax : x_scaled;
109     const int32_t y_clamped = y_scaled < smin ? smin : y_scaled > smax ? smax : y_scaled;
110     const int32_t z_clamped = z_scaled < smin ? smin : z_scaled > smax ? smax : z_scaled;
111     const int32_t w_clamped = w_scaled < smin ? smin : w_scaled > smax ? smax : w_scaled;
112 
113     // Add zero point to clamped value.
114     // The result is guaranteed to be in [qmin, qmax] range.
115     //
116     // This addition can not be safely done before clamping, because scaled values are in [-2147483520, 2147483519]
117     // range, so addition of zero point (which can be up to 255) can overflow signed 32-bit integer.
118     const int32_t x_biased = x_clamped + zero_point;
119     const int32_t y_biased = y_clamped + zero_point;
120     const int32_t z_biased = z_clamped + zero_point;
121     const int32_t w_biased = w_clamped + zero_point;
122 
123     output[0] = (uint8_t) x_biased;
124     output[1] = (uint8_t) y_biased;
125     output[2] = (uint8_t) z_biased;
126     output[3] = (uint8_t) w_biased;
127     output += 4;
128   }
129 }
130 
xnn_requantize_precise__scalar_unsigned64(size_t n,const int32_t * input,float scale,uint8_t zero_point,uint8_t qmin,uint8_t qmax,uint8_t * output)131 void xnn_requantize_precise__scalar_unsigned64(
132     size_t n,
133     const int32_t* input,
134     float scale,
135     uint8_t zero_point,
136     uint8_t qmin,
137     uint8_t qmax,
138     uint8_t* output)
139 {
140   assert(n % 4 == 0);
141   assert(scale < 1.0f);
142   assert(scale >= 0x1.0p-32f);
143 
144   const uint32_t scale_bits = fp32_to_bits(scale);
145   const uint32_t multiplier = (scale_bits & UINT32_C(0x007FFFFF)) | UINT32_C(0x00800000);
146   const uint32_t shift = 127 + 23 - (scale_bits >> 23);
147   assert(shift >= 24);
148   assert(shift < 56);
149 
150   const uint64_t rounding = UINT64_C(1) << (shift - 1);
151   const int32_t smin = (int32_t)(uint32_t) qmin - (int32_t)(uint32_t) zero_point;
152   const int32_t smax = (int32_t)(uint32_t) qmax - (int32_t)(uint32_t) zero_point;
153   for (; n != 0; n -= 4) {
154     const int32_t x = input[0];
155     const int32_t y = input[1];
156     const int32_t z = input[2];
157     const int32_t w = input[3];
158     input += 4;
159 
160     // Compute absolute value of input as unsigned 32-bit int.
161     // All further computations will work with unsigned values to avoid undefined behaviour on signed operations.
162     const uint32_t x_abs = (x >= 0) ? (uint32_t) x : -(uint32_t) x;
163     const uint32_t y_abs = (y >= 0) ? (uint32_t) y : -(uint32_t) y;
164     const uint32_t z_abs = (z >= 0) ? (uint32_t) z : -(uint32_t) z;
165     const uint32_t w_abs = (w >= 0) ? (uint32_t) w : -(uint32_t) w;
166 
167     // Compute full 64-bit product of 32-bit factors.
168     const uint64_t x_product = (uint64_t) x_abs * (uint64_t) multiplier;
169     const uint64_t y_product = (uint64_t) y_abs * (uint64_t) multiplier;
170     const uint64_t z_product = (uint64_t) z_abs * (uint64_t) multiplier;
171     const uint64_t w_product = (uint64_t) w_abs * (uint64_t) multiplier;
172 
173     // Shift the full 64-bit product right with rounding.
174     // Rounding is performed towards closest integer, with midpoints rounded up (same as away from zero).
175     //
176     // Note that although rounding is precomputed, it is dependent on shift value, and on processors with 64-bit
177     // "right shift with rounding" instruction each line below can be represented by just one such instruction
178     // (e.g. VRSHL.U64 on ARM NEON, URSHL in ARM64 Advanced SIMD).
179     const uint32_t x_abs_scaled = (uint32_t)((x_product + rounding) >> shift);
180     const uint32_t y_abs_scaled = (uint32_t)((y_product + rounding) >> shift);
181     const uint32_t z_abs_scaled = (uint32_t)((z_product + rounding) >> shift);
182     const uint32_t w_abs_scaled = (uint32_t)((w_product + rounding) >> shift);
183 
184     // Copy the sign of input to scaled absolute input value.
185     //
186     // On x86 processors with SSSE3 instruction set, this operation nicely maps to PSIGND instruction.
187     const int32_t x_scaled = (int32_t)(x >= 0 ? x_abs_scaled : -x_abs_scaled);
188     const int32_t y_scaled = (int32_t)(y >= 0 ? y_abs_scaled : -y_abs_scaled);
189     const int32_t z_scaled = (int32_t)(z >= 0 ? z_abs_scaled : -z_abs_scaled);
190     const int32_t w_scaled = (int32_t)(w >= 0 ? w_abs_scaled : -w_abs_scaled);
191 
192     // Clamp scaled value with zero point between (qmin - zero point) and (qmax - zero point).
193     const int32_t x_clamped = x_scaled < smin ? smin : x_scaled > smax ? smax : x_scaled;
194     const int32_t y_clamped = y_scaled < smin ? smin : y_scaled > smax ? smax : y_scaled;
195     const int32_t z_clamped = z_scaled < smin ? smin : z_scaled > smax ? smax : z_scaled;
196     const int32_t w_clamped = w_scaled < smin ? smin : w_scaled > smax ? smax : w_scaled;
197 
198     // Add zero point to clamped value.
199     // The result is guaranteed to be in [qmin, qmax] range.
200     //
201     // This addition can not be safely done before clamping, because scaled values are in [-2147483520, 2147483519]
202     // range, so addition of zero point (which can be up to 255) can overflow signed 32-bit integer.
203     const int32_t x_biased = x_clamped + zero_point;
204     const int32_t y_biased = y_clamped + zero_point;
205     const int32_t z_biased = z_clamped + zero_point;
206     const int32_t w_biased = w_clamped + zero_point;
207 
208     output[0] = (uint8_t) x_biased;
209     output[1] = (uint8_t) y_biased;
210     output[2] = (uint8_t) z_biased;
211     output[3] = (uint8_t) w_biased;
212     output += 4;
213   }
214 }
215 
xnn_requantize_precise__scalar_signed64(size_t n,const int32_t * input,float scale,uint8_t zero_point,uint8_t qmin,uint8_t qmax,uint8_t * output)216 void xnn_requantize_precise__scalar_signed64(
217     size_t n,
218     const int32_t* input,
219     float scale,
220     uint8_t zero_point,
221     uint8_t qmin,
222     uint8_t qmax,
223     uint8_t* output)
224 {
225   assert(n % 4 == 0);
226   assert(scale < 1.0f);
227   assert(scale >= 0x1.0p-32f);
228 
229   const uint32_t scale_bits = fp32_to_bits(scale);
230   const int32_t multiplier = ((int32_t) scale_bits & INT32_C(0x007FFFFF)) | INT32_C(0x00800000);
231   const uint32_t shift = 127 + 23 - (scale_bits >> 23);
232   assert(shift >= 24);
233   assert(shift < 56);
234 
235   const int64_t rounding = INT64_C(1) << (shift - 1);
236   const int32_t smin = (int32_t)(uint32_t) qmin - (int32_t)(uint32_t) zero_point;
237   const int32_t smax = (int32_t)(uint32_t) qmax - (int32_t)(uint32_t) zero_point;
238   for (; n != 0; n -= 4) {
239     const int32_t x = input[0];
240     const int32_t y = input[1];
241     const int32_t z = input[2];
242     const int32_t w = input[3];
243     input += 4;
244 
245     // Compute full 64-bit product of signed 32-bit factors.
246     //
247     // Note: multiplier can be treated as either signed or unsigned.
248     const int64_t x_product = (int64_t) x * (int64_t) multiplier;
249     const int64_t y_product = (int64_t) y * (int64_t) multiplier;
250     const int64_t z_product = (int64_t) z * (int64_t) multiplier;
251     const int64_t w_product = (int64_t) w * (int64_t) multiplier;
252 
253     // Adjust product before subsequent shift with rounding up to simulate shift with rounding away from zero.
254     const int64_t x_adjusted_product = x_product - (int64_t)(x < 0);
255     const int64_t y_adjusted_product = y_product - (int64_t)(y < 0);
256     const int64_t z_adjusted_product = z_product - (int64_t)(z < 0);
257     const int64_t w_adjusted_product = w_product - (int64_t)(w < 0);
258 
259     // Arithmetically shift the full 64-bit product right with rounding.
260     // Rounding is performed towards closest integer, with midpoints rounded up.
261     //
262     // Note that although rounding is precomputed, it is dependent on shift value, and on processors with 64-bit
263     // "right shift with rounding" instruction each line below can be represented by just one such instruction
264     // (e.g. VRSHL.S64 on ARM NEON, SRSHL in ARM64 Advanced SIMD).
265     const int32_t x_scaled = (int32_t) asr_s64(x_adjusted_product + rounding, shift);
266     const int32_t y_scaled = (int32_t) asr_s64(y_adjusted_product + rounding, shift);
267     const int32_t z_scaled = (int32_t) asr_s64(z_adjusted_product + rounding, shift);
268     const int32_t w_scaled = (int32_t) asr_s64(w_adjusted_product + rounding, shift);
269 
270     // Clamp scaled value with zero point between (qmin - zero point) and (qmax - zero point).
271     const int32_t x_clamped = x_scaled < smin ? smin : x_scaled > smax ? smax : x_scaled;
272     const int32_t y_clamped = y_scaled < smin ? smin : y_scaled > smax ? smax : y_scaled;
273     const int32_t z_clamped = z_scaled < smin ? smin : z_scaled > smax ? smax : z_scaled;
274     const int32_t w_clamped = w_scaled < smin ? smin : w_scaled > smax ? smax : w_scaled;
275 
276     // Add zero point to clamped value.
277     // The result is guaranteed to be in [qmin, qmax] range.
278     //
279     // This addition can not be safely done before clamping, because scaled values are in [-2147483520, 2147483519]
280     // range, so addition of zero point (which can be up to 255) can overflow signed 32-bit integer.
281     const int32_t x_biased = x_clamped + zero_point;
282     const int32_t y_biased = y_clamped + zero_point;
283     const int32_t z_biased = z_clamped + zero_point;
284     const int32_t w_biased = w_clamped + zero_point;
285 
286     output[0] = (uint8_t) x_biased;
287     output[1] = (uint8_t) y_biased;
288     output[2] = (uint8_t) z_biased;
289     output[3] = (uint8_t) w_biased;
290     output += 4;
291   }
292 }
293