scalar.h 3.9 KB

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