• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-ibilinear-chw/wasmsimd.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 
12 #include <wasm_simd128.h>
13 
14 #include <xnnpack/ibilinear.h>
15 
16 
xnn_f32_ibilinear_chw_ukernel__wasmsimd_p4(size_t output_pixels,size_t channels,const float ** restrict input,size_t input_offset,const float * restrict weights,float * restrict output,size_t input_increment)17 void xnn_f32_ibilinear_chw_ukernel__wasmsimd_p4(
18     size_t output_pixels,
19     size_t channels,
20     const float**restrict input,
21     size_t input_offset,
22     const float*restrict weights,
23     float*restrict output,
24     size_t input_increment) XNN_DISABLE_TSAN
25 {
26   assert(output_pixels != 0);
27   assert(channels != 0);
28   assert(input_increment % sizeof(float) == 0);
29 
30   do {
31     const float** i = input;
32     const float* w = weights;
33     size_t p = output_pixels;
34 
35     for (; p >= 4; p -= 4) {
36       const float* itl0 = (const float*) ((uintptr_t) i[0] + input_offset);
37       const float* ibl0 = (const float*) ((uintptr_t) i[1] + input_offset);
38       const float* itl1 = (const float*) ((uintptr_t) i[2] + input_offset);
39       const float* ibl1 = (const float*) ((uintptr_t) i[3] + input_offset);
40       const float* itl2 = (const float*) ((uintptr_t) i[4] + input_offset);
41       const float* ibl2 = (const float*) ((uintptr_t) i[5] + input_offset);
42       const float* itl3 = (const float*) ((uintptr_t) i[6] + input_offset);
43       const float* ibl3 = (const float*) ((uintptr_t) i[7] + input_offset);
44       i += 8;
45 
46       const v128_t vw0 = wasm_v128_load(w);
47       const v128_t vw1 = wasm_v128_load(w + 4);
48       w += 8;
49 
50       const v128_t vtltr0 = wasm_v64x2_load_splat(itl0);
51       const v128_t vblbr0 = wasm_v64x2_load_splat(ibl0);
52       const double vtltr1 = *((const double*) itl1);
53       const double vblbr1 = *((const double*) ibl1);
54       const v128_t vtltr2 = wasm_v64x2_load_splat(itl2);
55       const v128_t vblbr2 = wasm_v64x2_load_splat(ibl2);
56       const double vtltr3 = *((const double*) itl3);
57       const double vblbr3 = *((const double*) ibl3);
58 
59       const v128_t valphah = wasm_v32x4_shuffle(vw0, vw1, 0, 2, 4, 6);
60       const v128_t valphav = wasm_v32x4_shuffle(vw0, vw1, 1, 3, 5, 7);
61 
62       const v128_t vtltr01 = wasm_f64x2_replace_lane(vtltr0, 1, vtltr1);
63       const v128_t vblbr01 = wasm_f64x2_replace_lane(vblbr0, 1, vblbr1);
64       const v128_t vtltr23 = wasm_f64x2_replace_lane(vtltr2, 1, vtltr3);
65       const v128_t vblbr23 = wasm_f64x2_replace_lane(vblbr2, 1, vblbr3);
66 
67       const v128_t vldrd01 = wasm_f32x4_sub(vblbr01, vtltr01);
68       const v128_t vldrd23 = wasm_f32x4_sub(vblbr23, vtltr23);
69 
70       const v128_t vld = wasm_v32x4_shuffle(vldrd01, vldrd23, 0, 2, 4, 6);
71       const v128_t vrd = wasm_v32x4_shuffle(vldrd01, vldrd23, 1, 3, 5, 7);
72 
73       const v128_t vtl = wasm_v32x4_shuffle(vtltr01, vtltr23, 0, 2, 4, 6);
74       const v128_t vtr = wasm_v32x4_shuffle(vtltr01, vtltr23, 1, 3, 5, 7);
75 
76       const v128_t vl = wasm_f32x4_add(vtl, wasm_f32x4_mul(vld, valphav));
77       const v128_t vr = wasm_f32x4_add(vtr, wasm_f32x4_mul(vrd, valphav));
78 
79       const v128_t vd = wasm_f32x4_sub(vr, vl);
80       const v128_t vo = wasm_f32x4_add(vl, wasm_f32x4_mul(vd, valphah));
81 
82       wasm_v128_store(output, vo);
83       output += 4;
84     }
85 
86     if XNN_UNLIKELY(p != 0) {
87       if (p & 2) {
88         const v128_t vw = wasm_v128_load(w);
89         w += 4;
90 
91         const v128_t valphah = wasm_v32x4_shuffle(vw, vw, 0, 2, 0, 2);
92         const v128_t valphav = wasm_v32x4_shuffle(vw, vw, 1, 3, 1, 3);
93 
94         const float* itl0 = (const float*) ((uintptr_t) i[0] + input_offset);
95         const float* ibl0 = (const float*) ((uintptr_t) i[1] + input_offset);
96         const float* itl1 = (const float*) ((uintptr_t) i[2] + input_offset);
97         const float* ibl1 = (const float*) ((uintptr_t) i[3] + input_offset);
98         i += 4;
99 
100         const v128_t vtltr = wasm_f64x2_replace_lane(wasm_v64x2_load_splat(itl0), 1, *((const double*) itl1));
101         const v128_t vblbr = wasm_f64x2_replace_lane(wasm_v64x2_load_splat(ibl0), 1, *((const double*) ibl1));
102 
103         const v128_t vldrd = wasm_f32x4_sub(vblbr, vtltr);
104         const v128_t vld = wasm_v32x4_shuffle(vldrd, vldrd, 0, 2, 0, 2);
105         const v128_t vrd = wasm_v32x4_shuffle(vldrd, vldrd, 1, 3, 1, 3);
106 
107         const v128_t vtl = wasm_v32x4_shuffle(vtltr, vtltr, 0, 2, 0, 2);
108         const v128_t vtr = wasm_v32x4_shuffle(vtltr, vtltr, 1, 3, 1, 3);
109 
110         const v128_t vl = wasm_f32x4_add(vtl, wasm_f32x4_mul(vld, valphav));
111         const v128_t vr = wasm_f32x4_add(vtr, wasm_f32x4_mul(vrd, valphav));
112 
113         const v128_t vd = wasm_f32x4_sub(vr, vl);
114         const v128_t vo = wasm_f32x4_add(vl, wasm_f32x4_mul(vd, valphah));
115 
116         *((double*) output) = wasm_f64x2_extract_lane(vo, 0);
117         output += 2;
118       }
119 
120       if (p & 1) {
121         // We are computing the following formula:
122         //   result = (1 - alpha_h) * (1 - alpha_v) * top_left +
123         //                 alpha_h  * (1 - alpha_v) * top_right +
124         //            (1 - alpha_h) *      alpha_v  * bottom_left +
125         //                 alpha_h  *      alpha_v  * bottom_right.
126         //
127         // Rearranging gives
128         //   result =    left + alpha_h * (right        - left),
129         // where
130         //   left =  top_left + alpha_v * (bottom_left  - top_left),
131         //  right = top_right + alpha_v * (bottom_right - top_right).
132 
133         const float alphah = *w;
134         const v128_t valphav = wasm_v32x4_load_splat(w + 1);
135         w += 2;
136 
137         const float* itl = (const float*) ((uintptr_t) i[0] + input_offset);
138         const float* ibl = (const float*) ((uintptr_t) i[1] + input_offset);
139         i += 2;
140 
141         const v128_t vtltr = wasm_v64x2_load_splat(itl);
142         const v128_t vblbr = wasm_v64x2_load_splat(ibl);
143 
144         // Compute at once
145         //    left_diff = bottom_left  - top_left
146         //   right_diff = bottom_right - top_right
147         const v128_t vldrd = wasm_f32x4_sub(vblbr, vtltr);
148         const v128_t vlr = wasm_f32x4_add(vtltr, wasm_f32x4_mul(vldrd, valphav));
149 
150         // Extract them and compute the result.
151         const float l = wasm_f32x4_extract_lane(vlr, 0);
152         const float r = wasm_f32x4_extract_lane(vlr, 1);
153 
154         *output++ = l + alphah * (r - l);
155       }
156     }
157 
158     input_offset += input_increment;
159   } while (--channels != 0);
160 }
161