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