forked from vectorclass/version2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectormath_common.h
274 lines (233 loc) · 11.2 KB
/
vectormath_common.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
/*************************** vectormath_common.h ****************************
* Author: Agner Fog
* Date created: 2014-04-18
* Last modified: 2020-02-23
* Version: 2.01.01
* Project: vector classes
* Description:
* Header file containing common code for inline version of mathematical functions.
*
* For detailed instructions, see VectorClass.pdf
*
* (c) Copyright 2014-2020 Agner Fog.
* Apache License version 2.0 or later.
******************************************************************************/
#ifndef VECTORMATH_COMMON_H
#define VECTORMATH_COMMON_H 2
#ifdef VECTORMATH_LIB_H
#error conflicting header files. More than one implementation of mathematical functions included
#endif
#include <cmath>
#ifndef VECTORCLASS_H
#include "vectorclass.h"
#endif
#if VECTORCLASS_H < 20000
#error Incompatible versions of vector class library mixed
#endif
/******************************************************************************
Define NAN payload values
******************************************************************************/
#define NAN_LOG 0x101 // logarithm for x<0
#define NAN_POW 0x102 // negative number raised to non-integer power
#define NAN_HYP 0x104 // acosh for x<1 and atanh for abs(x)>1
/******************************************************************************
Define mathematical constants
******************************************************************************/
#define VM_PI 3.14159265358979323846 // pi
#define VM_PI_2 1.57079632679489661923 // pi / 2
#define VM_PI_4 0.785398163397448309616 // pi / 4
#define VM_SQRT2 1.41421356237309504880 // sqrt(2)
#define VM_LOG2E 1.44269504088896340736 // 1/log(2)
#define VM_LOG10E 0.434294481903251827651 // 1/log(10)
#define VM_LOG210 3.321928094887362347808 // log2(10)
#define VM_LN2 0.693147180559945309417 // log(2)
#define VM_LN10 2.30258509299404568402 // log(10)
#define VM_SMALLEST_NORMAL 2.2250738585072014E-308 // smallest normal number, double
#define VM_SMALLEST_NORMALF 1.17549435E-38f // smallest normal number, float
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/******************************************************************************
templates for producing infinite and nan in desired vector type
******************************************************************************/
template <class VTYPE>
static inline VTYPE infinite_vec();
template <>
inline Vec2d infinite_vec<Vec2d>() {
return infinite2d();
}
template <>
inline Vec4f infinite_vec<Vec4f>() {
return infinite4f();
}
#if MAX_VECTOR_SIZE >= 256
template <>
inline Vec4d infinite_vec<Vec4d>() {
return infinite4d();
}
template <>
inline Vec8f infinite_vec<Vec8f>() {
return infinite8f();
}
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
template <>
inline Vec8d infinite_vec<Vec8d>() {
return infinite8d();
}
template <>
inline Vec16f infinite_vec<Vec16f>() {
return infinite16f();
}
#endif // MAX_VECTOR_SIZE >= 512
/******************************************************************************
templates for polynomials
Using Estrin's scheme to make shorter dependency chains and use FMA, starting
longest dependency chains first.
******************************************************************************/
// template <typedef VECTYPE, typedef CTYPE>
template <class VTYPE, class CTYPE>
static inline VTYPE polynomial_2(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2) {
// calculates polynomial c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
//return = x2 * c2 + (x * c1 + c0);
return mul_add(x2, c2, mul_add(x, c1, c0));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_3(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3) {
// calculates polynomial c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
//return (c2 + c3*x)*x2 + (c1*x + c0);
return mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_4(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4) {
// calculates polynomial c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return (c2+c3*x)*x2 + ((c0+c1*x) + c4*x4);
return mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0) + c4*x4);
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_4n(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3) {
// calculates polynomial 1*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return (c2+c3*x)*x2 + ((c0+c1*x) + x4);
return mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0) + x4);
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_5(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5) {
// calculates polynomial c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return (c2+c3*x)*x2 + ((c4+c5*x)*x4 + (c0+c1*x));
return mul_add(mul_add(c3, x, c2), x2, mul_add(mul_add(c5, x, c4), x4, mul_add(c1, x, c0)));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_5n(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4) {
// calculates polynomial 1*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return (c2+c3*x)*x2 + ((c4+x)*x4 + (c0+c1*x));
return mul_add(mul_add(c3, x, c2), x2, mul_add(c4 + x, x4, mul_add(c1, x, c0)));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_6(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6) {
// calculates polynomial c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return (c4+c5*x+c6*x2)*x4 + ((c2+c3*x)*x2 + (c0+c1*x));
return mul_add(mul_add(c6, x2, mul_add(c5, x, c4)), x4, mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0)));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_6n(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5) {
// calculates polynomial 1*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return (c4+c5*x+x2)*x4 + ((c2+c3*x)*x2 + (c0+c1*x));
return mul_add(mul_add(c5, x, c4 + x2), x4, mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0)));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_7(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7) {
// calculates polynomial c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
//return ((c6+c7*x)*x2 + (c4+c5*x))*x4 + ((c2+c3*x)*x2 + (c0+c1*x));
return mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4, mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0)));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_8(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8) {
// calculates polynomial c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
VTYPE x8 = x4 * x4;
//return ((c6+c7*x)*x2 + (c4+c5*x))*x4 + (c8*x8 + (c2+c3*x)*x2 + (c0+c1*x));
return mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0) + c8*x8));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_9(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9) {
// calculates polynomial c9*x^9 + c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
VTYPE x8 = x4 * x4;
//return (((c6+c7*x)*x2 + (c4+c5*x))*x4 + (c8+c9*x)*x8) + ((c2+c3*x)*x2 + (c0+c1*x));
return mul_add(mul_add(c9, x, c8), x8, mul_add(
mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0))));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_10(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9, CTYPE c10) {
// calculates polynomial c10*x^10 + c9*x^9 + c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
VTYPE x8 = x4 * x4;
//return (((c6+c7*x)*x2 + (c4+c5*x))*x4 + (c8+c9*x+c10*x2)*x8) + ((c2+c3*x)*x2 + (c0+c1*x));
return mul_add(mul_add(x2, c10, mul_add(c9, x, c8)), x8,
mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0))));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_13(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9, CTYPE c10, CTYPE c11, CTYPE c12, CTYPE c13) {
// calculates polynomial c13*x^13 + c12*x^12 + ... + c1*x + c0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
VTYPE x8 = x4 * x4;
return mul_add(
mul_add(
mul_add(c13, x, c12), x4,
mul_add(mul_add(c11, x, c10), x2, mul_add(c9, x, c8))), x8,
mul_add(
mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0))));
}
template<class VTYPE, class CTYPE>
static inline VTYPE polynomial_13m(VTYPE const x, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9, CTYPE c10, CTYPE c11, CTYPE c12, CTYPE c13) {
// calculates polynomial c13*x^13 + c12*x^12 + ... + x + 0
// VTYPE may be a vector type, CTYPE is a scalar type
VTYPE x2 = x * x;
VTYPE x4 = x2 * x2;
VTYPE x8 = x4 * x4;
// return ((c8+c9*x) + (c10+c11*x)*x2 + (c12+c13*x)*x4)*x8 + (((c6+c7*x)*x2 + (c4+c5*x))*x4 + ((c2+c3*x)*x2 + x));
return mul_add(
mul_add(mul_add(c13, x, c12), x4, mul_add(mul_add(c11, x, c10), x2, mul_add(c9, x, c8))), x8,
mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4, mul_add(mul_add(c3, x, c2), x2, x)));
}
#ifdef VCL_NAMESPACE
}
#endif
#endif