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