simd3D.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "simd.h"
  24. #include "../math/FVector.h"
  25. // Linear 3D algebra for operating on 4 unrelated pixels in parallel.
  26. // Unlike simd.h, this is not a hardware abstraction layer using assembly intrinsics directly.
  27. // This module builds on top of simd.h for higher levels of abstraction.
  28. // The 4D SIMD vectors are stored as matrix rows, but the 3D math vectors are stored as the columns.
  29. // This allow treating each SIMD vector as a separate scalar element when
  30. // abstracting away the fact that we're operating on 4 pixels at a time.
  31. // Therefore less waste on padding when you only need 3 dimensions.
  32. // No need to rely on custom SIMD instructions that doesn't exist in the other set.
  33. // The only penalty is having to run all the operations together.
  34. #ifndef DFPSR_SIMD_3D
  35. #define DFPSR_SIMD_3D
  36. struct F32x4x3 {
  37. F32x4 v1, v2, v3;
  38. // Direct constructor given 3 rows of length 4
  39. F32x4x3(const F32x4& v1, const F32x4& v2, const F32x4& v3)
  40. : v1(v1), v2(v2), v3(v3) {}
  41. // Transposed constructor given 4 columns of length 3
  42. F32x4x3(const dsr::FVector3D& vx, const dsr::FVector3D& vy, const dsr::FVector3D& vz, const dsr::FVector3D& vw)
  43. : v1(F32x4(vx.x, vy.x, vz.x, vw.x)),
  44. v2(F32x4(vx.y, vy.y, vz.y, vw.y)),
  45. v3(F32x4(vx.z, vy.z, vz.z, vw.z)) {}
  46. // Transposed constructor given a single repeated column
  47. F32x4x3(const dsr::FVector3D& v)
  48. : v1(F32x4(v.x, v.x, v.x, v.x)),
  49. v2(F32x4(v.y, v.y, v.y, v.y)),
  50. v3(F32x4(v.z, v.z, v.z, v.z)) {}
  51. // In-place math operations
  52. inline F32x4x3& operator+=(const F32x4x3& offset) { this->v1 = this->v1 + offset.v1; this->v2 = this->v2 + offset.v2; this->v3 = this->v3 + offset.v3; return *this; }
  53. inline F32x4x3& operator-=(const F32x4x3& offset) { this->v1 = this->v1 - offset.v1; this->v2 = this->v2 - offset.v2; this->v3 = this->v3 - offset.v3; return *this; }
  54. inline F32x4x3& operator*=(const F32x4x3& offset) { this->v1 = this->v1 * offset.v1; this->v2 = this->v2 * offset.v2; this->v3 = this->v3 * offset.v3; return *this; }
  55. inline F32x4x3& operator+=(const F32x4& offset) { this->v1 = this->v1 + offset; this->v2 = this->v2 + offset; this->v3 = this->v3 + offset; return *this; }
  56. inline F32x4x3& operator-=(const F32x4& offset) { this->v1 = this->v1 - offset; this->v2 = this->v2 - offset; this->v3 = this->v3 - offset; return *this; }
  57. inline F32x4x3& operator*=(const F32x4& offset) { this->v1 = this->v1 * offset; this->v2 = this->v2 * offset; this->v3 = this->v3 * offset; return *this; }
  58. inline F32x4x3& operator+=(const float& offset) { this->v1 = this->v1 + offset; this->v2 = this->v2 + offset; this->v3 = this->v3 + offset; return *this; }
  59. inline F32x4x3& operator-=(const float& offset) { this->v1 = this->v1 - offset; this->v2 = this->v2 - offset; this->v3 = this->v3 - offset; return *this; }
  60. inline F32x4x3& operator*=(const float& offset) { this->v1 = this->v1 * offset; this->v2 = this->v2 * offset; this->v3 = this->v3 * offset; return *this; }
  61. };
  62. inline F32x4x3 operator+(const F32x4x3 &left, const F32x4x3 &right) {
  63. return F32x4x3(left.v1 + right.v1, left.v2 + right.v2, left.v3 + right.v3);
  64. }
  65. inline F32x4x3 operator+(const F32x4x3 &left, const F32x4 &right) {
  66. return F32x4x3(left.v1 + right, left.v2 + right, left.v3 + right);
  67. }
  68. inline F32x4x3 operator+(const F32x4x3 &left, const float &right) {
  69. return F32x4x3(left.v1 + right, left.v2 + right, left.v3 + right);
  70. }
  71. inline F32x4x3 operator-(const F32x4x3 &left, const F32x4x3 &right) {
  72. return F32x4x3(left.v1 - right.v1, left.v2 - right.v2, left.v3 - right.v3);
  73. }
  74. inline F32x4x3 operator-(const F32x4x3 &left, const F32x4 &right) {
  75. return F32x4x3(left.v1 - right, left.v2 - right, left.v3 - right);
  76. }
  77. inline F32x4x3 operator-(const F32x4x3 &left, const float &right) {
  78. return F32x4x3(left.v1 - right, left.v2 - right, left.v3 - right);
  79. }
  80. inline F32x4x3 operator*(const F32x4x3 &left, const F32x4x3 &right) {
  81. return F32x4x3(left.v1 * right.v1, left.v2 * right.v2, left.v3 * right.v3);
  82. }
  83. inline F32x4x3 operator*(const F32x4x3 &left, const F32x4 &right) {
  84. return F32x4x3(left.v1 * right, left.v2 * right, left.v3 * right);
  85. }
  86. inline F32x4x3 operator*(const F32x4x3 &left, const float &right) {
  87. return F32x4x3(left.v1 * right, left.v2 * right, left.v3 * right);
  88. }
  89. inline F32x4 dotProduct(const F32x4x3 &a, const F32x4x3 &b) {
  90. return (a.v1 * b.v1) + (a.v2 * b.v2) + (a.v3 * b.v3);
  91. }
  92. inline F32x4 squareLength(const F32x4x3 &v) {
  93. return dotProduct(v, v);
  94. }
  95. inline F32x4 length(const F32x4x3 &v) {
  96. return squareLength(v).squareRoot();
  97. }
  98. inline F32x4x3 normalize(const F32x4x3 &v) {
  99. return v * squareLength(v).reciprocalSquareRoot();
  100. }
  101. #endif