scalar.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2023 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. #ifndef DFPSR_MATH_SCALAR
  24. #define DFPSR_MATH_SCALAR
  25. #include <cmath>
  26. namespace dsr {
  27. // A minimum function that can take more than two arguments.
  28. // Post-condition: Returns the smallest of all given values, which must be comparable using the < operator and have the same type.
  29. template <typename T>
  30. inline T min(const T &a, const T &b) {
  31. return (a < b) ? a : b;
  32. }
  33. template <typename T, typename... TAIL>
  34. inline T min(const T &a, const T &b, TAIL... tail) {
  35. return min(min(a, b), tail...);
  36. }
  37. // A maximum function that can take more than two arguments.
  38. // Post-condition: Returns the largest of all given values, which must be comparable using the > operator and have the same type.
  39. template <typename T>
  40. inline T max(const T &a, const T &b) {
  41. return (a > b) ? a : b;
  42. }
  43. template <typename T, typename... TAIL>
  44. inline T max(const T &a, const T &b, TAIL... tail) {
  45. return max(max(a, b), tail...);
  46. }
  47. // Preconditions:
  48. // 0 <= a <= 255
  49. // 0 <= b <= 255
  50. // Postconditions:
  51. // Returns the normalized multiplication of a and b, where the 0..255 range represents decimal values from 0.0 to 1.0.
  52. // The result may not be less than zero or larger than any of the inputs.
  53. // Examples:
  54. // mulByte_8(0, 0) = 0
  55. // mulByte_8(x, 0) = 0
  56. // mulByte_8(0, x) = 0
  57. // mulByte_8(x, 255) = x
  58. // mulByte_8(255, x) = x
  59. // mulByte_8(255, 255) = 255
  60. static inline uint32_t mulByte_8(uint32_t a, uint32_t b) {
  61. // Approximate the reciprocal of an unsigned byte's maximum value 255 for normalization
  62. // 256³ / 255 ≈ 65793
  63. // Truncation goes down, so add half a unit before rounding to get the closest value
  64. // 2^24 / 2 = 8388608
  65. // No overflow for unsigned 32-bit integers
  66. // 255² * 65793 + 8388608 = 4286578433 < 2^32
  67. return (a * b * 65793 + 8388608) >> 24;
  68. }
  69. // Returns a modulo b where 0 <= a < b
  70. inline int signedModulo(int a, int b) {
  71. int result = 0;
  72. if (b > 0) {
  73. if (a >= 0) {
  74. result = a % b; // Simple modulo
  75. } else {
  76. result = (b - (-a % b)) % b; // Negative modulo
  77. }
  78. }
  79. return result;
  80. }
  81. inline int roundUp(int size, int alignment) {
  82. return size + (alignment - 1) - signedModulo(size - 1, alignment);
  83. }
  84. inline int roundDown(int size, int alignment) {
  85. return size - signedModulo(size, alignment);
  86. }
  87. inline float absDiff(float a, float b) {
  88. float result = a - b;
  89. if (result < 0.0f) {
  90. result = -result;
  91. }
  92. return result;
  93. }
  94. inline uint8_t absDiff(uint8_t a, uint8_t b) {
  95. int result = (int)a - (int)b;
  96. if (result < 0) {
  97. result = -result;
  98. }
  99. return (uint8_t)result;
  100. }
  101. inline uint16_t absDiff(uint16_t a, uint16_t b) {
  102. int result = (int)a - (int)b;
  103. if (result < 0) {
  104. result = -result;
  105. }
  106. return (uint16_t)result;
  107. }
  108. // Allowing compilation on older C++ versions
  109. // Only use for trivial types if you want to avoid cloning and destruction
  110. template <typename T>
  111. inline void swap(T &a, T &b) {
  112. T temp = a;
  113. a = b;
  114. b = temp;
  115. }
  116. // More compact than min(a, b) when reading from the target
  117. template <typename T>
  118. inline void replaceWithSmaller(T& target, T source) {
  119. if (source < target) {
  120. target = source;
  121. }
  122. }
  123. // More compact than max(a, b) when reading from the target
  124. template <typename T>
  125. inline void replaceWithLarger(T& target, T source) {
  126. if (source > target) {
  127. target = source;
  128. }
  129. }
  130. // True iff high and low bytes are equal
  131. // Equivalent to value % 257 == 0 because A + B * 256 = A * 257 when A = B.
  132. inline bool isUniformByteU16(uint16_t value) {
  133. return (value & 0x00FF) == ((value & 0xFF00) >> 8);
  134. }
  135. // A special rounding used for triangle rasterization
  136. inline int64_t safeRoundInt64(float value) {
  137. int64_t result = floor(value);
  138. if (value <= -1048576.0f || value >= 1048576.0f) { result = 0; }
  139. if (value < 0.0f) { result--; }
  140. return result;
  141. }
  142. }
  143. #endif