scalar.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "../base/DsrTraits.h"
  27. namespace dsr {
  28. // A minimum function that can take more than two arguments.
  29. // Post-condition: Returns the smallest of all given values, which must be comparable using the < operator and have the same type.
  30. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  31. inline T min(const T &a, const T &b) {
  32. return (a < b) ? a : b;
  33. }
  34. template <typename T, typename... TAIL, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  35. inline T min(const T &a, const T &b, TAIL... tail) {
  36. return min(min(a, b), tail...);
  37. }
  38. // A maximum function that can take more than two arguments.
  39. // Post-condition: Returns the largest of all given values, which must be comparable using the > operator and have the same type.
  40. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  41. inline T max(const T &a, const T &b) {
  42. return (a > b) ? a : b;
  43. }
  44. template <typename T, typename... TAIL, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  45. inline T max(const T &a, const T &b, TAIL... tail) {
  46. return max(max(a, b), tail...);
  47. }
  48. // Pre-condition: minValue <= maxValue
  49. // Post-condition: Returns value clamped from minValue to maxValue.
  50. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  51. T clamp(const T &minValue, T value, const T &maxValue) {
  52. if (value > maxValue) value = maxValue;
  53. if (value < minValue) value = minValue;
  54. return value;
  55. }
  56. // Returns a modulo b where 0 <= a < b
  57. template <typename I, typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_SignedInteger, I) && DSR_CHECK_PROPERTY(DsrTrait_Scalar_Integer, U))>
  58. inline int32_t signedModulo(I a, U b) {
  59. if (a >= 0) {
  60. return a % b; // Simple modulo
  61. } else {
  62. return (b - (-a % b)) % b; // Negative modulo
  63. }
  64. }
  65. template <typename I, typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_SignedInteger, I) && DSR_CHECK_PROPERTY(DsrTrait_Scalar_Integer, U))>
  66. inline I roundUp(I size, U alignment) {
  67. return size + (alignment - 1) - signedModulo(size - 1, alignment);
  68. }
  69. template <typename I, typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_SignedInteger, I) && DSR_CHECK_PROPERTY(DsrTrait_Scalar_Integer, U))>
  70. inline I roundDown(I size, U alignment) {
  71. return size - signedModulo(size, alignment);
  72. }
  73. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_Floating, T))>
  74. inline T absDiff(T a, T b) {
  75. float result = a - b;
  76. if (result < 0.0f) {
  77. result = -result;
  78. }
  79. return result;
  80. }
  81. inline uint8_t absDiff(uint8_t a, uint8_t b) {
  82. int32_t result = (int32_t)a - (int32_t)b;
  83. if (result < 0) {
  84. result = -result;
  85. }
  86. return (uint8_t)result;
  87. }
  88. inline uint16_t absDiff(uint16_t a, uint16_t b) {
  89. int32_t result = (int32_t)a - (int32_t)b;
  90. if (result < 0) {
  91. result = -result;
  92. }
  93. return (uint16_t)result;
  94. }
  95. // Only use this for trivial types, use std::swap for objects with non-trivial construction.
  96. template <typename T>
  97. inline void swap(T &a, T &b) {
  98. T temp = a;
  99. a = b;
  100. b = temp;
  101. }
  102. // More compact than min(a, b) when reading from the target
  103. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  104. inline void replaceWithSmaller(T &target, const T &source) {
  105. if (source < target) {
  106. target = source;
  107. }
  108. }
  109. // More compact than max(a, b) when reading from the target
  110. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  111. inline void replaceWithLarger(T &target, const T &source) {
  112. if (source > target) {
  113. target = source;
  114. }
  115. }
  116. }
  117. #endif