sreal.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Definitions for simple data type for real numbers.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_SREAL_H
  16. #define GCC_SREAL_H
  17. /* SREAL_PART_BITS has to be an even number. */
  18. #define SREAL_PART_BITS 32
  19. #define UINT64_BITS 64
  20. #define SREAL_MIN_SIG ((int64_t) 1 << (SREAL_PART_BITS - 2))
  21. #define SREAL_MAX_SIG (((int64_t) 1 << (SREAL_PART_BITS - 1)) - 1)
  22. #define SREAL_MAX_EXP (INT_MAX / 4)
  23. #define SREAL_BITS SREAL_PART_BITS
  24. /* Structure for holding a simple real number. */
  25. class sreal
  26. {
  27. public:
  28. /* Construct an uninitialized sreal. */
  29. sreal () : m_sig (-1), m_exp (-1) {}
  30. /* Construct a sreal. */
  31. sreal (int64_t sig, int exp = 0) : m_sig (sig), m_exp (exp)
  32. {
  33. normalize ();
  34. }
  35. void dump (FILE *) const;
  36. int64_t to_int () const;
  37. double to_double () const;
  38. sreal operator+ (const sreal &other) const;
  39. sreal operator- (const sreal &other) const;
  40. sreal operator* (const sreal &other) const;
  41. sreal operator/ (const sreal &other) const;
  42. bool operator< (const sreal &other) const
  43. {
  44. if (m_exp == other.m_exp)
  45. return m_sig < other.m_sig;
  46. else
  47. {
  48. bool negative = m_sig < 0;
  49. bool other_negative = other.m_sig < 0;
  50. if (negative != other_negative)
  51. return negative > other_negative;
  52. bool r = m_exp < other.m_exp;
  53. return negative ? !r : r;
  54. }
  55. }
  56. bool operator== (const sreal &other) const
  57. {
  58. return m_exp == other.m_exp && m_sig == other.m_sig;
  59. }
  60. sreal operator- () const
  61. {
  62. sreal tmp = *this;
  63. tmp.m_sig *= -1;
  64. return tmp;
  65. }
  66. sreal shift (int s) const
  67. {
  68. /* Zero needs no shifting. */
  69. if (!m_sig)
  70. return *this;
  71. gcc_checking_assert (s <= SREAL_MAX_EXP);
  72. gcc_checking_assert (s >= -SREAL_MAX_EXP);
  73. /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
  74. need to do so. */
  75. gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
  76. gcc_checking_assert (m_exp + s >= -SREAL_MAX_EXP);
  77. sreal tmp = *this;
  78. tmp.m_exp += s;
  79. return tmp;
  80. }
  81. /* Global minimum sreal can hold. */
  82. inline static sreal min ()
  83. {
  84. static sreal min = sreal (-SREAL_MAX_SIG, SREAL_MAX_EXP);
  85. return min;
  86. }
  87. /* Global minimum sreal can hold. */
  88. inline static sreal max ()
  89. {
  90. static sreal max = sreal (SREAL_MAX_SIG, SREAL_MAX_EXP);
  91. return max;
  92. }
  93. private:
  94. inline void normalize ();
  95. inline void normalize_up ();
  96. inline void normalize_down ();
  97. void shift_right (int amount);
  98. static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
  99. static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);
  100. int64_t m_sig; /* Significant. */
  101. signed int m_exp; /* Exponent. */
  102. };
  103. extern void debug (const sreal &ref);
  104. extern void debug (const sreal *ptr);
  105. inline sreal &operator+= (sreal &a, const sreal &b)
  106. {
  107. return a = a + b;
  108. }
  109. inline sreal &operator-= (sreal &a, const sreal &b)
  110. {
  111. return a = a - b;
  112. }
  113. inline sreal &operator/= (sreal &a, const sreal &b)
  114. {
  115. return a = a / b;
  116. }
  117. inline sreal &operator*= (sreal &a, const sreal &b)
  118. {
  119. return a = a * b;
  120. }
  121. inline bool operator!= (const sreal &a, const sreal &b)
  122. {
  123. return !(a == b);
  124. }
  125. inline bool operator> (const sreal &a, const sreal &b)
  126. {
  127. return !(a == b || a < b);
  128. }
  129. inline bool operator<= (const sreal &a, const sreal &b)
  130. {
  131. return a < b || a == b;
  132. }
  133. inline bool operator>= (const sreal &a, const sreal &b)
  134. {
  135. return a == b || a > b;
  136. }
  137. inline sreal operator<< (const sreal &a, int exp)
  138. {
  139. return a.shift (exp);
  140. }
  141. inline sreal operator>> (const sreal &a, int exp)
  142. {
  143. return a.shift (-exp);
  144. }
  145. /* Make significant to be >= SREAL_MIN_SIG.
  146. Make this separate method so inliner can handle hot path better. */
  147. inline void
  148. sreal::normalize_up ()
  149. {
  150. int64_t s = m_sig < 0 ? -1 : 1;
  151. unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  152. int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);
  153. gcc_checking_assert (shift > 0);
  154. sig <<= shift;
  155. m_exp -= shift;
  156. gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);
  157. /* Check underflow. */
  158. if (m_exp < -SREAL_MAX_EXP)
  159. {
  160. m_exp = -SREAL_MAX_EXP;
  161. sig = 0;
  162. }
  163. if (s == -1)
  164. m_sig = -sig;
  165. else
  166. m_sig = sig;
  167. }
  168. /* Make significant to be <= SREAL_MAX_SIG.
  169. Make this separate method so inliner can handle hot path better. */
  170. inline void
  171. sreal::normalize_down ()
  172. {
  173. int64_t s = m_sig < 0 ? -1 : 1;
  174. int last_bit;
  175. unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  176. int shift = floor_log2 (sig) - SREAL_PART_BITS + 2;
  177. gcc_checking_assert (shift > 0);
  178. last_bit = (sig >> (shift-1)) & 1;
  179. sig >>= shift;
  180. m_exp += shift;
  181. gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);
  182. /* Round the number. */
  183. sig += last_bit;
  184. if (sig > SREAL_MAX_SIG)
  185. {
  186. sig >>= 1;
  187. m_exp++;
  188. }
  189. /* Check overflow. */
  190. if (m_exp > SREAL_MAX_EXP)
  191. {
  192. m_exp = SREAL_MAX_EXP;
  193. sig = SREAL_MAX_SIG;
  194. }
  195. if (s == -1)
  196. m_sig = -sig;
  197. else
  198. m_sig = sig;
  199. }
  200. /* Normalize *this; the hot path. */
  201. inline void
  202. sreal::normalize ()
  203. {
  204. unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  205. if (sig == 0)
  206. m_exp = -SREAL_MAX_EXP;
  207. else if (sig > SREAL_MAX_SIG)
  208. normalize_down ();
  209. else if (sig < SREAL_MIN_SIG)
  210. normalize_up ();
  211. }
  212. #endif