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