• 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 
8 #include <wasm_simd128.h>
9 
10 #include <xnnpack/zip.h>
11 
12 
xnn_x32_zip_x4_ukernel__wasmsimd(size_t n,const uint32_t * input,uint32_t * output)13 void xnn_x32_zip_x4_ukernel__wasmsimd(
14     size_t n,
15     const uint32_t* input,
16     uint32_t* output)
17 {
18   assert(n != 0);
19   assert(n % sizeof(uint32_t) == 0);
20 
21   const float* x = (const float*) input;
22   const float* y = (const float*) ((uintptr_t) x + n);
23   const float* z = (const float*) ((uintptr_t) y + n);
24   const float* w = (const float*) ((uintptr_t) z + n);
25   float* o = (float*) output;
26 
27   while (n >= 4 * sizeof(uint32_t)) {
28     const v128_t vx = wasm_v128_load(x);
29     x += 4;
30     const v128_t vy = wasm_v128_load(y);
31     y += 4;
32     const v128_t vz = wasm_v128_load(z);
33     z += 4;
34     const v128_t vw = wasm_v128_load(w);
35     w += 4;
36 
37     const v128_t vxy_lo = wasm_v32x4_shuffle(vx, vy, 0, 4, 1, 5);
38     const v128_t vxy_hi = wasm_v32x4_shuffle(vx, vy, 2, 6, 3, 7);
39     const v128_t vzw_lo = wasm_v32x4_shuffle(vz, vw, 0, 4, 1, 5);
40     const v128_t vzw_hi = wasm_v32x4_shuffle(vz, vw, 2, 6, 3, 7);
41 
42     const v128_t vxyzw0 = wasm_v32x4_shuffle(vxy_lo, vzw_lo, 0, 1, 4, 5);
43     const v128_t vxyzw1 = wasm_v32x4_shuffle(vxy_lo, vzw_lo, 2, 3, 6, 7);
44     const v128_t vxyzw2 = wasm_v32x4_shuffle(vxy_hi, vzw_hi, 0, 1, 4, 5);
45     const v128_t vxyzw3 = wasm_v32x4_shuffle(vxy_hi, vzw_hi, 2, 3, 6, 7);
46 
47     wasm_v128_store(o, vxyzw0);
48     wasm_v128_store(o + 4, vxyzw1);
49     wasm_v128_store(o + 8, vxyzw2);
50     wasm_v128_store(o + 12, vxyzw3);
51     o += 16;
52     n -= 4 * sizeof(uint32_t);
53   }
54   if XNN_UNLIKELY(n != 0) {
55     if (n & (2 * sizeof(uint32_t))) {
56       const double vx = *((const double*) x);
57       x += 2;
58       const double vy = *((const double*) y);
59       y += 2;
60       const double vz = *((const double*) z);
61       z += 2;
62       const double vw = *((const double*) w);
63       w += 2;
64 
65       const v128_t vxy = wasm_f64x2_make(vx, vy);
66       const v128_t vzw = wasm_f64x2_make(vz, vw);
67 
68       const v128_t vxyzw_lo = wasm_v32x4_shuffle(vxy, vzw, 0, 2, 4, 6);
69       const v128_t vxyzw_hi = wasm_v32x4_shuffle(vxy, vzw, 1, 3, 5, 7);
70 
71       wasm_v128_store(o, vxyzw_lo);
72       wasm_v128_store(o + 4, vxyzw_hi);
73       o += 8;
74     }
75     if (n & (1 * sizeof(uint32_t))) {
76       const float vx = *x;
77       const float vy = *y;
78       const float vz = *z;
79       const float vw = *w;
80       o[0] = vx;
81       o[1] = vy;
82       o[2] = vz;
83       o[3] = vw;
84     }
85   }
86 }
87