Debian Codegraph @

src/simd/math.rs

raw symbols SCIP
1 /*
2 An almost-literal transliteration of AVX-optimized sin(), cos(), exp() and log()
3 functions by Giovanni Garberoglio, available at http://software-lisc.fbk.eu/avx_mathfun/
4
5 Copyright (C) 2012 Giovanni Garberoglio
6 Interdisciplinary Laboratory for Computational Science (LISC)
7 Fondazione Bruno Kessler and University of Trento
8 via Sommarive, 18
9 I-38123 Trento (Italy)
10
11 which was itself a translation of Simple SSE and SSE2 optimized sin, cos, log,
12 and exp by Julien Pommier, available at http://gruntthepeon.free.fr/ssemath/
13
14 Copyright (C) 2007 Julien Pommier
15
16 Both are provided under the zlib license:
17
18 This software is provided 'as-is', without any express or implied
19 warranty. In no event will the authors be held liable for any damages
20 arising from the use of this software.
21
22 Permission is granted to anyone to use this software for any purpose,
23 including commercial applications, and to alter it and redistribute it
24 freely, subject to the following restrictions:
25
26 1. The origin of this software must not be misrepresented; you must not
27 claim that you wrote the original software. If you use this software
28 in a product, an acknowledgment in the product documentation would be
29 appreciated but is not required.
30 2. Altered source versions must be plainly marked as such, and must not be
31 misrepresented as being the original software.
32 3. This notice may not be removed or altered from any source distribution.
33
34 */
35
36 use std::arch::x86_64::*;
37 use std::mem;
38
39 static X7F: i32 = 0x7f;
40 static SQRTHF: f32 = 0.70710678118654752;
41 static LOG_P0: f32 = 7.0376836292E-2;
42 static LOG_P1: f32 = -1.1514610310E-1;
43 static LOG_P2: f32 = 1.1676998740E-1;
44 static LOG_P3: f32 = -1.2420140846E-1;
45 static LOG_P4: f32 = 1.4249322787E-1;
46 static LOG_P5: f32 = -1.6668057665E-1;
47 static LOG_P6: f32 = 2.0000714765E-1;
48 static LOG_P7: f32 = -2.4999993993E-1;
49 static LOG_P8: f32 = 3.3333331174E-1;
50 static LOG_Q1: f32 = -2.12194440e-4;
51 static LOG_Q2: f32 = 0.693359375;
52
53 pub unsafe fn log256_ps(x: __m256) -> __m256 {
54 let one = _mm256_set1_ps(1.0);
55 let p5 = _mm256_set1_ps(0.5);
56
57 let invalid_mask = _mm256_cmp_ps(x, _mm256_setzero_ps(), _CMP_LE_OS);
58
59 let _ps256_min_norm_pos: __m256 = mem::transmute(_mm256_set1_epi32(0x00800000));
60 let mut x = _mm256_max_ps(x, _ps256_min_norm_pos); /* cut off denormalized stuff */
61
62 let mut imm0 = _mm256_srli_epi32(_mm256_castps_si256(x), 23);
63
64 /* keep only the fractional part */
65 let _ps256_inv_mant_mask: __m256 = mem::transmute(_mm256_set1_epi32(!0x7f800000));
66 x = _mm256_and_ps(x, _ps256_inv_mant_mask);
67 x = _mm256_or_ps(x, p5);
68
69 // this is again another AVX2 instruction
70 imm0 = _mm256_sub_epi32(imm0, _mm256_set1_epi32(X7F));
71 let mut e = _mm256_cvtepi32_ps(imm0);
72
73 e = _mm256_add_ps(e, one);
74
75 let mask = _mm256_cmp_ps(x, _mm256_set1_ps(SQRTHF), _CMP_LT_OS);
76 let mut tmp = _mm256_and_ps(x, mask);
77 x = _mm256_sub_ps(x, one);
78 e = _mm256_sub_ps(e, _mm256_and_ps(one, mask));
79 x = _mm256_add_ps(x, tmp);
80
81 let z = _mm256_mul_ps(x, x);
82
83 let mut y = _mm256_set1_ps(LOG_P0);
84 y = _mm256_mul_ps(y, x);
85 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P1));
86 y = _mm256_mul_ps(y, x);
87 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P2));
88 y = _mm256_mul_ps(y, x);
89 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P3));
90 y = _mm256_mul_ps(y, x);
91 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P4));
92 y = _mm256_mul_ps(y, x);
93 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P5));
94 y = _mm256_mul_ps(y, x);
95 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P6));
96 y = _mm256_mul_ps(y, x);
97 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P7));
98 y = _mm256_mul_ps(y, x);
99 y = _mm256_add_ps(y, _mm256_set1_ps(LOG_P8));
100 y = _mm256_mul_ps(y, x);
101
102 y = _mm256_mul_ps(y, z);
103
104 tmp = _mm256_mul_ps(e, _mm256_set1_ps(LOG_Q1));
105 y = _mm256_add_ps(y, tmp);
106
107 tmp = _mm256_mul_ps(z, p5);
108 y = _mm256_sub_ps(y, tmp);
109
110 tmp = _mm256_mul_ps(e, _mm256_set1_ps(LOG_Q2));
111 x = _mm256_add_ps(x, y);
112 x = _mm256_add_ps(x, tmp);
113 x = _mm256_or_ps(x, invalid_mask); // negative arg will be NAN
114 return x;
115 }
116
117 static EXP_HI: f32 = 88.3762626647949;
118 static EXP_LO: f32 = -88.3762626647949;
119 static LOG2EF: f32 = 1.44269504088896341;
120 static EXP_C1: f32 = 0.693359375;
121 static EXP_C2: f32 = -2.12194440e-4;
122 static EXP_P0: f32 = 1.9875691500E-4;
123 static EXP_P1: f32 = 1.3981999507E-3;
124 static EXP_P2: f32 = 8.3334519073E-3;
125 static EXP_P3: f32 = 4.1665795894E-2;
126 static EXP_P4: f32 = 1.6666665459E-1;
127 static EXP_P5: f32 = 5.0000001201E-1;
128
129 pub unsafe fn exp256_ps(x: __m256) -> __m256 {
130 let one = _mm256_set1_ps(1.0);
131
132 let mut x = _mm256_min_ps(x, _mm256_set1_ps(EXP_HI));
133 x = _mm256_max_ps(x, _mm256_set1_ps(EXP_LO));
134
135 /* express exp(x) as exp(g + n*log(2)) */
136 let mut fx = _mm256_mul_ps(x, _mm256_set1_ps(LOG2EF));
137 fx = _mm256_add_ps(fx, _mm256_set1_ps(0.5));
138
139 /* how to perform a floorf with SSE: just below */
140 //imm0 = _mm256_cvttps_epi32(fx);
141 //tmp = _mm256_cvtepi32_ps(imm0);
142
143 let mut tmp = _mm256_floor_ps(fx);
144
145 /* if greater, substract 1 */
146 //v8sf mask = _mm256_cmpgt_ps(tmp, fx);
147 let mut mask = _mm256_cmp_ps(tmp, fx, _CMP_GT_OS);
148 mask = _mm256_and_ps(mask, one);
149 fx = _mm256_sub_ps(tmp, mask);
150
151 tmp = _mm256_mul_ps(fx, _mm256_set1_ps(EXP_C1));
152 let mut z = _mm256_mul_ps(fx, _mm256_set1_ps(EXP_C2));
153 x = _mm256_sub_ps(x, tmp);
154 x = _mm256_sub_ps(x, z);
155
156 z = _mm256_mul_ps(x, x);
157
158 let mut y = _mm256_set1_ps(EXP_P0);
159 y = _mm256_mul_ps(y, x);
160 y = _mm256_add_ps(y, _mm256_set1_ps(EXP_P1));
161 y = _mm256_mul_ps(y, x);
162 y = _mm256_add_ps(y, _mm256_set1_ps(EXP_P2));
163 y = _mm256_mul_ps(y, x);
164 y = _mm256_add_ps(y, _mm256_set1_ps(EXP_P3));
165 y = _mm256_mul_ps(y, x);
166 y = _mm256_add_ps(y, _mm256_set1_ps(EXP_P4));
167 y = _mm256_mul_ps(y, x);
168 y = _mm256_add_ps(y, _mm256_set1_ps(EXP_P5));
169 y = _mm256_mul_ps(y, z);
170 y = _mm256_add_ps(y, x);
171 y = _mm256_add_ps(y, one);
172
173 /* build 2^n */
174 let mut imm0 = _mm256_cvttps_epi32(fx);
175 // another two AVX2 instructions
176 imm0 = _mm256_add_epi32(imm0, _mm256_set1_epi32(X7F));
177 imm0 = _mm256_slli_epi32(imm0, 23);
178 let pow2n = _mm256_castsi256_ps(imm0);
179 y = _mm256_mul_ps(y, pow2n);
180 return y;
181 }
182
183 pub unsafe fn powf256_ps(x: __m256, y: __m256) -> __m256 {
184 let invalid_mask = _mm256_cmp_ps(x, _mm256_setzero_ps(), _CMP_LE_OS);
185 let result = exp256_ps(_mm256_mul_ps(y, log256_ps(x)));
186 _mm256_or_ps(result, invalid_mask)
187 }
188
189 #[cfg(test)]
190 mod test {
191 use super::{exp256_ps, log256_ps, powf256_ps};
192 use approx::assert_relative_eq;
193 use std::arch::x86_64::*;
194 use std::{f32, mem};
195
196 #[test]
197 fn test_log256_ps() {
198 let scalar_result: Vec<_> = {
199 let vals: [f32; 8] = [0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
200 vals.iter().copied().map(f32::ln).collect()
201 };
202 let avx_result: Vec<_> = unsafe {
203 let vals = _mm256_set_ps(0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);
204 let result = log256_ps(vals);
205 let result: [f32; 8] = mem::transmute(result);
206 result.iter().rev().copied().collect()
207 };
208
209 assert_relative_eq!(scalar_result.as_slice(), avx_result.as_slice())
210 }
211
212 #[test]
213 fn test_negative_log_returns_nan() {
214 let avx_result: Vec<_> = unsafe {
215 let vals = _mm256_set_ps(-0.5, 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0);
216 let result = log256_ps(vals);
217 let result: [f32; 8] = mem::transmute(result);
218 result.iter().rev().copied().collect()
219 };
220 assert!(f32::is_nan(avx_result[0]));
221 assert!(!f32::is_nan(avx_result[1]));
222 assert!(f32::is_nan(avx_result[2]));
223 assert!(!f32::is_nan(avx_result[3]));
224 assert!(f32::is_nan(avx_result[4]));
225 assert!(!f32::is_nan(avx_result[5]));
226 assert!(f32::is_nan(avx_result[6]));
227 assert!(!f32::is_nan(avx_result[7]));
228 }
229
230 #[test]
231 fn test_exp256_ps() {
232 let scalar_result: Vec<_> = {
233 let vals: [f32; 8] = [-1.5, 0.5, 1.0, 2.0, 4.0, 5.0, 6.0, 10.0];
234 vals.iter().copied().map(f32::exp).collect()
235 };
236 let avx_result: Vec<_> = unsafe {
237 let vals = _mm256_set_ps(-1.5, 0.5, 1.0, 2.0, 4.0, 5.0, 6.0, 10.0);
238 let result = exp256_ps(vals);
239 let result: [f32; 8] = mem::transmute(result);
240 result.iter().rev().copied().collect()
241 };
242
243 assert_relative_eq!(scalar_result.as_slice(), avx_result.as_slice())
244 }
245
246 #[test]
247 fn test_powf256_ps() {
248 let exponent = 4.0;
249 let scalar_result: Vec<_> = {
250 let vals: [f32; 8] = [0.25, 0.5, 1.0, 2.0, 4.0, 5.0, 6.0, 10.0];
251 vals.iter().map(|&n| n.powf(exponent)).collect()
252 };
253 let avx_result: Vec<_> = unsafe {
254 let vals = _mm256_set_ps(0.25, 0.5, 1.0, 2.0, 4.0, 5.0, 6.0, 10.0);
255 let three = _mm256_set1_ps(exponent);
256 let result = powf256_ps(vals, three);
257 let result: [f32; 8] = mem::transmute(result);
258 result.iter().rev().copied().collect()
259 };
260
261 assert_relative_eq!(scalar_result.as_slice(), avx_result.as_slice())
262 }
263 }