1 // Copyright 2019 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 <xmmintrin.h>
9
10 #include <xnnpack/rmax.h>
11
12
xnn_f32_rmax_ukernel__sse(size_t n,const float * x,float * y)13 void xnn_f32_rmax_ukernel__sse(
14 size_t n,
15 const float* x,
16 float* y)
17 {
18 assert(n != 0);
19 assert(n % sizeof(float) == 0);
20
21 __m128 vmax0 = _mm_load_ss(x);
22 vmax0 = _mm_shuffle_ps(vmax0, vmax0, _MM_SHUFFLE(0, 0, 0, 0));
23 __m128 vmax1 = vmax0;
24 __m128 vmax2 = vmax0;
25 __m128 vmax3 = vmax0;
26 for (; n >= 64; n -= 64) {
27 const __m128 vx0 = _mm_loadu_ps(x);
28 const __m128 vx1 = _mm_loadu_ps(x + 4);
29 const __m128 vx2 = _mm_loadu_ps(x + 8);
30 const __m128 vx3 = _mm_loadu_ps(x + 12);
31 x += 16;
32
33 vmax0 = _mm_max_ps(vmax0, vx0);
34 vmax1 = _mm_max_ps(vmax1, vx1);
35 vmax2 = _mm_max_ps(vmax2, vx2);
36 vmax3 = _mm_max_ps(vmax3, vx3);
37 }
38 __m128 vmax = _mm_max_ps(_mm_max_ps(vmax0, vmax1), _mm_max_ps(vmax2, vmax3));
39 for (; n >= 16; n -= 16) {
40 const __m128 vx = _mm_loadu_ps(x);
41 vmax = _mm_max_ps(vmax, vx);
42 x += 4;
43 }
44 __m128 vmax_lo = _mm_max_ps(vmax, _mm_movehl_ps(vmax, vmax));
45 vmax_lo = _mm_max_ss(vmax_lo, _mm_shuffle_ps(vmax_lo, vmax_lo, _MM_SHUFFLE(3, 3, 1, 1)));
46 if XNN_UNLIKELY(n != 0) {
47 do {
48 vmax_lo = _mm_max_ss(vmax_lo, _mm_load_ss(x));
49 x += 1;
50 n -= 4;
51 } while (n != 0);
52 }
53 _mm_store_ss(y, vmax_lo);
54 }
55