Mercurial > hg > CbC > CbC_gcc
comparison gcc/sreal.c @ 131:84e7813d76e9
gcc-8.2
author | mir3636 |
---|---|
date | Thu, 25 Oct 2018 07:37:49 +0900 |
parents | 04ced10e8804 |
children | 1830386684a0 |
comparison
equal
deleted
inserted
replaced
111:04ced10e8804 | 131:84e7813d76e9 |
---|---|
1 /* Simple data type for real numbers for the GNU compiler. | 1 /* Simple data type for real numbers for the GNU compiler. |
2 Copyright (C) 2002-2017 Free Software Foundation, Inc. | 2 Copyright (C) 2002-2018 Free Software Foundation, Inc. |
3 | 3 |
4 This file is part of GCC. | 4 This file is part of GCC. |
5 | 5 |
6 GCC is free software; you can redistribute it and/or modify it under | 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 | 7 the terms of the GNU General Public License as published by the Free |
62 /* Print the content of struct sreal. */ | 62 /* Print the content of struct sreal. */ |
63 | 63 |
64 void | 64 void |
65 sreal::dump (FILE *file) const | 65 sreal::dump (FILE *file) const |
66 { | 66 { |
67 fprintf (file, "(%" PRIi64 " * 2^%d)", m_sig, m_exp); | 67 fprintf (file, "(%" PRIi64 " * 2^%d)", (int64_t)m_sig, m_exp); |
68 } | 68 } |
69 | 69 |
70 DEBUG_FUNCTION void | 70 DEBUG_FUNCTION void |
71 debug (const sreal &ref) | 71 debug (const sreal &ref) |
72 { | 72 { |
112 if (m_exp <= -SREAL_BITS) | 112 if (m_exp <= -SREAL_BITS) |
113 return 0; | 113 return 0; |
114 if (m_exp >= SREAL_PART_BITS) | 114 if (m_exp >= SREAL_PART_BITS) |
115 return sign * INTTYPE_MAXIMUM (int64_t); | 115 return sign * INTTYPE_MAXIMUM (int64_t); |
116 if (m_exp > 0) | 116 if (m_exp > 0) |
117 return sign * (SREAL_ABS (m_sig) << m_exp); | 117 return sign * (SREAL_ABS ((int64_t)m_sig) << m_exp); |
118 if (m_exp < 0) | 118 if (m_exp < 0) |
119 return m_sig >> -m_exp; | 119 return m_sig >> -m_exp; |
120 return m_sig; | 120 return m_sig; |
121 } | 121 } |
122 | 122 |
136 | 136 |
137 sreal | 137 sreal |
138 sreal::operator+ (const sreal &other) const | 138 sreal::operator+ (const sreal &other) const |
139 { | 139 { |
140 int dexp; | 140 int dexp; |
141 sreal tmp, r; | 141 sreal tmp; |
142 int64_t r_sig, r_exp; | |
142 | 143 |
143 const sreal *a_p = this, *b_p = &other, *bb; | 144 const sreal *a_p = this, *b_p = &other, *bb; |
144 | 145 |
145 if (a_p->m_exp < b_p->m_exp) | 146 if (a_p->m_exp < b_p->m_exp) |
146 std::swap (a_p, b_p); | 147 std::swap (a_p, b_p); |
147 | 148 |
148 dexp = a_p->m_exp - b_p->m_exp; | 149 dexp = a_p->m_exp - b_p->m_exp; |
149 r.m_exp = a_p->m_exp; | 150 r_exp = a_p->m_exp; |
150 if (dexp > SREAL_BITS) | 151 if (dexp > SREAL_BITS) |
151 { | 152 { |
152 r.m_sig = a_p->m_sig; | 153 r_sig = a_p->m_sig; |
154 | |
155 sreal r; | |
156 r.m_sig = r_sig; | |
157 r.m_exp = r_exp; | |
153 return r; | 158 return r; |
154 } | 159 } |
155 | 160 |
156 if (dexp == 0) | 161 if (dexp == 0) |
157 bb = b_p; | 162 bb = b_p; |
160 tmp = *b_p; | 165 tmp = *b_p; |
161 tmp.shift_right (dexp); | 166 tmp.shift_right (dexp); |
162 bb = &tmp; | 167 bb = &tmp; |
163 } | 168 } |
164 | 169 |
165 r.m_sig = a_p->m_sig + bb->m_sig; | 170 r_sig = a_p->m_sig + (int64_t)bb->m_sig; |
166 r.normalize (); | 171 sreal r (r_sig, r_exp); |
167 return r; | 172 return r; |
168 } | 173 } |
169 | 174 |
170 | 175 |
171 /* Return *this - other. */ | 176 /* Return *this - other. */ |
172 | 177 |
173 sreal | 178 sreal |
174 sreal::operator- (const sreal &other) const | 179 sreal::operator- (const sreal &other) const |
175 { | 180 { |
176 int dexp; | 181 int dexp; |
177 sreal tmp, r; | 182 sreal tmp; |
183 int64_t r_sig, r_exp; | |
178 const sreal *bb; | 184 const sreal *bb; |
179 const sreal *a_p = this, *b_p = &other; | 185 const sreal *a_p = this, *b_p = &other; |
180 | 186 |
181 int64_t sign = 1; | 187 int64_t sign = 1; |
182 if (a_p->m_exp < b_p->m_exp) | 188 if (a_p->m_exp < b_p->m_exp) |
184 sign = -1; | 190 sign = -1; |
185 std::swap (a_p, b_p); | 191 std::swap (a_p, b_p); |
186 } | 192 } |
187 | 193 |
188 dexp = a_p->m_exp - b_p->m_exp; | 194 dexp = a_p->m_exp - b_p->m_exp; |
189 r.m_exp = a_p->m_exp; | 195 r_exp = a_p->m_exp; |
190 if (dexp > SREAL_BITS) | 196 if (dexp > SREAL_BITS) |
191 { | 197 { |
192 r.m_sig = sign * a_p->m_sig; | 198 r_sig = sign * a_p->m_sig; |
199 | |
200 sreal r; | |
201 r.m_sig = r_sig; | |
202 r.m_exp = r_exp; | |
193 return r; | 203 return r; |
194 } | 204 } |
195 if (dexp == 0) | 205 if (dexp == 0) |
196 bb = b_p; | 206 bb = b_p; |
197 else | 207 else |
199 tmp = *b_p; | 209 tmp = *b_p; |
200 tmp.shift_right (dexp); | 210 tmp.shift_right (dexp); |
201 bb = &tmp; | 211 bb = &tmp; |
202 } | 212 } |
203 | 213 |
204 r.m_sig = sign * (a_p->m_sig - bb->m_sig); | 214 r_sig = sign * ((int64_t) a_p->m_sig - (int64_t)bb->m_sig); |
205 r.normalize (); | 215 sreal r (r_sig, r_exp); |
206 return r; | 216 return r; |
207 } | 217 } |
208 | 218 |
209 /* Return *this * other. */ | 219 /* Return *this * other. */ |
210 | 220 |
211 sreal | 221 sreal |
212 sreal::operator* (const sreal &other) const | 222 sreal::operator* (const sreal &other) const |
213 { | 223 { |
214 sreal r; | 224 sreal r; |
215 if (absu_hwi (m_sig) < SREAL_MIN_SIG || absu_hwi (other.m_sig) < SREAL_MIN_SIG) | 225 if (absu_hwi (m_sig) < SREAL_MIN_SIG |
226 || absu_hwi (other.m_sig) < SREAL_MIN_SIG) | |
216 { | 227 { |
217 r.m_sig = 0; | 228 r.m_sig = 0; |
218 r.m_exp = -SREAL_MAX_EXP; | 229 r.m_exp = -SREAL_MAX_EXP; |
219 } | 230 } |
220 else | 231 else |
221 { | 232 r.normalize (m_sig * (int64_t) other.m_sig, m_exp + other.m_exp); |
222 r.m_sig = m_sig * other.m_sig; | |
223 r.m_exp = m_exp + other.m_exp; | |
224 r.normalize (); | |
225 } | |
226 | 233 |
227 return r; | 234 return r; |
228 } | 235 } |
229 | 236 |
230 /* Return *this / other. */ | 237 /* Return *this / other. */ |
231 | 238 |
232 sreal | 239 sreal |
233 sreal::operator/ (const sreal &other) const | 240 sreal::operator/ (const sreal &other) const |
234 { | 241 { |
235 gcc_checking_assert (other.m_sig != 0); | 242 gcc_checking_assert (other.m_sig != 0); |
236 sreal r; | 243 sreal r (SREAL_SIGN (m_sig) |
237 r.m_sig | 244 * ((int64_t)SREAL_ABS (m_sig) << SREAL_PART_BITS) / other.m_sig, |
238 = SREAL_SIGN (m_sig) * (SREAL_ABS (m_sig) << SREAL_PART_BITS) / other.m_sig; | 245 m_exp - other.m_exp - SREAL_PART_BITS); |
239 r.m_exp = m_exp - other.m_exp - SREAL_PART_BITS; | |
240 r.normalize (); | |
241 return r; | 246 return r; |
242 } | 247 } |
243 | 248 |
244 /* Stream sreal value to OB. */ | 249 /* Stream sreal value to OB. */ |
245 | 250 |
270 /* Verify basic sreal operations. */ | 275 /* Verify basic sreal operations. */ |
271 | 276 |
272 static void | 277 static void |
273 sreal_verify_basics (void) | 278 sreal_verify_basics (void) |
274 { | 279 { |
275 sreal minimum = INT_MIN; | 280 sreal minimum = INT_MIN/2; |
276 sreal maximum = INT_MAX; | 281 sreal maximum = INT_MAX/2; |
277 | 282 |
278 sreal seven = 7; | 283 sreal seven = 7; |
279 sreal minus_two = -2; | 284 sreal minus_two = -2; |
280 sreal minus_nine = -9; | 285 sreal minus_nine = -9; |
281 | 286 |
282 ASSERT_EQ (INT_MIN, minimum.to_int ()); | 287 ASSERT_EQ (INT_MIN/2, minimum.to_int ()); |
283 ASSERT_EQ (INT_MAX, maximum.to_int ()); | 288 ASSERT_EQ (INT_MAX/2, maximum.to_int ()); |
284 | 289 |
285 ASSERT_FALSE (minus_two < minus_two); | 290 ASSERT_FALSE (minus_two < minus_two); |
286 ASSERT_FALSE (seven < seven); | 291 ASSERT_FALSE (seven < seven); |
287 ASSERT_TRUE (seven > minus_two); | 292 ASSERT_TRUE (seven > minus_two); |
288 ASSERT_TRUE (minus_two < seven); | 293 ASSERT_TRUE (minus_two < seven); |