BsSIMD.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Math/BsVector4.h"
  6. #include "Math/BsAABox.h"
  7. #include "Math/BsSphere.h"
  8. #define SIMDPP_ARCH_X86_SSE4_1
  9. #if BS_COMPILER == BS_COMPILER_MSVC
  10. #pragma warning(disable: 4244)
  11. #endif
  12. #include "ThirdParty/simdpp/simd.h"
  13. #if BS_COMPILER == BS_COMPILER_MSVC
  14. #pragma warning(default: 4244)
  15. #endif
  16. namespace bs
  17. {
  18. namespace simd
  19. {
  20. using namespace simdpp;
  21. /** @addtogroup Math
  22. * @{
  23. */
  24. /**
  25. * Version of bs::AABox suitable for SIMD use. Takes up a bit more memory than standard AABox and is always 16-byte
  26. * aligned.
  27. */
  28. struct AABox
  29. {
  30. /** Center of the bounds, W component unused. */
  31. SIMDPP_ALIGN(16) Vector4 center;
  32. /** Extents (half-size) of the bounds, W component unused. */
  33. SIMDPP_ALIGN(16) Vector4 extents;
  34. AABox()
  35. { }
  36. /** Initializes bounds from an AABox. */
  37. AABox(const bs::AABox& box)
  38. {
  39. center = Vector4(box.getCenter());
  40. extents = Vector4(box.getHalfSize());
  41. }
  42. /** Initializes bounds from a Sphere. */
  43. AABox(const Sphere& sphere)
  44. {
  45. center = Vector4(sphere.getCenter());
  46. float radius = sphere.getRadius();
  47. extents = Vector4(radius, radius, radius, 0.0f);
  48. }
  49. /** Initializes bounds from a vector representing the center and equal extents in all directions. */
  50. AABox(const Vector3& center, float extent)
  51. {
  52. this->center = Vector4(center);
  53. extents = Vector4(extent, extent, extent, 0.0f);
  54. }
  55. /** Returns true if the current bounds object intersects the provided object. */
  56. bool intersects(const AABox& other) const
  57. {
  58. auto myCenter = load<float32x4>(&center);
  59. auto otherCenter = load<float32x4>(&other.center);
  60. float32x4 diff = abs(sub(myCenter, otherCenter));
  61. auto myExtents = simd::load<float32x4>(&extents);
  62. auto otherExtents = simd::load<float32x4>(&other.extents);
  63. float32x4 extents = add(myExtents, otherExtents);
  64. return test_bits_any(bit_cast<uint32x4>(cmp_gt(diff, extents))) == false;
  65. }
  66. };
  67. /** @} */
  68. }
  69. }