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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
/********************************************************************************
* *
* D o u b l e - P r e c i s i o n 2 x 2 M a t r i x *
* *
*********************************************************************************
* Copyright (C) 2003,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library 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 library 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 Lesser 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/> *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXObject.h"
#include "FXVec2d.h"
#include "FXVec3d.h"
#include "FXMat2d.h"
#include "FXMat3d.h"
/*
Notes:
*/
using namespace FX;
/*******************************************************************************/
namespace FX {
// Initialize matrix from scalar
FXMat2d::FXMat2d(FXdouble s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_set1_pd(s));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_set1_pd(s));
_mm_storeu_pd(&m[1][0],_mm_set1_pd(s));
#else
m[0][0]=s; m[0][1]=s;
m[1][0]=s; m[1][1]=s;
#endif
}
// Initialize matrix from another matrix
FXMat2d::FXMat2d(const FXMat2d& s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_loadu_pd(&s[0][0]));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(&s[0][0]));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(&s[1][0]));
#else
m[0][0]=s[0][0]; m[0][1]=s[0][1];
m[1][0]=s[1][0]; m[1][1]=s[1][1];
#endif
}
// Initialize from rotation and scaling part of 3x3 matrix
FXMat2d::FXMat2d(const FXMat3d& s){
#if defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(&s[0][0]));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(&s[1][0]));
#else
m[0][0]=s[0][0]; m[0][1]=s[0][1];
m[1][0]=s[1][0]; m[1][1]=s[1][1];
#endif
}
// Initialize matrix from array
FXMat2d::FXMat2d(const FXdouble s[]){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_loadu_pd(s));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(s+0));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(s+2));
#else
m[0][0]=s[0]; m[0][1]=s[1];
m[1][0]=s[2]; m[1][1]=s[3];
#endif
}
// Initialize diagonal matrix
FXMat2d::FXMat2d(FXdouble a,FXdouble b){
m[0][0]=a; m[0][1]=0.0;
m[1][0]=0.0; m[1][1]=b;
}
// Initialize matrix from components
FXMat2d::FXMat2d(FXdouble a00,FXdouble a01,FXdouble a10,FXdouble a11){
m[0][0]=a00; m[0][1]=a01;
m[1][0]=a10; m[1][1]=a11;
}
// Initialize matrix from two vectors
FXMat2d::FXMat2d(const FXVec2d& a,const FXVec2d& b){
#if defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(a));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(b));
#else
m[0]=a;
m[1]=b;
#endif
}
// Assign from scalar
FXMat2d& FXMat2d::operator=(FXdouble s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_set1_pd(s));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_set1_pd(s));
_mm_storeu_pd(&m[1][0],_mm_set1_pd(s));
#else
m[0][0]=s; m[0][1]=s;
m[1][0]=s; m[1][1]=s;
#endif
return *this;
}
// Assignment operator
FXMat2d& FXMat2d::operator=(const FXMat2d& s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_loadu_pd(&s[0][0]));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(&s[0][0]));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(&s[1][0]));
#else
m[0][0]=s[0][0]; m[0][1]=s[0][1];
m[1][0]=s[1][0]; m[1][1]=s[1][1];
#endif
return *this;
}
// Assign from rotation and scaling part of 3x3 matrix
FXMat2d& FXMat2d::operator=(const FXMat3d& s){
#if defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(&s[0][0]));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(&s[1][0]));
#else
m[0][0]=s[0][0]; m[0][1]=s[0][1];
m[1][0]=s[1][0]; m[1][1]=s[1][1];
#endif
return *this;
}
// Assignment from array
FXMat2d& FXMat2d::operator=(const FXdouble s[]){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_loadu_pd(s));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(s+0));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(s+2));
#else
m[0][0]=s[0]; m[0][1]=s[1];
m[1][0]=s[2]; m[1][1]=s[3];
#endif
return *this;
}
// Set value from scalar
FXMat2d& FXMat2d::set(FXdouble s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_set1_pd(s));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_set1_pd(s));
_mm_storeu_pd(&m[1][0],_mm_set1_pd(s));
#else
m[0][0]=s; m[0][1]=s;
m[1][0]=s; m[1][1]=s;
#endif
return *this;
}
// Set value from another matrix
FXMat2d& FXMat2d::set(const FXMat2d& s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_loadu_pd(&s[0][0]));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(&s[0][0]));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(&s[1][0]));
#else
m[0][0]=s[0][0]; m[0][1]=s[0][1];
m[1][0]=s[1][0]; m[1][1]=s[1][1];
#endif
return *this;
}
// Set from rotation and scaling part of 3x3 matrix
FXMat2d& FXMat2d::set(const FXMat3d& s){
#if defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(&s[0][0]));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(&s[1][0]));
#else
m[0][0]=s[0][0]; m[0][1]=s[0][1];
m[1][0]=s[1][0]; m[1][1]=s[1][1];
#endif
return *this;
}
// Set value from array
FXMat2d& FXMat2d::set(const FXdouble s[]){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_loadu_pd(s));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(s+0));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(s+2));
#else
m[0][0]=s[0]; m[0][1]=s[1];
m[1][0]=s[2]; m[1][1]=s[3];
#endif
return *this;
}
// Set diagonal matrix
FXMat2d& FXMat2d::set(FXdouble a,FXdouble b){
m[0][0]=a; m[0][1]=0.0;
m[1][0]=0.0; m[1][1]=b;
return *this;
}
// Set value from components
FXMat2d& FXMat2d::set(FXdouble a00,FXdouble a01,FXdouble a10,FXdouble a11){
m[0][0]=a00; m[0][1]=a01;
m[1][0]=a10; m[1][1]=a11;
return *this;
}
// Set value from two vectors
FXMat2d& FXMat2d::set(const FXVec2d& a,const FXVec2d& b){
#if defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_loadu_pd(a));
_mm_storeu_pd(&m[1][0],_mm_loadu_pd(b));
#else
m[0]=a;
m[1]=b;
#endif
return *this;
}
// Add matrices
FXMat2d& FXMat2d::operator+=(const FXMat2d& s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_add_pd(_mm256_loadu_pd(&m[0][0]),_mm256_loadu_pd(&s[0][0])));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_add_pd(_mm_loadu_pd(&m[0][0]),_mm_loadu_pd(&s[0][0])));
_mm_storeu_pd(&m[1][0],_mm_add_pd(_mm_loadu_pd(&m[1][0]),_mm_loadu_pd(&s[1][0])));
#else
m[0][0]+=s[0][0]; m[0][1]+=s[0][1];
m[1][0]+=s[1][0]; m[1][1]+=s[1][1];
#endif
return *this;
}
// Subtract matrices
FXMat2d& FXMat2d::operator-=(const FXMat2d& s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_sub_pd(_mm256_loadu_pd(&m[0][0]),_mm256_loadu_pd(&s[0][0])));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_sub_pd(_mm_loadu_pd(&m[0][0]),_mm_loadu_pd(&s[0][0])));
_mm_storeu_pd(&m[1][0],_mm_sub_pd(_mm_loadu_pd(&m[1][0]),_mm_loadu_pd(&s[1][0])));
#else
m[0][0]-=s[0][0]; m[0][1]-=s[0][1];
m[1][0]-=s[1][0]; m[1][1]-=s[1][1];
#endif
return *this;
}
// Multiply matrix by scalar
FXMat2d& FXMat2d::operator*=(FXdouble s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_mul_pd(_mm256_loadu_pd(&m[0][0]),_mm256_set1_pd(s)));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_mul_pd(_mm_loadu_pd(&m[0][0]),_mm_set1_pd(s)));
_mm_storeu_pd(&m[1][0],_mm_mul_pd(_mm_loadu_pd(&m[1][0]),_mm_set1_pd(s)));
#else
m[0][0]*=s; m[0][1]*=s;
m[1][0]*=s; m[1][1]*=s;
#endif
return *this;
}
// Multiply matrix by matrix
FXMat2d& FXMat2d::operator*=(const FXMat2d& s){
#if defined(FOX_HAS_SSE2)
__m128d s0=_mm_loadu_pd(&s[0][0]);
__m128d s1=_mm_loadu_pd(&s[1][0]);
_mm_storeu_pd(&m[0][0],_mm_add_pd(_mm_mul_pd(_mm_set1_pd(m[0][0]),s0),_mm_mul_pd(_mm_set1_pd(m[0][1]),s1)));
_mm_storeu_pd(&m[1][0],_mm_add_pd(_mm_mul_pd(_mm_set1_pd(m[1][0]),s0),_mm_mul_pd(_mm_set1_pd(m[1][1]),s1)));
#else
FXdouble m00=m[0][0],m01=m[0][1],m10=m[1][0],m11=m[1][1];
m[0][0]=m00*s[0][0]+m01*s[1][0];
m[0][1]=m00*s[0][1]+m01*s[1][1];
m[1][0]=m10*s[0][0]+m11*s[1][0];
m[1][1]=m10*s[0][1]+m11*s[1][1];
#endif
return *this;
}
// Divide matrix by scalar
FXMat2d& FXMat2d::operator/=(FXdouble s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_div_pd(_mm256_loadu_pd(&m[0][0]),_mm256_set1_pd(s)));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_div_pd(_mm_loadu_pd(&m[0][0]),_mm_set1_pd(s)));
_mm_storeu_pd(&m[1][0],_mm_div_pd(_mm_loadu_pd(&m[1][0]),_mm_set1_pd(s)));
#else
m[0][0]/=s; m[0][1]/=s;
m[1][0]/=s; m[1][1]/=s;
#endif
return *this;
}
// Negate matrix
FXMat2d FXMat2d::operator-() const {
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_sub_pd(_mm256_set1_pd(0.0),_mm256_loadu_pd(&m[0][0])));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_sub_pd(_mm_set1_pd(0.0),_mm_loadu_pd(&m[0][0])));
_mm_storeu_pd(&r[1][0],_mm_sub_pd(_mm_set1_pd(0.0),_mm_loadu_pd(&m[1][0])));
return r;
#else
return FXMat2d(-m[0][0],-m[0][1],-m[1][0],-m[1][1]);
#endif
}
// Set to identity matrix
FXMat2d& FXMat2d::identity(){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_set_pd(1.0,0.0,0.0,1.0));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_set_pd(0.0,1.0));
_mm_storeu_pd(&m[1][0],_mm_set_pd(1.0,0.0));
#else
m[0][0]=1.0; m[0][1]=0.0;
m[1][0]=0.0; m[1][1]=1.0;
#endif
return *this;
}
// Return true if identity matrix
FXbool FXMat2d::isIdentity() const {
return m[0][0]==1.0 && m[0][1]==0.0 && m[1][0]==0.0 && m[1][1]==1.0;
}
// Rotate by cosine, sine
FXMat2d& FXMat2d::rot(FXdouble c,FXdouble s){
#if defined(FOX_HAS_SSE2)
__m128d cc=_mm_set1_pd(c);
__m128d ss=_mm_set1_pd(s);
__m128d uu=_mm_loadu_pd(&m[0][0]);
__m128d vv=_mm_loadu_pd(&m[1][0]);
_mm_storeu_pd(&m[0][0],_mm_add_pd(_mm_mul_pd(cc,uu),_mm_mul_pd(ss,vv)));
_mm_storeu_pd(&m[1][0],_mm_sub_pd(_mm_mul_pd(cc,vv),_mm_mul_pd(ss,uu)));
#else
FXdouble u,v;
u=m[0][0]; v=m[1][0]; m[0][0]=c*u+s*v; m[1][0]=c*v-s*u;
u=m[0][1]; v=m[1][1]; m[0][1]=c*u+s*v; m[1][1]=c*v-s*u;
#endif
return *this;
}
// Rotate by angle
FXMat2d& FXMat2d::rot(FXdouble phi){
return rot(Math::cos(phi),Math::sin(phi));
}
// Scale unqual
FXMat2d& FXMat2d::scale(FXdouble sx,FXdouble sy){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_mul_pd(_mm256_loadu_pd(&m[0][0]),_mm256_set_pd(sy,sy,sx,sx)));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_mul_pd(_mm_loadu_pd(&m[0][0]),_mm_set_pd(sx,sx)));
_mm_storeu_pd(&m[1][0],_mm_mul_pd(_mm_loadu_pd(&m[1][0]),_mm_set_pd(sy,sy)));
#else
m[0][0]*=sx; m[0][1]*=sx;
m[1][0]*=sy; m[1][1]*=sy;
#endif
return *this;
}
// Scale uniform
FXMat2d& FXMat2d::scale(FXdouble s){
#if defined(FOX_HAS_AVX)
_mm256_storeu_pd(&m[0][0],_mm256_mul_pd(_mm256_loadu_pd(&m[0][0]),_mm256_set1_pd(s)));
#elif defined(FOX_HAS_SSE2)
_mm_storeu_pd(&m[0][0],_mm_mul_pd(_mm_loadu_pd(&m[0][0]),_mm_set1_pd(s)));
_mm_storeu_pd(&m[1][0],_mm_mul_pd(_mm_loadu_pd(&m[1][0]),_mm_set1_pd(s)));
#else
m[0][0]*=s; m[0][1]*=s;
m[1][0]*=s; m[1][1]*=s;
#endif
return *this;
}
// Calculate determinant
FXdouble FXMat2d::det() const {
return m[0][0]*m[1][1]-m[0][1]*m[1][0];
}
// Transpose matrix
FXMat2d FXMat2d::transpose() const {
return FXMat2d(m[0][0],m[1][0],m[0][1],m[1][1]);
}
// Invert matrix
FXMat2d FXMat2d::invert() const {
FXdouble dd=m[0][0]*m[1][1]-m[0][1]*m[1][0];
return FXMat2d(m[1][1]/dd,-m[0][1]/dd,-m[1][0]/dd,m[0][0]/dd);
}
// Orthogonalize matrix
// Uses Gram-Schmidt orthogonalization on a row-by-row basis
FXMat2d orthogonalize(const FXMat2d& m){
FXMat2d result(m);
result[0]/=result[0].length();
result[1]-=result[0]*(result[1]*result[0]);
result[1]/=result[1].length();
return result;
}
// Matrix times vector
FXVec2d operator*(const FXMat2d& m,const FXVec2d& v){
#if defined(FOX_HAS_SSE3)
__m128d vv=_mm_loadu_pd(v);
__m128d r0=_mm_mul_pd(_mm_loadu_pd(&m[0][0]),vv);
__m128d r1=_mm_mul_pd(_mm_loadu_pd(&m[1][0]),vv);
FXVec2d r;
_mm_storeu_pd(&r[0],_mm_hadd_pd(r0,r1));
return r;
#else
return FXVec2d(m[0][0]*v[0]+m[0][1]*v[1],m[1][0]*v[0]+m[1][1]*v[1]);
#endif
}
// Vector times matrix
FXVec2d operator*(const FXVec2d& v,const FXMat2d& m){
#if defined(FOX_HAS_SSE2)
FXVec2d r;
__m128d r0=_mm_mul_pd(_mm_set1_pd(v[0]),_mm_loadu_pd(&m[0][0]));
__m128d r1=_mm_mul_pd(_mm_set1_pd(v[1]),_mm_loadu_pd(&m[1][0]));
_mm_storeu_pd(&r[0],_mm_add_pd(r0,r1));
return r;
#else
return FXVec2d(v[0]*m[0][0]+v[1]*m[1][0],v[0]*m[0][1]+v[1]*m[1][1]);
#endif
}
// Matrix and matrix add
FXMat2d operator+(const FXMat2d& a,const FXMat2d& b){
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_add_pd(_mm256_loadu_pd(&a[0][0]),_mm256_loadu_pd(&b[0][0])));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_add_pd(_mm_loadu_pd(&a[0][0]),_mm_loadu_pd(&b[0][0])));
_mm_storeu_pd(&r[1][0],_mm_add_pd(_mm_loadu_pd(&a[1][0]),_mm_loadu_pd(&b[1][0])));
return r;
#else
return FXMat2d(a[0][0]+b[0][0],a[0][1]+b[0][1],a[1][0]+b[1][0],a[1][1]+b[1][1]);
#endif
}
// Matrix and matrix subtract
FXMat2d operator-(const FXMat2d& a,const FXMat2d& b){
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_sub_pd(_mm256_loadu_pd(&a[0][0]),_mm256_loadu_pd(&b[0][0])));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_sub_pd(_mm_loadu_pd(&a[0][0]),_mm_loadu_pd(&b[0][0])));
_mm_storeu_pd(&r[1][0],_mm_sub_pd(_mm_loadu_pd(&a[1][0]),_mm_loadu_pd(&b[1][0])));
return r;
#else
return FXMat2d(a[0][0]-b[0][0],a[0][1]-b[0][1],a[1][0]-b[1][0],a[1][1]-b[1][1]);
#endif
}
// Matrix and matrix multiply
FXMat2d operator*(const FXMat2d& a,const FXMat2d& b){
#if defined(FOX_HAS_SSE2)
__m128d b0=_mm_loadu_pd(&b[0][0]);
__m128d b1=_mm_loadu_pd(&b[1][0]);
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_add_pd(_mm_mul_pd(_mm_set1_pd(a[0][0]),b0),_mm_mul_pd(_mm_set1_pd(a[0][1]),b1)));
_mm_storeu_pd(&r[1][0],_mm_add_pd(_mm_mul_pd(_mm_set1_pd(a[1][0]),b0),_mm_mul_pd(_mm_set1_pd(a[1][1]),b1)));
return r;
#else
return FXMat2d(a[0][0]*b[0][0]+a[0][1]*b[1][0],a[0][0]*b[0][1]+a[0][1]*b[1][1],a[1][0]*b[0][0]+a[1][1]*b[1][0],a[1][0]*b[0][1]+a[1][1]*b[1][1]);
#endif
}
// Multiply scalar by matrix
FXMat2d operator*(FXdouble x,const FXMat2d& m){
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_mul_pd(_mm256_set1_pd(x),_mm256_loadu_pd(&m[0][0])));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_mul_pd(_mm_set1_pd(x),_mm_loadu_pd(&m[0][0])));
_mm_storeu_pd(&r[1][0],_mm_mul_pd(_mm_set1_pd(x),_mm_loadu_pd(&m[1][0])));
return r;
#else
return FXMat2d(x*m[0][0],x*m[0][1],x*m[1][0],x*m[1][1]);
#endif
}
// Multiply matrix by scalar
FXMat2d operator*(const FXMat2d& m,FXdouble x){
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_mul_pd(_mm256_loadu_pd(&m[0][0]),_mm256_set1_pd(x)));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_mul_pd(_mm_loadu_pd(&m[0][0]),_mm_set1_pd(x)));
_mm_storeu_pd(&r[1][0],_mm_mul_pd(_mm_loadu_pd(&m[1][0]),_mm_set1_pd(x)));
return r;
#else
return FXMat2d(m[0][0]*x,m[0][1]*x,m[1][0]*x,m[1][1]*x);
#endif
}
// Divide scalar by matrix
FXMat2d operator/(FXdouble x,const FXMat2d& m){
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_div_pd(_mm256_set1_pd(x),_mm256_loadu_pd(m[0])));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_div_pd(_mm_set1_pd(x),_mm_loadu_pd(&m[0][0])));
_mm_storeu_pd(&r[1][0],_mm_div_pd(_mm_set1_pd(x),_mm_loadu_pd(&m[1][0])));
return r;
#else
return FXMat2d(x/m[0][0],x/m[0][1],x/m[1][0],x/m[1][1]);
#endif
}
// Divide matrix by scalar
FXMat2d operator/(const FXMat2d& m,FXdouble x){
#if defined(FOX_HAS_AVX)
FXMat2d r;
_mm256_storeu_pd(&r[0][0],_mm256_div_pd(_mm256_loadu_pd(&m[0][0]),_mm256_set1_pd(x)));
return r;
#elif defined(FOX_HAS_SSE2)
FXMat2d r;
_mm_storeu_pd(&r[0][0],_mm_div_pd(_mm_loadu_pd(&m[0][0]),_mm_set1_pd(x)));
_mm_storeu_pd(&r[1][0],_mm_div_pd(_mm_loadu_pd(&m[1][0]),_mm_set1_pd(x)));
return r;
#else
return FXMat2d(m[0][0]/x,m[0][1]/x,m[1][0]/x,m[1][1]/x);
#endif
}
// Matrix and matrix equality
FXbool operator==(const FXMat2d& a,const FXMat2d& b){
return a[0]==b[0] && a[1]==b[1];
}
// Matrix and matrix inequality
FXbool operator!=(const FXMat2d& a,const FXMat2d& b){
return a[0]!=b[0] || a[1]!=b[1];
}
// Matrix and scalar equality
FXbool operator==(const FXMat2d& a,FXdouble n){
return a[0]==n && a[1]==n;
}
// Matrix and scalar inequality
FXbool operator!=(const FXMat2d& a,FXdouble n){
return a[0]!=n || a[1]!=n;
}
// Scalar and matrix equality
FXbool operator==(FXdouble n,const FXMat2d& a){
return n==a[0] && n==a[1];
}
// Scalar and matrix inequality
FXbool operator!=(FXdouble n,const FXMat2d& a){
return n!=a[0] || n!=a[1];
}
// Save to archive
FXStream& operator<<(FXStream& store,const FXMat2d& m){
store << m[0] << m[1]; return store;
}
// Load from archive
FXStream& operator>>(FXStream& store,FXMat2d& m){
store >> m[0] >> m[1]; return store;
}
}
|