• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 #include <stdint.h>
8 #include <stddef.h>
9 
10 #include <wasm_simd128.h>
11 
12 #include <xnnpack/requantization-stubs.h>
13 
14 
xnn_qu8_requantize_fp32__wasmsimd(size_t n,const int32_t * input,float scale,uint8_t zero_point,uint8_t qmin,uint8_t qmax,uint8_t * output)15 void xnn_qu8_requantize_fp32__wasmsimd(
16     size_t n,
17     const int32_t* input,
18     float scale,
19     uint8_t zero_point,
20     uint8_t qmin,
21     uint8_t qmax,
22     uint8_t* output)
23 {
24   assert(n % 16 == 0);
25   assert(scale < 1.0f);
26   assert(scale >= 0x1.0p-32f);
27 
28   const v128_t vscale = wasm_f32x4_splat(scale);
29   const v128_t vfmin = wasm_f32x4_splat((float) ((int32_t) (uint32_t) qmin - (int32_t) (uint32_t) zero_point));
30   const v128_t vfmax = wasm_f32x4_splat((float) ((int32_t) (uint32_t) qmax - (int32_t) (uint32_t) zero_point));
31   const v128_t vfmagic = wasm_f32x4_splat(12582912.0f);
32   const v128_t vimagic = wasm_i32x4_splat(INT32_C(0x4B400000) - (int32_t) (uint32_t) zero_point);
33   for (; n != 0; n -= 16) {
34     const v128_t x = wasm_v128_load(input);
35     const v128_t y = wasm_v128_load(input + 4);
36     const v128_t z = wasm_v128_load(input + 8);
37     const v128_t w = wasm_v128_load(input + 12);
38     input += 16;
39 
40     // Convert int32_t input to FP32 and multiply by FP32 scale.
41     // Both operations involve statistically unbiased roundings:
42     // - Large int32_t values can't be exactly represented as FP32. The conversion instruction in WAsm SIMD would
43     //   round it to nearest FP32 value with ties to even.
44     // - Product of two FP32 values is generally not exactly representation as an FP32 value, and will be rounded
45     //   to nearest FP32 value with ties to even.
46     const v128_t x_scaled = wasm_f32x4_mul(wasm_f32x4_convert_i32x4(x), vscale);
47     const v128_t y_scaled = wasm_f32x4_mul(wasm_f32x4_convert_i32x4(y), vscale);
48     const v128_t z_scaled = wasm_f32x4_mul(wasm_f32x4_convert_i32x4(z), vscale);
49     const v128_t w_scaled = wasm_f32x4_mul(wasm_f32x4_convert_i32x4(w), vscale);
50 
51     // WAsm SIMD offers only a floating-point to integer conversion instruction with rounding towards zero.
52     // In lieu of conversion instruction with rounding-to-nearest-even, we use a magic trick of adding a large
53     // number (1.5 * 2**23) to scaled value to cause rounding to integer, and then substracing this magic number as
54     // integer. This trick works only in a limited range (absolute value of input must be less than 2**22), so
55     // generally we have to clamp input to this range before using the magic. However, clamping to any smaller range
56     // works just as well, and thus we clamp to [qmin - zero point, qmax - zero point] range so that after we add
57     // zero point to the result, it gets into target [qmin, qmax] range.
58     const v128_t x_clamped = wasm_f32x4_min(wasm_f32x4_max(x_scaled, vfmin), vfmax);
59     const v128_t y_clamped = wasm_f32x4_min(wasm_f32x4_max(y_scaled, vfmin), vfmax);
60     const v128_t z_clamped = wasm_f32x4_min(wasm_f32x4_max(z_scaled, vfmin), vfmax);
61     const v128_t w_clamped = wasm_f32x4_min(wasm_f32x4_max(w_scaled, vfmin), vfmax);
62 
63     // Conversion to integer using the "magic trick". Rounding is performed in the output of addition operation,
64     // and result is rounded to nearest even integer with ties to even.
65     const v128_t x_biased = wasm_i32x4_sub(wasm_f32x4_add(x_clamped, vfmagic), vimagic);
66     const v128_t y_biased = wasm_i32x4_sub(wasm_f32x4_add(y_clamped, vfmagic), vimagic);
67     const v128_t z_biased = wasm_i32x4_sub(wasm_f32x4_add(z_clamped, vfmagic), vimagic);
68     const v128_t w_biased = wasm_i32x4_sub(wasm_f32x4_add(w_clamped, vfmagic), vimagic);
69 
70     // Select low 8 bits of each 32-bit integer in the vectors for the output.
71     // Since result is already clamped to [qmin, qmax] subrange of [0, 255], saturation is not needed.
72     const v128_t xy_packed = wasm_v16x8_shuffle(x_biased, y_biased, 0, 2, 4, 6, 8, 10, 12, 14);
73     const v128_t zw_packed = wasm_v16x8_shuffle(z_biased, w_biased, 0, 2, 4, 6, 8, 10, 12, 14);
74     const v128_t xyzw_packed = wasm_v8x16_shuffle(xy_packed, zw_packed, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30);
75 
76     // 4x f32x4.convert_i32x4_s
77     // 4x f32x4.mul
78     // 4x f32x4.max
79     // 4x f32x4.min
80     // 4x f32x4.add
81     // 4x i32x4.sub
82     // 3x v8x16.shuffle
83     // ---------------------
84     // 29 instructions total
85 
86     wasm_v128_store(output, xyzw_packed);
87     output += 16;
88   }
89 }
90