UVec4.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Math/Vec4.h>
  5. JPH_NAMESPACE_BEGIN
  6. class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) UVec4
  7. {
  8. public:
  9. JPH_OVERRIDE_NEW_DELETE
  10. // Underlying vector type
  11. #if defined(JPH_USE_SSE)
  12. using Type = __m128i;
  13. #elif defined(JPH_USE_NEON)
  14. using Type = uint32x4_t;
  15. #else
  16. using Type = struct { uint32 mData[4]; };
  17. #endif
  18. /// Constructor
  19. UVec4() = default; ///< Intentionally not initialized for performance reasons
  20. UVec4(const UVec4 &inRHS) = default;
  21. JPH_INLINE UVec4(Type inRHS) : mValue(inRHS) { }
  22. /// Create a vector from 4 integer components
  23. JPH_INLINE UVec4(uint32 inX, uint32 inY, uint32 inZ, uint32 inW);
  24. /// Comparison
  25. JPH_INLINE bool operator == (UVec4Arg inV2) const;
  26. JPH_INLINE bool operator != (UVec4Arg inV2) const { return !(*this == inV2); }
  27. /// Swizzle the elements in inV
  28. template<uint32 SwizzleX, uint32 SwizzleY, uint32 SwizzleZ, uint32 SwizzleW>
  29. JPH_INLINE UVec4 Swizzle() const;
  30. /// Vector with all zeros
  31. static JPH_INLINE UVec4 sZero();
  32. /// Replicate int inV across all components
  33. static JPH_INLINE UVec4 sReplicate(uint32 inV);
  34. /// Load 1 int from memory and place it in the X component, zeros Y, Z and W
  35. static JPH_INLINE UVec4 sLoadInt(const uint32 *inV);
  36. /// Load 4 ints from memory
  37. static JPH_INLINE UVec4 sLoadInt4(const uint32 *inV);
  38. /// Load 4 ints from memory, aligned to 16 bytes
  39. static JPH_INLINE UVec4 sLoadInt4Aligned(const uint32 *inV);
  40. /// Gather 4 ints from memory at inBase + inOffsets[i] * Scale
  41. template <const int Scale>
  42. static JPH_INLINE UVec4 sGatherInt4(const uint32 *inBase, UVec4Arg inOffsets);
  43. /// Return the minimum value of each of the components
  44. static JPH_INLINE UVec4 sMin(UVec4Arg inV1, UVec4Arg inV2);
  45. /// Return the maximum of each of the components
  46. static JPH_INLINE UVec4 sMax(UVec4Arg inV1, UVec4Arg inV2);
  47. /// Equals (component wise)
  48. static JPH_INLINE UVec4 sEquals(UVec4Arg inV1, UVec4Arg inV2);
  49. /// Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of inControl = 1
  50. static JPH_INLINE UVec4 sSelect(UVec4Arg inV1, UVec4Arg inV2, UVec4Arg inControl);
  51. /// Logical or (component wise)
  52. static JPH_INLINE UVec4 sOr(UVec4Arg inV1, UVec4Arg inV2);
  53. /// Logical xor (component wise)
  54. static JPH_INLINE UVec4 sXor(UVec4Arg inV1, UVec4Arg inV2);
  55. /// Logical and (component wise)
  56. static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2);
  57. /// Logical not (component wise)
  58. static JPH_INLINE UVec4 sNot(UVec4Arg inV1);
  59. /// Sorts the elements in inIndex so that the values that correspond to trues in inValue are the first elements.
  60. /// The remaining elements will be set to inValue.w.
  61. /// I.e. if inValue = (true, false, true, false) and inIndex = (1, 2, 3, 4) the function returns (1, 3, 4, 4).
  62. static JPH_INLINE UVec4 sSort4True(UVec4Arg inValue, UVec4Arg inIndex);
  63. /// Get individual components
  64. #if defined(JPH_USE_SSE)
  65. JPH_INLINE uint32 GetX() const { return (uint32)_mm_cvtsi128_si32(mValue); }
  66. JPH_INLINE uint32 GetY() const { return mU32[1]; }
  67. JPH_INLINE uint32 GetZ() const { return mU32[2]; }
  68. JPH_INLINE uint32 GetW() const { return mU32[3]; }
  69. #elif defined(JPH_USE_NEON)
  70. JPH_INLINE uint32 GetX() const { return vgetq_lane_u32(mValue, 0); }
  71. JPH_INLINE uint32 GetY() const { return vgetq_lane_u32(mValue, 1); }
  72. JPH_INLINE uint32 GetZ() const { return vgetq_lane_u32(mValue, 2); }
  73. JPH_INLINE uint32 GetW() const { return vgetq_lane_u32(mValue, 3); }
  74. #else
  75. JPH_INLINE uint32 GetX() const { return mU32[0]; }
  76. JPH_INLINE uint32 GetY() const { return mU32[1]; }
  77. JPH_INLINE uint32 GetZ() const { return mU32[2]; }
  78. JPH_INLINE uint32 GetW() const { return mU32[3]; }
  79. #endif
  80. /// Set individual components
  81. JPH_INLINE void SetX(uint32 inX) { mU32[0] = inX; }
  82. JPH_INLINE void SetY(uint32 inY) { mU32[1] = inY; }
  83. JPH_INLINE void SetZ(uint32 inZ) { mU32[2] = inZ; }
  84. JPH_INLINE void SetW(uint32 inW) { mU32[3] = inW; }
  85. /// Get component by index
  86. JPH_INLINE uint32 operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 4); return mU32[inCoordinate]; }
  87. JPH_INLINE uint32 & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 4); return mU32[inCoordinate]; }
  88. /// Multiplies each of the 4 integer components with an integer (discards any overflow)
  89. JPH_INLINE UVec4 operator * (UVec4Arg inV2) const;
  90. /// Adds an integer value to all integer components (discards any overflow)
  91. JPH_INLINE UVec4 operator + (UVec4Arg inV2);
  92. /// Add two integer vectors (component wise)
  93. JPH_INLINE UVec4 & operator += (UVec4Arg inV2);
  94. /// Replicate the X component to all components
  95. JPH_INLINE UVec4 SplatX() const;
  96. /// Replicate the Y component to all components
  97. JPH_INLINE UVec4 SplatY() const;
  98. /// Replicate the Z component to all components
  99. JPH_INLINE UVec4 SplatZ() const;
  100. /// Replicate the W component to all components
  101. JPH_INLINE UVec4 SplatW() const;
  102. /// Convert each component from an int to a float
  103. JPH_INLINE Vec4 ToFloat() const;
  104. /// Reinterpret UVec4 as a Vec4 (doesn't change the bits)
  105. JPH_INLINE Vec4 ReinterpretAsFloat() const;
  106. /// Store 4 ints to memory
  107. JPH_INLINE void StoreInt4(uint32 *outV) const;
  108. /// Store 4 ints to memory, aligned to 16 bytes
  109. JPH_INLINE void StoreInt4Aligned(uint32 *outV) const;
  110. /// Test if any of the components are true (true is when highest bit of component is set)
  111. JPH_INLINE bool TestAnyTrue() const;
  112. /// Test if any of X, Y or Z components are true (true is when highest bit of component is set)
  113. JPH_INLINE bool TestAnyXYZTrue() const;
  114. /// Test if all components are true (true is when highest bit of component is set)
  115. JPH_INLINE bool TestAllTrue() const;
  116. /// Test if X, Y and Z components are true (true is when highest bit of component is set)
  117. JPH_INLINE bool TestAllXYZTrue() const;
  118. /// Count the number of components that are true (true is when highest bit of component is set)
  119. JPH_INLINE int CountTrues() const;
  120. /// Store if X is true in bit 0, Y in bit 1, Z in bit 2 and W in bit 3 (true is when highest bit of component is set)
  121. JPH_INLINE int GetTrues() const;
  122. /// Shift all components by Count bits to the left (filling with zeros from the left)
  123. template <const uint Count>
  124. JPH_INLINE UVec4 LogicalShiftLeft() const;
  125. /// Shift all components by Count bits to the right (filling with zeros from the right)
  126. template <const uint Count>
  127. JPH_INLINE UVec4 LogicalShiftRight() const;
  128. /// Shift all components by Count bits to the right (shifting in the value of the highest bit)
  129. template <const uint Count>
  130. JPH_INLINE UVec4 ArithmeticShiftRight() const;
  131. /// Takes the lower 4 16 bits and expands them to X, Y, Z and W
  132. JPH_INLINE UVec4 Expand4Uint16Lo() const;
  133. /// Takes the upper 4 16 bits and expands them to X, Y, Z and W
  134. JPH_INLINE UVec4 Expand4Uint16Hi() const;
  135. /// Takes byte 0 .. 3 and expands them to X, Y, Z and W
  136. JPH_INLINE UVec4 Expand4Byte0() const;
  137. /// Takes byte 4 .. 7 and expands them to X, Y, Z and W
  138. JPH_INLINE UVec4 Expand4Byte4() const;
  139. /// Takes byte 8 .. 11 and expands them to X, Y, Z and W
  140. JPH_INLINE UVec4 Expand4Byte8() const;
  141. /// Takes byte 12 .. 15 and expands them to X, Y, Z and W
  142. JPH_INLINE UVec4 Expand4Byte12() const;
  143. /// Shift vector components by 4 - Count floats to the left, so if Count = 1 the resulting vector is (W, 0, 0, 0), when Count = 3 the resulting vector is (Y, Z, W, 0)
  144. JPH_INLINE UVec4 ShiftComponents4Minus(int inCount) const;
  145. /// To String
  146. friend ostream & operator << (ostream &inStream, UVec4Arg inV)
  147. {
  148. inStream << inV.mU32[0] << ", " << inV.mU32[1] << ", " << inV.mU32[2] << ", " << inV.mU32[3];
  149. return inStream;
  150. }
  151. union
  152. {
  153. Type mValue;
  154. uint32 mU32[4];
  155. };
  156. private:
  157. static const UVec4 sFourMinusXShuffle[];
  158. };
  159. static_assert(is_trivial<UVec4>(), "Is supposed to be a trivial type!");
  160. JPH_NAMESPACE_END
  161. #include "UVec4.inl"