noSimd.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. // Functions used to simplify template programming when using functions both with and without simd.h.
  24. #ifndef DFPSR_NO_SIMD
  25. #define DFPSR_NO_SIMD
  26. #include <stdint.h>
  27. #include <cmath>
  28. #include "SafePointer.h"
  29. #include "DsrTraits.h"
  30. namespace dsr {
  31. // Type conversions.
  32. inline int32_t truncateToI32(float value) { return (int32_t)value; }
  33. inline uint32_t truncateToU32(float value) { return (uint32_t)value; }
  34. inline float floatFromI32(int32_t value) { return (float)value; }
  35. inline float floatFromU32(uint32_t value) { return (float)value; }
  36. inline int32_t I32FromU32(uint32_t value) { return (int32_t)value; }
  37. inline uint32_t U32FromI32(int32_t value) { return (uint32_t)value; }
  38. // Memory read operations.
  39. inline uint32_t gather_U32(dsr::SafePointer<const uint32_t> data, const uint32_t &elementOffset) { return data[elementOffset]; }
  40. inline int32_t gather_I32(dsr::SafePointer<const int32_t> data, const uint32_t &elementOffset) { return data[elementOffset]; }
  41. inline float gather_F32(dsr::SafePointer<const float> data, const uint32_t &elementOffset) { return data[elementOffset]; }
  42. // Comparisons between all lanes, which is one lane for scalar types.
  43. inline bool allLanesEqual (const uint8_t& left, const uint8_t& right) { return left == right; }
  44. inline bool allLanesEqual (const uint16_t& left, const uint16_t& right) { return left == right; }
  45. inline bool allLanesEqual (const uint32_t& left, const uint32_t& right) { return left == right; }
  46. inline bool allLanesEqual (const int32_t& left, const int32_t& right) { return left == right; }
  47. inline bool allLanesEqual (const float& left, const float& right) { return abs(left - right) < 0.0001f; }
  48. inline bool allLanesNotEqual (const uint8_t& left, const uint8_t& right) { return left != right; }
  49. inline bool allLanesNotEqual (const uint16_t& left, const uint16_t& right) { return left != right; }
  50. inline bool allLanesNotEqual (const uint32_t& left, const uint32_t& right) { return left != right; }
  51. inline bool allLanesNotEqual (const int32_t& left, const int32_t& right) { return left != right; }
  52. inline bool allLanesNotEqual (const float& left, const float& right) { return abs(left - right) >= 0.0001f; }
  53. inline bool allLanesGreater (const uint8_t& left, const uint8_t& right) { return left > right; }
  54. inline bool allLanesGreater (const uint16_t& left, const uint16_t& right) { return left > right; }
  55. inline bool allLanesGreater (const uint32_t& left, const uint32_t& right) { return left > right; }
  56. inline bool allLanesGreater (const int32_t& left, const int32_t& right) { return left > right; }
  57. inline bool allLanesGreater (const float& left, const float& right) { return left > right; }
  58. inline bool allLanesGreaterOrEqual(const uint8_t& left, const uint8_t& right) { return left >= right; }
  59. inline bool allLanesGreaterOrEqual(const uint16_t& left, const uint16_t& right) { return left >= right; }
  60. inline bool allLanesGreaterOrEqual(const uint32_t& left, const uint32_t& right) { return left >= right; }
  61. inline bool allLanesGreaterOrEqual(const int32_t& left, const int32_t& right) { return left >= right; }
  62. inline bool allLanesGreaterOrEqual(const float& left, const float& right) { return left >= right; }
  63. inline bool allLanesLesser (const uint8_t& left, const uint8_t& right) { return left < right; }
  64. inline bool allLanesLesser (const uint16_t& left, const uint16_t& right) { return left < right; }
  65. inline bool allLanesLesser (const uint32_t& left, const uint32_t& right) { return left < right; }
  66. inline bool allLanesLesser (const int32_t& left, const int32_t& right) { return left < right; }
  67. inline bool allLanesLesser (const float& left, const float& right) { return left < right; }
  68. inline bool allLanesLesserOrEqual (const uint8_t& left, const uint8_t& right) { return left <= right; }
  69. inline bool allLanesLesserOrEqual (const uint16_t& left, const uint16_t& right) { return left <= right; }
  70. inline bool allLanesLesserOrEqual (const uint32_t& left, const uint32_t& right) { return left <= right; }
  71. inline bool allLanesLesserOrEqual (const int32_t& left, const int32_t& right) { return left <= right; }
  72. inline bool allLanesLesserOrEqual (const float& left, const float& right) { return left <= right; }
  73. template <uint32_t bitOffset>
  74. inline uint32_t bitShiftLeftImmediate(const uint32_t& left) {
  75. static_assert(bitOffset < 32u, "Immediate left shift of 32-bit values may not shift more than 31 bits!");
  76. return left << bitOffset;
  77. }
  78. template <uint32_t bitOffset>
  79. inline uint32_t bitShiftRightImmediate(const uint32_t& left) {
  80. static_assert(bitOffset < 32u, "Immediate right shift of 32-bit values may not shift more than 31 bits!");
  81. return left >> bitOffset;
  82. }
  83. template <uint16_t bitOffset>
  84. inline uint16_t bitShiftLeftImmediate(const uint16_t& left) {
  85. static_assert(bitOffset < 16u, "Immediate left shift of 16-bit values may not shift more than 15 bits!");
  86. return left << bitOffset;
  87. }
  88. template <uint16_t bitOffset>
  89. inline uint16_t bitShiftRightImmediate(const uint16_t& left) {
  90. static_assert(bitOffset < 16u, "Immediate right shift of 16-bit values may not shift more than 15 bits!");
  91. return left >> bitOffset;
  92. }
  93. template <uint8_t bitOffset>
  94. inline uint8_t bitShiftLeftImmediate(const uint8_t& left) {
  95. static_assert(bitOffset < 8u, "Immediate left shift of 8-bit values may not shift more than 7 bits!");
  96. return left << bitOffset;
  97. }
  98. template <uint8_t bitOffset>
  99. inline uint8_t bitShiftRightImmediate(const uint8_t& left) {
  100. static_assert(bitOffset < 8u, "Immediate right shift of 8-bit values may not shift more than 7 bits!");
  101. return left >> bitOffset;
  102. }
  103. // A minimum function that can take more than two arguments.
  104. // Post-condition: Returns the smallest of all given values, which must be comparable using the < operator and have the same type.
  105. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  106. inline T min(const T &a, const T &b) {
  107. return (a < b) ? a : b;
  108. }
  109. template <typename T, typename... TAIL, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  110. inline T min(const T &a, const T &b, TAIL... tail) {
  111. return min(min(a, b), tail...);
  112. }
  113. // A maximum function that can take more than two arguments.
  114. // Post-condition: Returns the largest of all given values, which must be comparable using the > operator and have the same type.
  115. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  116. inline T max(const T &a, const T &b) {
  117. return (a > b) ? a : b;
  118. }
  119. template <typename T, typename... TAIL, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  120. inline T max(const T &a, const T &b, TAIL... tail) {
  121. return max(max(a, b), tail...);
  122. }
  123. // TODO: Implement min and max for integer vectors in simd.h.
  124. // Start by implementing vectorized comparisons and blend functions as a fallback for unsupported types.
  125. // Pre-condition: minValue <= maxValue
  126. // Post-condition: Returns value clamped from minValue to maxValue.
  127. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any, T))>
  128. inline T clamp(const T &minValue, const T &value, const T &maxValue) {
  129. return max(minValue, min(value, maxValue));
  130. }
  131. // Post-condition: Returns value clamped to minValue.
  132. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any, T))>
  133. inline T clampLower(const T &minValue, const T &value) {
  134. return max(minValue, value);
  135. }
  136. // Post-condition: Returns value clamped to maxValue.
  137. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any, T))>
  138. inline T clampUpper(const T &value, const T &maxValue) {
  139. return min(value, maxValue);
  140. }
  141. inline float reciprocal(float value) { return 1.0f / value; }
  142. inline float reciprocalSquareRoot(float value) { return 1.0f / sqrt(value); }
  143. inline float squareRoot(float value) { return sqrt(value); }
  144. // TODO: Add more functions from simd.h.
  145. }
  146. #endif