scalar.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "../../base/noSimd.h"
  26. namespace dsr {
  27. // Returns a modulo b where 0 <= a < b
  28. template <typename I, typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_SignedInteger, I) && DSR_CHECK_PROPERTY(DsrTrait_Scalar_Integer, U))>
  29. inline int32_t signedModulo(I a, U b) {
  30. if (a >= 0) {
  31. return a % b; // Simple modulo
  32. } else {
  33. return (b - (-a % b)) % b; // Negative modulo
  34. }
  35. }
  36. template <typename I, typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_SignedInteger, I) && DSR_CHECK_PROPERTY(DsrTrait_Scalar_Integer, U))>
  37. inline I roundUp(I size, U alignment) {
  38. return size + (alignment - 1) - signedModulo(size - 1, alignment);
  39. }
  40. template <typename I, typename U, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_SignedInteger, I) && DSR_CHECK_PROPERTY(DsrTrait_Scalar_Integer, U))>
  41. inline I roundDown(I size, U alignment) {
  42. return size - signedModulo(size, alignment);
  43. }
  44. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar_Floating, T))>
  45. inline T absDiff(T a, T b) {
  46. float result = a - b;
  47. if (result < 0.0f) {
  48. result = -result;
  49. }
  50. return result;
  51. }
  52. inline uint8_t absDiff(uint8_t a, uint8_t b) {
  53. int32_t result = (int32_t)a - (int32_t)b;
  54. if (result < 0) {
  55. result = -result;
  56. }
  57. return (uint8_t)result;
  58. }
  59. inline uint16_t absDiff(uint16_t a, uint16_t b) {
  60. int32_t result = (int32_t)a - (int32_t)b;
  61. if (result < 0) {
  62. result = -result;
  63. }
  64. return (uint16_t)result;
  65. }
  66. // Only use this for trivial types, use std::swap for objects with non-trivial construction.
  67. template <typename T>
  68. inline void swap(T &a, T &b) {
  69. T temp = a;
  70. a = b;
  71. b = temp;
  72. }
  73. // More compact than min(a, b) when reading from the target
  74. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  75. inline void replaceWithSmaller(T &target, const T &source) {
  76. if (source < target) {
  77. target = source;
  78. }
  79. }
  80. // More compact than max(a, b) when reading from the target
  81. template <typename T, DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Scalar, T))>
  82. inline void replaceWithLarger(T &target, const T &source) {
  83. if (source > target) {
  84. target = source;
  85. }
  86. }
  87. }
  88. #endif