-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtnt_math_utils.h
385 lines (322 loc) · 9.94 KB
/
tnt_math_utils.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
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
////////////////////////////////////////////////////////////////////////////////
// HYPERJAMA MATH UTILITIES //
// By Alex Christensen //
// based on NIST's jama_lu.h //
// http://math.nist.gov/tnt/ //
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2012 Alex Christensen //
// //
// Permission is hereby granted, free of charge, to any person obtaining a //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation //
// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
// and/or sell copies of the Software, and to permit persons to whom the //
// Software is furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
// DEALINGS IN THE SOFTWARE. //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// Changes: //
// //
// includes <complex> //
// added hyperjama_conj, _axpy, _dot, _dotc, _nrm2 functions //
// //
////////////////////////////////////////////////////////////////////////////////
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
/* needed for fabs, sqrt() below */
#include <cmath>
#include <complex>
namespace TNT
{
/**
@returns hypotenuse of real (non-complex) scalars a and b by
avoiding underflow/overflow
using (a * sqrt( 1 + (b/a) * (b/a))), rather than
sqrt(a*a + b*b).
*/
template <class Real>
Real hypot(const Real &a, const Real &b)
{
if (a== 0)
return abs(b);
else
{
Real c = b/a;
return fabs(a) * sqrt(1 + c*c);
}
}
#ifdef _WIN32
#pragma message("Note: Visual Studio requires these flags in the command line to parallelize and vectorize (Project>Properties>Configuration Properties>C/C++>Command Line)")
#pragma message("/openmp /arch:SSE2 /DHYPERJAMA_SSE2")
#else
#pragma message("Note: G++ requires these flags to parallelize and vectorize")
#pragma message("-fopenmp -DHYPERJAMA_SSE2")
#endif
#define HYPERJAMA_BLAS
//conjugate complex elements, do nothing to non-complex elements
template <class T> inline T hyperjama_conj( T x){return x ;}
template <class T> inline std::complex<T> hyperjama_conj(std::complex<T> x){return conj(x);}
//saxpy, caxpy, daxpy, zaxpy template
template <class Real>
void _axpy(const Real* x, Real* y, int n, Real a){
for(int i=0;i<n;i++)
y[i]+=a*x[i];
}
//sdot, cdot, ddot, zdot template
template <class T>
T _dot(int n, const T* x, const T* y){
T sum(0);
for(int i=0;i<n;i++)
sum+=x[i]*y[i];
return sum;
}
//cdotc, zdotc template (sdot,ddot also)
template <class T>
inline T _dotc(int n, const T* x, const T* y){
T sum(0);
for(int i=0;i<n;i++)
sum+=hyperjama_conj(x[i])*y[i];
return sum;
}
//snrm2, dnrm2, scnrm2, dznrm2 template
//
//this is unoptimized, but not used in the innermost loops
template <class T>
inline T _nrm2(const T* x, int n){
T norm(0);
for(int i=0;i<n;i++){
if(x[i]!=T(0)){
T c(abs(norm)/abs(x[i]));
norm=abs(x[i])*sqrt(1+abs(c)*abs(c));
}
}
return norm;
}
//overloaded functions that use sse and sse2 intrinsic functions
//optimized for processors with hyperthreading, which benefit from
//grouped read and operation commands
#ifdef HYPERJAMA_SSE2
#include <emmintrin.h>
//saxpy
inline void _axpy(const float* x, float* y, int n, float a){
if(((size_t)x)%16!=((size_t)y)%16||((size_t)x)%sizeof(float)!=0)
for(int i=0;i<n;i++)
y[i]+=a*x[i];
else{
//process unaligned portions
while(((size_t)x)%16){
if(n<=0)
return;
y[0]+=a*x[0];
x++;
y++;
n--;
}
__m128 sum0;
__m128 sum1;
__m128 reg0,reg1,reg2,reg3;
__m128 areg=_mm_set1_ps(a);
__m128 prod;
//add floats 8 at a time
while(n>=8){
//read floats into MMX registers (8 from each array)
reg0=_mm_load_ps(x );
reg1=_mm_load_ps(x+4);
reg2=_mm_load_ps(y );
reg3=_mm_load_ps(y+4);
//add floats
prod=_mm_mul_ps(reg0,areg);
sum0=_mm_add_ps(prod,reg2);
prod=_mm_mul_ps(reg1,areg);
sum1=_mm_add_ps(prod,reg3);
//put float sums into y
_mm_store_ps(y ,sum0);
_mm_store_ps(y+4,sum1);
x+=8;
y+=8;
n-=8;
}
//add beyond the last multiple of 8
for(int i=0;i<n;i++)
y[i]+=a*x[i];
}
}
//daxpy
inline void _axpy(const double* x, double* y, int n, double a){
if(((size_t)x)%16!=((size_t)y)%16||((size_t)x)%sizeof(double)!=0)
for(int i=0;i<n;i++)
y[i]+=a*x[i];
else{
//process unaligned portions
while(((size_t)x)%16){
if(n<=0)
return;
y[0]+=a*x[0];
x++;
y++;
n--;
}
__m128d sum0;
__m128d sum1;
__m128d reg0,reg1,reg2,reg3;
__m128d areg=_mm_set1_pd(a);
__m128d prod;
//add doubles 4 at a time
while(n>=8){
//read floats into MMX registers (4 from each array)
reg0=_mm_load_pd(x );
reg1=_mm_load_pd(x+2);
reg2=_mm_load_pd(y );
reg3=_mm_load_pd(y+2);
//add floats
prod=_mm_mul_pd(reg0,areg);
sum0=_mm_add_pd(prod,reg2);
prod=_mm_mul_pd(reg1,areg);
sum1=_mm_add_pd(prod,reg3);
//put float sums into y
_mm_store_pd(y ,sum0);
_mm_store_pd(y+2,sum1);
x+=4;
y+=4;
n-=4;
}
//add beyond the last multiple of 4
for(int i=0;i<n;i++)
y[i]+=a*x[i];
}
}
//sdot
inline float _dot(int n, const float* X, const float* Y){
//if one is aligned and one unaligned, perform the non-SSE code
if(((size_t)X)%16!=((size_t)Y)%16||((size_t)X)%sizeof(float)!=0){
float sum=0;
for(int i=0;i<n;i++)
sum+=X[i]*Y[i];
return sum;
}
else{
//to add before aligned sections and after the last multiple of 8
float sum=0;
//process unaligned section of array
while(((size_t)X)%16){
if(n<=0)
return sum;
sum+=X[0]*Y[0];
Y++;
X++;
n--;
}
//find aligned memory on the stack to put the sums
float sums[8];
float* pSums=sums;
if(((size_t)pSums)%16!=0)
pSums=(float*)((((size_t)pSums)&(~15))+16);
pSums[0]=0;
pSums[1]=0;
pSums[2]=0;
pSums[3]=0;
__m128 sum0=_mm_setzero_ps();
__m128 sum1=_mm_setzero_ps();
__m128 reg0,reg1,reg2,reg3,reg4,reg5;
//add floats 8 at a time
while(n>=8){
//read floats into MMX registers (8 from each array)
reg0=_mm_load_ps(X );
reg1=_mm_load_ps(X+4);
reg2=_mm_load_ps(Y );
reg3=_mm_load_ps(Y+4);
//multiply floats together
reg4=_mm_mul_ps(reg0,reg2);
reg5=_mm_mul_ps(reg1,reg3);
//add to sums
sum0=_mm_add_ps(sum0,reg4);
sum1=_mm_add_ps(sum1,reg5);
X+=8;
Y+=8;
n-=8;
}
//move the sums from the xmm registers to the stack
sum0=_mm_add_ps(sum0,sum1);
_mm_store_ps(pSums,sum0);
//add beyond where the inner loop stopped
for(int i=0;i<n;i++)
sum+=X[i]*Y[i];
return sum+pSums[0]+pSums[1]+pSums[2]+pSums[3];
}
}
//ddot
inline double _dot(int n, const double* X, const double* Y){
//if one is aligned and one unaligned, perform the non-SSE2 code
if(((size_t)X)%16!=((size_t)Y)%16||((size_t)X)%sizeof(double)!=0){
double sum=0;
for(int i=0;i<n;i++)
sum+=X[i]*Y[i];
return sum;
}
else{
//to add before aligned sections and after the last multiple of 8
double sum=0;
//process unaligned section of array
while(((size_t)X)%16){
if(n<=0)
return sum;
sum+=X[0]*Y[0];
Y++;
X++;
n--;
}
//find aligned memory on the stack to put the sums
double sums[4];
double* pSums=sums;
if(((size_t)pSums)%16!=0)
pSums=(double*)((((size_t)pSums)&(~15))+16);
pSums[0]=0;
pSums[1]=0;
__m128d sum0=_mm_setzero_pd();
__m128d sum1=_mm_setzero_pd();
__m128d reg0,reg1,reg2,reg3,reg4,reg5;
//add doubles 4 at a time
while(n>=4){
//read doubles into MMX registers (4 from each array)
reg0=_mm_load_pd(X );
reg1=_mm_load_pd(X+2);
reg2=_mm_load_pd(Y );
reg3=_mm_load_pd(Y+2);
//multiply doubles together
reg4=_mm_mul_pd(reg0,reg2);
reg5=_mm_mul_pd(reg1,reg3);
//add to sums
sum0=_mm_add_pd(sum0,reg4);
sum1=_mm_add_pd(sum1,reg5);
X+=4;
Y+=4;
n-=4;
}
//move the sums from the xmm registers to the stack
sum0=_mm_add_pd(sum0,sum1);
_mm_store_pd(pSums,sum0);
//add beyond where the inner loop stopped
for(int i=0;i<n;i++)
sum+=X[i]*Y[i];
return sum+pSums[0]+pSums[1];
}
}
//conjugated dot products are the same as non-conjugated dot products for real numbers
inline float _dotc(int n, const float *x, const float *y){return _dot(n,x,y);}
inline double _dotc(int n, const double *x, const double *y){return _dot(n,x,y);}
#endif
} /* TNT namespace */
#endif
/* MATH_UTILS_H */