FixedPoint.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. inline FixedPoint operator-(const FixedPoint& value) {
  82. return FixedPoint(0 - value.getMantissa());
  83. }
  84. // Multiplication is faster against whole integers, by not having to reduce the result
  85. inline FixedPoint operator*(const FixedPoint &left, const FixedPoint &right) {
  86. return FixedPoint((left.getMantissa() * right.getMantissa()) / 65536);
  87. }
  88. inline FixedPoint operator*(const FixedPoint &left, int64_t right) {
  89. clampForSaturatedWhole(right);
  90. return FixedPoint(left.getMantissa() * right);
  91. }
  92. inline FixedPoint operator*(int64_t left, const FixedPoint &right) {
  93. clampForSaturatedWhole(left);
  94. return FixedPoint(left * right.getMantissa());
  95. }
  96. // Returns value rounded to the closest integer.
  97. int32_t fixedPoint_round(const FixedPoint& value);
  98. // Returns a floating-point approximation of value.
  99. double fixedPoint_approximate(const FixedPoint& value);
  100. FixedPoint fixedPoint_min(const FixedPoint &left, const FixedPoint &right);
  101. FixedPoint fixedPoint_max(const FixedPoint &left, const FixedPoint &right);
  102. FixedPoint fixedPoint_divide(const FixedPoint &left, const FixedPoint &right);
  103. FixedPoint fixedPoint_divide(const FixedPoint &left, int64_t right);
  104. inline FixedPoint operator/(const FixedPoint &left, const FixedPoint &right) {
  105. return fixedPoint_divide(left, right);
  106. }
  107. inline FixedPoint operator/(const FixedPoint &left, int64_t right) {
  108. return fixedPoint_divide(left, right);
  109. }
  110. inline FixedPoint operator/(int64_t left, const FixedPoint &right) {
  111. return fixedPoint_divide(FixedPoint::fromWhole(left), right);
  112. }
  113. // Gets the real element of value's square root.
  114. // Because square roots of negative numbers are only using the imaginary dimension, this results in zero for all non-positive inputs.
  115. FixedPoint fixedPoint_squareRoot(const FixedPoint& value);
  116. inline bool operator==(const FixedPoint &left, const FixedPoint &right) {
  117. return left.getMantissa() == right.getMantissa();
  118. }
  119. inline bool operator==(const FixedPoint &left, int64_t right) {
  120. return left.getMantissa() == right * 65536;
  121. }
  122. inline bool operator==(int64_t left, const FixedPoint &right) {
  123. return left * 65536 == right.getMantissa();
  124. }
  125. inline bool operator!=(const FixedPoint &left, const FixedPoint &right) {
  126. return left.getMantissa() != right.getMantissa();
  127. }
  128. inline bool operator!=(const FixedPoint &left, int64_t right) {
  129. return left.getMantissa() != right * 65536;
  130. }
  131. inline bool operator!=(int64_t left, const FixedPoint &right) {
  132. return left * 65536 != right.getMantissa();
  133. }
  134. inline bool operator>(const FixedPoint &left, const FixedPoint &right) {
  135. return left.getMantissa() > right.getMantissa();
  136. }
  137. inline bool operator>(const FixedPoint &left, int64_t right) {
  138. return left.getMantissa() > right * 65536;
  139. }
  140. inline bool operator>(int64_t left, const FixedPoint &right) {
  141. return left * 65536 > right.getMantissa();
  142. }
  143. inline bool operator<(const FixedPoint &left, const FixedPoint &right) {
  144. return left.getMantissa() < right.getMantissa();
  145. }
  146. inline bool operator<(const FixedPoint &left, int64_t right) {
  147. return left.getMantissa() < right * 65536;
  148. }
  149. inline bool operator<(int64_t left, const FixedPoint &right) {
  150. return left * 65536 < right.getMantissa();
  151. }
  152. inline bool operator>=(const FixedPoint &left, const FixedPoint &right) {
  153. return left.getMantissa() >= right.getMantissa();
  154. }
  155. inline bool operator>=(const FixedPoint &left, int64_t right) {
  156. return left.getMantissa() >= right * 65536;
  157. }
  158. inline bool operator>=(int64_t left, const FixedPoint &right) {
  159. return left * 65536 >= right.getMantissa();
  160. }
  161. inline bool operator<=(const FixedPoint &left, const FixedPoint &right) {
  162. return left.getMantissa() <= right.getMantissa();
  163. }
  164. inline bool operator<=(const FixedPoint &left, int64_t right) {
  165. return left.getMantissa() <= right * 65536;
  166. }
  167. inline bool operator<=(int64_t left, const FixedPoint &right) {
  168. return left * 65536 <= right.getMantissa();
  169. }
  170. }
  171. #endif