UVec4.h 7.6 KB

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