comparison gcc/real.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* real.c - software floating point emulation. 1 /* real.c - software floating point emulation.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
3 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Stephen L. Moshier (moshier@world.std.com). 3 Contributed by Stephen L. Moshier (moshier@world.std.com).
6 Re-written by Richard Henderson <rth@redhat.com> 4 Re-written by Richard Henderson <rth@redhat.com>
7 5
8 This file is part of GCC. 6 This file is part of GCC.
9 7
23 21
24 #include "config.h" 22 #include "config.h"
25 #include "system.h" 23 #include "system.h"
26 #include "coretypes.h" 24 #include "coretypes.h"
27 #include "tm.h" 25 #include "tm.h"
26 #include "rtl.h"
28 #include "tree.h" 27 #include "tree.h"
29 #include "diagnostic-core.h"
30 #include "real.h"
31 #include "realmpfr.h" 28 #include "realmpfr.h"
32 #include "tm_p.h"
33 #include "dfp.h" 29 #include "dfp.h"
34 30
35 /* The floating point model used internally is not exactly IEEE 754 31 /* The floating point model used internally is not exactly IEEE 754
36 compliant, and close to the description in the ISO C99 standard, 32 compliant, and close to the description in the ISO C99 standard,
37 section 5.2.4.2.2 Characteristics of floating types. 33 section 5.2.4.2.2 Characteristics of floating types.
57 E must be large enough to hold the smallest supported denormal number 53 E must be large enough to hold the smallest supported denormal number
58 in a normalized form. 54 in a normalized form.
59 55
60 Both of these requirements are easily satisfied. The largest target 56 Both of these requirements are easily satisfied. The largest target
61 significand is 113 bits; we store at least 160. The smallest 57 significand is 113 bits; we store at least 160. The smallest
62 denormal number fits in 17 exponent bits; we store 26. 58 denormal number fits in 17 exponent bits; we store 26. */
63
64 Note that the decimal string conversion routines are sensitive to
65 rounding errors. Since the raw arithmetic routines do not themselves
66 have guard digits or rounding, the computation of 10**exp can
67 accumulate more than a few digits of error. The previous incarnation
68 of real.c successfully used a 144-bit fraction; given the current
69 layout of REAL_VALUE_TYPE we're forced to expand to at least 160 bits. */
70 59
71 60
72 /* Used to classify two numbers simultaneously. */ 61 /* Used to classify two numbers simultaneously. */
73 #define CLASS2(A, B) ((A) << 2 | (B)) 62 #define CLASS2(A, B) ((A) << 2 | (B))
74 63
550 case CLASS2 (rvc_nan, rvc_nan): 539 case CLASS2 (rvc_nan, rvc_nan):
551 /* ANY + NaN = NaN. */ 540 /* ANY + NaN = NaN. */
552 case CLASS2 (rvc_normal, rvc_inf): 541 case CLASS2 (rvc_normal, rvc_inf):
553 /* R + Inf = Inf. */ 542 /* R + Inf = Inf. */
554 *r = *b; 543 *r = *b;
544 /* Make resulting NaN value to be qNaN. The caller has the
545 responsibility to avoid the operation if flag_signaling_nans
546 is on. */
547 r->signalling = 0;
555 r->sign = sign ^ subtract_p; 548 r->sign = sign ^ subtract_p;
556 return false; 549 return false;
557 550
558 case CLASS2 (rvc_normal, rvc_zero): 551 case CLASS2 (rvc_normal, rvc_zero):
559 case CLASS2 (rvc_inf, rvc_zero): 552 case CLASS2 (rvc_inf, rvc_zero):
563 case CLASS2 (rvc_nan, rvc_inf): 556 case CLASS2 (rvc_nan, rvc_inf):
564 /* NaN + ANY = NaN. */ 557 /* NaN + ANY = NaN. */
565 case CLASS2 (rvc_inf, rvc_normal): 558 case CLASS2 (rvc_inf, rvc_normal):
566 /* Inf + R = Inf. */ 559 /* Inf + R = Inf. */
567 *r = *a; 560 *r = *a;
561 /* Make resulting NaN value to be qNaN. The caller has the
562 responsibility to avoid the operation if flag_signaling_nans
563 is on. */
564 r->signalling = 0;
568 return false; 565 return false;
569 566
570 case CLASS2 (rvc_inf, rvc_inf): 567 case CLASS2 (rvc_inf, rvc_inf):
571 if (subtract_p) 568 if (subtract_p)
572 /* Inf - Inf = NaN. */ 569 /* Inf - Inf = NaN. */
685 case CLASS2 (rvc_normal, rvc_nan): 682 case CLASS2 (rvc_normal, rvc_nan):
686 case CLASS2 (rvc_inf, rvc_nan): 683 case CLASS2 (rvc_inf, rvc_nan):
687 case CLASS2 (rvc_nan, rvc_nan): 684 case CLASS2 (rvc_nan, rvc_nan):
688 /* ANY * NaN = NaN. */ 685 /* ANY * NaN = NaN. */
689 *r = *b; 686 *r = *b;
687 /* Make resulting NaN value to be qNaN. The caller has the
688 responsibility to avoid the operation if flag_signaling_nans
689 is on. */
690 r->signalling = 0;
690 r->sign = sign; 691 r->sign = sign;
691 return false; 692 return false;
692 693
693 case CLASS2 (rvc_nan, rvc_zero): 694 case CLASS2 (rvc_nan, rvc_zero):
694 case CLASS2 (rvc_nan, rvc_normal): 695 case CLASS2 (rvc_nan, rvc_normal):
695 case CLASS2 (rvc_nan, rvc_inf): 696 case CLASS2 (rvc_nan, rvc_inf):
696 /* NaN * ANY = NaN. */ 697 /* NaN * ANY = NaN. */
697 *r = *a; 698 *r = *a;
699 /* Make resulting NaN value to be qNaN. The caller has the
700 responsibility to avoid the operation if flag_signaling_nans
701 is on. */
702 r->signalling = 0;
698 r->sign = sign; 703 r->sign = sign;
699 return false; 704 return false;
700 705
701 case CLASS2 (rvc_zero, rvc_inf): 706 case CLASS2 (rvc_zero, rvc_inf):
702 case CLASS2 (rvc_inf, rvc_zero): 707 case CLASS2 (rvc_inf, rvc_zero):
835 case CLASS2 (rvc_normal, rvc_nan): 840 case CLASS2 (rvc_normal, rvc_nan):
836 case CLASS2 (rvc_inf, rvc_nan): 841 case CLASS2 (rvc_inf, rvc_nan):
837 case CLASS2 (rvc_nan, rvc_nan): 842 case CLASS2 (rvc_nan, rvc_nan):
838 /* ANY / NaN = NaN. */ 843 /* ANY / NaN = NaN. */
839 *r = *b; 844 *r = *b;
845 /* Make resulting NaN value to be qNaN. The caller has the
846 responsibility to avoid the operation if flag_signaling_nans
847 is on. */
848 r->signalling = 0;
840 r->sign = sign; 849 r->sign = sign;
841 return false; 850 return false;
842 851
843 case CLASS2 (rvc_nan, rvc_zero): 852 case CLASS2 (rvc_nan, rvc_zero):
844 case CLASS2 (rvc_nan, rvc_normal): 853 case CLASS2 (rvc_nan, rvc_normal):
845 case CLASS2 (rvc_nan, rvc_inf): 854 case CLASS2 (rvc_nan, rvc_inf):
846 /* NaN / ANY = NaN. */ 855 /* NaN / ANY = NaN. */
847 *r = *a; 856 *r = *a;
857 /* Make resulting NaN value to be qNaN. The caller has the
858 responsibility to avoid the operation if flag_signaling_nans
859 is on. */
860 r->signalling = 0;
848 r->sign = sign; 861 r->sign = sign;
849 return false; 862 return false;
850 863
851 case CLASS2 (rvc_inf, rvc_normal): 864 case CLASS2 (rvc_inf, rvc_normal):
852 /* Inf / R = Inf. */ 865 /* Inf / R = Inf. */
945 958
946 default: 959 default:
947 gcc_unreachable (); 960 gcc_unreachable ();
948 } 961 }
949 962
963 if (a->decimal || b->decimal)
964 return decimal_do_compare (a, b, nan_result);
965
950 if (a->sign != b->sign) 966 if (a->sign != b->sign)
951 return -a->sign - -b->sign; 967 return -a->sign - -b->sign;
952
953 if (a->decimal || b->decimal)
954 return decimal_do_compare (a, b, nan_result);
955 968
956 if (REAL_EXP (a) > REAL_EXP (b)) 969 if (REAL_EXP (a) > REAL_EXP (b))
957 ret = 1; 970 ret = 1;
958 else if (REAL_EXP (a) < REAL_EXP (b)) 971 else if (REAL_EXP (a) < REAL_EXP (b))
959 ret = -1; 972 ret = -1;
973 switch (r->cl) 986 switch (r->cl)
974 { 987 {
975 case rvc_zero: 988 case rvc_zero:
976 case rvc_inf: 989 case rvc_inf:
977 case rvc_nan: 990 case rvc_nan:
991 /* Make resulting NaN value to be qNaN. The caller has the
992 responsibility to avoid the operation if flag_signaling_nans
993 is on. */
994 r->signalling = 0;
978 break; 995 break;
979 996
980 case rvc_normal: 997 case rvc_normal:
981 if (r->decimal) 998 if (r->decimal)
982 { 999 {
1031 memset (r, '\0', sizeof (*r)); 1048 memset (r, '\0', sizeof (*r));
1032 return do_divide (r, op0, op1); 1049 return do_divide (r, op0, op1);
1033 1050
1034 case MIN_EXPR: 1051 case MIN_EXPR:
1035 if (op1->cl == rvc_nan) 1052 if (op1->cl == rvc_nan)
1053 {
1036 *r = *op1; 1054 *r = *op1;
1055 /* Make resulting NaN value to be qNaN. The caller has the
1056 responsibility to avoid the operation if flag_signaling_nans
1057 is on. */
1058 r->signalling = 0;
1059 }
1037 else if (do_compare (op0, op1, -1) < 0) 1060 else if (do_compare (op0, op1, -1) < 0)
1038 *r = *op0; 1061 *r = *op0;
1039 else 1062 else
1040 *r = *op1; 1063 *r = *op1;
1041 break; 1064 break;
1042 1065
1043 case MAX_EXPR: 1066 case MAX_EXPR:
1044 if (op1->cl == rvc_nan) 1067 if (op1->cl == rvc_nan)
1068 {
1045 *r = *op1; 1069 *r = *op1;
1070 /* Make resulting NaN value to be qNaN. The caller has the
1071 responsibility to avoid the operation if flag_signaling_nans
1072 is on. */
1073 r->signalling = 0;
1074 }
1046 else if (do_compare (op0, op1, 1) < 0) 1075 else if (do_compare (op0, op1, 1) < 0)
1047 *r = *op1; 1076 *r = *op1;
1048 else 1077 else
1049 *r = *op0; 1078 *r = *op0;
1050 break; 1079 break;
1083 REAL_VALUE_TYPE r; 1112 REAL_VALUE_TYPE r;
1084 real_arithmetic (&r, ABS_EXPR, op0, NULL); 1113 real_arithmetic (&r, ABS_EXPR, op0, NULL);
1085 return r; 1114 return r;
1086 } 1115 }
1087 1116
1117 /* Return whether OP0 == OP1. */
1118
1119 bool
1120 real_equal (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1121 {
1122 return do_compare (op0, op1, -1) == 0;
1123 }
1124
1125 /* Return whether OP0 < OP1. */
1126
1127 bool
1128 real_less (const REAL_VALUE_TYPE *op0, const REAL_VALUE_TYPE *op1)
1129 {
1130 return do_compare (op0, op1, 1) < 0;
1131 }
1132
1088 bool 1133 bool
1089 real_compare (int icode, const REAL_VALUE_TYPE *op0, 1134 real_compare (int icode, const REAL_VALUE_TYPE *op0,
1090 const REAL_VALUE_TYPE *op1) 1135 const REAL_VALUE_TYPE *op1)
1091 { 1136 {
1092 enum tree_code code = (enum tree_code) icode; 1137 enum tree_code code = (enum tree_code) icode;
1093 1138
1094 switch (code) 1139 switch (code)
1095 { 1140 {
1096 case LT_EXPR: 1141 case LT_EXPR:
1097 return do_compare (op0, op1, 1) < 0; 1142 return real_less (op0, op1);
1098 case LE_EXPR: 1143 case LE_EXPR:
1099 return do_compare (op0, op1, 1) <= 0; 1144 return do_compare (op0, op1, 1) <= 0;
1100 case GT_EXPR: 1145 case GT_EXPR:
1101 return do_compare (op0, op1, -1) > 0; 1146 return do_compare (op0, op1, -1) > 0;
1102 case GE_EXPR: 1147 case GE_EXPR:
1103 return do_compare (op0, op1, -1) >= 0; 1148 return do_compare (op0, op1, -1) >= 0;
1104 case EQ_EXPR: 1149 case EQ_EXPR:
1105 return do_compare (op0, op1, -1) == 0; 1150 return real_equal (op0, op1);
1106 case NE_EXPR: 1151 case NE_EXPR:
1107 return do_compare (op0, op1, -1) != 0; 1152 return do_compare (op0, op1, -1) != 0;
1108 case UNORDERED_EXPR: 1153 case UNORDERED_EXPR:
1109 return op0->cl == rvc_nan || op1->cl == rvc_nan; 1154 return op0->cl == rvc_nan || op1->cl == rvc_nan;
1110 case ORDERED_EXPR: 1155 case ORDERED_EXPR:
1155 switch (r->cl) 1200 switch (r->cl)
1156 { 1201 {
1157 case rvc_zero: 1202 case rvc_zero:
1158 case rvc_inf: 1203 case rvc_inf:
1159 case rvc_nan: 1204 case rvc_nan:
1205 /* Make resulting NaN value to be qNaN. The caller has the
1206 responsibility to avoid the operation if flag_signaling_nans
1207 is on. */
1208 r->signalling = 0;
1160 break; 1209 break;
1161 1210
1162 case rvc_normal: 1211 case rvc_normal:
1163 exp += REAL_EXP (op0); 1212 exp += REAL_EXP (op0);
1164 if (exp > MAX_EXP) 1213 if (exp > MAX_EXP)
1188 real_isnan (const REAL_VALUE_TYPE *r) 1237 real_isnan (const REAL_VALUE_TYPE *r)
1189 { 1238 {
1190 return (r->cl == rvc_nan); 1239 return (r->cl == rvc_nan);
1191 } 1240 }
1192 1241
1242 /* Determine whether a floating-point value X is a signaling NaN. */
1243 bool real_issignaling_nan (const REAL_VALUE_TYPE *r)
1244 {
1245 return real_isnan (r) && r->signalling;
1246 }
1247
1193 /* Determine whether a floating-point value X is finite. */ 1248 /* Determine whether a floating-point value X is finite. */
1194 1249
1195 bool 1250 bool
1196 real_isfinite (const REAL_VALUE_TYPE *r) 1251 real_isfinite (const REAL_VALUE_TYPE *r)
1197 { 1252 {
1256 return false; 1311 return false;
1257 1312
1258 return true; 1313 return true;
1259 } 1314 }
1260 1315
1261 /* Try to change R into its exact multiplicative inverse in machine 1316 /* Try to change R into its exact multiplicative inverse in format FMT.
1262 mode MODE. Return true if successful. */ 1317 Return true if successful. */
1263 1318
1264 bool 1319 bool
1265 exact_real_inverse (enum machine_mode mode, REAL_VALUE_TYPE *r) 1320 exact_real_inverse (format_helper fmt, REAL_VALUE_TYPE *r)
1266 { 1321 {
1267 const REAL_VALUE_TYPE *one = real_digit (1); 1322 const REAL_VALUE_TYPE *one = real_digit (1);
1268 REAL_VALUE_TYPE u; 1323 REAL_VALUE_TYPE u;
1269 int i; 1324 int i;
1270 1325
1276 if (r->sig[i] != 0) 1331 if (r->sig[i] != 0)
1277 return false; 1332 return false;
1278 if (r->sig[SIGSZ-1] != SIG_MSB) 1333 if (r->sig[SIGSZ-1] != SIG_MSB)
1279 return false; 1334 return false;
1280 1335
1281 /* Find the inverse and truncate to the required mode. */ 1336 /* Find the inverse and truncate to the required format. */
1282 do_divide (&u, one, r); 1337 do_divide (&u, one, r);
1283 real_convert (&u, mode, &u); 1338 real_convert (&u, fmt, &u);
1284 1339
1285 /* The rounding may have overflowed. */ 1340 /* The rounding may have overflowed. */
1286 if (u.cl != rvc_normal) 1341 if (u.cl != rvc_normal)
1287 return false; 1342 return false;
1288 for (i = 0; i < SIGSZ-1; ++i) 1343 for (i = 0; i < SIGSZ-1; ++i)
1298 /* Return true if arithmetic on values in IMODE that were promoted 1353 /* Return true if arithmetic on values in IMODE that were promoted
1299 from values in TMODE is equivalent to direct arithmetic on values 1354 from values in TMODE is equivalent to direct arithmetic on values
1300 in TMODE. */ 1355 in TMODE. */
1301 1356
1302 bool 1357 bool
1303 real_can_shorten_arithmetic (enum machine_mode imode, enum machine_mode tmode) 1358 real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
1304 { 1359 {
1305 const struct real_format *tfmt, *ifmt; 1360 const struct real_format *tfmt, *ifmt;
1306 tfmt = REAL_MODE_FORMAT (tmode); 1361 tfmt = REAL_MODE_FORMAT (tmode);
1307 ifmt = REAL_MODE_FORMAT (imode); 1362 ifmt = REAL_MODE_FORMAT (imode);
1308 /* These conditions are conservative rather than trying to catch the 1363 /* These conditions are conservative rather than trying to catch the
1338 return 0; 1393 return 0;
1339 1394
1340 case rvc_inf: 1395 case rvc_inf:
1341 case rvc_nan: 1396 case rvc_nan:
1342 overflow: 1397 overflow:
1343 i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); 1398 i = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
1344 if (!r->sign) 1399 if (!r->sign)
1345 i--; 1400 i--;
1346 return i; 1401 return i;
1347 1402
1348 case rvc_normal: 1403 case rvc_normal:
1377 default: 1432 default:
1378 gcc_unreachable (); 1433 gcc_unreachable ();
1379 } 1434 }
1380 } 1435 }
1381 1436
1382 /* Likewise, but to an integer pair, HI+LOW. */ 1437 /* Likewise, but producing a wide-int of PRECISION. If the value cannot
1383 1438 be represented in precision, *FAIL is set to TRUE. */
1384 void 1439
1385 real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh, 1440 wide_int
1386 const REAL_VALUE_TYPE *r) 1441 real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
1387 { 1442 {
1388 REAL_VALUE_TYPE t; 1443 HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
1389 HOST_WIDE_INT low, high;
1390 int exp; 1444 int exp;
1445 int words, w;
1446 wide_int result;
1391 1447
1392 switch (r->cl) 1448 switch (r->cl)
1393 { 1449 {
1394 case rvc_zero: 1450 case rvc_zero:
1395 underflow: 1451 underflow:
1396 low = high = 0; 1452 return wi::zero (precision);
1397 break;
1398 1453
1399 case rvc_inf: 1454 case rvc_inf:
1400 case rvc_nan: 1455 case rvc_nan:
1401 overflow: 1456 overflow:
1402 high = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); 1457 *fail = true;
1458
1403 if (r->sign) 1459 if (r->sign)
1404 low = 0; 1460 return wi::set_bit_in_zero (precision - 1, precision);
1405 else 1461 else
1406 { 1462 return ~wi::set_bit_in_zero (precision - 1, precision);
1407 high--;
1408 low = -1;
1409 }
1410 break;
1411 1463
1412 case rvc_normal: 1464 case rvc_normal:
1413 if (r->decimal) 1465 if (r->decimal)
1414 { 1466 return decimal_real_to_integer (r, fail, precision);
1415 decimal_real_to_integer2 (plow, phigh, r);
1416 return;
1417 }
1418 1467
1419 exp = REAL_EXP (r); 1468 exp = REAL_EXP (r);
1420 if (exp <= 0) 1469 if (exp <= 0)
1421 goto underflow; 1470 goto underflow;
1422 /* Only force overflow for unsigned overflow. Signed overflow is 1471 /* Only force overflow for unsigned overflow. Signed overflow is
1423 undefined, so it doesn't matter what we return, and some callers 1472 undefined, so it doesn't matter what we return, and some callers
1424 expect to be able to use this routine for both signed and 1473 expect to be able to use this routine for both signed and
1425 unsigned conversions. */ 1474 unsigned conversions. */
1426 if (exp > 2*HOST_BITS_PER_WIDE_INT) 1475 if (exp > precision)
1427 goto overflow; 1476 goto overflow;
1428 1477
1429 rshift_significand (&t, r, 2*HOST_BITS_PER_WIDE_INT - exp); 1478 /* Put the significand into a wide_int that has precision W, which
1430 if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG) 1479 is the smallest HWI-multiple that has at least PRECISION bits.
1431 { 1480 This ensures that the top bit of the significand is in the
1432 high = t.sig[SIGSZ-1]; 1481 top bit of the wide_int. */
1433 low = t.sig[SIGSZ-2]; 1482 words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
1434 } 1483 w = words * HOST_BITS_PER_WIDE_INT;
1484
1485 #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
1486 for (int i = 0; i < words; i++)
1487 {
1488 int j = SIGSZ - words + i;
1489 val[i] = (j < 0) ? 0 : r->sig[j];
1490 }
1491 #else
1492 gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
1493 for (int i = 0; i < words; i++)
1494 {
1495 int j = SIGSZ - (words * 2) + (i * 2);
1496 if (j < 0)
1497 val[i] = 0;
1498 else
1499 val[i] = r->sig[j];
1500 j += 1;
1501 if (j >= 0)
1502 val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
1503 }
1504 #endif
1505 /* Shift the value into place and truncate to the desired precision. */
1506 result = wide_int::from_array (val, words, w);
1507 result = wi::lrshift (result, w - exp);
1508 result = wide_int::from (result, precision, UNSIGNED);
1509
1510 if (r->sign)
1511 return -result;
1435 else 1512 else
1436 { 1513 return result;
1437 gcc_assert (HOST_BITS_PER_WIDE_INT == 2*HOST_BITS_PER_LONG);
1438 high = t.sig[SIGSZ-1];
1439 high = high << (HOST_BITS_PER_LONG - 1) << 1;
1440 high |= t.sig[SIGSZ-2];
1441
1442 low = t.sig[SIGSZ-3];
1443 low = low << (HOST_BITS_PER_LONG - 1) << 1;
1444 low |= t.sig[SIGSZ-4];
1445 }
1446
1447 if (r->sign)
1448 {
1449 if (low == 0)
1450 high = -high;
1451 else
1452 low = -low, high = ~high;
1453 }
1454 break;
1455 1514
1456 default: 1515 default:
1457 gcc_unreachable (); 1516 gcc_unreachable ();
1458 } 1517 }
1459
1460 *plow = low;
1461 *phigh = high;
1462 } 1518 }
1463 1519
1464 /* A subroutine of real_to_decimal. Compute the quotient and remainder 1520 /* A subroutine of real_to_decimal. Compute the quotient and remainder
1465 of NUM / DEN. Return the quotient and place the remainder in NUM. 1521 of NUM / DEN. Return the quotient and place the remainder in NUM.
1466 It is expected that NUM / DEN are close enough that the quotient is 1522 It is expected that NUM / DEN are close enough that the quotient is
1506 #define M_LOG10_2 0.30102999566398119521 1562 #define M_LOG10_2 0.30102999566398119521
1507 1563
1508 void 1564 void
1509 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig, 1565 real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
1510 size_t buf_size, size_t digits, 1566 size_t buf_size, size_t digits,
1511 int crop_trailing_zeros, enum machine_mode mode) 1567 int crop_trailing_zeros, machine_mode mode)
1512 { 1568 {
1513 const struct real_format *fmt = NULL; 1569 const struct real_format *fmt = NULL;
1514 const REAL_VALUE_TYPE *one, *ten; 1570 const REAL_VALUE_TYPE *one, *ten;
1515 REAL_VALUE_TYPE r, pten, u, v; 1571 REAL_VALUE_TYPE r, pten, u, v;
1516 int dec_exp, cmp_one, digit; 1572 int dec_exp, cmp_one, digit;
1796 last--; 1852 last--;
1797 1853
1798 /* Append the exponent. */ 1854 /* Append the exponent. */
1799 sprintf (last, "e%+d", dec_exp); 1855 sprintf (last, "e%+d", dec_exp);
1800 1856
1801 #ifdef ENABLE_CHECKING
1802 /* Verify that we can read the original value back in. */ 1857 /* Verify that we can read the original value back in. */
1803 if (mode != VOIDmode) 1858 if (flag_checking && mode != VOIDmode)
1804 { 1859 {
1805 real_from_string (&r, str); 1860 real_from_string (&r, str);
1806 real_convert (&r, mode, &r); 1861 real_convert (&r, mode, &r);
1807 gcc_assert (real_identical (&r, r_orig)); 1862 gcc_assert (real_identical (&r, r_orig));
1808 } 1863 }
1809 #endif
1810 } 1864 }
1811 1865
1812 /* Likewise, except always uses round-to-nearest. */ 1866 /* Likewise, except always uses round-to-nearest. */
1813 1867
1814 void 1868 void
2029 normalize (r); 2083 normalize (r);
2030 } 2084 }
2031 else 2085 else
2032 { 2086 {
2033 /* Decimal floating point. */ 2087 /* Decimal floating point. */
2034 const REAL_VALUE_TYPE *ten = ten_to_ptwo (0); 2088 const char *cstr = str;
2035 int d; 2089 mpfr_t m;
2036 2090 bool inexact;
2037 while (*str == '0') 2091
2038 str++; 2092 while (*cstr == '0')
2039 while (ISDIGIT (*str)) 2093 cstr++;
2040 { 2094 if (*cstr == '.')
2041 d = *str++ - '0'; 2095 {
2042 do_multiply (r, r, ten); 2096 cstr++;
2043 if (d) 2097 while (*cstr == '0')
2044 do_add (r, r, real_digit (d), 0); 2098 cstr++;
2045 }
2046 if (*str == '.')
2047 {
2048 str++;
2049 if (r->cl == rvc_zero)
2050 {
2051 while (*str == '0')
2052 str++, exp--;
2053 }
2054 while (ISDIGIT (*str))
2055 {
2056 d = *str++ - '0';
2057 do_multiply (r, r, ten);
2058 if (d)
2059 do_add (r, r, real_digit (d), 0);
2060 exp--;
2061 }
2062 } 2099 }
2063 2100
2064 /* If the mantissa is zero, ignore the exponent. */ 2101 /* If the mantissa is zero, ignore the exponent. */
2065 if (r->cl == rvc_zero) 2102 if (!ISDIGIT (*cstr))
2066 goto is_a_zero; 2103 goto is_a_zero;
2067 2104
2068 if (*str == 'e' || *str == 'E') 2105 /* Nonzero value, possibly overflowing or underflowing. */
2069 { 2106 mpfr_init2 (m, SIGNIFICAND_BITS);
2070 bool exp_neg = false; 2107 inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
2071 2108 /* The result should never be a NaN, and because the rounding is
2072 str++; 2109 toward zero should never be an infinity. */
2073 if (*str == '-') 2110 gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
2074 { 2111 if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
2075 exp_neg = true; 2112 {
2076 str++; 2113 mpfr_clear (m);
2077 } 2114 goto underflow;
2078 else if (*str == '+') 2115 }
2079 str++; 2116 else if (mpfr_get_exp (m) > MAX_EXP - 4)
2080 2117 {
2081 d = 0; 2118 mpfr_clear (m);
2082 while (ISDIGIT (*str)) 2119 goto overflow;
2083 { 2120 }
2084 d *= 10; 2121 else
2085 d += *str - '0'; 2122 {
2086 if (d > MAX_EXP) 2123 real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
2087 { 2124 /* 1 to 3 bits may have been shifted off (with a sticky bit)
2088 /* Overflowed the exponent. */ 2125 because the hex digits used in real_from_mpfr did not
2089 if (exp_neg) 2126 start with a digit 8 to f, but the exponent bounds above
2090 goto underflow; 2127 should have avoided underflow or overflow. */
2091 else 2128 gcc_assert (r->cl == rvc_normal);
2092 goto overflow; 2129 /* Set a sticky bit if mpfr_strtofr was inexact. */
2093 } 2130 r->sig[0] |= inexact;
2094 str++; 2131 mpfr_clear (m);
2095 } 2132 }
2096 if (exp_neg)
2097 d = -d;
2098 exp += d;
2099 }
2100
2101 if (exp)
2102 times_pten (r, exp);
2103 } 2133 }
2104 2134
2105 r->sign = sign; 2135 r->sign = sign;
2106 return 0; 2136 return 0;
2107 2137
2119 } 2149 }
2120 2150
2121 /* Legacy. Similar, but return the result directly. */ 2151 /* Legacy. Similar, but return the result directly. */
2122 2152
2123 REAL_VALUE_TYPE 2153 REAL_VALUE_TYPE
2124 real_from_string2 (const char *s, enum machine_mode mode) 2154 real_from_string2 (const char *s, format_helper fmt)
2125 { 2155 {
2126 REAL_VALUE_TYPE r; 2156 REAL_VALUE_TYPE r;
2127 2157
2128 real_from_string (&r, s); 2158 real_from_string (&r, s);
2129 if (mode != VOIDmode) 2159 if (fmt)
2130 real_convert (&r, mode, &r); 2160 real_convert (&r, fmt, &r);
2131 2161
2132 return r; 2162 return r;
2133 } 2163 }
2134 2164
2135 /* Initialize R from string S and desired MODE. */ 2165 /* Initialize R from string S and desired format FMT. */
2136 2166
2137 void 2167 void
2138 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, enum machine_mode mode) 2168 real_from_string3 (REAL_VALUE_TYPE *r, const char *s, format_helper fmt)
2139 { 2169 {
2140 if (DECIMAL_FLOAT_MODE_P (mode)) 2170 if (fmt.decimal_p ())
2141 decimal_real_from_string (r, s); 2171 decimal_real_from_string (r, s);
2142 else 2172 else
2143 real_from_string (r, s); 2173 real_from_string (r, s);
2144 2174
2145 if (mode != VOIDmode) 2175 if (fmt)
2146 real_convert (r, mode, r); 2176 real_convert (r, fmt, r);
2147 } 2177 }
2148 2178
2149 /* Initialize R from the integer pair HIGH+LOW. */ 2179 /* Initialize R from the wide_int VAL_IN. Round it to format FMT if
2180 FMT is nonnull. */
2150 2181
2151 void 2182 void
2152 real_from_integer (REAL_VALUE_TYPE *r, enum machine_mode mode, 2183 real_from_integer (REAL_VALUE_TYPE *r, format_helper fmt,
2153 unsigned HOST_WIDE_INT low, HOST_WIDE_INT high, 2184 const wide_int_ref &val_in, signop sgn)
2154 int unsigned_p) 2185 {
2155 { 2186 if (val_in == 0)
2156 if (low == 0 && high == 0)
2157 get_zero (r, 0); 2187 get_zero (r, 0);
2158 else 2188 else
2159 { 2189 {
2190 unsigned int len = val_in.get_precision ();
2191 int i, j, e = 0;
2192 int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
2193 const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
2194 * HOST_BITS_PER_WIDE_INT);
2195
2160 memset (r, 0, sizeof (*r)); 2196 memset (r, 0, sizeof (*r));
2161 r->cl = rvc_normal; 2197 r->cl = rvc_normal;
2162 r->sign = high < 0 && !unsigned_p; 2198 r->sign = wi::neg_p (val_in, sgn);
2163 SET_REAL_EXP (r, 2 * HOST_BITS_PER_WIDE_INT); 2199
2200 /* We have to ensure we can negate the largest negative number. */
2201 wide_int val = wide_int::from (val_in, maxbitlen, sgn);
2164 2202
2165 if (r->sign) 2203 if (r->sign)
2166 { 2204 val = -val;
2167 high = ~high; 2205
2168 if (low == 0) 2206 /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
2169 high += 1; 2207 won't work with precisions that are not a multiple of
2170 else 2208 HOST_BITS_PER_WIDE_INT. */
2171 low = -low; 2209 len += HOST_BITS_PER_WIDE_INT - 1;
2172 } 2210
2173 2211 /* Ensure we can represent the largest negative number. */
2212 len += 1;
2213
2214 len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
2215
2216 /* Cap the size to the size allowed by real.h. */
2217 if (len > realmax)
2218 {
2219 HOST_WIDE_INT cnt_l_z;
2220 cnt_l_z = wi::clz (val);
2221
2222 if (maxbitlen - cnt_l_z > realmax)
2223 {
2224 e = maxbitlen - cnt_l_z - realmax;
2225
2226 /* This value is too large, we must shift it right to
2227 preserve all the bits we can, and then bump the
2228 exponent up by that amount. */
2229 val = wi::lrshift (val, e);
2230 }
2231 len = realmax;
2232 }
2233
2234 /* Clear out top bits so elt will work with precisions that aren't
2235 a multiple of HOST_BITS_PER_WIDE_INT. */
2236 val = wide_int::from (val, len, sgn);
2237 len = len / HOST_BITS_PER_WIDE_INT;
2238
2239 SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
2240
2241 j = SIGSZ - 1;
2174 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT) 2242 if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
2175 { 2243 for (i = len - 1; i >= 0; i--)
2176 r->sig[SIGSZ-1] = high; 2244 {
2177 r->sig[SIGSZ-2] = low; 2245 r->sig[j--] = val.elt (i);
2178 } 2246 if (j < 0)
2247 break;
2248 }
2179 else 2249 else
2180 { 2250 {
2181 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT); 2251 gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
2182 r->sig[SIGSZ-1] = high >> (HOST_BITS_PER_LONG - 1) >> 1; 2252 for (i = len - 1; i >= 0; i--)
2183 r->sig[SIGSZ-2] = high; 2253 {
2184 r->sig[SIGSZ-3] = low >> (HOST_BITS_PER_LONG - 1) >> 1; 2254 HOST_WIDE_INT e = val.elt (i);
2185 r->sig[SIGSZ-4] = low; 2255 r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
2256 if (j < 0)
2257 break;
2258 r->sig[j--] = e;
2259 if (j < 0)
2260 break;
2261 }
2186 } 2262 }
2187 2263
2188 normalize (r); 2264 normalize (r);
2189 } 2265 }
2190 2266
2191 if (DECIMAL_FLOAT_MODE_P (mode)) 2267 if (fmt.decimal_p ())
2192 decimal_from_integer (r); 2268 decimal_from_integer (r);
2193 else if (mode != VOIDmode) 2269 if (fmt)
2194 real_convert (r, mode, r); 2270 real_convert (r, fmt, r);
2195 } 2271 }
2196 2272
2197 /* Render R, an integral value, as a floating point constant with no 2273 /* Render R, an integral value, as a floating point constant with no
2198 specified exponent. */ 2274 specified exponent. */
2199 2275
2270 int i; 2346 int i;
2271 2347
2272 for (i = 0; i < n; ++i) 2348 for (i = 0; i < n; ++i)
2273 t *= t; 2349 t *= t;
2274 2350
2275 real_from_integer (&tens[n], VOIDmode, t, 0, 1); 2351 real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
2276 } 2352 }
2277 else 2353 else
2278 { 2354 {
2279 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1); 2355 const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
2280 do_multiply (&tens[n], t, t); 2356 do_multiply (&tens[n], t, t);
2309 2385
2310 gcc_assert (n >= 0); 2386 gcc_assert (n >= 0);
2311 gcc_assert (n <= 9); 2387 gcc_assert (n <= 9);
2312 2388
2313 if (n > 0 && num[n].cl == rvc_zero) 2389 if (n > 0 && num[n].cl == rvc_zero)
2314 real_from_integer (&num[n], VOIDmode, n, 0, 1); 2390 real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
2315 2391
2316 return &num[n]; 2392 return &num[n];
2317 } 2393 }
2318 2394
2319 /* Multiply R by 10**EXP. */ 2395 /* Multiply R by 10**EXP. */
2362 2438
2363 } 2439 }
2364 return &value; 2440 return &value;
2365 } 2441 }
2366 2442
2367 /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */ 2443 /* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */
2368 2444
2369 const REAL_VALUE_TYPE * 2445 #define CACHED_FRACTION(NAME, N) \
2370 dconst_third_ptr (void) 2446 const REAL_VALUE_TYPE * \
2371 { 2447 NAME (void) \
2372 static REAL_VALUE_TYPE value; 2448 { \
2373 2449 static REAL_VALUE_TYPE value; \
2374 /* Initialize mathematical constants for constant folding builtins. 2450 \
2375 These constants need to be given to at least 160 bits precision. */ 2451 /* Initialize mathematical constants for constant folding builtins. \
2376 if (value.cl == rvc_zero) 2452 These constants need to be given to at least 160 bits \
2377 { 2453 precision. */ \
2378 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3)); 2454 if (value.cl == rvc_zero) \
2379 } 2455 real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \
2380 return &value; 2456 return &value; \
2381 } 2457 }
2458
2459 CACHED_FRACTION (dconst_third_ptr, 3)
2460 CACHED_FRACTION (dconst_quarter_ptr, 4)
2461 CACHED_FRACTION (dconst_sixth_ptr, 6)
2462 CACHED_FRACTION (dconst_ninth_ptr, 9)
2382 2463
2383 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */ 2464 /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
2384 2465
2385 const REAL_VALUE_TYPE * 2466 const REAL_VALUE_TYPE *
2386 dconst_sqrt2_ptr (void) 2467 dconst_sqrt2_ptr (void)
2413 is parsed as a number and placed in the significand. Return true 2494 is parsed as a number and placed in the significand. Return true
2414 if the string was successfully parsed. */ 2495 if the string was successfully parsed. */
2415 2496
2416 bool 2497 bool
2417 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet, 2498 real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
2418 enum machine_mode mode) 2499 format_helper fmt)
2419 { 2500 {
2420 const struct real_format *fmt;
2421
2422 fmt = REAL_MODE_FORMAT (mode);
2423 gcc_assert (fmt);
2424
2425 if (*str == 0) 2501 if (*str == 0)
2426 { 2502 {
2427 if (quiet) 2503 if (quiet)
2428 get_canonical_qnan (r, 0); 2504 get_canonical_qnan (r, 0);
2429 else 2505 else
2493 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan); 2569 lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
2494 2570
2495 /* Our MSB is always unset for NaNs. */ 2571 /* Our MSB is always unset for NaNs. */
2496 r->sig[SIGSZ-1] &= ~SIG_MSB; 2572 r->sig[SIGSZ-1] &= ~SIG_MSB;
2497 2573
2498 /* Force quiet or signalling NaN. */ 2574 /* Force quiet or signaling NaN. */
2499 r->signalling = !quiet; 2575 r->signalling = !quiet;
2500 } 2576 }
2501 2577
2502 return true; 2578 return true;
2503 } 2579 }
2504 2580
2505 /* Fills R with the largest finite value representable in mode MODE. 2581 /* Fills R with the largest finite value representable in mode MODE.
2506 If SIGN is nonzero, R is set to the most negative finite value. */ 2582 If SIGN is nonzero, R is set to the most negative finite value. */
2507 2583
2508 void 2584 void
2509 real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode) 2585 real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
2510 { 2586 {
2511 const struct real_format *fmt; 2587 const struct real_format *fmt;
2512 int np2; 2588 int np2;
2513 2589
2514 fmt = REAL_MODE_FORMAT (mode); 2590 fmt = REAL_MODE_FORMAT (mode);
2539 } 2615 }
2540 2616
2541 /* Fills R with 2**N. */ 2617 /* Fills R with 2**N. */
2542 2618
2543 void 2619 void
2544 real_2expN (REAL_VALUE_TYPE *r, int n, enum machine_mode fmode) 2620 real_2expN (REAL_VALUE_TYPE *r, int n, format_helper fmt)
2545 { 2621 {
2546 memset (r, 0, sizeof (*r)); 2622 memset (r, 0, sizeof (*r));
2547 2623
2548 n++; 2624 n++;
2549 if (n > MAX_EXP) 2625 if (n > MAX_EXP)
2554 { 2630 {
2555 r->cl = rvc_normal; 2631 r->cl = rvc_normal;
2556 SET_REAL_EXP (r, n); 2632 SET_REAL_EXP (r, n);
2557 r->sig[SIGSZ-1] = SIG_MSB; 2633 r->sig[SIGSZ-1] = SIG_MSB;
2558 } 2634 }
2559 if (DECIMAL_FLOAT_MODE_P (fmode)) 2635 if (fmt.decimal_p ())
2560 decimal_real_convert (r, fmode, r); 2636 decimal_real_convert (r, fmt, r);
2561 } 2637 }
2562 2638
2563 2639
2564 static void 2640 static void
2565 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r) 2641 round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
2577 } 2653 }
2578 /* FIXME. We can come here via fp_easy_constant 2654 /* FIXME. We can come here via fp_easy_constant
2579 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not 2655 (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
2580 investigated whether this convert needs to be here, or 2656 investigated whether this convert needs to be here, or
2581 something else is missing. */ 2657 something else is missing. */
2582 decimal_real_convert (r, DFmode, r); 2658 decimal_real_convert (r, REAL_MODE_FORMAT (DFmode), r);
2583 } 2659 }
2584 2660
2585 p2 = fmt->p; 2661 p2 = fmt->p;
2586 emin2m1 = fmt->emin - 1; 2662 emin2m1 = fmt->emin - 1;
2587 emax2 = fmt->emax; 2663 emax2 = fmt->emax;
2589 np2 = SIGNIFICAND_BITS - p2; 2665 np2 = SIGNIFICAND_BITS - p2;
2590 switch (r->cl) 2666 switch (r->cl)
2591 { 2667 {
2592 underflow: 2668 underflow:
2593 get_zero (r, r->sign); 2669 get_zero (r, r->sign);
2670 /* FALLTHRU */
2594 case rvc_zero: 2671 case rvc_zero:
2595 if (!fmt->has_signed_zero) 2672 if (!fmt->has_signed_zero)
2596 r->sign = 0; 2673 r->sign = 0;
2597 return; 2674 return;
2598 2675
2683 2760
2684 /* Clear out trailing garbage. */ 2761 /* Clear out trailing garbage. */
2685 clear_significand_below (r, np2); 2762 clear_significand_below (r, np2);
2686 } 2763 }
2687 2764
2688 /* Extend or truncate to a new mode. */ 2765 /* Extend or truncate to a new format. */
2689 2766
2690 void 2767 void
2691 real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode, 2768 real_convert (REAL_VALUE_TYPE *r, format_helper fmt,
2692 const REAL_VALUE_TYPE *a) 2769 const REAL_VALUE_TYPE *a)
2693 { 2770 {
2694 const struct real_format *fmt;
2695
2696 fmt = REAL_MODE_FORMAT (mode);
2697 gcc_assert (fmt);
2698
2699 *r = *a; 2771 *r = *a;
2700 2772
2701 if (a->decimal || fmt->b == 10) 2773 if (a->decimal || fmt->b == 10)
2702 decimal_real_convert (r, mode, a); 2774 decimal_real_convert (r, fmt, a);
2703 2775
2704 round_for_format (fmt, r); 2776 round_for_format (fmt, r);
2777
2778 /* Make resulting NaN value to be qNaN. The caller has the
2779 responsibility to avoid the operation if flag_signaling_nans
2780 is on. */
2781 if (r->cl == rvc_nan)
2782 r->signalling = 0;
2705 2783
2706 /* round_for_format de-normalizes denormals. Undo just that part. */ 2784 /* round_for_format de-normalizes denormals. Undo just that part. */
2707 if (r->cl == rvc_normal) 2785 if (r->cl == rvc_normal)
2708 normalize (r); 2786 normalize (r);
2709 } 2787 }
2710 2788
2711 /* Legacy. Likewise, except return the struct directly. */ 2789 /* Legacy. Likewise, except return the struct directly. */
2712 2790
2713 REAL_VALUE_TYPE 2791 REAL_VALUE_TYPE
2714 real_value_truncate (enum machine_mode mode, REAL_VALUE_TYPE a) 2792 real_value_truncate (format_helper fmt, REAL_VALUE_TYPE a)
2715 { 2793 {
2716 REAL_VALUE_TYPE r; 2794 REAL_VALUE_TYPE r;
2717 real_convert (&r, mode, &a); 2795 real_convert (&r, fmt, &a);
2718 return r; 2796 return r;
2719 } 2797 }
2720 2798
2721 /* Return true if truncating to MODE is exact. */ 2799 /* Return true if truncating to FMT is exact. */
2722 2800
2723 bool 2801 bool
2724 exact_real_truncate (enum machine_mode mode, const REAL_VALUE_TYPE *a) 2802 exact_real_truncate (format_helper fmt, const REAL_VALUE_TYPE *a)
2725 { 2803 {
2726 const struct real_format *fmt;
2727 REAL_VALUE_TYPE t; 2804 REAL_VALUE_TYPE t;
2728 int emin2m1; 2805 int emin2m1;
2729
2730 fmt = REAL_MODE_FORMAT (mode);
2731 gcc_assert (fmt);
2732 2806
2733 /* Don't allow conversion to denormals. */ 2807 /* Don't allow conversion to denormals. */
2734 emin2m1 = fmt->emin - 1; 2808 emin2m1 = fmt->emin - 1;
2735 if (REAL_EXP (a) <= emin2m1) 2809 if (REAL_EXP (a) <= emin2m1)
2736 return false; 2810 return false;
2737 2811
2738 /* After conversion to the new mode, the value must be identical. */ 2812 /* After conversion to the new format, the value must be identical. */
2739 real_convert (&t, mode, a); 2813 real_convert (&t, fmt, a);
2740 return real_identical (&t, a); 2814 return real_identical (&t, a);
2741 } 2815 }
2742 2816
2743 /* Write R to the given target format. Place the words of the result 2817 /* Write R to the given target format. Place the words of the result
2744 in target word order in BUF. There are always 32 bits in each 2818 in target word order in BUF. There are always 32 bits in each
2745 long, no matter the size of the host long. 2819 long, no matter the size of the host long.
2746 2820
2747 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */ 2821 Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
2748 2822
2749 long 2823 long
2750 real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig, 2824 real_to_target (long *buf, const REAL_VALUE_TYPE *r_orig,
2751 const struct real_format *fmt) 2825 format_helper fmt)
2752 { 2826 {
2753 REAL_VALUE_TYPE r; 2827 REAL_VALUE_TYPE r;
2754 long buf1; 2828 long buf1;
2755 2829
2756 r = *r_orig; 2830 r = *r_orig;
2761 (*fmt->encode) (fmt, buf, &r); 2835 (*fmt->encode) (fmt, buf, &r);
2762 2836
2763 return *buf; 2837 return *buf;
2764 } 2838 }
2765 2839
2766 /* Similar, but look up the format from MODE. */
2767
2768 long
2769 real_to_target (long *buf, const REAL_VALUE_TYPE *r, enum machine_mode mode)
2770 {
2771 const struct real_format *fmt;
2772
2773 fmt = REAL_MODE_FORMAT (mode);
2774 gcc_assert (fmt);
2775
2776 return real_to_target_fmt (buf, r, fmt);
2777 }
2778
2779 /* Read R from the given target format. Read the words of the result 2840 /* Read R from the given target format. Read the words of the result
2780 in target word order in BUF. There are always 32 bits in each 2841 in target word order in BUF. There are always 32 bits in each
2781 long, no matter the size of the host long. */ 2842 long, no matter the size of the host long. */
2782 2843
2783 void 2844 void
2784 real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf, 2845 real_from_target (REAL_VALUE_TYPE *r, const long *buf, format_helper fmt)
2785 const struct real_format *fmt)
2786 { 2846 {
2787 (*fmt->decode) (fmt, r, buf); 2847 (*fmt->decode) (fmt, r, buf);
2788 } 2848 }
2789 2849
2790 /* Similar, but look up the format from MODE. */
2791
2792 void
2793 real_from_target (REAL_VALUE_TYPE *r, const long *buf, enum machine_mode mode)
2794 {
2795 const struct real_format *fmt;
2796
2797 fmt = REAL_MODE_FORMAT (mode);
2798 gcc_assert (fmt);
2799
2800 (*fmt->decode) (fmt, r, buf);
2801 }
2802
2803 /* Return the number of bits of the largest binary value that the 2850 /* Return the number of bits of the largest binary value that the
2804 significand of MODE will hold. */ 2851 significand of FMT will hold. */
2805 /* ??? Legacy. Should get access to real_format directly. */ 2852 /* ??? Legacy. Should get access to real_format directly. */
2806 2853
2807 int 2854 int
2808 significand_size (enum machine_mode mode) 2855 significand_size (format_helper fmt)
2809 { 2856 {
2810 const struct real_format *fmt;
2811
2812 fmt = REAL_MODE_FORMAT (mode);
2813 if (fmt == NULL) 2857 if (fmt == NULL)
2814 return 0; 2858 return 0;
2815 2859
2816 if (fmt->b == 10) 2860 if (fmt->b == 10)
2817 { 2861 {
2818 /* Return the size in bits of the largest binary value that can be 2862 /* Return the size in bits of the largest binary value that can be
2819 held by the decimal coefficient for this mode. This is one more 2863 held by the decimal coefficient for this format. This is one more
2820 than the number of bits required to hold the largest coefficient 2864 than the number of bits required to hold the largest coefficient
2821 of this mode. */ 2865 of this format. */
2822 double log2_10 = 3.3219281; 2866 double log2_10 = 3.3219281;
2823 return fmt->p * log2_10; 2867 return fmt->p * log2_10;
2824 } 2868 }
2825 return fmt->p; 2869 return fmt->p;
2826 } 2870 }
2841 case rvc_zero: 2885 case rvc_zero:
2842 case rvc_inf: 2886 case rvc_inf:
2843 return h; 2887 return h;
2844 2888
2845 case rvc_normal: 2889 case rvc_normal:
2846 h |= REAL_EXP (r) << 3; 2890 h |= (unsigned int)REAL_EXP (r) << 3;
2847 break; 2891 break;
2848 2892
2849 case rvc_nan: 2893 case rvc_nan:
2850 if (r->signalling) 2894 if (r->signalling)
2851 h ^= (unsigned int)-1; 2895 h ^= (unsigned int)-1;
2855 2899
2856 default: 2900 default:
2857 gcc_unreachable (); 2901 gcc_unreachable ();
2858 } 2902 }
2859 2903
2860 if (sizeof(unsigned long) > sizeof(unsigned int)) 2904 if (sizeof (unsigned long) > sizeof (unsigned int))
2861 for (i = 0; i < SIGSZ; ++i) 2905 for (i = 0; i < SIGSZ; ++i)
2862 { 2906 {
2863 unsigned long s = r->sig[i]; 2907 unsigned long s = r->sig[i];
2864 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2)); 2908 h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
2865 } 2909 }
2997 24, 3041 24,
2998 -125, 3042 -125,
2999 128, 3043 128,
3000 31, 3044 31,
3001 31, 3045 31,
3002 false, 3046 32,
3003 true, 3047 false,
3004 true, 3048 true,
3005 true, 3049 true,
3006 true, 3050 true,
3007 true, 3051 true,
3008 true, 3052 true,
3009 false 3053 true,
3054 false,
3055 "ieee_single"
3010 }; 3056 };
3011 3057
3012 const struct real_format mips_single_format = 3058 const struct real_format mips_single_format =
3013 { 3059 {
3014 encode_ieee_single, 3060 encode_ieee_single,
3018 24, 3064 24,
3019 -125, 3065 -125,
3020 128, 3066 128,
3021 31, 3067 31,
3022 31, 3068 31,
3023 false, 3069 32,
3024 true, 3070 false,
3025 true, 3071 true,
3026 true, 3072 true,
3027 true, 3073 true,
3028 true, 3074 true,
3029 false, 3075 true,
3030 true 3076 false,
3077 true,
3078 "mips_single"
3031 }; 3079 };
3032 3080
3033 const struct real_format motorola_single_format = 3081 const struct real_format motorola_single_format =
3034 { 3082 {
3035 encode_ieee_single, 3083 encode_ieee_single,
3039 24, 3087 24,
3040 -125, 3088 -125,
3041 128, 3089 128,
3042 31, 3090 31,
3043 31, 3091 31,
3044 false, 3092 32,
3045 true, 3093 false,
3046 true, 3094 true,
3047 true, 3095 true,
3048 true, 3096 true,
3049 true, 3097 true,
3050 true, 3098 true,
3051 true 3099 true,
3100 true,
3101 "motorola_single"
3052 }; 3102 };
3053 3103
3054 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE 3104 /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
3055 single precision with the following differences: 3105 single precision with the following differences:
3056 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT 3106 - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
3071 24, 3121 24,
3072 -125, 3122 -125,
3073 129, 3123 129,
3074 31, 3124 31,
3075 31, 3125 31,
3076 true, 3126 0,
3077 false, 3127 true,
3078 false, 3128 false,
3079 false, 3129 false,
3080 true, 3130 false,
3081 true, 3131 true,
3082 false, 3132 true,
3083 false 3133 false,
3134 false,
3135 "spu_single"
3084 }; 3136 };
3085 3137
3086 /* IEEE double-precision format. */ 3138 /* IEEE double-precision format. */
3087 3139
3088 static void encode_ieee_double (const struct real_format *fmt, 3140 static void encode_ieee_double (const struct real_format *fmt,
3280 53, 3332 53,
3281 -1021, 3333 -1021,
3282 1024, 3334 1024,
3283 63, 3335 63,
3284 63, 3336 63,
3285 false, 3337 64,
3286 true, 3338 false,
3287 true, 3339 true,
3288 true, 3340 true,
3289 true, 3341 true,
3290 true, 3342 true,
3291 true, 3343 true,
3292 false 3344 true,
3345 false,
3346 "ieee_double"
3293 }; 3347 };
3294 3348
3295 const struct real_format mips_double_format = 3349 const struct real_format mips_double_format =
3296 { 3350 {
3297 encode_ieee_double, 3351 encode_ieee_double,
3301 53, 3355 53,
3302 -1021, 3356 -1021,
3303 1024, 3357 1024,
3304 63, 3358 63,
3305 63, 3359 63,
3306 false, 3360 64,
3307 true, 3361 false,
3308 true, 3362 true,
3309 true, 3363 true,
3310 true, 3364 true,
3311 true, 3365 true,
3312 false, 3366 true,
3313 true 3367 false,
3368 true,
3369 "mips_double"
3314 }; 3370 };
3315 3371
3316 const struct real_format motorola_double_format = 3372 const struct real_format motorola_double_format =
3317 { 3373 {
3318 encode_ieee_double, 3374 encode_ieee_double,
3322 53, 3378 53,
3323 -1021, 3379 -1021,
3324 1024, 3380 1024,
3325 63, 3381 63,
3326 63, 3382 63,
3327 false, 3383 64,
3328 true, 3384 false,
3329 true, 3385 true,
3330 true, 3386 true,
3331 true, 3387 true,
3332 true, 3388 true,
3333 true, 3389 true,
3334 true 3390 true,
3391 true,
3392 "motorola_double"
3335 }; 3393 };
3336 3394
3337 /* IEEE extended real format. This comes in three flavors: Intel's as 3395 /* IEEE extended real format. This comes in three flavors: Intel's as
3338 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel 3396 a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
3339 12- and 16-byte images may be big- or little endian; Motorola's is 3397 12- and 16-byte images may be big- or little endian; Motorola's is
3466 const REAL_VALUE_TYPE *r) 3524 const REAL_VALUE_TYPE *r)
3467 { 3525 {
3468 long intermed[3]; 3526 long intermed[3];
3469 encode_ieee_extended (fmt, intermed, r); 3527 encode_ieee_extended (fmt, intermed, r);
3470 3528
3529 if (r->cl == rvc_inf)
3530 /* For infinity clear the explicit integer bit again, so that the
3531 format matches the canonical infinity generated by the FPU. */
3532 intermed[1] = 0;
3533
3471 /* Motorola chips are assumed always to be big-endian. Also, the 3534 /* Motorola chips are assumed always to be big-endian. Also, the
3472 padding in a Motorola extended real goes between the exponent and 3535 padding in a Motorola extended real goes between the exponent and
3473 the mantissa. At this point the mantissa is entirely within 3536 the mantissa. At this point the mantissa is entirely within
3474 elements 0 and 1 of intermed, and the exponent entirely within 3537 elements 0 and 1 of intermed, and the exponent entirely within
3475 element 2, so all we have to do is swap the order around, and 3538 element 2, so all we have to do is swap the order around, and
3661 64, 3724 64,
3662 -16382, 3725 -16382,
3663 16384, 3726 16384,
3664 95, 3727 95,
3665 95, 3728 95,
3666 false, 3729 0,
3667 true, 3730 false,
3668 true, 3731 true,
3669 true, 3732 true,
3670 true, 3733 true,
3671 true, 3734 true,
3672 true, 3735 true,
3673 true 3736 true,
3737 true,
3738 "ieee_extended_motorola"
3674 }; 3739 };
3675 3740
3676 const struct real_format ieee_extended_intel_96_format = 3741 const struct real_format ieee_extended_intel_96_format =
3677 { 3742 {
3678 encode_ieee_extended_intel_96, 3743 encode_ieee_extended_intel_96,
3682 64, 3747 64,
3683 -16381, 3748 -16381,
3684 16384, 3749 16384,
3685 79, 3750 79,
3686 79, 3751 79,
3687 false, 3752 65,
3688 true, 3753 false,
3689 true, 3754 true,
3690 true, 3755 true,
3691 true, 3756 true,
3692 true, 3757 true,
3693 true, 3758 true,
3694 false 3759 true,
3760 false,
3761 "ieee_extended_intel_96"
3695 }; 3762 };
3696 3763
3697 const struct real_format ieee_extended_intel_128_format = 3764 const struct real_format ieee_extended_intel_128_format =
3698 { 3765 {
3699 encode_ieee_extended_intel_128, 3766 encode_ieee_extended_intel_128,
3703 64, 3770 64,
3704 -16381, 3771 -16381,
3705 16384, 3772 16384,
3706 79, 3773 79,
3707 79, 3774 79,
3708 false, 3775 65,
3709 true, 3776 false,
3710 true, 3777 true,
3711 true, 3778 true,
3712 true, 3779 true,
3713 true, 3780 true,
3714 true, 3781 true,
3715 false 3782 true,
3783 false,
3784 "ieee_extended_intel_128"
3716 }; 3785 };
3717 3786
3718 /* The following caters to i386 systems that set the rounding precision 3787 /* The following caters to i386 systems that set the rounding precision
3719 to 53 bits instead of 64, e.g. FreeBSD. */ 3788 to 53 bits instead of 64, e.g. FreeBSD. */
3720 const struct real_format ieee_extended_intel_96_round_53_format = 3789 const struct real_format ieee_extended_intel_96_round_53_format =
3726 53, 3795 53,
3727 -16381, 3796 -16381,
3728 16384, 3797 16384,
3729 79, 3798 79,
3730 79, 3799 79,
3731 false, 3800 33,
3732 true, 3801 false,
3733 true, 3802 true,
3734 true, 3803 true,
3735 true, 3804 true,
3736 true, 3805 true,
3737 true, 3806 true,
3738 false 3807 true,
3808 false,
3809 "ieee_extended_intel_96_round_53"
3739 }; 3810 };
3740 3811
3741 /* IBM 128-bit extended precision format: a pair of IEEE double precision 3812 /* IBM 128-bit extended precision format: a pair of IEEE double precision
3742 numbers whose sum is equal to the extended precision value. The number 3813 numbers whose sum is equal to the extended precision value. The number
3743 with greater magnitude is first. This format has the same magnitude 3814 with greater magnitude is first. This format has the same magnitude
3814 53, 3885 53,
3815 -1021 + 53, 3886 -1021 + 53,
3816 1024, 3887 1024,
3817 127, 3888 127,
3818 -1, 3889 -1,
3819 false, 3890 0,
3820 true, 3891 false,
3821 true, 3892 true,
3822 true, 3893 true,
3823 true, 3894 true,
3824 true, 3895 true,
3825 true, 3896 true,
3826 false 3897 true,
3898 false,
3899 "ibm_extended"
3827 }; 3900 };
3828 3901
3829 const struct real_format mips_extended_format = 3902 const struct real_format mips_extended_format =
3830 { 3903 {
3831 encode_ibm_extended, 3904 encode_ibm_extended,
3835 53, 3908 53,
3836 -1021 + 53, 3909 -1021 + 53,
3837 1024, 3910 1024,
3838 127, 3911 127,
3839 -1, 3912 -1,
3840 false, 3913 0,
3841 true, 3914 false,
3842 true, 3915 true,
3843 true, 3916 true,
3844 true, 3917 true,
3845 true, 3918 true,
3846 false, 3919 true,
3847 true 3920 false,
3921 true,
3922 "mips_extended"
3848 }; 3923 };
3849 3924
3850 3925
3851 /* IEEE quad precision format. */ 3926 /* IEEE quad precision format. */
3852 3927
4098 113, 4173 113,
4099 -16381, 4174 -16381,
4100 16384, 4175 16384,
4101 127, 4176 127,
4102 127, 4177 127,
4103 false, 4178 128,
4104 true, 4179 false,
4105 true, 4180 true,
4106 true, 4181 true,
4107 true, 4182 true,
4108 true, 4183 true,
4109 true, 4184 true,
4110 false 4185 true,
4186 false,
4187 "ieee_quad"
4111 }; 4188 };
4112 4189
4113 const struct real_format mips_quad_format = 4190 const struct real_format mips_quad_format =
4114 { 4191 {
4115 encode_ieee_quad, 4192 encode_ieee_quad,
4119 113, 4196 113,
4120 -16381, 4197 -16381,
4121 16384, 4198 16384,
4122 127, 4199 127,
4123 127, 4200 127,
4124 false, 4201 128,
4125 true, 4202 false,
4126 true, 4203 true,
4127 true, 4204 true,
4128 true, 4205 true,
4129 true, 4206 true,
4130 false, 4207 true,
4131 true 4208 false,
4209 true,
4210 "mips_quad"
4132 }; 4211 };
4133 4212
4134 /* Descriptions of VAX floating point formats can be found beginning at 4213 /* Descriptions of VAX floating point formats can be found beginning at
4135 4214
4136 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format 4215 http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
4419 24, 4498 24,
4420 -127, 4499 -127,
4421 127, 4500 127,
4422 15, 4501 15,
4423 15, 4502 15,
4424 false, 4503 0,
4425 false, 4504 false,
4426 false, 4505 false,
4427 false, 4506 false,
4428 false, 4507 false,
4429 false, 4508 false,
4430 false, 4509 false,
4431 false 4510 false,
4511 false,
4512 "vax_f"
4432 }; 4513 };
4433 4514
4434 const struct real_format vax_d_format = 4515 const struct real_format vax_d_format =
4435 { 4516 {
4436 encode_vax_d, 4517 encode_vax_d,
4440 56, 4521 56,
4441 -127, 4522 -127,
4442 127, 4523 127,
4443 15, 4524 15,
4444 15, 4525 15,
4445 false, 4526 0,
4446 false, 4527 false,
4447 false, 4528 false,
4448 false, 4529 false,
4449 false, 4530 false,
4450 false, 4531 false,
4451 false, 4532 false,
4452 false 4533 false,
4534 false,
4535 "vax_d"
4453 }; 4536 };
4454 4537
4455 const struct real_format vax_g_format = 4538 const struct real_format vax_g_format =
4456 { 4539 {
4457 encode_vax_g, 4540 encode_vax_g,
4461 53, 4544 53,
4462 -1023, 4545 -1023,
4463 1023, 4546 1023,
4464 15, 4547 15,
4465 15, 4548 15,
4466 false, 4549 0,
4467 false, 4550 false,
4468 false, 4551 false,
4469 false, 4552 false,
4470 false, 4553 false,
4471 false, 4554 false,
4472 false, 4555 false,
4473 false 4556 false,
4557 false,
4558 "vax_g"
4474 }; 4559 };
4475 4560
4476 /* Encode real R into a single precision DFP value in BUF. */ 4561 /* Encode real R into a single precision DFP value in BUF. */
4477 static void 4562 static void
4478 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED, 4563 encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
4537 7, 4622 7,
4538 -94, 4623 -94,
4539 97, 4624 97,
4540 31, 4625 31,
4541 31, 4626 31,
4542 false, 4627 32,
4543 true, 4628 false,
4544 true, 4629 true,
4545 true, 4630 true,
4546 true, 4631 true,
4547 true, 4632 true,
4548 true, 4633 true,
4549 false 4634 true,
4635 false,
4636 "decimal_single"
4550 }; 4637 };
4551 4638
4552 /* Double precision decimal floating point (IEEE 754). */ 4639 /* Double precision decimal floating point (IEEE 754). */
4553 const struct real_format decimal_double_format = 4640 const struct real_format decimal_double_format =
4554 { 4641 {
4559 16, 4646 16,
4560 -382, 4647 -382,
4561 385, 4648 385,
4562 63, 4649 63,
4563 63, 4650 63,
4564 false, 4651 64,
4565 true, 4652 false,
4566 true, 4653 true,
4567 true, 4654 true,
4568 true, 4655 true,
4569 true, 4656 true,
4570 true, 4657 true,
4571 false 4658 true,
4659 false,
4660 "decimal_double"
4572 }; 4661 };
4573 4662
4574 /* Quad precision decimal floating point (IEEE 754). */ 4663 /* Quad precision decimal floating point (IEEE 754). */
4575 const struct real_format decimal_quad_format = 4664 const struct real_format decimal_quad_format =
4576 { 4665 {
4581 34, 4670 34,
4582 -6142, 4671 -6142,
4583 6145, 4672 6145,
4584 127, 4673 127,
4585 127, 4674 127,
4586 false, 4675 128,
4587 true, 4676 false,
4588 true, 4677 true,
4589 true, 4678 true,
4590 true, 4679 true,
4591 true, 4680 true,
4592 true, 4681 true,
4593 false 4682 true,
4683 false,
4684 "decimal_quad"
4594 }; 4685 };
4595 4686
4596 /* Encode half-precision floats. This routine is used both for the IEEE 4687 /* Encode half-precision floats. This routine is used both for the IEEE
4597 ARM alternative encodings. */ 4688 ARM alternative encodings. */
4598 static void 4689 static void
4718 11, 4809 11,
4719 -13, 4810 -13,
4720 16, 4811 16,
4721 15, 4812 15,
4722 15, 4813 15,
4723 false, 4814 16,
4724 true, 4815 false,
4725 true, 4816 true,
4726 true, 4817 true,
4727 true, 4818 true,
4728 true, 4819 true,
4729 true, 4820 true,
4730 false 4821 true,
4822 false,
4823 "ieee_half"
4731 }; 4824 };
4732 4825
4733 /* ARM's alternative half-precision format, similar to IEEE but with 4826 /* ARM's alternative half-precision format, similar to IEEE but with
4734 no reserved exponent value for NaNs and infinities; rather, it just 4827 no reserved exponent value for NaNs and infinities; rather, it just
4735 extends the range of exponents by one. */ 4828 extends the range of exponents by one. */
4742 11, 4835 11,
4743 -13, 4836 -13,
4744 17, 4837 17,
4745 15, 4838 15,
4746 15, 4839 15,
4747 false, 4840 0,
4748 true, 4841 false,
4749 false, 4842 true,
4750 false, 4843 false,
4751 true, 4844 false,
4752 true, 4845 true,
4753 false, 4846 true,
4754 false 4847 false,
4848 false,
4849 "arm_half"
4755 }; 4850 };
4756 4851
4757 /* A synthetic "format" for internal arithmetic. It's the size of the 4852 /* A synthetic "format" for internal arithmetic. It's the size of the
4758 internal significand minus the two bits needed for proper rounding. 4853 internal significand minus the two bits needed for proper rounding.
4759 The encode and decode routines exist only to satisfy our paranoia 4854 The encode and decode routines exist only to satisfy our paranoia
4787 SIGNIFICAND_BITS - 2, 4882 SIGNIFICAND_BITS - 2,
4788 -MAX_EXP, 4883 -MAX_EXP,
4789 MAX_EXP, 4884 MAX_EXP,
4790 -1, 4885 -1,
4791 -1, 4886 -1,
4792 false, 4887 0,
4793 false, 4888 false,
4794 true, 4889 false,
4795 true, 4890 true,
4796 false, 4891 true,
4797 true, 4892 false,
4798 true, 4893 true,
4799 false 4894 true,
4895 false,
4896 "real_internal"
4800 }; 4897 };
4801 4898
4802 /* Calculate the square root of X in mode MODE, and store the result 4899 /* Calculate X raised to the integer exponent N in format FMT and store
4803 in R. Return TRUE if the operation does not raise an exception.
4804 For details see "High Precision Division and Square Root",
4805 Alan H. Karp and Peter Markstein, HP Lab Report 93-93-42, June
4806 1993. http://www.hpl.hp.com/techreports/93/HPL-93-42.pdf. */
4807
4808 bool
4809 real_sqrt (REAL_VALUE_TYPE *r, enum machine_mode mode,
4810 const REAL_VALUE_TYPE *x)
4811 {
4812 static REAL_VALUE_TYPE halfthree;
4813 static bool init = false;
4814 REAL_VALUE_TYPE h, t, i;
4815 int iter, exp;
4816
4817 /* sqrt(-0.0) is -0.0. */
4818 if (real_isnegzero (x))
4819 {
4820 *r = *x;
4821 return false;
4822 }
4823
4824 /* Negative arguments return NaN. */
4825 if (real_isneg (x))
4826 {
4827 get_canonical_qnan (r, 0);
4828 return false;
4829 }
4830
4831 /* Infinity and NaN return themselves. */
4832 if (!real_isfinite (x))
4833 {
4834 *r = *x;
4835 return false;
4836 }
4837
4838 if (!init)
4839 {
4840 do_add (&halfthree, &dconst1, &dconsthalf, 0);
4841 init = true;
4842 }
4843
4844 /* Initial guess for reciprocal sqrt, i. */
4845 exp = real_exponent (x);
4846 real_ldexp (&i, &dconst1, -exp/2);
4847
4848 /* Newton's iteration for reciprocal sqrt, i. */
4849 for (iter = 0; iter < 16; iter++)
4850 {
4851 /* i(n+1) = i(n) * (1.5 - 0.5*i(n)*i(n)*x). */
4852 do_multiply (&t, x, &i);
4853 do_multiply (&h, &t, &i);
4854 do_multiply (&t, &h, &dconsthalf);
4855 do_add (&h, &halfthree, &t, 1);
4856 do_multiply (&t, &i, &h);
4857
4858 /* Check for early convergence. */
4859 if (iter >= 6 && real_identical (&i, &t))
4860 break;
4861
4862 /* ??? Unroll loop to avoid copying. */
4863 i = t;
4864 }
4865
4866 /* Final iteration: r = i*x + 0.5*i*x*(1.0 - i*(i*x)). */
4867 do_multiply (&t, x, &i);
4868 do_multiply (&h, &t, &i);
4869 do_add (&i, &dconst1, &h, 1);
4870 do_multiply (&h, &t, &i);
4871 do_multiply (&i, &dconsthalf, &h);
4872 do_add (&h, &t, &i, 0);
4873
4874 /* ??? We need a Tuckerman test to get the last bit. */
4875
4876 real_convert (r, mode, &h);
4877 return true;
4878 }
4879
4880 /* Calculate X raised to the integer exponent N in mode MODE and store
4881 the result in R. Return true if the result may be inexact due to 4900 the result in R. Return true if the result may be inexact due to
4882 loss of precision. The algorithm is the classic "left-to-right binary 4901 loss of precision. The algorithm is the classic "left-to-right binary
4883 method" described in section 4.6.3 of Donald Knuth's "Seminumerical 4902 method" described in section 4.6.3 of Donald Knuth's "Seminumerical
4884 Algorithms", "The Art of Computer Programming", Volume 2. */ 4903 Algorithms", "The Art of Computer Programming", Volume 2. */
4885 4904
4886 bool 4905 bool
4887 real_powi (REAL_VALUE_TYPE *r, enum machine_mode mode, 4906 real_powi (REAL_VALUE_TYPE *r, format_helper fmt,
4888 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n) 4907 const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
4889 { 4908 {
4890 unsigned HOST_WIDE_INT bit; 4909 unsigned HOST_WIDE_INT bit;
4891 REAL_VALUE_TYPE t; 4910 REAL_VALUE_TYPE t;
4892 bool inexact = false; 4911 bool inexact = false;
4907 } 4926 }
4908 else 4927 else
4909 neg = false; 4928 neg = false;
4910 4929
4911 t = *x; 4930 t = *x;
4912 bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); 4931 bit = HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1);
4913 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++) 4932 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
4914 { 4933 {
4915 if (init) 4934 if (init)
4916 { 4935 {
4917 inexact |= do_multiply (&t, &t, &t); 4936 inexact |= do_multiply (&t, &t, &t);
4924 } 4943 }
4925 4944
4926 if (neg) 4945 if (neg)
4927 inexact |= do_divide (&t, &dconst1, &t); 4946 inexact |= do_divide (&t, &dconst1, &t);
4928 4947
4929 real_convert (r, mode, &t); 4948 real_convert (r, fmt, &t);
4930 return inexact; 4949 return inexact;
4931 } 4950 }
4932 4951
4933 /* Round X to the nearest integer not larger in absolute value, i.e. 4952 /* Round X to the nearest integer not larger in absolute value, i.e.
4934 towards zero, placing the result in R in mode MODE. */ 4953 towards zero, placing the result in R in format FMT. */
4935 4954
4936 void 4955 void
4937 real_trunc (REAL_VALUE_TYPE *r, enum machine_mode mode, 4956 real_trunc (REAL_VALUE_TYPE *r, format_helper fmt,
4938 const REAL_VALUE_TYPE *x) 4957 const REAL_VALUE_TYPE *x)
4939 { 4958 {
4940 do_fix_trunc (r, x); 4959 do_fix_trunc (r, x);
4941 if (mode != VOIDmode) 4960 if (fmt)
4942 real_convert (r, mode, r); 4961 real_convert (r, fmt, r);
4943 } 4962 }
4944 4963
4945 /* Round X to the largest integer not greater in value, i.e. round 4964 /* Round X to the largest integer not greater in value, i.e. round
4946 down, placing the result in R in mode MODE. */ 4965 down, placing the result in R in format FMT. */
4947 4966
4948 void 4967 void
4949 real_floor (REAL_VALUE_TYPE *r, enum machine_mode mode, 4968 real_floor (REAL_VALUE_TYPE *r, format_helper fmt,
4950 const REAL_VALUE_TYPE *x) 4969 const REAL_VALUE_TYPE *x)
4951 { 4970 {
4952 REAL_VALUE_TYPE t; 4971 REAL_VALUE_TYPE t;
4953 4972
4954 do_fix_trunc (&t, x); 4973 do_fix_trunc (&t, x);
4955 if (! real_identical (&t, x) && x->sign) 4974 if (! real_identical (&t, x) && x->sign)
4956 do_add (&t, &t, &dconstm1, 0); 4975 do_add (&t, &t, &dconstm1, 0);
4957 if (mode != VOIDmode) 4976 if (fmt)
4958 real_convert (r, mode, &t); 4977 real_convert (r, fmt, &t);
4959 else 4978 else
4960 *r = t; 4979 *r = t;
4961 } 4980 }
4962 4981
4963 /* Round X to the smallest integer not less then argument, i.e. round 4982 /* Round X to the smallest integer not less then argument, i.e. round
4964 up, placing the result in R in mode MODE. */ 4983 up, placing the result in R in format FMT. */
4965 4984
4966 void 4985 void
4967 real_ceil (REAL_VALUE_TYPE *r, enum machine_mode mode, 4986 real_ceil (REAL_VALUE_TYPE *r, format_helper fmt,
4968 const REAL_VALUE_TYPE *x) 4987 const REAL_VALUE_TYPE *x)
4969 { 4988 {
4970 REAL_VALUE_TYPE t; 4989 REAL_VALUE_TYPE t;
4971 4990
4972 do_fix_trunc (&t, x); 4991 do_fix_trunc (&t, x);
4973 if (! real_identical (&t, x) && ! x->sign) 4992 if (! real_identical (&t, x) && ! x->sign)
4974 do_add (&t, &t, &dconst1, 0); 4993 do_add (&t, &t, &dconst1, 0);
4975 if (mode != VOIDmode) 4994 if (fmt)
4976 real_convert (r, mode, &t); 4995 real_convert (r, fmt, &t);
4977 else 4996 else
4978 *r = t; 4997 *r = t;
4979 } 4998 }
4980 4999
4981 /* Round X to the nearest integer, but round halfway cases away from 5000 /* Round X to the nearest integer, but round halfway cases away from
4982 zero. */ 5001 zero. */
4983 5002
4984 void 5003 void
4985 real_round (REAL_VALUE_TYPE *r, enum machine_mode mode, 5004 real_round (REAL_VALUE_TYPE *r, format_helper fmt,
4986 const REAL_VALUE_TYPE *x) 5005 const REAL_VALUE_TYPE *x)
4987 { 5006 {
4988 do_add (r, x, &dconsthalf, x->sign); 5007 do_add (r, x, &dconsthalf, x->sign);
4989 do_fix_trunc (r, r); 5008 do_fix_trunc (r, r);
4990 if (mode != VOIDmode) 5009 if (fmt)
4991 real_convert (r, mode, r); 5010 real_convert (r, fmt, r);
4992 } 5011 }
4993 5012
4994 /* Set the sign of R to the sign of X. */ 5013 /* Set the sign of R to the sign of X. */
4995 5014
4996 void 5015 void
4997 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x) 5016 real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
4998 { 5017 {
4999 r->sign = x->sign; 5018 r->sign = x->sign;
5000 } 5019 }
5001 5020
5002 /* Check whether the real constant value given is an integer. */ 5021 /* Check whether the real constant value given is an integer.
5022 Returns false for signaling NaN. */
5003 5023
5004 bool 5024 bool
5005 real_isinteger (const REAL_VALUE_TYPE *c, enum machine_mode mode) 5025 real_isinteger (const REAL_VALUE_TYPE *c, format_helper fmt)
5006 { 5026 {
5007 REAL_VALUE_TYPE cint; 5027 REAL_VALUE_TYPE cint;
5008 5028
5009 real_trunc (&cint, mode, c); 5029 real_trunc (&cint, fmt, c);
5010 return real_identical (c, &cint); 5030 return real_identical (c, &cint);
5031 }
5032
5033 /* Check whether C is an integer that fits in a HOST_WIDE_INT,
5034 storing it in *INT_OUT if so. */
5035
5036 bool
5037 real_isinteger (const REAL_VALUE_TYPE *c, HOST_WIDE_INT *int_out)
5038 {
5039 REAL_VALUE_TYPE cint;
5040
5041 HOST_WIDE_INT n = real_to_integer (c);
5042 real_from_integer (&cint, VOIDmode, n, SIGNED);
5043 if (real_identical (c, &cint))
5044 {
5045 *int_out = n;
5046 return true;
5047 }
5048 return false;
5011 } 5049 }
5012 5050
5013 /* Write into BUF the maximum representable finite floating-point 5051 /* Write into BUF the maximum representable finite floating-point
5014 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex 5052 number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
5015 float string. LEN is the size of BUF, and the buffer must be large 5053 float string. LEN is the size of BUF, and the buffer must be large
5039 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4]; 5077 buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
5040 } 5078 }
5041 5079
5042 gcc_assert (strlen (buf) < len); 5080 gcc_assert (strlen (buf) < len);
5043 } 5081 }
5082
5083 /* True if mode M has a NaN representation and
5084 the treatment of NaN operands is important. */
5085
5086 bool
5087 HONOR_NANS (machine_mode m)
5088 {
5089 return MODE_HAS_NANS (m) && !flag_finite_math_only;
5090 }
5091
5092 bool
5093 HONOR_NANS (const_tree t)
5094 {
5095 return HONOR_NANS (element_mode (t));
5096 }
5097
5098 bool
5099 HONOR_NANS (const_rtx x)
5100 {
5101 return HONOR_NANS (GET_MODE (x));
5102 }
5103
5104 /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
5105
5106 bool
5107 HONOR_SNANS (machine_mode m)
5108 {
5109 return flag_signaling_nans && HONOR_NANS (m);
5110 }
5111
5112 bool
5113 HONOR_SNANS (const_tree t)
5114 {
5115 return HONOR_SNANS (element_mode (t));
5116 }
5117
5118 bool
5119 HONOR_SNANS (const_rtx x)
5120 {
5121 return HONOR_SNANS (GET_MODE (x));
5122 }
5123
5124 /* As for HONOR_NANS, but true if the mode can represent infinity and
5125 the treatment of infinite values is important. */
5126
5127 bool
5128 HONOR_INFINITIES (machine_mode m)
5129 {
5130 return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
5131 }
5132
5133 bool
5134 HONOR_INFINITIES (const_tree t)
5135 {
5136 return HONOR_INFINITIES (element_mode (t));
5137 }
5138
5139 bool
5140 HONOR_INFINITIES (const_rtx x)
5141 {
5142 return HONOR_INFINITIES (GET_MODE (x));
5143 }
5144
5145 /* Like HONOR_NANS, but true if the given mode distinguishes between
5146 positive and negative zero, and the sign of zero is important. */
5147
5148 bool
5149 HONOR_SIGNED_ZEROS (machine_mode m)
5150 {
5151 return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
5152 }
5153
5154 bool
5155 HONOR_SIGNED_ZEROS (const_tree t)
5156 {
5157 return HONOR_SIGNED_ZEROS (element_mode (t));
5158 }
5159
5160 bool
5161 HONOR_SIGNED_ZEROS (const_rtx x)
5162 {
5163 return HONOR_SIGNED_ZEROS (GET_MODE (x));
5164 }
5165
5166 /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
5167 and the rounding mode is important. */
5168
5169 bool
5170 HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
5171 {
5172 return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
5173 }
5174
5175 bool
5176 HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
5177 {
5178 return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
5179 }
5180
5181 bool
5182 HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
5183 {
5184 return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
5185 }