Vec4.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Math/Float4.h>
  5. #include <Math/Swizzle.h>
  6. #include <Math/MathTypes.h>
  7. namespace JPH {
  8. class [[nodiscard]] Vec4
  9. {
  10. public:
  11. // Underlying vector type
  12. #if defined(JPH_USE_SSE)
  13. using Type = __m128;
  14. #elif defined(JPH_USE_NEON)
  15. using Type = float32x4_t;
  16. #else
  17. #error Undefined
  18. #endif
  19. /// Constructor
  20. Vec4() = default; ///< Intentionally not initialized for performance reasons
  21. Vec4(const Vec4 &inRHS) = default;
  22. explicit JPH_INLINE Vec4(Vec3Arg inRHS); ///< WARNING: W component undefined!
  23. JPH_INLINE Vec4(Vec3Arg inRHS, float inW);
  24. JPH_INLINE Vec4(Type inRHS) : mValue(inRHS) { }
  25. /// Create a vector from 4 components
  26. JPH_INLINE Vec4(float inX, float inY, float inZ, float inW);
  27. /// Vector with all zeros
  28. static JPH_INLINE Vec4 sZero();
  29. /// Vector with all NaN's
  30. static JPH_INLINE Vec4 sNaN();
  31. /// Replicate inV across all components
  32. static JPH_INLINE Vec4 sReplicate(float inV);
  33. /// Load 4 floats from memory
  34. static JPH_INLINE Vec4 sLoadFloat4(const Float4 *inV);
  35. /// Load 4 floats from memory, 16 bytes aligned
  36. static JPH_INLINE Vec4 sLoadFloat4Aligned(const Float4 *inV);
  37. /// Gather 4 floats from memory at inBase + inOffsets[i] * Scale
  38. template <const int Scale>
  39. static JPH_INLINE Vec4 sGatherFloat4(const float *inBase, UVec4Arg inOffsets);
  40. /// Return the minimum value of each of the components
  41. static JPH_INLINE Vec4 sMin(Vec4Arg inV1, Vec4Arg inV2);
  42. /// Return the maximum of each of the components
  43. static JPH_INLINE Vec4 sMax(Vec4Arg inV1, Vec4Arg inV2);
  44. /// Equals (component wise)
  45. static JPH_INLINE UVec4 sEquals(Vec4Arg inV1, Vec4Arg inV2);
  46. /// Less than (component wise)
  47. static JPH_INLINE UVec4 sLess(Vec4Arg inV1, Vec4Arg inV2);
  48. /// Less than or equal (component wise)
  49. static JPH_INLINE UVec4 sLessOrEqual(Vec4Arg inV1, Vec4Arg inV2);
  50. /// Greater than (component wise)
  51. static JPH_INLINE UVec4 sGreater(Vec4Arg inV1, Vec4Arg inV2);
  52. /// Greater than or equal (component wise)
  53. static JPH_INLINE UVec4 sGreaterOrEqual(Vec4Arg inV1, Vec4Arg inV2);
  54. /// Calculates inMul1 * inMul2 + inAdd
  55. static JPH_INLINE Vec4 sFusedMultiplyAdd(Vec4Arg inMul1, Vec4Arg inMul2, Vec4Arg inAdd);
  56. /// Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of inControl = 1
  57. static JPH_INLINE Vec4 sSelect(Vec4Arg inV1, Vec4Arg inV2, UVec4Arg inControl);
  58. /// Logical or (component wise)
  59. static JPH_INLINE Vec4 sOr(Vec4Arg inV1, Vec4Arg inV2);
  60. /// Logical xor (component wise)
  61. static JPH_INLINE Vec4 sXor(Vec4Arg inV1, Vec4Arg inV2);
  62. /// Logical and (component wise)
  63. static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2);
  64. /// Sort the four elements of ioValue and sort ioIndex at the same time.
  65. /// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
  66. static JPH_INLINE void sSort4(Vec4 &ioValue, UVec4 &ioIndex);
  67. /// Reverse sort the four elements of ioValue (highest first) and sort ioIndex at the same time.
  68. /// Based on a sorting network: http://en.wikipedia.org/wiki/Sorting_network
  69. static JPH_INLINE void sSort4Reverse(Vec4 &ioValue, UVec4 &ioIndex);
  70. /// Get individual components
  71. #if defined(JPH_USE_SSE)
  72. JPH_INLINE float GetX() const { return _mm_cvtss_f32(mValue); }
  73. JPH_INLINE float GetY() const { return mF32[1]; }
  74. JPH_INLINE float GetZ() const { return mF32[2]; }
  75. JPH_INLINE float GetW() const { return mF32[3]; }
  76. #elif defined(JPH_USE_NEON)
  77. JPH_INLINE float GetX() const { return vgetq_lane_f32(mValue, 0); }
  78. JPH_INLINE float GetY() const { return vgetq_lane_f32(mValue, 1); }
  79. JPH_INLINE float GetZ() const { return vgetq_lane_f32(mValue, 2); }
  80. JPH_INLINE float GetW() const { return vgetq_lane_f32(mValue, 3); }
  81. #else
  82. #error Undefined
  83. #endif
  84. /// Set individual components
  85. JPH_INLINE void SetX(float inX) { mF32[0] = inX; }
  86. JPH_INLINE void SetY(float inY) { mF32[1] = inY; }
  87. JPH_INLINE void SetZ(float inZ) { mF32[2] = inZ; }
  88. JPH_INLINE void SetW(float inW) { mF32[3] = inW; }
  89. /// Get float component by index
  90. JPH_INLINE float operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
  91. JPH_INLINE float & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mF32[inCoordinate]; }
  92. /// Comparison
  93. JPH_INLINE bool operator == (Vec4Arg inV2) const;
  94. JPH_INLINE bool operator != (Vec4Arg inV2) const { return !(*this == inV2); }
  95. /// Test if two vectors are close
  96. JPH_INLINE bool IsClose(Vec4Arg inV2, float inMaxDistSq = 1.0e-12f) const;
  97. /// Test if vector is normalized
  98. JPH_INLINE bool IsNormalized(float inTolerance = 1.0e-6f) const;
  99. /// Test if vector contains NaN elements
  100. JPH_INLINE bool IsNaN() const;
  101. /// Multiply two float vectors (component wise)
  102. JPH_INLINE Vec4 operator * (Vec4Arg inV2) const;
  103. /// Multiply vector with float
  104. JPH_INLINE Vec4 operator * (float inV2) const;
  105. /// Multiply vector with float
  106. friend JPH_INLINE Vec4 operator * (float inV1, Vec4Arg inV2);
  107. /// Divide vector by float
  108. JPH_INLINE Vec4 operator / (float inV2) const;
  109. /// Multiply vector with float
  110. JPH_INLINE Vec4 & operator *= (float inV2);
  111. /// Multiply vector with vector
  112. JPH_INLINE Vec4 & operator *= (Vec4Arg inV2);
  113. /// Divide vector by float
  114. JPH_INLINE Vec4 & operator /= (float inV2);
  115. /// Add two float vectors (component wise)
  116. JPH_INLINE Vec4 operator + (Vec4Arg inV2) const;
  117. /// Add two float vectors (component wise)
  118. JPH_INLINE Vec4 & operator += (Vec4Arg inV2);
  119. /// Negate
  120. JPH_INLINE Vec4 operator - () const;
  121. /// Subtract two float vectors (component wise)
  122. JPH_INLINE Vec4 operator - (Vec4Arg inV2) const;
  123. /// Add two float vectors (component wise)
  124. JPH_INLINE Vec4 & operator -= (Vec4Arg inV2);
  125. /// Divide (component wise)
  126. JPH_INLINE Vec4 operator / (Vec4Arg inV2) const;
  127. /// Swizzle the elements in inV
  128. template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
  129. JPH_INLINE Vec4 Swizzle() const;
  130. /// Replicate the X component to all components
  131. JPH_INLINE Vec4 SplatX() const;
  132. /// Replicate the Y component to all components
  133. JPH_INLINE Vec4 SplatY() const;
  134. /// Replicate the Z component to all components
  135. JPH_INLINE Vec4 SplatZ() const;
  136. /// Replicate the W component to all components
  137. JPH_INLINE Vec4 SplatW() const;
  138. /// Return the absolute value of each of the components
  139. JPH_INLINE Vec4 Abs() const;
  140. /// Reciprocal vector (1 / value) for each of the components
  141. JPH_INLINE Vec4 Reciprocal() const;
  142. /// Dot product, returns the dot product in X, Y and Z components
  143. JPH_INLINE Vec4 DotV(Vec4Arg inV2) const;
  144. /// Dot product
  145. JPH_INLINE float Dot(Vec4Arg inV2) const;
  146. /// Squared length of vector
  147. JPH_INLINE float LengthSq() const;
  148. /// Length of vector
  149. JPH_INLINE float Length() const;
  150. /// Normalize vector
  151. JPH_INLINE Vec4 Normalized() const;
  152. /// Store 4 floats to memory
  153. JPH_INLINE void StoreFloat4(Float4 *outV) const;
  154. /// Convert each component from a float to an int
  155. JPH_INLINE UVec4 ToInt() const;
  156. /// Reinterpret Vec4 as a UVec4 (doesn't change the bits)
  157. JPH_INLINE UVec4 ReinterpretAsInt() const;
  158. /// Store if X is negative in bit 0, Y in bit 1, Z in bit 2 and W in bit 3
  159. JPH_INLINE int GetSignBits() const;
  160. /// Get the minimum of X, Y, Z and W
  161. JPH_INLINE float ReduceMin() const;
  162. /// Get the maximum of X, Y, Z and W
  163. JPH_INLINE float ReduceMax() const;
  164. /// Component wise square root
  165. JPH_INLINE Vec4 Sqrt() const;
  166. /// Get vector that contains the sign of each element (returns 1.0f if positive, -1.0f if negative)
  167. JPH_INLINE Vec4 GetSign() const;
  168. /// To String
  169. friend ostream & operator << (ostream &inStream, Vec4Arg inV)
  170. {
  171. inStream << inV.mF32[0] << ", " << inV.mF32[1] << ", " << inV.mF32[2] << ", " << inV.mF32[3];
  172. return inStream;
  173. }
  174. union
  175. {
  176. Type mValue;
  177. float mF32[4];
  178. };
  179. };
  180. static_assert(is_trivial<Vec4>(), "Is supposed to be a trivial type!");
  181. } // JPH
  182. #include "Vec4.inl"