BVec16.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. /// A vector consisting of 16 bytes
  7. class [[nodiscard]] alignas(JPH_VECTOR_ALIGNMENT) BVec16
  8. {
  9. public:
  10. JPH_OVERRIDE_NEW_DELETE
  11. // Underlying vector type
  12. #if defined(JPH_USE_SSE)
  13. using Type = __m128i;
  14. #elif defined(JPH_USE_NEON)
  15. using Type = uint8x16_t;
  16. #else
  17. using Type = struct { uint64 mData[2]; };
  18. #endif
  19. /// Constructor
  20. BVec16() = default; ///< Intentionally not initialized for performance reasons
  21. BVec16(const BVec16 &inRHS) = default;
  22. BVec16 & operator = (const BVec16 &inRHS) = default;
  23. JPH_INLINE BVec16(Type inRHS) : mValue(inRHS) { }
  24. /// Create a vector from 16 bytes
  25. JPH_INLINE BVec16(uint8 inB0, uint8 inB1, uint8 inB2, uint8 inB3, uint8 inB4, uint8 inB5, uint8 inB6, uint8 inB7, uint8 inB8, uint8 inB9, uint8 inB10, uint8 inB11, uint8 inB12, uint8 inB13, uint8 inB14, uint8 inB15);
  26. /// Create a vector from two uint64's
  27. JPH_INLINE BVec16(uint64 inV0, uint64 inV1);
  28. /// Comparison
  29. JPH_INLINE bool operator == (BVec16Arg inV2) const;
  30. JPH_INLINE bool operator != (BVec16Arg inV2) const { return !(*this == inV2); }
  31. /// Vector with all zeros
  32. static JPH_INLINE BVec16 sZero();
  33. /// Replicate int inV across all components
  34. static JPH_INLINE BVec16 sReplicate(uint8 inV);
  35. /// Load 16 bytes from memory
  36. static JPH_INLINE BVec16 sLoadByte16(const uint8 *inV);
  37. /// Equals (component wise), highest bit of each component that is set is considered true
  38. static JPH_INLINE BVec16 sEquals(BVec16Arg inV1, BVec16Arg inV2);
  39. /// Logical or (component wise)
  40. static JPH_INLINE BVec16 sOr(BVec16Arg inV1, BVec16Arg inV2);
  41. /// Logical xor (component wise)
  42. static JPH_INLINE BVec16 sXor(BVec16Arg inV1, BVec16Arg inV2);
  43. /// Logical and (component wise)
  44. static JPH_INLINE BVec16 sAnd(BVec16Arg inV1, BVec16Arg inV2);
  45. /// Logical not (component wise)
  46. static JPH_INLINE BVec16 sNot(BVec16Arg inV1);
  47. /// Get component by index
  48. JPH_INLINE uint8 operator [] (uint inCoordinate) const { JPH_ASSERT(inCoordinate < 16); return mU8[inCoordinate]; }
  49. JPH_INLINE uint8 & operator [] (uint inCoordinate) { JPH_ASSERT(inCoordinate < 16); return mU8[inCoordinate]; }
  50. /// Test if any of the components are true (true is when highest bit of component is set)
  51. JPH_INLINE bool TestAnyTrue() const;
  52. /// Test if all components are true (true is when highest bit of component is set)
  53. JPH_INLINE bool TestAllTrue() const;
  54. /// Store if mU8[0] is true in bit 0, mU8[1] in bit 1, etc. (true is when highest bit of component is set)
  55. JPH_INLINE int GetTrues() const;
  56. /// To String
  57. friend ostream & operator << (ostream &inStream, BVec16Arg inV)
  58. {
  59. inStream << uint(inV.mU8[0]) << ", " << uint(inV.mU8[1]) << ", " << uint(inV.mU8[2]) << ", " << uint(inV.mU8[3]) << ", "
  60. << uint(inV.mU8[4]) << ", " << uint(inV.mU8[5]) << ", " << uint(inV.mU8[6]) << ", " << uint(inV.mU8[7]) << ", "
  61. << uint(inV.mU8[8]) << ", " << uint(inV.mU8[9]) << ", " << uint(inV.mU8[10]) << ", " << uint(inV.mU8[11]) << ", "
  62. << uint(inV.mU8[12]) << ", " << uint(inV.mU8[13]) << ", " << uint(inV.mU8[14]) << ", " << uint(inV.mU8[15]);
  63. return inStream;
  64. }
  65. union
  66. {
  67. Type mValue;
  68. uint8 mU8[16];
  69. uint64 mU64[2];
  70. };
  71. };
  72. static_assert(std::is_trivial<BVec16>(), "Is supposed to be a trivial type!");
  73. JPH_NAMESPACE_END
  74. #include "BVec16.inl"