GCC Middle and Back End API Reference
sreal.h
Go to the documentation of this file.
1/* Definitions for simple data type for real numbers.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
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
8Software Foundation; either version 3, or (at your option) any later
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
17along 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#define SREAL_PART_BITS 31
24
25#define UINT64_BITS 64
26
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)
29#define SREAL_MAX_EXP (INT_MAX / 4)
30
31#define SREAL_BITS SREAL_PART_BITS
32
33#define SREAL_SIGN(v) (v < 0 ? -1: 1)
34#define SREAL_ABS(v) (v < 0 ? -v: v)
35
36struct output_block;
37class lto_input_block;
38
39/* Structure for holding a simple real number. */
40class sreal
41{
42public:
43 /* Construct an uninitialized sreal. */
44 sreal () : m_sig (-1), m_exp (-1) {}
45
46 /* Construct a sreal. */
47 sreal (int64_t sig, int exp = 0)
48 {
49 normalize (sig, exp);
50 }
51
52 void dump (FILE *) const;
53 int64_t to_int () const;
54 int64_t to_nearest_int () const;
55 double to_double () const;
56 void stream_out (struct output_block *);
57 static sreal stream_in (class lto_input_block *);
58 sreal operator+ (const sreal &other) const;
59 sreal operator- (const sreal &other) const;
60 sreal operator* (const sreal &other) const;
61 sreal operator/ (const sreal &other) const;
62
63 bool operator< (const sreal &other) const
64 {
65 if (m_exp == other.m_exp)
66 return m_sig < other.m_sig;
67 else
68 {
69 bool negative = m_sig < 0;
70 bool other_negative = other.m_sig < 0;
71
73 return negative > other_negative;
74
75 bool r = m_exp < other.m_exp;
76 return negative ? !r : r;
77 }
78 }
79
80 bool operator== (const sreal &other) const
81 {
82 return m_exp == other.m_exp && m_sig == other.m_sig;
83 }
84
86 {
87 sreal tmp = *this;
88 tmp.m_sig *= -1;
89
90 return tmp;
91 }
92
93 sreal shift (int s) const
94 {
95 /* Zero needs no shifting. */
96 if (!m_sig)
97 return *this;
100
101 /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
102 need to do so. */
105
106 sreal tmp = *this;
107 tmp.m_exp += s;
108
109 return tmp;
110 }
111
112 /* Global minimum sreal can hold. */
113 inline static sreal min ()
114 {
115 sreal min;
116 /* This never needs normalization. */
119 return min;
120 }
121
122 /* Global minimum sreal can hold. */
123 inline static sreal max ()
124 {
125 sreal max;
126 /* This never needs normalization. */
129 return max;
130 }
131
132private:
133 inline void normalize (int64_t new_sig, signed int new_exp);
134 inline void normalize_up (int64_t new_sig, signed int new_exp);
135 inline void normalize_down (int64_t new_sig, signed int new_exp);
136 void shift_right (int amount);
137 static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
138 static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);
139
140 int32_t m_sig; /* Significant. */
141 signed int m_exp; /* Exponent. */
142};
143
144extern void debug (const sreal &ref);
145extern void debug (const sreal *ptr);
146
147inline sreal &operator+= (sreal &a, const sreal &b)
148{
149 return a = a + b;
150}
151
152inline sreal &operator-= (sreal &a, const sreal &b)
153{
154 return a = a - b;
155}
156
157inline sreal &operator/= (sreal &a, const sreal &b)
158{
159 return a = a / b;
160}
161
162inline sreal &operator*= (sreal &a, const sreal &b)
163{
164 return a = a * b;
165}
166
167inline bool operator!= (const sreal &a, const sreal &b)
168{
169 return !(a == b);
170}
171
172inline bool operator> (const sreal &a, const sreal &b)
173{
174 return !(a == b || a < b);
175}
176
177inline bool operator<= (const sreal &a, const sreal &b)
178{
179 return a < b || a == b;
180}
181
182inline bool operator>= (const sreal &a, const sreal &b)
183{
184 return a == b || a > b;
185}
186
187inline sreal operator<< (const sreal &a, int exp)
188{
189 return a.shift (exp);
190}
191
192inline sreal operator>> (const sreal &a, int exp)
193{
194 return a.shift (-exp);
195}
196
197/* Make significant to be >= SREAL_MIN_SIG.
198
199 Make this separate method so inliner can handle hot path better. */
200
201inline void
203{
204 unsigned HOST_WIDE_INT sig = absu_hwi (new_sig);
205 int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);
206
208 sig <<= shift;
209 new_exp -= shift;
211
212 /* Check underflow. */
213 if (new_exp < -SREAL_MAX_EXP)
214 {
216 sig = 0;
217 }
218 m_exp = new_exp;
219 if (SREAL_SIGN (new_sig) == -1)
220 m_sig = -sig;
221 else
222 m_sig = sig;
223}
224
225/* Make significant to be <= SREAL_MAX_SIG.
226
227 Make this separate method so inliner can handle hot path better. */
228
229inline void
231{
232 int last_bit;
233 unsigned HOST_WIDE_INT sig = absu_hwi (new_sig);
234 int shift = floor_log2 (sig) - SREAL_PART_BITS + 2;
235
237 last_bit = (sig >> (shift-1)) & 1;
238 sig >>= shift;
239 new_exp += shift;
241
242 /* Round the number. */
243 sig += last_bit;
244 if (sig > SREAL_MAX_SIG)
245 {
246 sig >>= 1;
247 new_exp++;
248 }
249
250 /* Check overflow. */
252 {
254 sig = SREAL_MAX_SIG;
255 }
256 m_exp = new_exp;
257 if (SREAL_SIGN (new_sig) == -1)
258 m_sig = -sig;
259 else
260 m_sig = sig;
261}
262
263/* Normalize *this; the hot path. */
264
265inline void
267{
268 unsigned HOST_WIDE_INT sig = absu_hwi (new_sig);
269
270 if (sig == 0)
271 {
272 m_sig = 0;
274 }
275 else if (sig > SREAL_MAX_SIG)
277 else if (sig < SREAL_MIN_SIG)
279 else
280 {
281 m_sig = new_sig;
282 m_exp = new_exp;
283 }
284}
285
286#endif
Definition lto-streamer.h:342
Definition sreal.h:41
static sreal stream_in(class lto_input_block *)
Definition sreal.cc:280
void dump(FILE *) const
Definition sreal.cc:65
void normalize_up(int64_t new_sig, signed int new_exp)
Definition sreal.h:202
sreal operator+(const sreal &other) const
Definition sreal.cc:157
static sreal max()
Definition sreal.h:123
void stream_out(struct output_block *)
Definition sreal.cc:271
int32_t m_sig
Definition sreal.h:140
sreal()
Definition sreal.h:44
int64_t to_int() const
Definition sreal.cc:108
void shift_right(int amount)
Definition sreal.cc:90
static sreal signedless_plus(const sreal &a, const sreal &b, bool negative)
int64_t to_nearest_int() const
Definition sreal.cc:126
bool operator==(const sreal &other) const
Definition sreal.h:80
void normalize_down(int64_t new_sig, signed int new_exp)
Definition sreal.h:230
sreal(int64_t sig, int exp=0)
Definition sreal.h:47
sreal operator*(const sreal &other) const
Definition sreal.cc:241
double to_double() const
Definition sreal.cc:146
sreal shift(int s) const
Definition sreal.h:93
static sreal signedless_minus(const sreal &a, const sreal &b, bool negative)
static sreal min()
Definition sreal.h:113
sreal operator/(const sreal &other) const
Definition sreal.cc:259
signed int m_exp
Definition sreal.h:141
void normalize(int64_t new_sig, signed int new_exp)
Definition sreal.h:266
bool operator<(const sreal &other) const
Definition sreal.h:63
sreal operator-() const
Definition sreal.h:85
bool debug
Definition collect-utils.cc:34
double exp(double)
unsigned int shift
Definition ggc-page.cc:233
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
int floor_log2(unsigned HOST_WIDE_INT x)
Definition hwint.cc:36
unsigned HOST_WIDE_INT absu_hwi(HOST_WIDE_INT)
Definition hwint.h:352
poly_int< N, C > r
Definition poly-int.h:770
Ca const poly_int< N, Cb > & b
Definition poly-int.h:767
Ca & a
Definition poly-int.h:766
bool operator!=(const sreal &a, const sreal &b)
Definition sreal.h:167
#define SREAL_SIGN(v)
Definition sreal.h:33
sreal operator>>(const sreal &a, int exp)
Definition sreal.h:192
sreal & operator/=(sreal &a, const sreal &b)
Definition sreal.h:157
#define SREAL_MAX_SIG
Definition sreal.h:28
bool operator<=(const sreal &a, const sreal &b)
Definition sreal.h:177
sreal & operator*=(sreal &a, const sreal &b)
Definition sreal.h:162
sreal operator<<(const sreal &a, int exp)
Definition sreal.h:187
bool operator>(const sreal &a, const sreal &b)
Definition sreal.h:172
#define SREAL_PART_BITS
Definition sreal.h:23
#define SREAL_MAX_EXP
Definition sreal.h:29
sreal & operator-=(sreal &a, const sreal &b)
Definition sreal.h:152
#define SREAL_MIN_SIG
Definition sreal.h:27
sreal & operator+=(sreal &a, const sreal &b)
Definition sreal.h:147
bool operator>=(const sreal &a, const sreal &b)
Definition sreal.h:182
Definition lto-streamer.h:699
#define gcc_checking_assert(EXPR)
Definition system.h:828