sreal.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Definitions for simple data type for real numbers.
  2. Copyright (C) 2002-2018 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. #define SREAL_SIGN(v) (v < 0 ? -1: 1)
  25. #define SREAL_ABS(v) (v < 0 ? -v: v)
  26. struct output_block;
  27. struct lto_input_block;
  28. /* Structure for holding a simple real number. */
  29. class sreal
  30. {
  31. public:
  32. /* Construct an uninitialized sreal. */
  33. sreal () : m_sig (-1), m_exp (-1) {}
  34. /* Construct a sreal. */
  35. sreal (int64_t sig, int exp = 0) : m_sig (sig), m_exp (exp)
  36. {
  37. normalize ();
  38. }
  39. void dump (FILE *) const;
  40. int64_t to_int () const;
  41. double to_double () const;
  42. void stream_out (struct output_block *);
  43. static sreal stream_in (struct lto_input_block *);
  44. sreal operator+ (const sreal &other) const;
  45. sreal operator- (const sreal &other) const;
  46. sreal operator* (const sreal &other) const;
  47. sreal operator/ (const sreal &other) const;
  48. bool operator< (const sreal &other) const
  49. {
  50. if (m_exp == other.m_exp)
  51. return m_sig < other.m_sig;
  52. else
  53. {
  54. bool negative = m_sig < 0;
  55. bool other_negative = other.m_sig < 0;
  56. if (negative != other_negative)
  57. return negative > other_negative;
  58. bool r = m_exp < other.m_exp;
  59. return negative ? !r : r;
  60. }
  61. }
  62. bool operator== (const sreal &other) const
  63. {
  64. return m_exp == other.m_exp && m_sig == other.m_sig;
  65. }
  66. sreal operator- () const
  67. {
  68. sreal tmp = *this;
  69. tmp.m_sig *= -1;
  70. return tmp;
  71. }
  72. sreal shift (int s) const
  73. {
  74. /* Zero needs no shifting. */
  75. if (!m_sig)
  76. return *this;
  77. gcc_checking_assert (s <= SREAL_MAX_EXP);
  78. gcc_checking_assert (s >= -SREAL_MAX_EXP);
  79. /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
  80. need to do so. */
  81. gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
  82. gcc_checking_assert (m_exp + s >= -SREAL_MAX_EXP);
  83. sreal tmp = *this;
  84. tmp.m_exp += s;
  85. return tmp;
  86. }
  87. /* Global minimum sreal can hold. */
  88. inline static sreal min ()
  89. {
  90. sreal min;
  91. /* This never needs normalization. */
  92. min.m_sig = -SREAL_MAX_SIG;
  93. min.m_exp = SREAL_MAX_EXP;
  94. return min;
  95. }
  96. /* Global minimum sreal can hold. */
  97. inline static sreal max ()
  98. {
  99. sreal max;
  100. /* This never needs normalization. */
  101. max.m_sig = SREAL_MAX_SIG;
  102. max.m_exp = SREAL_MAX_EXP;
  103. return max;
  104. }
  105. private:
  106. inline void normalize ();
  107. inline void normalize_up ();
  108. inline void normalize_down ();
  109. void shift_right (int amount);
  110. static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
  111. static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);
  112. int64_t m_sig; /* Significant. */
  113. signed int m_exp; /* Exponent. */
  114. };
  115. extern void debug (const sreal &ref);
  116. extern void debug (const sreal *ptr);
  117. inline sreal &operator+= (sreal &a, const sreal &b)
  118. {
  119. return a = a + b;
  120. }
  121. inline sreal &operator-= (sreal &a, const sreal &b)
  122. {
  123. return a = a - b;
  124. }
  125. inline sreal &operator/= (sreal &a, const sreal &b)
  126. {
  127. return a = a / b;
  128. }
  129. inline sreal &operator*= (sreal &a, const sreal &b)
  130. {
  131. return a = a * b;
  132. }
  133. inline bool operator!= (const sreal &a, const sreal &b)
  134. {
  135. return !(a == b);
  136. }
  137. inline bool operator> (const sreal &a, const sreal &b)
  138. {
  139. return !(a == b || a < b);
  140. }
  141. inline bool operator<= (const sreal &a, const sreal &b)
  142. {
  143. return a < b || a == b;
  144. }
  145. inline bool operator>= (const sreal &a, const sreal &b)
  146. {
  147. return a == b || a > b;
  148. }
  149. inline sreal operator<< (const sreal &a, int exp)
  150. {
  151. return a.shift (exp);
  152. }
  153. inline sreal operator>> (const sreal &a, int exp)
  154. {
  155. return a.shift (-exp);
  156. }
  157. /* Make significant to be >= SREAL_MIN_SIG.
  158. Make this separate method so inliner can handle hot path better. */
  159. inline void
  160. sreal::normalize_up ()
  161. {
  162. unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  163. int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);
  164. gcc_checking_assert (shift > 0);
  165. sig <<= shift;
  166. m_exp -= shift;
  167. gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);
  168. /* Check underflow. */
  169. if (m_exp < -SREAL_MAX_EXP)
  170. {
  171. m_exp = -SREAL_MAX_EXP;
  172. sig = 0;
  173. }
  174. if (SREAL_SIGN (m_sig) == -1)
  175. m_sig = -sig;
  176. else
  177. m_sig = sig;
  178. }
  179. /* Make significant to be <= SREAL_MAX_SIG.
  180. Make this separate method so inliner can handle hot path better. */
  181. inline void
  182. sreal::normalize_down ()
  183. {
  184. int last_bit;
  185. unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  186. int shift = floor_log2 (sig) - SREAL_PART_BITS + 2;
  187. gcc_checking_assert (shift > 0);
  188. last_bit = (sig >> (shift-1)) & 1;
  189. sig >>= shift;
  190. m_exp += shift;
  191. gcc_checking_assert (sig <= SREAL_MAX_SIG && sig >= SREAL_MIN_SIG);
  192. /* Round the number. */
  193. sig += last_bit;
  194. if (sig > SREAL_MAX_SIG)
  195. {
  196. sig >>= 1;
  197. m_exp++;
  198. }
  199. /* Check overflow. */
  200. if (m_exp > SREAL_MAX_EXP)
  201. {
  202. m_exp = SREAL_MAX_EXP;
  203. sig = SREAL_MAX_SIG;
  204. }
  205. if (SREAL_SIGN (m_sig) == -1)
  206. m_sig = -sig;
  207. else
  208. m_sig = sig;
  209. }
  210. /* Normalize *this; the hot path. */
  211. inline void
  212. sreal::normalize ()
  213. {
  214. unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
  215. if (sig == 0)
  216. m_exp = -SREAL_MAX_EXP;
  217. else if (sig > SREAL_MAX_SIG)
  218. normalize_down ();
  219. else if (sig < SREAL_MIN_SIG)
  220. normalize_up ();
  221. }
  222. #endif