]>
Commit | Line | Data |
---|---|---|
909e2256 | 1 | /* Decimal floating point support. |
79b87c74 | 2 | Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc. |
909e2256 JG |
3 | |
4 | This file is part of GCC. | |
5 | ||
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 | |
8 | Software Foundation; either version 2, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA | |
19 | 02110-1301, USA. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "tree.h" | |
26 | #include "toplev.h" | |
27 | #include "real.h" | |
28 | #include "tm_p.h" | |
29 | #include "dfp.h" | |
30 | ||
31 | /* The order of the following headers is important for making sure | |
32 | decNumber structure is large enough to hold decimal128 digits. */ | |
33 | ||
34 | #include "decimal128.h" | |
35 | #include "decimal64.h" | |
36 | #include "decimal32.h" | |
37 | #include "decNumber.h" | |
38 | ||
7af1a035 BE |
39 | static uint32_t |
40 | dfp_byte_swap (uint32_t in) | |
909e2256 | 41 | { |
7af1a035 | 42 | uint32_t out = 0; |
909e2256 JG |
43 | unsigned char *p = (unsigned char *) &out; |
44 | union { | |
7af1a035 | 45 | uint32_t i; |
909e2256 JG |
46 | unsigned char b[4]; |
47 | } u; | |
48 | ||
49 | u.i = in; | |
50 | p[0] = u.b[3]; | |
51 | p[1] = u.b[2]; | |
52 | p[2] = u.b[1]; | |
53 | p[3] = u.b[0]; | |
54 | ||
55 | return out; | |
56 | } | |
57 | ||
58 | /* Initialize R (a real with the decimal flag set) from DN. Can | |
59 | utilize status passed in via CONTEXT, if a previous operation had | |
60 | interesting status. */ | |
61 | ||
62 | static void | |
63 | decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context) | |
64 | { | |
65 | memset (r, 0, sizeof (REAL_VALUE_TYPE)); | |
66 | ||
67 | r->cl = rvc_normal; | |
68 | if (decNumberIsZero (dn)) | |
69 | r->cl = rvc_zero; | |
70 | if (decNumberIsNaN (dn)) | |
71 | r->cl = rvc_nan; | |
72 | if (decNumberIsInfinite (dn)) | |
73 | r->cl = rvc_inf; | |
74 | if (context->status & DEC_Overflow) | |
75 | r->cl = rvc_inf; | |
76 | if (decNumberIsNegative (dn)) | |
77 | r->sign = 1; | |
78 | r->decimal = 1; | |
79 | ||
80 | if (r->cl != rvc_normal) | |
81 | return; | |
82 | ||
83 | decContextDefault (context, DEC_INIT_DECIMAL128); | |
84 | context->traps = 0; | |
85 | ||
86 | decimal128FromNumber ((decimal128 *) r->sig, dn, context); | |
87 | } | |
88 | ||
89 | /* Create decimal encoded R from string S. */ | |
90 | ||
91 | void | |
92 | decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s) | |
93 | { | |
94 | decNumber dn; | |
95 | decContext set; | |
96 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
97 | set.traps = 0; | |
98 | ||
5f754896 | 99 | decNumberFromString (&dn, s, &set); |
909e2256 JG |
100 | |
101 | /* It would be more efficient to store directly in decNumber format, | |
102 | but that is impractical from current data structure size. | |
103 | Encoding as a decimal128 is much more compact. */ | |
104 | decimal_from_decnumber (r, &dn, &set); | |
105 | } | |
106 | ||
107 | /* Initialize a decNumber from a REAL_VALUE_TYPE. */ | |
108 | ||
109 | static void | |
110 | decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn) | |
111 | { | |
112 | decContext set; | |
113 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
114 | set.traps = 0; | |
115 | ||
116 | switch (r->cl) | |
117 | { | |
118 | case rvc_zero: | |
119 | decNumberZero (dn); | |
120 | break; | |
121 | case rvc_inf: | |
5f754896 | 122 | decNumberFromString (dn, "Infinity", &set); |
909e2256 JG |
123 | break; |
124 | case rvc_nan: | |
125 | if (r->signalling) | |
5f754896 | 126 | decNumberFromString (dn, "snan", &set); |
909e2256 | 127 | else |
5f754896 | 128 | decNumberFromString (dn, "nan", &set); |
909e2256 JG |
129 | break; |
130 | case rvc_normal: | |
131 | gcc_assert (r->decimal); | |
5f754896 | 132 | decimal128ToNumber ((const decimal128 *) r->sig, dn); |
909e2256 JG |
133 | break; |
134 | default: | |
135 | gcc_unreachable (); | |
136 | } | |
137 | ||
138 | /* Fix up sign bit. */ | |
139 | if (r->sign != decNumberIsNegative (dn)) | |
f64ad1d3 | 140 | dn->bits ^= DECNEG; |
909e2256 JG |
141 | } |
142 | ||
143 | /* Encode a real into an IEEE 754R decimal32 type. */ | |
144 | ||
83f676b3 | 145 | void |
909e2256 JG |
146 | encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED, |
147 | long *buf, const REAL_VALUE_TYPE *r) | |
148 | { | |
149 | decNumber dn; | |
150 | decimal32 d32; | |
151 | decContext set; | |
152 | ||
153 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
154 | set.traps = 0; | |
155 | ||
156 | decimal_to_decnumber (r, &dn); | |
157 | decimal32FromNumber (&d32, &dn, &set); | |
158 | ||
159 | if (FLOAT_WORDS_BIG_ENDIAN) | |
160 | buf[0] = *(uint32_t *) d32.bytes; | |
161 | else | |
162 | buf[0] = dfp_byte_swap (*(uint32_t *) d32.bytes); | |
163 | } | |
164 | ||
165 | /* Decode an IEEE 754R decimal32 type into a real. */ | |
166 | ||
83f676b3 RS |
167 | void |
168 | decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED, | |
169 | REAL_VALUE_TYPE *r, const long *buf) | |
909e2256 JG |
170 | { |
171 | decNumber dn; | |
172 | decimal32 d32; | |
173 | decContext set; | |
174 | ||
175 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
176 | set.traps = 0; | |
177 | ||
178 | if (FLOAT_WORDS_BIG_ENDIAN) | |
179 | *((uint32_t *) d32.bytes) = (uint32_t) buf[0]; | |
180 | else | |
181 | *((uint32_t *) d32.bytes) = dfp_byte_swap ((uint32_t) buf[0]); | |
182 | ||
183 | decimal32ToNumber (&d32, &dn); | |
184 | decimal_from_decnumber (r, &dn, &set); | |
185 | } | |
186 | ||
187 | /* Encode a real into an IEEE 754R decimal64 type. */ | |
188 | ||
83f676b3 | 189 | void |
909e2256 JG |
190 | encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED, |
191 | long *buf, const REAL_VALUE_TYPE *r) | |
192 | { | |
193 | decNumber dn; | |
194 | decimal64 d64; | |
195 | decContext set; | |
196 | ||
197 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
198 | set.traps = 0; | |
199 | ||
200 | decimal_to_decnumber (r, &dn); | |
201 | decimal64FromNumber (&d64, &dn, &set); | |
202 | ||
203 | if (FLOAT_WORDS_BIG_ENDIAN) | |
204 | { | |
205 | buf[0] = *(uint32_t *) &d64.bytes[0]; | |
206 | buf[1] = *(uint32_t *) &d64.bytes[4]; | |
207 | } | |
208 | else | |
209 | { | |
210 | buf[1] = dfp_byte_swap (*(uint32_t *) &d64.bytes[0]); | |
211 | buf[0] = dfp_byte_swap (*(uint32_t *) &d64.bytes[4]); | |
212 | } | |
213 | } | |
214 | ||
215 | /* Decode an IEEE 754R decimal64 type into a real. */ | |
216 | ||
83f676b3 | 217 | void |
909e2256 JG |
218 | decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED, |
219 | REAL_VALUE_TYPE *r, const long *buf) | |
220 | { | |
221 | decNumber dn; | |
222 | decimal64 d64; | |
223 | decContext set; | |
224 | ||
225 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
226 | set.traps = 0; | |
227 | ||
228 | if (FLOAT_WORDS_BIG_ENDIAN) | |
229 | { | |
230 | *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0]; | |
231 | *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1]; | |
232 | } | |
233 | else | |
234 | { | |
235 | *((uint32_t *) &d64.bytes[0]) = dfp_byte_swap ((uint32_t) buf[1]); | |
236 | *((uint32_t *) &d64.bytes[4]) = dfp_byte_swap ((uint32_t) buf[0]); | |
237 | } | |
238 | ||
239 | decimal64ToNumber (&d64, &dn); | |
240 | decimal_from_decnumber (r, &dn, &set); | |
241 | } | |
242 | ||
243 | /* Encode a real into an IEEE 754R decimal128 type. */ | |
244 | ||
83f676b3 | 245 | void |
909e2256 JG |
246 | encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED, |
247 | long *buf, const REAL_VALUE_TYPE *r) | |
248 | { | |
249 | decNumber dn; | |
250 | decContext set; | |
251 | decimal128 d128; | |
252 | ||
253 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
254 | set.traps = 0; | |
255 | ||
256 | decimal_to_decnumber (r, &dn); | |
257 | decimal128FromNumber (&d128, &dn, &set); | |
258 | ||
259 | if (FLOAT_WORDS_BIG_ENDIAN) | |
260 | { | |
261 | buf[0] = *(uint32_t *) &d128.bytes[0]; | |
262 | buf[1] = *(uint32_t *) &d128.bytes[4]; | |
263 | buf[2] = *(uint32_t *) &d128.bytes[8]; | |
264 | buf[3] = *(uint32_t *) &d128.bytes[12]; | |
265 | } | |
266 | else | |
267 | { | |
268 | buf[0] = dfp_byte_swap (*(uint32_t *) &d128.bytes[12]); | |
269 | buf[1] = dfp_byte_swap (*(uint32_t *) &d128.bytes[8]); | |
270 | buf[2] = dfp_byte_swap (*(uint32_t *) &d128.bytes[4]); | |
271 | buf[3] = dfp_byte_swap (*(uint32_t *) &d128.bytes[0]); | |
272 | } | |
273 | } | |
274 | ||
275 | /* Decode an IEEE 754R decimal128 type into a real. */ | |
276 | ||
83f676b3 | 277 | void |
909e2256 JG |
278 | decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED, |
279 | REAL_VALUE_TYPE *r, const long *buf) | |
280 | { | |
281 | decNumber dn; | |
282 | decimal128 d128; | |
283 | decContext set; | |
284 | ||
285 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
286 | set.traps = 0; | |
287 | ||
288 | if (FLOAT_WORDS_BIG_ENDIAN) | |
289 | { | |
290 | *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0]; | |
291 | *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1]; | |
292 | *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2]; | |
293 | *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3]; | |
294 | } | |
295 | else | |
296 | { | |
297 | *((uint32_t *) &d128.bytes[0]) = dfp_byte_swap ((uint32_t) buf[3]); | |
298 | *((uint32_t *) &d128.bytes[4]) = dfp_byte_swap ((uint32_t) buf[2]); | |
299 | *((uint32_t *) &d128.bytes[8]) = dfp_byte_swap ((uint32_t) buf[1]); | |
300 | *((uint32_t *) &d128.bytes[12]) = dfp_byte_swap ((uint32_t) buf[0]); | |
301 | } | |
302 | ||
303 | decimal128ToNumber (&d128, &dn); | |
304 | decimal_from_decnumber (r, &dn, &set); | |
305 | } | |
306 | ||
307 | /* Helper function to convert from a binary real internal | |
308 | representation. */ | |
309 | ||
310 | static void | |
311 | decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from, | |
312 | enum machine_mode mode) | |
313 | { | |
314 | char string[256]; | |
5f754896 | 315 | const decimal128 *const d128 = (const decimal128 *) from->sig; |
909e2256 JG |
316 | |
317 | decimal128ToString (d128, string); | |
318 | real_from_string3 (to, string, mode); | |
319 | } | |
320 | ||
321 | ||
322 | /* Helper function to convert from a binary real internal | |
323 | representation. */ | |
324 | ||
325 | static void | |
326 | decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from) | |
327 | { | |
328 | char string[256]; | |
329 | ||
330 | /* We convert to string, then to decNumber then to decimal128. */ | |
331 | real_to_decimal (string, from, sizeof (string), 0, 1); | |
332 | decimal_real_from_string (to, string); | |
333 | } | |
334 | ||
335 | /* Helper function to real.c:do_compare() to handle decimal internal | |
6416ae7f | 336 | representation including when one of the operands is still in the |
909e2256 JG |
337 | binary internal representation. */ |
338 | ||
339 | int | |
340 | decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b, | |
341 | int nan_result) | |
342 | { | |
343 | decContext set; | |
344 | decNumber dn, dn2, dn3; | |
345 | REAL_VALUE_TYPE a1, b1; | |
346 | ||
347 | /* If either operand is non-decimal, create temporary versions. */ | |
348 | if (!a->decimal) | |
349 | { | |
350 | decimal_from_binary (&a1, a); | |
351 | a = &a1; | |
352 | } | |
353 | if (!b->decimal) | |
354 | { | |
355 | decimal_from_binary (&b1, b); | |
356 | b = &b1; | |
357 | } | |
358 | ||
359 | /* Convert into decNumber form for comparison operation. */ | |
360 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
361 | set.traps = 0; | |
5f754896 KG |
362 | decimal128ToNumber ((const decimal128 *) a->sig, &dn2); |
363 | decimal128ToNumber ((const decimal128 *) b->sig, &dn3); | |
909e2256 JG |
364 | |
365 | /* Finally, do the comparison. */ | |
366 | decNumberCompare (&dn, &dn2, &dn3, &set); | |
367 | ||
368 | /* Return the comparison result. */ | |
369 | if (decNumberIsNaN (&dn)) | |
370 | return nan_result; | |
371 | else if (decNumberIsZero (&dn)) | |
372 | return 0; | |
373 | else if (decNumberIsNegative (&dn)) | |
374 | return -1; | |
375 | else | |
376 | return 1; | |
377 | } | |
378 | ||
379 | /* Helper to round_for_format, handling decimal float types. */ | |
380 | ||
381 | void | |
382 | decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r) | |
383 | { | |
384 | decNumber dn; | |
385 | decContext set; | |
386 | ||
387 | /* Real encoding occurs later. */ | |
388 | if (r->cl != rvc_normal) | |
389 | return; | |
390 | ||
391 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
392 | set.traps = 0; | |
393 | decimal128ToNumber ((decimal128 *) r->sig, &dn); | |
394 | ||
395 | if (fmt == &decimal_quad_format) | |
396 | { | |
397 | /* The internal format is already in this format. */ | |
398 | return; | |
399 | } | |
400 | else if (fmt == &decimal_single_format) | |
401 | { | |
402 | decimal32 d32; | |
403 | decContextDefault (&set, DEC_INIT_DECIMAL32); | |
404 | set.traps = 0; | |
405 | ||
406 | decimal32FromNumber (&d32, &dn, &set); | |
407 | decimal32ToNumber (&d32, &dn); | |
408 | } | |
409 | else if (fmt == &decimal_double_format) | |
410 | { | |
411 | decimal64 d64; | |
412 | decContextDefault (&set, DEC_INIT_DECIMAL64); | |
413 | set.traps = 0; | |
414 | ||
415 | decimal64FromNumber (&d64, &dn, &set); | |
416 | decimal64ToNumber (&d64, &dn); | |
417 | } | |
418 | else | |
419 | gcc_unreachable (); | |
420 | ||
421 | decimal_from_decnumber (r, &dn, &set); | |
422 | } | |
423 | ||
424 | /* Extend or truncate to a new mode. Handles conversions between | |
425 | binary and decimal types. */ | |
426 | ||
427 | void | |
428 | decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode, | |
429 | const REAL_VALUE_TYPE *a) | |
430 | { | |
431 | const struct real_format *fmt = REAL_MODE_FORMAT (mode); | |
432 | ||
433 | if (a->decimal && fmt->b == 10) | |
434 | return; | |
435 | if (a->decimal) | |
436 | decimal_to_binary (r, a, mode); | |
437 | else | |
438 | decimal_from_binary (r, a); | |
439 | } | |
440 | ||
441 | /* Render R_ORIG as a decimal floating point constant. Emit DIGITS | |
442 | significant digits in the result, bounded by BUF_SIZE. If DIGITS | |
443 | is 0, choose the maximum for the representation. If | |
444 | CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring | |
445 | DIGITS or CROP_TRAILING_ZEROS. */ | |
446 | ||
83f676b3 RS |
447 | void |
448 | decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, | |
449 | size_t buf_size, | |
450 | size_t digits ATTRIBUTE_UNUSED, | |
451 | int crop_trailing_zeros ATTRIBUTE_UNUSED) | |
909e2256 | 452 | { |
5f754896 | 453 | const decimal128 *const d128 = (const decimal128*) r_orig->sig; |
909e2256 JG |
454 | |
455 | /* decimal128ToString requires space for at least 24 characters; | |
456 | Require two more for suffix. */ | |
457 | gcc_assert (buf_size >= 24); | |
458 | decimal128ToString (d128, str); | |
459 | } | |
460 | ||
461 | static bool | |
462 | decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, | |
463 | const REAL_VALUE_TYPE *op1, int subtract_p) | |
464 | { | |
465 | decNumber dn; | |
466 | decContext set; | |
467 | decNumber dn2, dn3; | |
468 | ||
469 | decimal_to_decnumber (op0, &dn2); | |
470 | decimal_to_decnumber (op1, &dn3); | |
471 | ||
472 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
473 | set.traps = 0; | |
474 | ||
475 | if (subtract_p) | |
476 | decNumberSubtract (&dn, &dn2, &dn3, &set); | |
477 | else | |
478 | decNumberAdd (&dn, &dn2, &dn3, &set); | |
479 | ||
480 | decimal_from_decnumber (r, &dn, &set); | |
481 | ||
482 | /* Return true, if inexact. */ | |
483 | return (set.status & DEC_Inexact); | |
484 | } | |
485 | ||
486 | /* Compute R = OP0 * OP1. */ | |
487 | ||
488 | static bool | |
489 | decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, | |
490 | const REAL_VALUE_TYPE *op1) | |
491 | { | |
492 | decContext set; | |
493 | decNumber dn, dn2, dn3; | |
494 | ||
495 | decimal_to_decnumber (op0, &dn2); | |
496 | decimal_to_decnumber (op1, &dn3); | |
497 | ||
498 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
499 | set.traps = 0; | |
500 | ||
501 | decNumberMultiply (&dn, &dn2, &dn3, &set); | |
502 | decimal_from_decnumber (r, &dn, &set); | |
503 | ||
504 | /* Return true, if inexact. */ | |
505 | return (set.status & DEC_Inexact); | |
506 | } | |
507 | ||
508 | /* Compute R = OP0 / OP1. */ | |
509 | ||
510 | static bool | |
511 | decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, | |
512 | const REAL_VALUE_TYPE *op1) | |
513 | { | |
514 | decContext set; | |
515 | decNumber dn, dn2, dn3; | |
516 | ||
517 | decimal_to_decnumber (op0, &dn2); | |
518 | decimal_to_decnumber (op1, &dn3); | |
519 | ||
520 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
521 | set.traps = 0; | |
522 | ||
523 | decNumberDivide (&dn, &dn2, &dn3, &set); | |
524 | decimal_from_decnumber (r, &dn, &set); | |
525 | ||
526 | /* Return true, if inexact. */ | |
527 | return (set.status & DEC_Inexact); | |
528 | } | |
529 | ||
530 | /* Set R to A truncated to an integral value toward zero (decimal | |
531 | floating point). */ | |
532 | ||
533 | void | |
534 | decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a) | |
535 | { | |
536 | decNumber dn, dn2; | |
537 | decContext set; | |
538 | ||
539 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
540 | set.traps = 0; | |
541 | set.round = DEC_ROUND_DOWN; | |
5f754896 | 542 | decimal128ToNumber ((const decimal128 *) a->sig, &dn2); |
909e2256 JG |
543 | |
544 | decNumberToIntegralValue (&dn, &dn2, &set); | |
545 | decimal_from_decnumber (r, &dn, &set); | |
546 | } | |
547 | ||
548 | /* Render decimal float value R as an integer. */ | |
549 | ||
550 | HOST_WIDE_INT | |
551 | decimal_real_to_integer (const REAL_VALUE_TYPE *r) | |
552 | { | |
553 | decContext set; | |
554 | decNumber dn, dn2, dn3; | |
555 | REAL_VALUE_TYPE to; | |
556 | char string[256]; | |
557 | ||
558 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
559 | set.traps = 0; | |
560 | set.round = DEC_ROUND_DOWN; | |
5f754896 | 561 | decimal128ToNumber ((const decimal128 *) r->sig, &dn); |
909e2256 JG |
562 | |
563 | decNumberToIntegralValue (&dn2, &dn, &set); | |
564 | decNumberZero (&dn3); | |
565 | decNumberRescale (&dn, &dn2, &dn3, &set); | |
566 | ||
567 | /* Convert to REAL_VALUE_TYPE and call appropriate conversion | |
568 | function. */ | |
569 | decNumberToString (&dn, string); | |
570 | real_from_string (&to, string); | |
571 | return real_to_integer (&to); | |
572 | } | |
573 | ||
574 | /* Likewise, but to an integer pair, HI+LOW. */ | |
575 | ||
576 | void | |
577 | decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh, | |
578 | const REAL_VALUE_TYPE *r) | |
579 | { | |
580 | decContext set; | |
581 | decNumber dn, dn2, dn3; | |
582 | REAL_VALUE_TYPE to; | |
583 | char string[256]; | |
584 | ||
585 | decContextDefault (&set, DEC_INIT_DECIMAL128); | |
586 | set.traps = 0; | |
587 | set.round = DEC_ROUND_DOWN; | |
5f754896 | 588 | decimal128ToNumber ((const decimal128 *) r->sig, &dn); |
909e2256 JG |
589 | |
590 | decNumberToIntegralValue (&dn2, &dn, &set); | |
591 | decNumberZero (&dn3); | |
592 | decNumberRescale (&dn, &dn2, &dn3, &set); | |
593 | ||
594 | /* Conver to REAL_VALUE_TYPE and call appropriate conversion | |
595 | function. */ | |
596 | decNumberToString (&dn, string); | |
597 | real_from_string (&to, string); | |
598 | real_to_integer2 (plow, phigh, &to); | |
599 | } | |
600 | ||
0b59f49d BE |
601 | /* Perform the decimal floating point operation described by CODE. |
602 | For a unary operation, OP1 will be NULL. This function returns | |
603 | true if the result may be inexact due to loss of precision. */ | |
909e2256 JG |
604 | |
605 | bool | |
0b59f49d | 606 | decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code, |
909e2256 JG |
607 | const REAL_VALUE_TYPE *op0, |
608 | const REAL_VALUE_TYPE *op1) | |
609 | { | |
0b59f49d | 610 | REAL_VALUE_TYPE a, b; |
909e2256 | 611 | |
0b59f49d | 612 | /* If either operand is non-decimal, create temporaries. */ |
909e2256 JG |
613 | if (!op0->decimal) |
614 | { | |
0b59f49d BE |
615 | decimal_from_binary (&a, op0); |
616 | op0 = &a; | |
909e2256 JG |
617 | } |
618 | if (op1 && !op1->decimal) | |
619 | { | |
0b59f49d BE |
620 | decimal_from_binary (&b, op1); |
621 | op1 = &b; | |
909e2256 JG |
622 | } |
623 | ||
624 | switch (code) | |
625 | { | |
626 | case PLUS_EXPR: | |
0b59f49d | 627 | return decimal_do_add (r, op0, op1, 0); |
909e2256 JG |
628 | |
629 | case MINUS_EXPR: | |
0b59f49d | 630 | return decimal_do_add (r, op0, op1, 1); |
909e2256 JG |
631 | |
632 | case MULT_EXPR: | |
0b59f49d | 633 | return decimal_do_multiply (r, op0, op1); |
909e2256 JG |
634 | |
635 | case RDIV_EXPR: | |
0b59f49d | 636 | return decimal_do_divide (r, op0, op1); |
909e2256 JG |
637 | |
638 | case MIN_EXPR: | |
639 | if (op1->cl == rvc_nan) | |
640 | *r = *op1; | |
641 | else if (real_compare (UNLT_EXPR, op0, op1)) | |
642 | *r = *op0; | |
643 | else | |
644 | *r = *op1; | |
0b59f49d | 645 | return false; |
909e2256 JG |
646 | |
647 | case MAX_EXPR: | |
648 | if (op1->cl == rvc_nan) | |
649 | *r = *op1; | |
650 | else if (real_compare (LT_EXPR, op0, op1)) | |
651 | *r = *op1; | |
652 | else | |
653 | *r = *op0; | |
0b59f49d | 654 | return false; |
909e2256 JG |
655 | |
656 | case NEGATE_EXPR: | |
657 | { | |
909e2256 | 658 | *r = *op0; |
79b87c74 MM |
659 | /* Flip sign bit. */ |
660 | decimal128FlipSign ((decimal128 *) r->sig); | |
909e2256 JG |
661 | /* Keep sign field in sync. */ |
662 | r->sign ^= 1; | |
663 | } | |
0b59f49d | 664 | return false; |
909e2256 JG |
665 | |
666 | case ABS_EXPR: | |
667 | { | |
909e2256 | 668 | *r = *op0; |
79b87c74 MM |
669 | /* Clear sign bit. */ |
670 | decimal128ClearSign ((decimal128 *) r->sig); | |
909e2256 JG |
671 | /* Keep sign field in sync. */ |
672 | r->sign = 0; | |
673 | } | |
0b59f49d | 674 | return false; |
909e2256 JG |
675 | |
676 | case FIX_TRUNC_EXPR: | |
677 | decimal_do_fix_trunc (r, op0); | |
0b59f49d | 678 | return false; |
909e2256 JG |
679 | |
680 | default: | |
681 | gcc_unreachable (); | |
682 | } | |
909e2256 JG |
683 | } |
684 | ||
685 | /* Fills R with the largest finite value representable in mode MODE. | |
686 | If SIGN is nonzero, R is set to the most negative finite value. */ | |
687 | ||
688 | void | |
689 | decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode) | |
690 | { | |
5f754896 | 691 | const char *max; |
909e2256 JG |
692 | |
693 | switch (mode) | |
694 | { | |
695 | case SDmode: | |
5f754896 | 696 | max = "9.999999E96"; |
909e2256 JG |
697 | break; |
698 | case DDmode: | |
5f754896 | 699 | max = "9.999999999999999E384"; |
909e2256 JG |
700 | break; |
701 | case TDmode: | |
5f754896 | 702 | max = "9.999999999999999999999999999999999E6144"; |
909e2256 JG |
703 | break; |
704 | default: | |
705 | gcc_unreachable (); | |
706 | } | |
707 | ||
708 | decimal_real_from_string (r, max); | |
709 | if (sign) | |
79b87c74 | 710 | decimal128SetSign ((decimal128 *) r->sig, 1); |
909e2256 | 711 | } |