annotate gcc/sreal.h @ 158:494b0b89df80 default tip

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