scalar.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Returns a modulo b where 0 <= a < b
  48. inline int signedModulo(int a, int b) {
  49. int result = 0;
  50. if (b > 0) {
  51. if (a >= 0) {
  52. result = a % b; // Simple modulo
  53. } else {
  54. result = (b - (-a % b)) % b; // Negative modulo
  55. }
  56. }
  57. return result;
  58. }
  59. inline int roundUp(int size, int alignment) {
  60. return size + (alignment - 1) - signedModulo(size - 1, alignment);
  61. }
  62. inline int roundDown(int size, int alignment) {
  63. return size - signedModulo(size, alignment);
  64. }
  65. inline float absDiff(float a, float b) {
  66. float result = a - b;
  67. if (result < 0.0f) {
  68. result = -result;
  69. }
  70. return result;
  71. }
  72. inline uint8_t absDiff(uint8_t a, uint8_t b) {
  73. int result = (int)a - (int)b;
  74. if (result < 0) {
  75. result = -result;
  76. }
  77. return (uint8_t)result;
  78. }
  79. inline uint16_t absDiff(uint16_t a, uint16_t b) {
  80. int result = (int)a - (int)b;
  81. if (result < 0) {
  82. result = -result;
  83. }
  84. return (uint16_t)result;
  85. }
  86. // Allowing compilation on older C++ versions
  87. // Only use for trivial types if you want to avoid cloning and destruction
  88. template <typename T>
  89. inline void swap(T &a, T &b) {
  90. T temp = a;
  91. a = b;
  92. b = temp;
  93. }
  94. // More compact than min(a, b) when reading from the target
  95. template <typename T>
  96. inline void replaceWithSmaller(T& target, T source) {
  97. if (source < target) {
  98. target = source;
  99. }
  100. }
  101. // More compact than max(a, b) when reading from the target
  102. template <typename T>
  103. inline void replaceWithLarger(T& target, T source) {
  104. if (source > target) {
  105. target = source;
  106. }
  107. }
  108. }
  109. #endif