• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-sigmoid/sse-p5-div.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 <emmintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16 
17 
xnn_f32_sigmoid_ukernel__sse2_p5_div_x8(size_t n,const float * x,float * y,const void * params)18 void xnn_f32_sigmoid_ukernel__sse2_p5_div_x8(
19     size_t n,
20     const float* x,
21     float* y,
22     const void* params) XNN_DISABLE_TSAN
23 {
24   assert(n % sizeof(float) == 0);
25 
26   const __m128 vsign_mask = _mm_set1_ps(-0.0f);
27   const __m128 vmagic_bias = _mm_set1_ps(0x1.8000FEp23f);
28   const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f);
29   const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.62E400p-1f);
30   const __m128 vminus_ln2_lo = _mm_set1_ps(-0x1.7F7D1Cp-20f);
31   const __m128 vc5 = _mm_set1_ps(0x1.0F9F9Cp-7f);
32   const __m128 vc4 = _mm_set1_ps(0x1.573A1Ap-5f);
33   const __m128 vc3 = _mm_set1_ps(0x1.555A80p-3f);
34   const __m128 vc2 = _mm_set1_ps(0x1.FFFDC6p-2f);
35   const __m128 vc1 = _mm_set1_ps(0x1.FFFFF6p-1f);
36   const __m128 vone = _mm_set1_ps(1.0f);
37   const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f);
38 
39   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
40     const __m128 vx0123 = _mm_loadu_ps(x);
41     const __m128 vx4567 = _mm_loadu_ps(x + 4);
42 
43     const __m128 vz0123 = _mm_or_ps(vx0123, vsign_mask);
44     const __m128 vz4567 = _mm_or_ps(vx4567, vsign_mask);
45 
46     __m128 vn0123 = _mm_add_ps(_mm_mul_ps(vz0123, vlog2e), vmagic_bias);
47     __m128 vn4567 = _mm_add_ps(_mm_mul_ps(vz4567, vlog2e), vmagic_bias);
48 
49     const __m128 vs0123 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn0123), 23));
50     const __m128 vs4567 = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn4567), 23));
51 
52     vn0123 = _mm_sub_ps(vn0123, vmagic_bias);
53     vn4567 = _mm_sub_ps(vn4567, vmagic_bias);
54 
55     __m128 vt0123 = _mm_add_ps(_mm_mul_ps(vn0123, vminus_ln2_hi), vz0123);
56     __m128 vt4567 = _mm_add_ps(_mm_mul_ps(vn4567, vminus_ln2_hi), vz4567);
57 
58     vt0123 = _mm_add_ps(_mm_mul_ps(vn0123, vminus_ln2_lo), vt0123);
59     vt4567 = _mm_add_ps(_mm_mul_ps(vn4567, vminus_ln2_lo), vt4567);
60 
61     __m128 vp0123 = _mm_add_ps(_mm_mul_ps(vc5, vt0123), vc4);
62     __m128 vp4567 = _mm_add_ps(_mm_mul_ps(vc5, vt4567), vc4);
63 
64     vp0123 = _mm_add_ps(_mm_mul_ps(vp0123, vt0123), vc3);
65     vp4567 = _mm_add_ps(_mm_mul_ps(vp4567, vt4567), vc3);
66 
67     vp0123 = _mm_add_ps(_mm_mul_ps(vp0123, vt0123), vc2);
68     vp4567 = _mm_add_ps(_mm_mul_ps(vp4567, vt4567), vc2);
69 
70     vp0123 = _mm_add_ps(_mm_mul_ps(vp0123, vt0123), vc1);
71     vp4567 = _mm_add_ps(_mm_mul_ps(vp4567, vt4567), vc1);
72 
73     vt0123 = _mm_mul_ps(vt0123, vs0123);
74     vt4567 = _mm_mul_ps(vt4567, vs4567);
75 
76     __m128 ve0123 = _mm_add_ps(_mm_mul_ps(vt0123, vp0123), vs0123);
77     __m128 ve4567 = _mm_add_ps(_mm_mul_ps(vt4567, vp4567), vs4567);
78 
79     __m128 vd0123 = _mm_add_ps(ve0123, vone);
80     __m128 vd4567 = _mm_add_ps(ve4567, vone);
81 
82     __m128 vf0123 = _mm_div_ps(ve0123, vd0123);
83     __m128 vf4567 = _mm_div_ps(ve4567, vd4567);
84 
85     vf0123 = _mm_andnot_ps(_mm_cmplt_ps(vz0123, vdenorm_cutoff), vf0123);
86     vf4567 = _mm_andnot_ps(_mm_cmplt_ps(vz4567, vdenorm_cutoff), vf4567);
87 
88     const __m128 vm0123 = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx0123)));
89     const __m128 vm4567 = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx4567)));
90 
91     vf0123 = _mm_or_ps(_mm_and_ps(vf0123, vm0123), _mm_andnot_ps(vm0123, _mm_sub_ps(vone, vf0123)));
92     vf4567 = _mm_or_ps(_mm_and_ps(vf4567, vm4567), _mm_andnot_ps(vm4567, _mm_sub_ps(vone, vf4567)));
93 
94     _mm_storeu_ps(y, vf0123);
95     _mm_storeu_ps(y + 4, vf4567);
96 
97     x += 8;
98     y += 8;
99   }
100   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
101     const __m128 vx = _mm_loadu_ps(x);
102 
103     const __m128 vz = _mm_or_ps(vx, vsign_mask);
104 
105     __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
106     const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23));
107     vn = _mm_sub_ps(vn, vmagic_bias);
108 
109     __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz);
110     vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt);
111 
112     __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4);
113     vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3);
114     vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2);
115     vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1);
116 
117     vt = _mm_mul_ps(vt, vs);
118     __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs);
119 
120     __m128 vd = _mm_add_ps(ve, vone);
121     __m128 vf = _mm_div_ps(ve, vd);
122 
123     vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
124     const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx)));
125     vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf)));
126 
127     _mm_storeu_ps(y, vf);
128 
129     x += 4;
130     y += 4;
131   }
132   if XNN_UNLIKELY(n != 0) {
133     const __m128 vx = _mm_loadu_ps(x);
134 
135     const __m128 vz = _mm_or_ps(vx, vsign_mask);
136 
137     __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
138     const __m128 vs = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(vn), 23));
139     vn = _mm_sub_ps(vn, vmagic_bias);
140 
141     __m128 vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_hi), vz);
142     vt = _mm_add_ps(_mm_mul_ps(vn, vminus_ln2_lo), vt);
143 
144     __m128 vp = _mm_add_ps(_mm_mul_ps(vc5, vt), vc4);
145     vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc3);
146     vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc2);
147     vp = _mm_add_ps(_mm_mul_ps(vp, vt), vc1);
148 
149     vt = _mm_mul_ps(vt, vs);
150     __m128 ve = _mm_add_ps(_mm_mul_ps(vt, vp), vs);
151 
152     __m128 vd = _mm_add_ps(ve, vone);
153     __m128 vf = _mm_div_ps(ve, vd);
154 
155     vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
156     const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx)));
157     vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf)));
158 
159     if (n & (2 * sizeof(float))) {
160       _mm_storel_pi((__m64*) y, vf);
161       vf = _mm_movehl_ps(vf, vf);
162       y += 2;
163     }
164     if (n & (1 * sizeof(float))) {
165       _mm_store_ss(y, vf);
166     }
167   }
168 }
169