• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-sigmoid/avx512f-p5-scalef.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 <immintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/vunary.h>
17 
18 
xnn_f32_sigmoid_ukernel__avx512f_rr1_p5_scalef_div_x48(size_t n,const float * x,float * y,const void * params)19 void xnn_f32_sigmoid_ukernel__avx512f_rr1_p5_scalef_div_x48(
20     size_t n,
21     const float* x,
22     float* y,
23     const void* params)
24 {
25   assert(n % sizeof(float) == 0);
26 
27   const __m512i vsign_mask = _mm512_set1_epi32(0x80000000);
28   const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
29   const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f);
30   const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
31   const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
32   const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
33   const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
34   const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
35   const __m512 vone = _mm512_set1_ps(1.0f);
36 
37   for (; n >= 48 * sizeof(float); n -= 48 * sizeof(float)) {
38     const __m512 vx0 = _mm512_loadu_ps(x);
39     const __m512 vx1 = _mm512_loadu_ps(x + 16);
40     const __m512 vx2 = _mm512_loadu_ps(x + 32);
41     x += 48;
42 
43     const __m512 vz0 = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx0), vsign_mask));
44     const __m512 vz1 = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx1), vsign_mask));
45     const __m512 vz2 = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx2), vsign_mask));
46 
47     __m512 vn0 = _mm512_mul_ps(vz0, vlog2e);
48     __m512 vn1 = _mm512_mul_ps(vz1, vlog2e);
49     __m512 vn2 = _mm512_mul_ps(vz2, vlog2e);
50 
51     vn0 = _mm512_roundscale_ps(vn0, 0);
52     vn1 = _mm512_roundscale_ps(vn1, 0);
53     vn2 = _mm512_roundscale_ps(vn2, 0);
54 
55     __m512 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2, vz0);
56     __m512 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2, vz1);
57     __m512 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2, vz2);
58 
59     __m512 vp0 = _mm512_fmadd_ps(vc5, vt0, vc4);
60     __m512 vp1 = _mm512_fmadd_ps(vc5, vt1, vc4);
61     __m512 vp2 = _mm512_fmadd_ps(vc5, vt2, vc4);
62 
63     vp0 = _mm512_fmadd_ps(vp0, vt0, vc3);
64     vp1 = _mm512_fmadd_ps(vp1, vt1, vc3);
65     vp2 = _mm512_fmadd_ps(vp2, vt2, vc3);
66 
67     vp0 = _mm512_fmadd_ps(vp0, vt0, vc2);
68     vp1 = _mm512_fmadd_ps(vp1, vt1, vc2);
69     vp2 = _mm512_fmadd_ps(vp2, vt2, vc2);
70 
71     vp0 = _mm512_fmadd_ps(vp0, vt0, vc1);
72     vp1 = _mm512_fmadd_ps(vp1, vt1, vc1);
73     vp2 = _mm512_fmadd_ps(vp2, vt2, vc1);
74 
75     vp0 = _mm512_fmadd_ps(vp0, vt0, vone);
76     vp1 = _mm512_fmadd_ps(vp1, vt1, vone);
77     vp2 = _mm512_fmadd_ps(vp2, vt2, vone);
78 
79     const __m512 ve0 = _mm512_scalef_ps(vp0, vn0);
80     const __m512 ve1 = _mm512_scalef_ps(vp1, vn1);
81     const __m512 ve2 = _mm512_scalef_ps(vp2, vn2);
82 
83     const __m512 vd0 = _mm512_add_ps(ve0, vone);
84     const __m512 vd1 = _mm512_add_ps(ve1, vone);
85     const __m512 vd2 = _mm512_add_ps(ve2, vone);
86 
87     __m512 vf0 = _mm512_div_ps(ve0, vd0);
88     __m512 vf1 = _mm512_div_ps(ve1, vd1);
89     __m512 vf2 = _mm512_div_ps(ve2, vd2);
90 
91     vf0 = _mm512_mask_sub_ps(vf0, _mm512_testn_epi32_mask(_mm512_castps_si512(vx0), vsign_mask), vone, vf0);
92     vf1 = _mm512_mask_sub_ps(vf1, _mm512_testn_epi32_mask(_mm512_castps_si512(vx1), vsign_mask), vone, vf1);
93     vf2 = _mm512_mask_sub_ps(vf2, _mm512_testn_epi32_mask(_mm512_castps_si512(vx2), vsign_mask), vone, vf2);
94 
95     _mm512_storeu_ps(y, vf0);
96     _mm512_storeu_ps(y + 16, vf1);
97     _mm512_storeu_ps(y + 32, vf2);
98     y += 48;
99   }
100   for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
101     const __m512 vx = _mm512_loadu_ps(x);
102     x += 16;
103 
104     const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
105 
106     const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0);
107 
108     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
109 
110     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
111     vp = _mm512_fmadd_ps(vp, vt, vc3);
112     vp = _mm512_fmadd_ps(vp, vt, vc2);
113     vp = _mm512_fmadd_ps(vp, vt, vc1);
114     vp = _mm512_fmadd_ps(vp, vt, vone);
115 
116     const __m512 ve = _mm512_scalef_ps(vp, vn);
117     const __m512 vd = _mm512_add_ps(ve, vone);
118 
119     __m512 vf = _mm512_div_ps(ve, vd);
120 
121     vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
122 
123     _mm512_storeu_ps(y, vf);
124     y += 16;
125   }
126   if XNN_UNLIKELY(n != 0) {
127     assert(n >= 1 * sizeof(float));
128     assert(n <= 15 * sizeof(float));
129 
130     // Prepare mask for valid 32-bit elements (depends on n).
131     n >>= 2 /* log2(sizeof(float)) */;
132     const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
133 
134     const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
135     const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
136 
137     const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0);
138 
139     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
140 
141     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
142     vp = _mm512_fmadd_ps(vp, vt, vc3);
143     vp = _mm512_fmadd_ps(vp, vt, vc2);
144     vp = _mm512_fmadd_ps(vp, vt, vc1);
145     vp = _mm512_fmadd_ps(vp, vt, vone);
146 
147     const __m512 ve = _mm512_scalef_ps(vp, vn);
148     const __m512 vd = _mm512_add_ps(ve, vone);
149 
150     __m512 vf = _mm512_div_ps(ve, vd);
151 
152     vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
153 
154     _mm512_mask_storeu_ps(y, vmask, vf);
155   }
156 }
157