FixedPoint.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2019 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_FIXED_POINT
  25. #define DFPSR_FIXED_POINT
  26. #include "../api/stringAPI.h"
  27. namespace dsr {
  28. // One extra unit in early clamping allows fractions to extend the range further
  29. // int16_t goes from -32768 to +32767, but when having additional fractions, one can get close to the -32769 to 32768 range
  30. inline void clampForSaturatedWhole(int64_t& value) {
  31. if (value > 32768) { value = 32768; }
  32. if (value < -32769) { value = -32769; }
  33. }
  34. inline void clampForInt32(int64_t& value) {
  35. if (value > 2147483647) { value = 2147483647; }
  36. if (value < -2147483648) { value = -2147483648; }
  37. }
  38. // A deterministic saturated fixed point number for graphics and virtual machines.
  39. // Uses 16-bits for whole signed integers and 16-bits for the remaining 1/65536 fractions.
  40. // The fromMantissa constructor can be used to store 32-bit indices directly in the mantissa.
  41. // If used as a value, the index is taken as 1/65536 fractions.
  42. // Retreive correctly using getMantissa.
  43. // Default initialized to zero for convenience.
  44. struct FixedPoint {
  45. private:
  46. int32_t mantissa = 0;
  47. public:
  48. FixedPoint();
  49. explicit FixedPoint(int64_t newMantissa);
  50. static FixedPoint fromWhole(int64_t wholeInteger);
  51. static FixedPoint fromMantissa(int64_t mantissa);
  52. static FixedPoint zero();
  53. static FixedPoint epsilon();
  54. static FixedPoint half();
  55. static FixedPoint one();
  56. static FixedPoint fromText(const ReadableString& content);
  57. inline int64_t getMantissa() const {
  58. return (int64_t)this->mantissa;
  59. }
  60. };
  61. String& string_toStreamIndented(String& target, const FixedPoint& value, const ReadableString& indentation);
  62. // Addition and subtraction is faster against its own type, by being in the same scale
  63. inline FixedPoint operator+(const FixedPoint &left, const FixedPoint &right) {
  64. return FixedPoint(left.getMantissa() + right.getMantissa());
  65. }
  66. inline FixedPoint operator+(const FixedPoint &left, int32_t right) {
  67. return FixedPoint(left.getMantissa() + (right * 65536));
  68. }
  69. inline FixedPoint operator+(int32_t left, const FixedPoint &right) {
  70. return FixedPoint((left * 65536) + right.getMantissa());
  71. }
  72. inline FixedPoint operator-(const FixedPoint &left, const FixedPoint &right) {
  73. return FixedPoint(left.getMantissa() - right.getMantissa());
  74. }
  75. inline FixedPoint operator-(const FixedPoint &left, int32_t right) {
  76. return FixedPoint(left.getMantissa() - (right * 65536));
  77. }
  78. inline FixedPoint operator-(int32_t left, const FixedPoint &right) {
  79. return FixedPoint((left * 65536) - right.getMantissa());
  80. }
  81. // Multiplication is faster against whole integers, by not having to reduce the result
  82. inline FixedPoint operator*(const FixedPoint &left, const FixedPoint &right) {
  83. return FixedPoint((left.getMantissa() * right.getMantissa()) / 65536);
  84. }
  85. inline FixedPoint operator*(const FixedPoint &left, int64_t right) {
  86. clampForSaturatedWhole(right);
  87. return FixedPoint(left.getMantissa() * right);
  88. }
  89. inline FixedPoint operator*(int64_t left, const FixedPoint &right) {
  90. clampForSaturatedWhole(left);
  91. return FixedPoint(left * right.getMantissa());
  92. }
  93. int32_t fixedPoint_round(const FixedPoint& value);
  94. double fixedPoint_approximate(const FixedPoint& value);
  95. FixedPoint fixedPoint_min(const FixedPoint &left, const FixedPoint &right);
  96. FixedPoint fixedPoint_max(const FixedPoint &left, const FixedPoint &right);
  97. FixedPoint fixedPoint_divide(const FixedPoint &left, const FixedPoint &right);
  98. FixedPoint fixedPoint_divide(const FixedPoint &left, int64_t right);
  99. inline FixedPoint operator/(const FixedPoint &left, const FixedPoint &right) {
  100. return fixedPoint_divide(left, right);
  101. }
  102. inline FixedPoint operator/(const FixedPoint &left, int64_t right) {
  103. return fixedPoint_divide(left, right);
  104. }
  105. inline FixedPoint operator/(int64_t left, const FixedPoint &right) {
  106. return fixedPoint_divide(FixedPoint::fromWhole(left), right);
  107. }
  108. // Gets the real element of value's square root.
  109. // Because square roots of negative numbers are only using the imaginary dimension, this results in zero for all non-positive inputs.
  110. FixedPoint fixedPoint_squareRoot(const FixedPoint& value);
  111. inline bool operator==(const FixedPoint &left, const FixedPoint &right) {
  112. return left.getMantissa() == right.getMantissa();
  113. }
  114. inline bool operator==(const FixedPoint &left, int64_t right) {
  115. return left.getMantissa() == right * 65536;
  116. }
  117. inline bool operator==(int64_t left, const FixedPoint &right) {
  118. return left * 65536 == right.getMantissa();
  119. }
  120. inline bool operator!=(const FixedPoint &left, const FixedPoint &right) {
  121. return left.getMantissa() != right.getMantissa();
  122. }
  123. inline bool operator!=(const FixedPoint &left, int64_t right) {
  124. return left.getMantissa() != right * 65536;
  125. }
  126. inline bool operator!=(int64_t left, const FixedPoint &right) {
  127. return left * 65536 != right.getMantissa();
  128. }
  129. inline bool operator>(const FixedPoint &left, const FixedPoint &right) {
  130. return left.getMantissa() > right.getMantissa();
  131. }
  132. inline bool operator>(const FixedPoint &left, int64_t right) {
  133. return left.getMantissa() > right * 65536;
  134. }
  135. inline bool operator>(int64_t left, const FixedPoint &right) {
  136. return left * 65536 > right.getMantissa();
  137. }
  138. inline bool operator<(const FixedPoint &left, const FixedPoint &right) {
  139. return left.getMantissa() < right.getMantissa();
  140. }
  141. inline bool operator<(const FixedPoint &left, int64_t right) {
  142. return left.getMantissa() < right * 65536;
  143. }
  144. inline bool operator<(int64_t left, const FixedPoint &right) {
  145. return left * 65536 < right.getMantissa();
  146. }
  147. inline bool operator>=(const FixedPoint &left, const FixedPoint &right) {
  148. return left.getMantissa() >= right.getMantissa();
  149. }
  150. inline bool operator>=(const FixedPoint &left, int64_t right) {
  151. return left.getMantissa() >= right * 65536;
  152. }
  153. inline bool operator>=(int64_t left, const FixedPoint &right) {
  154. return left * 65536 >= right.getMantissa();
  155. }
  156. inline bool operator<=(const FixedPoint &left, const FixedPoint &right) {
  157. return left.getMantissa() <= right.getMantissa();
  158. }
  159. inline bool operator<=(const FixedPoint &left, int64_t right) {
  160. return left.getMantissa() <= right * 65536;
  161. }
  162. inline bool operator<=(int64_t left, const FixedPoint &right) {
  163. return left * 65536 <= right.getMantissa();
  164. }
  165. }
  166. #endif