xref: /aosp_15_r20/external/XNNPACK/src/xx-fill/wasmsimd-x64.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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 
8 #include <wasm_simd128.h>
9 
10 #include <xnnpack/fill.h>
11 
12 
xnn_xx_fill_ukernel__wasmsimd_x64(size_t rows,size_t channels,void * output,size_t output_stride,const uint32_t fill_pattern)13 void xnn_xx_fill_ukernel__wasmsimd_x64(
14     size_t rows,
15     size_t channels,
16     void* output,
17     size_t output_stride,
18     const uint32_t fill_pattern)
19 {
20   assert(rows != 0);
21   assert(channels != 0);
22 
23   const size_t output_increment = output_stride - channels;
24 
25   const v128_t vfill_pattern = wasm_i32x4_splat(fill_pattern);
26   do {
27     size_t c = channels;
28     for (; c >= 64 * sizeof(uint8_t); c -= 64 * sizeof(uint8_t)) {
29       wasm_v128_store(output, vfill_pattern);
30       wasm_v128_store((uint8_t*) output + 16, vfill_pattern);
31       wasm_v128_store((uint8_t*) output + 32, vfill_pattern);
32       wasm_v128_store((uint8_t*) output + 48, vfill_pattern);
33       output = ((uint8_t*) output + 64);
34     }
35     for (; c >= 16 * sizeof(uint8_t); c -= 16 * sizeof(uint8_t)) {
36       wasm_v128_store(output, vfill_pattern);
37       output = ((uint8_t*) output + 16);
38     }
39     if XNN_UNLIKELY(c != 0) {
40       if XNN_LIKELY(c & (8 * sizeof(uint8_t))) {
41         *((double*) output) = wasm_f64x2_extract_lane(vfill_pattern, 0);
42         output = ((uint8_t*) output + 8);
43       }
44       uint32_t vfill_subpattern = fill_pattern;
45       if XNN_LIKELY(c & (4 * sizeof(uint8_t))) {
46         *((uint32_t*) output) = vfill_subpattern;
47         output = ((uint8_t*) output + 4);
48       }
49       if XNN_LIKELY(c & (2 * sizeof(uint8_t))) {
50         *((uint16_t*) output) = (uint16_t) vfill_subpattern;
51         vfill_subpattern >>= 16;
52         output = ((uint8_t*) output + 2);
53       }
54       if XNN_LIKELY(c & (1 * sizeof(uint8_t))) {
55         *((uint8_t*) output) = (uint8_t) vfill_subpattern;
56         output = ((uint8_t*) output + 1);
57       }
58     }
59     output = (void*) ((uintptr_t) output + output_increment);
60   } while (--rows != 0);
61 }
62