-
Notifications
You must be signed in to change notification settings - Fork 0
/
avir_float8_avx.h
359 lines (305 loc) · 7.02 KB
/
avir_float8_avx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//$ nobt
//$ nocpp
/**
* @file avir_float8_avx.h
*
* @brief Inclusion file for the "float8" type.
*
* This file includes the "float8" AVX-based type used for SIMD variable
* storage and processing.
*
* AVIR Copyright (c) 2015-2020 Aleksey Vaneev
*/
#ifndef AVIR_FLOAT8_AVX_INCLUDED
#define AVIR_FLOAT8_AVX_INCLUDED
#include <immintrin.h>
#include "avir_dil.h"
namespace avir {
/**
* @brief SIMD packed 8-float type.
*
* This class implements a packed 8-float type that can be used to perform
* parallel computation using SIMD instructions on AVX-enabled processors.
* This class can be used as the "fptype" argument of the avir::fpclass_def
* or avir::fpclass_def_dil class.
*/
class float8
{
public:
float8()
{
}
float8( const float8& s )
: value( s.value )
{
}
float8( const __m256 s )
: value( s )
{
}
float8( const float s )
: value( _mm256_set1_ps( s ))
{
}
float8& operator = ( const float8& s )
{
value = s.value;
return( *this );
}
float8& operator = ( const __m256 s )
{
value = s;
return( *this );
}
float8& operator = ( const float s )
{
value = _mm256_set1_ps( s );
return( *this );
}
operator float () const
{
return( _mm_cvtss_f32( _mm256_extractf128_ps( value, 0 )));
}
/**
* @param p Pointer to memory from where the value should be loaded,
* should be 32-byte aligned.
* @return float8 value loaded from the specified memory location.
*/
static float8 load( const float* const p )
{
return( _mm256_load_ps( p ));
}
/**
* @param p Pointer to memory from where the value should be loaded,
* may have any alignment.
* @return float8 value loaded from the specified memory location.
*/
static float8 loadu( const float* const p )
{
return( _mm256_loadu_ps( p ));
}
/**
* @param p Pointer to memory from where the value should be loaded,
* may have any alignment.
* @param lim The maximum number of elements to load, >0.
* @return float8 value loaded from the specified memory location, with
* elements beyond "lim" set to 0.
*/
static float8 loadu( const float* const p, const int lim )
{
__m128 lo;
__m128 hi;
if( lim > 4 )
{
lo = _mm_loadu_ps( p );
hi = loadu4( p + 4, lim - 4 );
}
else
{
lo = loadu4( p, lim );
hi = _mm_setzero_ps();
}
return( _mm256_insertf128_ps( _mm256_castps128_ps256( lo ), hi, 1 ));
}
/**
* Function stores *this value to the specified memory location.
*
* @param[out] p Output memory location, should be 32-byte aligned.
*/
void store( float* const p ) const
{
_mm256_store_ps( p, value );
}
/**
* Function stores *this value to the specified memory location.
*
* @param[out] p Output memory location, may have any alignment.
*/
void storeu( float* const p ) const
{
_mm256_storeu_ps( p, value );
}
/**
* Function stores "lim" lower elements of *this value to the specified
* memory location.
*
* @param[out] p Output memory location, may have any alignment.
* @param lim The number of lower elements to store, >0.
*/
void storeu( float* p, int lim ) const
{
__m128 v;
if( lim > 4 )
{
_mm_storeu_ps( p, _mm256_extractf128_ps( value, 0 ));
v = _mm256_extractf128_ps( value, 1 );
p += 4;
lim -= 4;
}
else
{
v = _mm256_extractf128_ps( value, 0 );
}
if( lim > 2 )
{
if( lim > 3 )
{
_mm_storeu_ps( p, v );
}
else
{
_mm_storel_pi( (__m64*) p, v );
_mm_store_ss( p + 2, _mm_movehl_ps( v, v ));
}
}
else
{
if( lim == 2 )
{
_mm_storel_pi( (__m64*) p, v );
}
else
{
_mm_store_ss( p, v );
}
}
}
float8& operator += ( const float8& s )
{
value = _mm256_add_ps( value, s.value );
return( *this );
}
float8& operator -= ( const float8& s )
{
value = _mm256_sub_ps( value, s.value );
return( *this );
}
float8& operator *= ( const float8& s )
{
value = _mm256_mul_ps( value, s.value );
return( *this );
}
float8& operator /= ( const float8& s )
{
value = _mm256_div_ps( value, s.value );
return( *this );
}
float8 operator + ( const float8& s ) const
{
return( _mm256_add_ps( value, s.value ));
}
float8 operator - ( const float8& s ) const
{
return( _mm256_sub_ps( value, s.value ));
}
float8 operator * ( const float8& s ) const
{
return( _mm256_mul_ps( value, s.value ));
}
float8 operator / ( const float8& s ) const
{
return( _mm256_div_ps( value, s.value ));
}
/**
* @return Horizontal sum of elements.
*/
float hadd() const
{
__m128 v = _mm_add_ps( _mm256_extractf128_ps( value, 0 ),
_mm256_extractf128_ps( value, 1 ));
v = _mm_hadd_ps( v, v );
v = _mm_hadd_ps( v, v );
return( _mm_cvtss_f32( v ));
}
/**
* Function performs in-place addition of a value located in memory and
* the specified value.
*
* @param p Pointer to value where addition happens. May be unaligned.
* @param v Value to add.
*/
static void addu( float* const p, const float8& v )
{
( loadu( p ) + v ).storeu( p );
}
/**
* Function performs in-place addition of a value located in memory and
* the specified value. Limited to the specfied number of elements.
*
* @param p Pointer to value where addition happens. May be unaligned.
* @param v Value to add.
* @param lim The element number limit, >0.
*/
static void addu( float* const p, const float8& v, const int lim )
{
( loadu( p, lim ) + v ).storeu( p, lim );
}
__m256 value; ///< Packed value of 8 floats.
///<
private:
/**
* @param p Pointer to memory from where the value should be loaded,
* may have any alignment.
* @param lim The maximum number of elements to load, >0.
* @return __m128 value loaded from the specified memory location, with
* elements beyond "lim" set to 0.
*/
static __m128 loadu4( const float* const p, const int lim )
{
if( lim > 2 )
{
if( lim > 3 )
{
return( _mm_loadu_ps( p ));
}
else
{
return( _mm_set_ps( 0.0f, p[ 2 ], p[ 1 ], p[ 0 ]));
}
}
else
{
if( lim == 2 )
{
return( _mm_set_ps( 0.0f, 0.0f, p[ 1 ], p[ 0 ]));
}
else
{
return( _mm_load_ss( p ));
}
}
}
};
/**
* SIMD rounding function, exact result.
*
* @param v Value to round.
* @return Rounded SIMD value.
*/
inline float8 round( const float8& v )
{
return( _mm256_round_ps( v.value,
( _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC )));
}
/**
* SIMD function "clamps" (clips) the specified packed values so that they are
* not lesser than "minv", and not greater than "maxv".
*
* @param Value Value to clamp.
* @param minv Minimal allowed value.
* @param maxv Maximal allowed value.
* @return The clamped value.
*/
inline float8 clamp( const float8& Value, const float8& minv,
const float8& maxv )
{
return( _mm256_min_ps( _mm256_max_ps( Value.value, minv.value ),
maxv.value ));
}
typedef fpclass_def_dil< float, avir :: float8 > fpclass_float8_dil; ///<
///< Class that can be used as the "fpclass" template parameter of the
///< avir::CImageResizer class to perform calculation using
///< de-interleaved SIMD algorithm, using SIMD float8 type.
///<
} // namespace avir
#endif // AVIR_FLOAT8_AVX_INCLUDED