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