annotate gcc/sreal.h @ 128:fe568345ddd5

fix CbC-example
author mir3636
date Wed, 11 Apr 2018 19:32:28 +0900
parents 04ced10e8804
children 84e7813d76e9
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.
kono
parents: 0
diff changeset
2 Copyright (C) 2002-2017 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
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 /* SREAL_PART_BITS has to be an even number. */
111
kono
parents: 0
diff changeset
24 #define SREAL_PART_BITS 32
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
111
kono
parents: 0
diff changeset
26 #define UINT64_BITS 64
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
111
kono
parents: 0
diff changeset
28 #define SREAL_MIN_SIG ((int64_t) 1 << (SREAL_PART_BITS - 2))
kono
parents: 0
diff changeset
29 #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
30 #define SREAL_MAX_EXP (INT_MAX / 4)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 #define SREAL_BITS SREAL_PART_BITS
111
kono
parents: 0
diff changeset
33
kono
parents: 0
diff changeset
34 #define SREAL_SIGN(v) (v < 0 ? -1: 1)
kono
parents: 0
diff changeset
35 #define SREAL_ABS(v) (v < 0 ? -v: v)
kono
parents: 0
diff changeset
36
kono
parents: 0
diff changeset
37 struct output_block;
kono
parents: 0
diff changeset
38 struct lto_input_block;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 /* Structure for holding a simple real number. */
111
kono
parents: 0
diff changeset
41 class sreal
kono
parents: 0
diff changeset
42 {
kono
parents: 0
diff changeset
43 public:
kono
parents: 0
diff changeset
44 /* Construct an uninitialized sreal. */
kono
parents: 0
diff changeset
45 sreal () : m_sig (-1), m_exp (-1) {}
kono
parents: 0
diff changeset
46
kono
parents: 0
diff changeset
47 /* Construct a sreal. */
kono
parents: 0
diff changeset
48 sreal (int64_t sig, int exp = 0) : m_sig (sig), m_exp (exp)
kono
parents: 0
diff changeset
49 {
kono
parents: 0
diff changeset
50 normalize ();
kono
parents: 0
diff changeset
51 }
kono
parents: 0
diff changeset
52
kono
parents: 0
diff changeset
53 void dump (FILE *) const;
kono
parents: 0
diff changeset
54 int64_t to_int () const;
kono
parents: 0
diff changeset
55 double to_double () const;
kono
parents: 0
diff changeset
56 void stream_out (struct output_block *);
kono
parents: 0
diff changeset
57 static sreal stream_in (struct lto_input_block *);
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 sreal operator/ (const sreal &other) const;
kono
parents: 0
diff changeset
62
kono
parents: 0
diff changeset
63 bool operator< (const sreal &other) const
kono
parents: 0
diff changeset
64 {
kono
parents: 0
diff changeset
65 if (m_exp == other.m_exp)
kono
parents: 0
diff changeset
66 return m_sig < other.m_sig;
kono
parents: 0
diff changeset
67 else
kono
parents: 0
diff changeset
68 {
kono
parents: 0
diff changeset
69 bool negative = m_sig < 0;
kono
parents: 0
diff changeset
70 bool other_negative = other.m_sig < 0;
kono
parents: 0
diff changeset
71
kono
parents: 0
diff changeset
72 if (negative != other_negative)
kono
parents: 0
diff changeset
73 return negative > other_negative;
kono
parents: 0
diff changeset
74
kono
parents: 0
diff changeset
75 bool r = m_exp < other.m_exp;
kono
parents: 0
diff changeset
76 return negative ? !r : r;
kono
parents: 0
diff changeset
77 }
kono
parents: 0
diff changeset
78 }
kono
parents: 0
diff changeset
79
kono
parents: 0
diff changeset
80 bool operator== (const sreal &other) const
kono
parents: 0
diff changeset
81 {
kono
parents: 0
diff changeset
82 return m_exp == other.m_exp && m_sig == other.m_sig;
kono
parents: 0
diff changeset
83 }
kono
parents: 0
diff changeset
84
kono
parents: 0
diff changeset
85 sreal operator- () const
kono
parents: 0
diff changeset
86 {
kono
parents: 0
diff changeset
87 sreal tmp = *this;
kono
parents: 0
diff changeset
88 tmp.m_sig *= -1;
kono
parents: 0
diff changeset
89
kono
parents: 0
diff changeset
90 return tmp;
kono
parents: 0
diff changeset
91 }
kono
parents: 0
diff changeset
92
kono
parents: 0
diff changeset
93 sreal shift (int s) const
kono
parents: 0
diff changeset
94 {
kono
parents: 0
diff changeset
95 /* Zero needs no shifting. */
kono
parents: 0
diff changeset
96 if (!m_sig)
kono
parents: 0
diff changeset
97 return *this;
kono
parents: 0
diff changeset
98 gcc_checking_assert (s <= SREAL_MAX_EXP);
kono
parents: 0
diff changeset
99 gcc_checking_assert (s >= -SREAL_MAX_EXP);
kono
parents: 0
diff changeset
100
kono
parents: 0
diff changeset
101 /* Overflows/drop to 0 could be handled gracefully, but hopefully we do not
kono
parents: 0
diff changeset
102 need to do so. */
kono
parents: 0
diff changeset
103 gcc_checking_assert (m_exp + s <= SREAL_MAX_EXP);
kono
parents: 0
diff changeset
104 gcc_checking_assert (m_exp + s >= -SREAL_MAX_EXP);
kono
parents: 0
diff changeset
105
kono
parents: 0
diff changeset
106 sreal tmp = *this;
kono
parents: 0
diff changeset
107 tmp.m_exp += s;
kono
parents: 0
diff changeset
108
kono
parents: 0
diff changeset
109 return tmp;
kono
parents: 0
diff changeset
110 }
kono
parents: 0
diff changeset
111
kono
parents: 0
diff changeset
112 /* Global minimum sreal can hold. */
kono
parents: 0
diff changeset
113 inline static sreal min ()
kono
parents: 0
diff changeset
114 {
kono
parents: 0
diff changeset
115 sreal min;
kono
parents: 0
diff changeset
116 /* This never needs normalization. */
kono
parents: 0
diff changeset
117 min.m_sig = -SREAL_MAX_SIG;
kono
parents: 0
diff changeset
118 min.m_exp = SREAL_MAX_EXP;
kono
parents: 0
diff changeset
119 return min;
kono
parents: 0
diff changeset
120 }
kono
parents: 0
diff changeset
121
kono
parents: 0
diff changeset
122 /* Global minimum sreal can hold. */
kono
parents: 0
diff changeset
123 inline static sreal max ()
kono
parents: 0
diff changeset
124 {
kono
parents: 0
diff changeset
125 sreal max;
kono
parents: 0
diff changeset
126 /* This never needs normalization. */
kono
parents: 0
diff changeset
127 max.m_sig = SREAL_MAX_SIG;
kono
parents: 0
diff changeset
128 max.m_exp = SREAL_MAX_EXP;
kono
parents: 0
diff changeset
129 return max;
kono
parents: 0
diff changeset
130 }
kono
parents: 0
diff changeset
131
kono
parents: 0
diff changeset
132 private:
kono
parents: 0
diff changeset
133 inline void normalize ();
kono
parents: 0
diff changeset
134 inline void normalize_up ();
kono
parents: 0
diff changeset
135 inline void normalize_down ();
kono
parents: 0
diff changeset
136 void shift_right (int amount);
kono
parents: 0
diff changeset
137 static sreal signedless_plus (const sreal &a, const sreal &b, bool negative);
kono
parents: 0
diff changeset
138 static sreal signedless_minus (const sreal &a, const sreal &b, bool negative);
kono
parents: 0
diff changeset
139
kono
parents: 0
diff changeset
140 int64_t m_sig; /* Significant. */
kono
parents: 0
diff changeset
141 signed int m_exp; /* Exponent. */
kono
parents: 0
diff changeset
142 };
kono
parents: 0
diff changeset
143
kono
parents: 0
diff changeset
144 extern void debug (const sreal &ref);
kono
parents: 0
diff changeset
145 extern void debug (const sreal *ptr);
kono
parents: 0
diff changeset
146
kono
parents: 0
diff changeset
147 inline sreal &operator+= (sreal &a, const sreal &b)
kono
parents: 0
diff changeset
148 {
kono
parents: 0
diff changeset
149 return a = a + b;
kono
parents: 0
diff changeset
150 }
kono
parents: 0
diff changeset
151
kono
parents: 0
diff changeset
152 inline sreal &operator-= (sreal &a, const sreal &b)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 {
111
kono
parents: 0
diff changeset
154 return a = a - b;
kono
parents: 0
diff changeset
155 }
kono
parents: 0
diff changeset
156
kono
parents: 0
diff changeset
157 inline sreal &operator/= (sreal &a, const sreal &b)
kono
parents: 0
diff changeset
158 {
kono
parents: 0
diff changeset
159 return a = a / b;
kono
parents: 0
diff changeset
160 }
kono
parents: 0
diff changeset
161
kono
parents: 0
diff changeset
162 inline sreal &operator*= (sreal &a, const sreal &b)
kono
parents: 0
diff changeset
163 {
kono
parents: 0
diff changeset
164 return a = a * b;
kono
parents: 0
diff changeset
165 }
kono
parents: 0
diff changeset
166
kono
parents: 0
diff changeset
167 inline bool operator!= (const sreal &a, const sreal &b)
kono
parents: 0
diff changeset
168 {
kono
parents: 0
diff changeset
169 return !(a == b);
kono
parents: 0
diff changeset
170 }
kono
parents: 0
diff changeset
171
kono
parents: 0
diff changeset
172 inline bool operator> (const sreal &a, const sreal &b)
kono
parents: 0
diff changeset
173 {
kono
parents: 0
diff changeset
174 return !(a == b || a < b);
kono
parents: 0
diff changeset
175 }
kono
parents: 0
diff changeset
176
kono
parents: 0
diff changeset
177 inline bool operator<= (const sreal &a, const sreal &b)
kono
parents: 0
diff changeset
178 {
kono
parents: 0
diff changeset
179 return a < b || a == b;
kono
parents: 0
diff changeset
180 }
kono
parents: 0
diff changeset
181
kono
parents: 0
diff changeset
182 inline bool operator>= (const sreal &a, const sreal &b)
kono
parents: 0
diff changeset
183 {
kono
parents: 0
diff changeset
184 return a == b || a > b;
kono
parents: 0
diff changeset
185 }
kono
parents: 0
diff changeset
186
kono
parents: 0
diff changeset
187 inline sreal operator<< (const sreal &a, int exp)
kono
parents: 0
diff changeset
188 {
kono
parents: 0
diff changeset
189 return a.shift (exp);
kono
parents: 0
diff changeset
190 }
kono
parents: 0
diff changeset
191
kono
parents: 0
diff changeset
192 inline sreal operator>> (const sreal &a, int exp)
kono
parents: 0
diff changeset
193 {
kono
parents: 0
diff changeset
194 return a.shift (-exp);
kono
parents: 0
diff changeset
195 }
kono
parents: 0
diff changeset
196
kono
parents: 0
diff changeset
197 /* Make significant to be >= SREAL_MIN_SIG.
kono
parents: 0
diff changeset
198
kono
parents: 0
diff changeset
199 Make this separate method so inliner can handle hot path better. */
kono
parents: 0
diff changeset
200
kono
parents: 0
diff changeset
201 inline void
kono
parents: 0
diff changeset
202 sreal::normalize_up ()
kono
parents: 0
diff changeset
203 {
kono
parents: 0
diff changeset
204 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
kono
parents: 0
diff changeset
205 int shift = SREAL_PART_BITS - 2 - floor_log2 (sig);
kono
parents: 0
diff changeset
206
kono
parents: 0
diff changeset
207 gcc_checking_assert (shift > 0);
kono
parents: 0
diff changeset
208 sig <<= shift;
kono
parents: 0
diff changeset
209 m_exp -= shift;
kono
parents: 0
diff changeset
210 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
211
111
kono
parents: 0
diff changeset
212 /* Check underflow. */
kono
parents: 0
diff changeset
213 if (m_exp < -SREAL_MAX_EXP)
kono
parents: 0
diff changeset
214 {
kono
parents: 0
diff changeset
215 m_exp = -SREAL_MAX_EXP;
kono
parents: 0
diff changeset
216 sig = 0;
kono
parents: 0
diff changeset
217 }
kono
parents: 0
diff changeset
218 if (SREAL_SIGN (m_sig) == -1)
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
kono
parents: 0
diff changeset
229 sreal::normalize_down ()
kono
parents: 0
diff changeset
230 {
kono
parents: 0
diff changeset
231 int last_bit;
kono
parents: 0
diff changeset
232 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
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;
kono
parents: 0
diff changeset
238 m_exp += shift;
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;
kono
parents: 0
diff changeset
246 m_exp++;
kono
parents: 0
diff changeset
247 }
kono
parents: 0
diff changeset
248
kono
parents: 0
diff changeset
249 /* Check overflow. */
kono
parents: 0
diff changeset
250 if (m_exp > SREAL_MAX_EXP)
kono
parents: 0
diff changeset
251 {
kono
parents: 0
diff changeset
252 m_exp = SREAL_MAX_EXP;
kono
parents: 0
diff changeset
253 sig = SREAL_MAX_SIG;
kono
parents: 0
diff changeset
254 }
kono
parents: 0
diff changeset
255 if (SREAL_SIGN (m_sig) == -1)
kono
parents: 0
diff changeset
256 m_sig = -sig;
kono
parents: 0
diff changeset
257 else
kono
parents: 0
diff changeset
258 m_sig = sig;
kono
parents: 0
diff changeset
259 }
kono
parents: 0
diff changeset
260
kono
parents: 0
diff changeset
261 /* Normalize *this; the hot path. */
kono
parents: 0
diff changeset
262
kono
parents: 0
diff changeset
263 inline void
kono
parents: 0
diff changeset
264 sreal::normalize ()
kono
parents: 0
diff changeset
265 {
kono
parents: 0
diff changeset
266 unsigned HOST_WIDE_INT sig = absu_hwi (m_sig);
kono
parents: 0
diff changeset
267
kono
parents: 0
diff changeset
268 if (sig == 0)
kono
parents: 0
diff changeset
269 m_exp = -SREAL_MAX_EXP;
kono
parents: 0
diff changeset
270 else if (sig > SREAL_MAX_SIG)
kono
parents: 0
diff changeset
271 normalize_down ();
kono
parents: 0
diff changeset
272 else if (sig < SREAL_MIN_SIG)
kono
parents: 0
diff changeset
273 normalize_up ();
kono
parents: 0
diff changeset
274 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
275
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 #endif