sreal.h 6.4 KB

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