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
|
/*
Abstract class for 64-bit floating point numbers
Based on vectorclass (VCL), mainly for templating with double
@author: minh
@date: 2016-09-24
*/
#ifndef VECTORF64_H
#define VECTORF64_H
//typedef int64_t Vec1db;
//typedef bool Vec1db;
/*****************************************************************************
*
* Vec1db: Vector of 1 Booleans for use with Vec1d
*
*****************************************************************************/
class Vec1db {
public:
bool xmm; // Double vector
// Default constructor:
Vec1db() {
}
// Constructor to broadcast scalar value:
Vec1db(bool b) {
xmm = b;
}
// Assignment operator to broadcast scalar value:
Vec1db & operator = (bool b) {
*this = Vec1db(b);
return *this;
}
private: // Prevent constructing from int, etc.
Vec1db(int b);
Vec1db & operator = (int x);
public:
// Member function to change a single element in vector
// Note: This function is inefficient. Use load function if changing more than one element
Vec1db const & insert(uint32_t index, bool value) {
xmm = value;
return *this;
}
// Member function extract a single element from vector
bool extract(uint32_t index) const {
return xmm;
}
// Extract a single element. Operator [] can only read an element, not write.
bool operator [] (uint32_t index) const {
return extract(index);
}
static int size() {
return 1;
}
};
/*****************************************************************************
*
* Operators for Vec1db
*
*****************************************************************************/
// vector operator & : bitwise and
static inline Vec1db operator & (Vec1db const & a, Vec1db const & b) {
return Vec1db(a.xmm && b.xmm);
}
static inline Vec1db operator && (Vec1db const & a, Vec1db const & b) {
return Vec1db(a.xmm && b.xmm);
}
// vector operator &= : bitwise and
static inline Vec1db & operator &= (Vec1db & a, Vec1db const & b) {
a = a & b;
return a;
}
/*****************************************************************************
*
* Vec1d: Vector of 1 double precision floating point values
*
*****************************************************************************/
class Vec1d {
public:
double xmm; // double vector
// Default constructor:
Vec1d() {
}
// Constructor to broadcast the same value into all elements:
Vec1d(double d) {
xmm = d;
}
// Member function to load from array (unaligned)
Vec1d & load(double const * p) {
xmm = *p;
return *this;
}
// Member function to load from array, aligned by 8
Vec1d const & load_a(double const * p) {
xmm = *p;
return *this;
}
// Partial load. Load n elements and set the rest to 0
Vec1d & load_partial(int n, double const * p) {
switch (n) {
case 1:
xmm = *p; break;
default:
xmm = 0.0;
}
return *this;
}
// Member function to store into array (unaligned)
void store(double * p) const {
*p = xmm;
}
// Member function to store into array, aligned by 8
void store_a(double * p) const {
*p = xmm;
}
// cut off vector to n elements. The last 4-n elements are set to zero
Vec1d & cutoff(int n) {
if (n == 0)
xmm = 0.0;
return *this;
}
// Member function to change a single element in vector
// Note: This function is inefficient. Use load function if changing more than one element
Vec1d const & insert(uint32_t index, double value) {
xmm = value;
return *this;
};
// Member function extract a single element from vector
double extract(uint32_t index) const {
return xmm;
}
// Extract a single element. Use store function if extracting more than one element.
// Operator [] can only read an element, not write.
double operator [] (uint32_t index) const {
return extract(index);
}
static int size() {
return 1;
}
};
/*****************************************************************************
*
* Operators for Vec1d
*
*****************************************************************************/
// vector operator + : add element by element
static inline Vec1d operator + (Vec1d const & a, Vec1d const & b) {
return Vec1d(a.xmm + b.xmm);
}
// vector operator + : add vector and scalar
static inline Vec1d operator + (Vec1d const & a, double b) {
return a + Vec1d(b);
}
static inline Vec1d operator + (double a, Vec1d const & b) {
return Vec1d(a) + b;
}
// vector operator += : add
static inline Vec1d & operator += (Vec1d & a, Vec1d const & b) {
a = a + b;
return a;
}
// postfix operator ++
static inline Vec1d operator ++ (Vec1d & a, int) {
Vec1d a0 = a;
a = a + 1.0;
return a0;
}
// prefix operator ++
static inline Vec1d & operator ++ (Vec1d & a) {
a = a + 1.0;
return a;
}
// vector operator - : subtract element by element
static inline Vec1d operator - (Vec1d const & a, Vec1d const & b) {
return Vec1d(a.xmm - b.xmm);
}
// vector operator - : subtract vector and scalar
static inline Vec1d operator - (Vec1d const & a, double b) {
return a - Vec1d(b);
}
static inline Vec1d operator - (double a, Vec1d const & b) {
return Vec1d(a) - b;
}
// vector operator - : unary minus
// Change sign bit, even for 0, INF and NAN
static inline Vec1d operator - (Vec1d const & a) {
return Vec1d(-a.xmm);
}
// vector operator -= : subtract
static inline Vec1d & operator -= (Vec1d & a, Vec1d const & b) {
a = a - b;
return a;
}
// postfix operator --
static inline Vec1d operator -- (Vec1d & a, int) {
Vec1d a0 = a;
a = a - 1.0;
return a0;
}
// prefix operator --
static inline Vec1d & operator -- (Vec1d & a) {
a = a - 1.0;
return a;
}
// vector operator * : multiply element by element
static inline Vec1d operator * (Vec1d const & a, Vec1d const & b) {
return Vec1d(a.xmm * b.xmm);
}
// vector operator * : multiply vector and scalar
static inline Vec1d operator * (Vec1d const & a, double b) {
return a * Vec1d(b);
}
static inline Vec1d operator * (double a, Vec1d const & b) {
return Vec1d(a) * b;
}
// vector operator *= : multiply
static inline Vec1d & operator *= (Vec1d & a, Vec1d const & b) {
a = a * b;
return a;
}
// vector operator / : divide all elements by same integer
static inline Vec1d operator / (Vec1d const & a, Vec1d const & b) {
return Vec1d(a.xmm/b.xmm);
}
// vector operator / : divide vector and scalar
static inline Vec1d operator / (Vec1d const & a, double b) {
return a / Vec1d(b);
}
static inline Vec1d operator / (double a, Vec1d const & b) {
return Vec1d(a) / b;
}
// vector operator /= : divide
static inline Vec1d & operator /= (Vec1d & a, Vec1d const & b) {
a = a / b;
return a;
}
// vector operator == : returns true for elements for which a == b
static inline Vec1db operator == (Vec1d const & a, Vec1d const & b) {
return Vec1db(a.xmm == b.xmm);
}
// vector operator != : returns true for elements for which a != b
static inline Vec1db operator != (Vec1d const & a, Vec1d const & b) {
return Vec1db(a.xmm != b.xmm);
}
// vector operator < : returns true for elements for which a < b
static inline Vec1db operator < (Vec1d const & a, Vec1d const & b) {
return Vec1db(a.xmm < b.xmm);
}
// vector operator <= : returns true for elements for which a <= b
static inline Vec1db operator <= (Vec1d const & a, Vec1d const & b) {
return Vec1db(a.xmm <= b.xmm);
}
// vector operator > : returns true for elements for which a > b
static inline Vec1db operator > (Vec1d const & a, Vec1d const & b) {
return b < a;
}
// vector operator >= : returns true for elements for which a >= b
static inline Vec1db operator >= (Vec1d const & a, Vec1d const & b) {
return b <= a;
}
// General arithmetic functions, etc.
// Horizontal add: Calculates the sum of all vector elements.
static inline double horizontal_add (Vec1d const & a) {
return a.xmm;
}
// function max: a > b ? a : b
static inline Vec1d max(Vec1d const & a, Vec1d const & b) {
return max(a.xmm,b.xmm);
}
// function min: a < b ? a : b
static inline Vec1d min(Vec1d const & a, Vec1d const & b) {
return min(a.xmm,b.xmm);
}
// function abs: absolute value
// Removes sign bit, even for -0.0f, -INF and -NAN
static inline Vec1d abs(Vec1d const & a) {
return Vec1d(fabs(a.xmm));
}
// function log: logarithm
// Removes sign bit, even for -0.0f, -INF and -NAN
static inline Vec1d log(Vec1d const & a) {
return Vec1d(log(a.xmm));
}
// Fused multiply and add functions
// Multiply and add
static inline Vec1d mul_add(Vec1d const & a, Vec1d const & b, Vec1d const & c) {
return a * b + c;
}
// Multiply and subtract
static inline Vec1d mul_sub(Vec1d const & a, Vec1d const & b, Vec1d const & c) {
return a * b - c;
}
// Multiply and inverse subtract
static inline Vec1d nmul_add(Vec1d const & a, Vec1d const & b, Vec1d const & c) {
return c - a * b;
}
/*****************************************************************************
*
* Horizontal Boolean functions
*
*****************************************************************************/
// horizontal_and. Returns true if all bits are 1
static inline bool horizontal_and (Vec1db const & a) {
return a.xmm;
}
// horizontal_or. Returns true if at least one bit is 1
static inline bool horizontal_or (Vec1db const & a) {
return a.xmm;
}
// instances of exp_d template
static inline Vec1d exp(Vec1d const & x) {
return Vec1d(exp(x.xmm));
}
#endif //VECTORF64_H
|