FixedPoint.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // zlib open source license
  2. //
  3. // Copyright (c) 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_FIXED_POINT
  24. #define DFPSR_FIXED_POINT
  25. #include "../base/text.h"
  26. namespace dsr {
  27. // One extra unit in early clamping allows fractions to extend the range further
  28. // int16_t goes from -32768 to +32767, but when having additional fractions, one can get close to the -32769 to 32768 range
  29. inline void clampForSaturatedWhole(int64_t& value) {
  30. if (value > 32768) { value = 32768; }
  31. if (value < -32769) { value = -32769; }
  32. }
  33. inline void clampForInt32(int64_t& value) {
  34. if (value > 2147483647) { value = 2147483647; }
  35. if (value < -2147483648) { value = -2147483648; }
  36. }
  37. // A deterministic saturated fixed point number for graphics and virtual machines.
  38. // Uses 16-bits for whole signed integers and 16-bits for the remaining 1/65536 fractions.
  39. // The fromMantissa constructor can be used to store 32-bit indices directly in the mantissa.
  40. // If used as a value, the index is taken as 1/65536 fractions.
  41. // Retreive correctly using getMantissa.
  42. // Default initialized to zero for convenience.
  43. struct FixedPoint {
  44. private:
  45. int32_t mantissa = 0;
  46. public:
  47. FixedPoint();
  48. explicit FixedPoint(int64_t newMantissa);
  49. static FixedPoint fromWhole(int64_t wholeInteger);
  50. static FixedPoint fromMantissa(int64_t mantissa);
  51. static FixedPoint zero();
  52. static FixedPoint epsilon();
  53. static FixedPoint half();
  54. static FixedPoint one();
  55. static FixedPoint fromText(const ReadableString& content);
  56. inline int64_t getMantissa() const {
  57. return (int64_t)this->mantissa;
  58. }
  59. };
  60. String& string_toStreamIndented(String& target, const FixedPoint& value, const ReadableString& indentation);
  61. // Addition and subtraction is faster against its own type, by being in the same scale
  62. inline FixedPoint operator+(const FixedPoint &left, const FixedPoint &right) {
  63. return FixedPoint(left.getMantissa() + right.getMantissa());
  64. }
  65. inline FixedPoint operator+(const FixedPoint &left, int32_t right) {
  66. return FixedPoint(left.getMantissa() + (right * 65536));
  67. }
  68. inline FixedPoint operator+(int32_t left, const FixedPoint &right) {
  69. return FixedPoint((left * 65536) + right.getMantissa());
  70. }
  71. inline FixedPoint operator-(const FixedPoint &left, const FixedPoint &right) {
  72. return FixedPoint(left.getMantissa() - right.getMantissa());
  73. }
  74. inline FixedPoint operator-(const FixedPoint &left, int32_t right) {
  75. return FixedPoint(left.getMantissa() - (right * 65536));
  76. }
  77. inline FixedPoint operator-(int32_t left, const FixedPoint &right) {
  78. return FixedPoint((left * 65536) - right.getMantissa());
  79. }
  80. // Multiplication is faster against whole integers, by not having to reduce the result
  81. inline FixedPoint operator*(const FixedPoint &left, const FixedPoint &right) {
  82. return FixedPoint((left.getMantissa() * right.getMantissa()) / 65536);
  83. }
  84. inline FixedPoint operator*(const FixedPoint &left, int64_t right) {
  85. clampForSaturatedWhole(right);
  86. return FixedPoint(left.getMantissa() * right);
  87. }
  88. inline FixedPoint operator*(int64_t left, const FixedPoint &right) {
  89. clampForSaturatedWhole(left);
  90. return FixedPoint(left * right.getMantissa());
  91. }
  92. int32_t fixedPoint_round(const FixedPoint& value);
  93. double fixedPoint_approximate(const FixedPoint& value);
  94. FixedPoint fixedPoint_min(const FixedPoint &left, const FixedPoint &right);
  95. FixedPoint fixedPoint_max(const FixedPoint &left, const FixedPoint &right);
  96. FixedPoint fixedPoint_divide(const FixedPoint &left, const FixedPoint &right);
  97. FixedPoint fixedPoint_divide(const FixedPoint &left, int64_t right);
  98. inline FixedPoint operator/(const FixedPoint &left, const FixedPoint &right) {
  99. return fixedPoint_divide(left, right);
  100. }
  101. inline FixedPoint operator/(const FixedPoint &left, int64_t right) {
  102. return fixedPoint_divide(left, right);
  103. }
  104. inline FixedPoint operator/(int64_t left, const FixedPoint &right) {
  105. return fixedPoint_divide(FixedPoint::fromWhole(left), right);
  106. }
  107. // Gets the real element of value's square root.
  108. // Because square roots of negative numbers are only using the imaginary dimension, this results in zero for all non-positive inputs.
  109. FixedPoint fixedPoint_squareRoot(const FixedPoint& value);
  110. inline bool operator==(const FixedPoint &left, const FixedPoint &right) {
  111. return left.getMantissa() == right.getMantissa();
  112. }
  113. inline bool operator==(const FixedPoint &left, int64_t right) {
  114. return left.getMantissa() == right * 65536;
  115. }
  116. inline bool operator==(int64_t left, const FixedPoint &right) {
  117. return left * 65536 == right.getMantissa();
  118. }
  119. inline bool operator!=(const FixedPoint &left, const FixedPoint &right) {
  120. return left.getMantissa() != right.getMantissa();
  121. }
  122. inline bool operator!=(const FixedPoint &left, int64_t right) {
  123. return left.getMantissa() != right * 65536;
  124. }
  125. inline bool operator!=(int64_t left, const FixedPoint &right) {
  126. return left * 65536 != right.getMantissa();
  127. }
  128. inline bool operator>(const FixedPoint &left, const FixedPoint &right) {
  129. return left.getMantissa() > right.getMantissa();
  130. }
  131. inline bool operator>(const FixedPoint &left, int64_t right) {
  132. return left.getMantissa() > right * 65536;
  133. }
  134. inline bool operator>(int64_t left, const FixedPoint &right) {
  135. return left * 65536 > right.getMantissa();
  136. }
  137. inline bool operator<(const FixedPoint &left, const FixedPoint &right) {
  138. return left.getMantissa() < right.getMantissa();
  139. }
  140. inline bool operator<(const FixedPoint &left, int64_t right) {
  141. return left.getMantissa() < right * 65536;
  142. }
  143. inline bool operator<(int64_t left, const FixedPoint &right) {
  144. return left * 65536 < right.getMantissa();
  145. }
  146. inline bool operator>=(const FixedPoint &left, const FixedPoint &right) {
  147. return left.getMantissa() >= right.getMantissa();
  148. }
  149. inline bool operator>=(const FixedPoint &left, int64_t right) {
  150. return left.getMantissa() >= right * 65536;
  151. }
  152. inline bool operator>=(int64_t left, const FixedPoint &right) {
  153. return left * 65536 >= right.getMantissa();
  154. }
  155. inline bool operator<=(const FixedPoint &left, const FixedPoint &right) {
  156. return left.getMantissa() <= right.getMantissa();
  157. }
  158. inline bool operator<=(const FixedPoint &left, int64_t right) {
  159. return left.getMantissa() <= right * 65536;
  160. }
  161. inline bool operator<=(int64_t left, const FixedPoint &right) {
  162. return left * 65536 <= right.getMantissa();
  163. }
  164. }
  165. #endif