[go: up one dir, main page]

Menu

[r490]: / trunk / src / fptype.h  Maximize  Restore  History

Download this file

398 lines (333 with data), 12.3 kB

  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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************
* This file is part of mdcore.
* Coypright (c) 2010 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
/* Global defines. */
#ifndef FPTYPE_DEFINED
#ifdef FPTYPE_DOUBLE
/** The default basic type is double. */
typedef double FPTYPE;
#define FPTYPE_EPSILON DBL_EPSILON
#ifndef FPTYPE_DOUBLE
#define FPTYPE_DOUBLE
#endif
#define FPTYPE_ONE 1.0
#define FPTYPE_TWO 2.0
#define FPTYPE_HALF 0.5
#define FPTYPE_ZERO 0.0
#define FPTYPE_SQRT sqrt
#define FPTYPE_FMAX fmax
#define FPTYPE_FMIN fmin
#define FPTYPE_FABS fabs
#define FPTYPE_LOG log
#define FPTYPE_COPYSIGN copysign
#else
/** The basic type is set to float. */
typedef float FPTYPE;
#define FPTYPE_EPSILON FLT_EPSILON
#define FPTYPE_ONE 1.0f
#define FPTYPE_ZERO 0.0f
#define FPTYPE_TWO 2.0f
#define FPTYPE_HALF 0.5f
#define FPTYPE_SQRT sqrtf
#define FPTYPE_FMAX fmaxf
#define FPTYPE_FMIN fminf
#define FPTYPE_FABS fabsf
#define FPTYPE_LOG logf
#define FPTYPE_COPYSIGN copysignf
#ifndef FPTYPE_SINGLE
#define FPTYPE_SINGLE
#endif
#endif
#define FPTYPE_DEFINED
#endif
/* Get the inlining right. */
#ifndef INLINE
# if __GNUC__ && !__GNUC_STDC_INLINE__
# define INLINE extern inline
# else
# define INLINE inline
# endif
#endif
/* Define some macros for single/double precision vector operations. */
#if defined(FPTYPE_SINGLE)
#if defined(__AVX__)
#define VEC_SINGLE
#define VEC_SIZE 8
#define VEC_ALIGN 32
#define VECTORIZE
#define VEC_TYPE __m256
#define VEC_LOAD(a) _mm256_load_ps(a)
#define VEC_SET1(a) _mm256_set1_ps(a)
#define VEC_SET(a,b,c,d,e,f,g,h) _mm256_set_ps(h,g,f,e,d,c,b,a)
#elif ( defined(__SSE__) || defined(__ALTIVEC__) )
#define VEC_SINGLE
#define VEC_SIZE 4
#define VEC_ALIGN 16
#define VEC_TYPE __m128
#define VEC_LOAD(a) _mm_load_ps(a)
#define VEC_SET1(a) _mm_set1_ps(a)
#define VEC_SET(a,b,c,d) _mm_set_ps(d,c,b,a)
#define VECTORIZE
#endif
#else
#if defined(__AVX__)
#define VEC_DOUBLE
#define VEC_SIZE 4
#define VEC_ALIGN 32
#define VEC_TYPE __m256d
#define VEC_LOAD(a) _mm256_load_pd(a)
#define VEC_SET1(a) _mm256_set1_pd(a)
#define VEC_SET(a,b,c,d) _mm256_set_pd(d,c,b,a)
#define VECTORIZE
#elif defined(__SSE2__)
#define VEC_DOUBLE
#define VEC_SIZE 4
#define VEC_ALIGN 16
#define VEC_TYPE __m128d[2]
#define VEC_LOAD(a) _mm_load_pd(a)
#define VEC_SET1(a) _mm_set1_pd(a)
#define VEC_SET(a,b) _mm_set_pd(b,a)
#define VECTORIZE
#endif
#endif
/* Get headers for intrinsic functions. */
#include <immintrin.h>
/** Macro to easily define vector types. */
#define vector(elcount, type) __attribute__((vector_size((elcount)*sizeof(type)))) type
/* Some extra functions function for Alti-Vec instruction set. */
#ifdef __ALTIVEC__
#include <altivec.h>
__attribute__ ((always_inline)) INLINE vector float vec_sqrt( vector float a ) {
vector float z = ( vector float ){ 0.0f };
vector float estimate = vec_rsqrte( a );
vector float estimateSquared = vec_madd( estimate, estimate, z );
vector float halfEstimate = vec_madd( estimate, (vector float){0.5}, z );
return vec_madd( a, vec_madd( vec_nmsub( a, estimateSquared, (vector float){1.0} ), halfEstimate, estimate ), z);
}
/* inline static vector float vec_load4 ( float a , float b , float c , float d ) {
return vec_mergeh( vec_mergeh( vec_promote(a,0) , vec_promote(c,0) ) , vec_mergeh( vec_promote(b,0) , vec_promote(d,0) ) );
} */
#define vec_load4(a,b,c,d) vec_mergeh( vec_mergeh( vec_promote((a),0) , vec_promote((c),0) ) , vec_mergeh( vec_promote((b),0) , vec_promote((d),0) ) )
#define vec_mul(a,b) vec_madd((a),(b),(vector float){0.0f})
#endif
/**
* @brief Inlined function to compute the distance^2 between two vectors.
*
* @param x1 The first vector.
* @param x2 The second vector.
* @param dx An array in which @c x1 - @c x2 will be stored.
*
* @return The Euclidian distance squared between @c x1 and @c x2.
*
* Depending on the processor features, this function will use
* SSE registers and horizontal adds.
*/
__attribute__ ((always_inline)) INLINE FPTYPE fptype_r2 ( FPTYPE *x1 , FPTYPE *x2 , FPTYPE *dx ) {
#if defined(VECTORIZE) && defined(FPTYPE_SINGLE) && defined(__SSE4_1__)
union {
vector(4,float) v;
float f[4];
} a, b, c, d;
/* Load x1 and x2 into a and b. */
a.v = _mm_load_ps( x1 );
b.v = _mm_load_ps( x2 );
/* Compute the difference and store in dx. */
c.v = a.v - b.v;
_mm_store_ps( dx , c.v );
/* Use the built-in dot-product instruction. */
d.v = _mm_dp_ps( c.v , c.v , 0x71 );
/* Return the sum of squares. */
return d.f[0];
#elif defined(VECTORIZE) && defined(FPTYPE_SINGLE) && defined(__SSE3__)
union {
vector(4,float) v;
float f[4];
} a, b, c, d;
/* Load x1 and x2 into a and b. */
a.v = _mm_load_ps( x1 );
b.v = _mm_load_ps( x2 );
/* Compute the difference and store in dx. */
c.v = a.v - b.v;
_mm_store_ps( dx , c.v );
/* Square the entries (use a different register so that c can be stored). */
d.v = c.v * c.v;
/* Add horizontally twice to get the sum of the four entries
in the lowest float. */
d.v = _mm_hadd_ps( d.v , d.v );
d.v = _mm_hadd_ps( d.v , d.v );
/* Return the sum of squares. */
return d.f[0];
#elif defined(VECTORIZE) && defined(FPTYPE_DOUBLE) && defined(__AVX__)
union {
__m256d v;
double f[4];
} a, b, c, d;
/* Load x1 and x2 into a and b. */
a.v = _mm256_load_pd( x1 );
b.v = _mm256_load_pd( x2 );
/* Compute the difference and store in dx. */
c.v = a.v - b.v;
_mm256_store_pd( dx , c.v );
/* Square the entries (use a different register so that c can be stored). */
d.v = c.v * c.v;
/* Add horizontally twice to get the sum of the four entries
in the lowest double. */
d.v = _mm256_hadd_pd( d.v , d.v );
/* Return the sum of squares. */
return d.f[0] + d.f[2];
#elif defined(VECTORIZE) && defined(FPTYPE_DOUBLE) && defined(__SSE4_1__)
union {
vector(2,double) v;
double f[2];
} a1, a2, b1, b2, c1, c2, d1;
/* Load x1 and x2 into a and b. */
a1.v = _mm_load_pd( x1 );
b1.v = _mm_load_pd( x2 );
a2.v = _mm_load_pd( &x1[2] );
b2.v = _mm_load_pd( &x2[2] );
/* Compute the difference and store in dx. */
c1.v = a1.v - b1.v;
c2.v = a2.v - b2.v;
_mm_store_pd( dx , c1.v );
_mm_store_pd( &dx[2] , c2.v );
/* Use the built-in dot-product instruction. */
d1.v = _mm_dp_pd( c1.v , c1.v , 0x31 ) + c2.v * c2.v;
/* Return the sum of squares. */
return d1.f[0];
#elif defined(VECTORIZE) && defined(FPTYPE_DOUBLE) && defined(__SSE3__)
union {
vector(2,double) v;
double f[2];
} a1, a2, b1, b2, c1, c2, d1, d2;
/* Load x1 and x2 into a and b. */
a1.v = _mm_load_pd( x1 );
b1.v = _mm_load_pd( x2 );
a2.v = _mm_load_pd( &x1[2] );
b2.v = _mm_load_pd( &x2[2] );
/* Compute the difference and store in dx. */
c1.v = a1.v - b1.v;
c2.v = a2.v - b2.v;
_mm_store_pd( dx , c1.v );
_mm_store_pd( &dx[2] , c2.v );
/* Square the entries (use a different register so that c can be stored). */
d1.v = c1.v * c1.v;
d2.v = c2.v * c2.v;
/* Add horizontally twice to get the sum of the four entries
in the lowest double. */
d1.v = _mm_hadd_pd( d1.v , d2.v );
d1.v = _mm_hadd_pd( d1.v , d1.v );
/* Return the sum of squares. */
return d1.f[0];
#else
dx[0] = x1[0] - x2[0];
dx[1] = x1[1] - x2[1];
dx[2] = x1[2] - x2[2];
return dx[0]*dx[0] + dx[1]*dx[1] + dx[2]*dx[2];
#endif
}
/**
* @brief Inlined function to compute the dot product of two vectors.
*
* @param x The firstvector.
* @param x2 The second vector.
*
* @return The dot product of @c x1 and @c x2.
*
* Depending on the processor features, this function will use
* SSE registers and horizontal adds.
*/
__attribute__ ((always_inline)) INLINE FPTYPE fptype_dprod ( FPTYPE *x1 , FPTYPE *x2 ) {
#if defined(VECTORIZE) && defined(FPTYPE_SINGLE) && defined(__SSE4_1__)
union {
vector(4,float) v;
float f[4];
} a, b, d;
/* Load x1 and x2 into a and b. */
a.v = _mm_load_ps( x1 );
b.v = _mm_load_ps( x2 );
/* Use the built-in dot-product instruction. */
d.v = _mm_dp_ps( a.v , b.v , 0x71 );
/* Return the sum of squares. */
return d.f[0];
#elif defined(VECTORIZE) && defined(FPTYPE_SINGLE) && defined(__SSE3__)
union {
vector(4,float) v;
float f[4];
} a, b, d;
/* Load x1 and x2 into a and b. */
a.v = _mm_load_ps( x1 );
b.v = _mm_load_ps( x2 );
/* Square the entries. */
d.v = a.v * b.v;
/* Add horizontally twice to get the sum of the four entries
in the lowest float. */
d.v = _mm_hadd_ps( d.v , d.v );
d.v = _mm_hadd_ps( d.v , d.v );
/* Return the sum of squares. */
return d.f[0];
#elif defined(VECTORIZE) && defined(FPTYPE_DOUBLE) && defined(__AVX__)
union {
__m256d v;
double f[4];
} a, b, d;
/* Load x1 and x2 into a and b. */
a.v = _mm256_load_pd( x1 );
b.v = _mm256_load_pd( x2 );
/* Square the entries (use a different register so that c can be stored). */
d.v = a.v * b.v;
/* Add horizontally. */
d.v = _mm256_hadd_pd( d.v , d.v );
/* Return the sum of squares. */
return d.f[0] + d.f[2];
#elif defined(VECTORIZE) && defined(FPTYPE_DOUBLE) && defined(__SSE4_1__)
union {
vector(2,double) v;
double f[2];
} a1, a2, b1, b2, d1;
/* Load x1 and x2 into a and b. */
a1.v = _mm_load_pd( x1 );
b1.v = _mm_load_pd( x2 );
a2.v = _mm_load_pd( &x1[2] );
b2.v = _mm_load_pd( &x2[2] );
/* Use the built-in dot-product instruction. */
d1.v = _mm_dp_pd( a1.v , b1.v , 0x31 ) + a2.v * b2.v;
/* Return the sum of squares. */
return d1.f[0];
#elif defined(VECTORIZE) && defined(FPTYPE_DOUBLE) && defined(__SSE3__)
union {
vector(2,double) v;
double f[2];
} a1, a2, b1, b2, c1, c2, d1, d2;
/* Load x1 and x2 into a and b. */
a1.v = _mm_load_pd( x1 );
b1.v = _mm_load_pd( x2 );
a2.v = _mm_load_pd( &x1[2] );
b2.v = _mm_load_pd( &x2[2] );
/* Square the entries (use a different register so that c can be stored). */
d1.v = a1.v * b1.v;
d2.v = a2.v * b2.v;
/* Add horizontally twice to get the sum of the four entries
in the lowest double. */
d1.v = _mm_hadd_pd( d1.v , d2.v );
d1.v = _mm_hadd_pd( d1.v , d1.v );
/* Return the sum of squares. */
return d1.f[0];
#else
return x1[0]*x2[0] + x1[1]*x2[1] + x1[2]*x2[2];
#endif
}