• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vbinary/vopc-avx.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2019 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 <immintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/vbinary.h>
16 
17 
18 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
19 
xnn_f32_vmaxc_ukernel__avx_x8(size_t n,const float * a,const float * b,float * y,const union xnn_f32_output_params params[restrict static1])20 void xnn_f32_vmaxc_ukernel__avx_x8(
21     size_t n,
22     const float* a,
23     const float* b,
24     float* y,
25     const union xnn_f32_output_params params[restrict static 1])
26 {
27   assert(n != 0);
28   assert(n % sizeof(float) == 0);
29 
30   const __m256 vy_min = _mm256_broadcast_ps((const __m128*) params->sse.min);
31   const __m256 vy_max = _mm256_broadcast_ps((const __m128*) params->sse.max);
32 
33   const __m256 vb = _mm256_broadcast_ss(b);
34   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
35     const __m256 va01234567 = _mm256_loadu_ps(a);
36     a += 8;
37 
38     __m256 vy01234567 = _mm256_max_ps(va01234567, vb);
39 
40     vy01234567 = _mm256_max_ps(vy01234567, vy_min);
41 
42     vy01234567 = _mm256_min_ps(vy01234567, vy_max);
43 
44     _mm256_storeu_ps(y, vy01234567);
45     y += 8;
46   }
47   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
48     const __m256 va = _mm256_loadu_ps(a);
49     a += 8;
50 
51     __m256 vy = _mm256_max_ps(va, vb);
52     vy = _mm256_max_ps(vy, vy_min);
53     vy = _mm256_min_ps(vy, vy_max);
54     _mm256_storeu_ps(y, vy);
55     y += 8;
56   }
57   if XNN_UNLIKELY(n != 0) {
58     assert(n >= 1 * sizeof(float));
59     assert(n <= 7 * sizeof(float));
60     __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
61 
62     const __m256 va = _mm256_maskload_ps(a, vmask);
63 
64     __m256 vy = _mm256_max_ps(va, vb);
65     vy = _mm256_max_ps(vy, vy_min);
66     vy = _mm256_min_ps(vy, vy_max);
67 
68     // _mm256_maskstore_ps(y, vmask, vy) could be used here, but triggers msan failures (probably an msan bug).
69     __m128 vy_lo = _mm256_castps256_ps128(vy);
70     if (n & (4 * sizeof(float))) {
71       _mm_storeu_ps(y, vy_lo);
72       vy_lo = _mm256_extractf128_ps(vy, 1);
73       y += 4;
74     }
75     if (n & (2 * sizeof(float))) {
76       _mm_storel_pi((__m64*) y, vy_lo);
77       vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
78       y += 2;
79     }
80     if (n & (1 * sizeof(float))) {
81       _mm_store_ss(y, vy_lo);
82     }
83   }
84 }
85