| ︙ | | |
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
+
-
-
-
-
-
+
-
-
-
-
+
-
|
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclTomMath.h"
#include <float.h>
#include <math.h>
/*
* Older MSVC has no copysign function, but it's available at least since
* MSVC++ 12.0 (that is Visual Studio 2013).
*/
#ifdef _WIN32
#if (defined(_MSC_VER) && (_MSC_VER < 1800))
inline static double
copysign(double a, double b) {
return _copysign(a, b);
#define copysign _copysign
}
#endif
/*
* This code supports (at least hypothetically), IBM, Cray, VAX and IEEE-754
* floating point; of these, only IEEE-754 can represent NaN. IEEE-754 can be
* uniquely determined by radix and by the widths of significand and exponent.
*/
|
| ︙ | | |
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
-
+
|
Tcl_WideUInt significand, int nSigDigs,
long exponent);
#ifdef IEEE_FLOATING_POINT
static double MakeNaN(int signum, Tcl_WideUInt tag);
#endif
static double RefineApproximation(double approx,
mp_int *exactSignificand, int exponent);
static void MulPow5(mp_int *, unsigned, mp_int *);
static mp_err MulPow5(mp_int *, unsigned, mp_int *) MP_WUR;
static int NormalizeRightward(Tcl_WideUInt *);
static int RequiredPrecision(Tcl_WideUInt);
static void DoubleToExpAndSig(double, Tcl_WideUInt *, int *,
int *);
static void TakeAbsoluteValue(Double *, int *);
static char * FormatInfAndNaN(Double *, int *, char **);
static char * FormatZero(int *, char **);
|
| ︙ | | |
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
+
|
size_t acceptLen; /* Number of characters following that
* point. */
int status = TCL_OK; /* Status to return to caller. */
char d = 0; /* Last hexadecimal digit scanned; initialized
* to avoid a compiler warning. */
int shift = 0; /* Amount to shift when accumulating binary */
int explicitOctal = 0;
mp_err err = MP_OKAY;
#define ALL_BITS ((Tcl_WideUInt)-1)
#define MOST_BITS (ALL_BITS >> 1)
/*
* Initialize bytes to start of the object's string rep if the caller
* didn't pass anything else.
|
| ︙ | | |
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
-
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
|
if ((octalSignificandWide != 0)
&& (((size_t)shift >=
CHAR_BIT*sizeof(Tcl_WideUInt))
|| (octalSignificandWide >
((Tcl_WideUInt)-1 >> shift)))) {
octalSignificandOverflow = 1;
mp_init_u64(&octalSignificandBig,
err = mp_init_u64(&octalSignificandBig,
octalSignificandWide);
}
}
if (!octalSignificandOverflow) {
octalSignificandWide =
(octalSignificandWide << shift) + (c - '0');
} else {
if (err == MP_OKAY) {
mp_mul_2d(&octalSignificandBig, shift,
&octalSignificandBig);
mp_add_d(&octalSignificandBig, (mp_digit)(c - '0'),
&octalSignificandBig);
err = mp_mul_2d(&octalSignificandBig, shift,
&octalSignificandBig);
}
if (err == MP_OKAY) {
err = mp_add_d(&octalSignificandBig, (mp_digit)(c - '0'),
&octalSignificandBig);
}
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
}
if (numSigDigs != 0) {
numSigDigs += numTrailZeros+1;
} else {
numSigDigs = 1;
}
|
| ︙ | | |
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
|
* large shifts first.
*/
if (significandWide != 0 &&
((size_t)shift >= CHAR_BIT*sizeof(Tcl_WideUInt) ||
significandWide > ((Tcl_WideUInt)-1 >> shift))) {
significandOverflow = 1;
mp_init_u64(&significandBig,
err = mp_init_u64(&significandBig,
significandWide);
}
}
if (!significandOverflow) {
significandWide = (significandWide << shift) + d;
} else {
mp_mul_2d(&significandBig, shift, &significandBig);
mp_add_d(&significandBig, (mp_digit) d, &significandBig);
}
} else if (err == MP_OKAY) {
err = mp_mul_2d(&significandBig, shift, &significandBig);
if (err == MP_OKAY) {
err = mp_add_d(&significandBig, (mp_digit) d, &significandBig);
}
}
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
numTrailZeros = 0;
state = HEXADECIMAL;
break;
case BINARY:
acceptState = state;
|
| ︙ | | |
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
|
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
|
* large shifts first.
*/
if (significandWide != 0 &&
((size_t)shift >= CHAR_BIT*sizeof(Tcl_WideUInt) ||
significandWide > ((Tcl_WideUInt)-1 >> shift))) {
significandOverflow = 1;
mp_init_u64(&significandBig,
err = mp_init_u64(&significandBig,
significandWide);
}
}
if (!significandOverflow) {
significandWide = (significandWide << shift) + 1;
} else {
mp_mul_2d(&significandBig, shift, &significandBig);
mp_add_d(&significandBig, (mp_digit) 1, &significandBig);
}
} else if (err == MP_OKAY) {
err = mp_mul_2d(&significandBig, shift, &significandBig);
if (err == MP_OKAY) {
err = mp_add_d(&significandBig, (mp_digit) 1, &significandBig);
}
}
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
numTrailZeros = 0;
state = BINARY;
break;
case ZERO_D:
if (c == '0') {
|
| ︙ | | |
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
|
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
|
-
+
-
-
+
+
+
+
+
-
+
-
-
+
+
+
+
+
-
+
-
-
+
+
-
-
+
+
-
+
-
+
+
+
+
-
+
-
+
-
-
+
+
-
+
-
+
+
+
+
|
acceptState, bytes);
case BINARY:
shift = numTrailZeros;
if (!significandOverflow && significandWide != 0 &&
((size_t)shift >= CHAR_BIT*sizeof(Tcl_WideUInt) ||
significandWide > (MOST_BITS + signum) >> shift)) {
significandOverflow = 1;
mp_init_u64(&significandBig, significandWide);
err = mp_init_u64(&significandBig, significandWide);
}
if (shift) {
if (!significandOverflow) {
significandWide <<= shift;
} else {
mp_mul_2d(&significandBig, shift, &significandBig);
} else if (err == MP_OKAY) {
err = mp_mul_2d(&significandBig, shift, &significandBig);
}
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
goto returnInteger;
case HEXADECIMAL:
/*
* Returning a hex integer. Final scaling step.
*/
shift = 4 * numTrailZeros;
if (!significandOverflow && significandWide !=0 &&
((size_t)shift >= CHAR_BIT*sizeof(Tcl_WideUInt) ||
significandWide > (MOST_BITS + signum) >> shift)) {
significandOverflow = 1;
mp_init_u64(&significandBig, significandWide);
err = mp_init_u64(&significandBig, significandWide);
}
if (shift) {
if (!significandOverflow) {
significandWide <<= shift;
} else {
mp_mul_2d(&significandBig, shift, &significandBig);
} else if (err == MP_OKAY) {
err = mp_mul_2d(&significandBig, shift, &significandBig);
}
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
goto returnInteger;
case OCTAL:
/*
* Returning an octal integer. Final scaling step.
*/
shift = 3 * numTrailZeros;
if (!octalSignificandOverflow && octalSignificandWide != 0 &&
((size_t)shift >= CHAR_BIT*sizeof(Tcl_WideUInt) ||
octalSignificandWide > (MOST_BITS + signum) >> shift)) {
octalSignificandOverflow = 1;
mp_init_u64(&octalSignificandBig,
err = mp_init_u64(&octalSignificandBig,
octalSignificandWide);
}
if (shift) {
if (!octalSignificandOverflow) {
octalSignificandWide <<= shift;
} else {
mp_mul_2d(&octalSignificandBig, shift,
} else if (err == MP_OKAY) {
err = mp_mul_2d(&octalSignificandBig, shift,
&octalSignificandBig);
}
}
if (!octalSignificandOverflow) {
if (octalSignificandWide > (MOST_BITS + signum)) {
mp_init_u64(&octalSignificandBig,
if ((err == MP_OKAY) && (octalSignificandWide > (MOST_BITS + signum))) {
err = mp_init_u64(&octalSignificandBig,
octalSignificandWide);
octalSignificandOverflow = 1;
} else {
objPtr->typePtr = &tclIntType;
if (signum) {
objPtr->internalRep.wideValue =
- (Tcl_WideInt) octalSignificandWide;
} else {
objPtr->internalRep.wideValue =
(Tcl_WideInt) octalSignificandWide;
}
}
}
if (octalSignificandOverflow) {
if ((err == MP_OKAY) && octalSignificandOverflow) {
if (signum) {
(void)mp_neg(&octalSignificandBig, &octalSignificandBig);
err = mp_neg(&octalSignificandBig, &octalSignificandBig);
}
TclSetBignumIntRep(objPtr, &octalSignificandBig);
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
break;
case ZERO:
case DECIMAL:
significandOverflow = AccumulateDecimalDigit(0, numTrailZeros-1,
&significandWide, &significandBig, significandOverflow);
if (!significandOverflow && (significandWide > MOST_BITS+signum)){
if ((err == MP_OKAY) && !significandOverflow && (significandWide > MOST_BITS+signum)) {
significandOverflow = 1;
mp_init_u64(&significandBig, significandWide);
err = mp_init_u64(&significandBig, significandWide);
}
returnInteger:
if (!significandOverflow) {
if (significandWide > MOST_BITS+signum) {
mp_init_u64(&significandBig,
if ((err == MP_OKAY) && (significandWide > MOST_BITS+signum)) {
err = mp_init_u64(&significandBig,
significandWide);
significandOverflow = 1;
} else {
objPtr->typePtr = &tclIntType;
if (signum) {
objPtr->internalRep.wideValue =
- (Tcl_WideInt) significandWide;
} else {
objPtr->internalRep.wideValue =
(Tcl_WideInt) significandWide;
}
}
}
if (significandOverflow) {
if ((err == MP_OKAY) && significandOverflow) {
if (signum) {
(void)mp_neg(&significandBig, &significandBig);
err = mp_neg(&significandBig, &significandBig);
}
TclSetBignumIntRep(objPtr, &significandBig);
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
break;
case FRACTION:
case EXPONENT:
/*
|
| ︙ | | |
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
|
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
|
-
+
|
}
objPtr->typePtr = &tclDoubleType;
break;
#ifdef IEEE_FLOATING_POINT
case sNAN:
case sNANFINISH:
objPtr->internalRep.doubleValue = MakeNaN(signum,significandWide);
objPtr->internalRep.doubleValue = MakeNaN(signum, significandWide);
objPtr->typePtr = &tclDoubleType;
break;
#endif
case INITIAL:
/* This case only to silence compiler warning. */
Tcl_Panic("TclParseNumber: state INITIAL can't happen here");
}
|
| ︙ | | |
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
|
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
|
-
+
+
+
-
-
-
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
+
-
-
+
+
+
+
|
} else if (numZeros >= maxpow10_wide
|| w > ((Tcl_WideUInt)-1-digit)/pow10_wide[numZeros+1]) {
/*
* Wide multiplication will overflow. Expand the number to a
* bignum and fall through into the bignum case.
*/
mp_init_u64(bignumRepPtr, w);
if (mp_init_u64(bignumRepPtr, w) != MP_OKAY) {
return 0;
}
} else {
/*
* Wide multiplication.
*/
*wideRepPtr = w * pow10_wide[numZeros+1] + digit;
return 0;
}
}
/*
* Bignum multiplication.
*/
if (numZeros < log10_DIGIT_MAX) {
/*
* Up to about 8 zeros - single digit multiplication.
*/
mp_mul_d(bignumRepPtr, (mp_digit) pow10_wide[numZeros+1],
bignumRepPtr);
mp_add_d(bignumRepPtr, (mp_digit) digit, bignumRepPtr);
if ((mp_mul_d(bignumRepPtr, (mp_digit) pow10_wide[numZeros+1],
bignumRepPtr) != MP_OKAY)
|| (mp_add_d(bignumRepPtr, (mp_digit) digit, bignumRepPtr) != MP_OKAY))
return 0;
} else {
mp_err err;
/*
* More than single digit multiplication. Multiply by the appropriate
* small powers of 5, and then shift. Large strings of zeroes are
* eaten 256 at a time; this is less efficient than it could be, but
* seems implausible. We presume that MP_DIGIT_BIT is at least 27. The
* first multiplication, by up to 10**7, is done with a one-DIGIT
* multiply (this presumes that MP_DIGIT_BIT >= 24).
*/
n = numZeros + 1;
mp_mul_d(bignumRepPtr, (mp_digit) pow10_wide[n&0x7], bignumRepPtr);
for (i=3; i<=7; ++i) {
err = mp_mul_d(bignumRepPtr, (mp_digit) pow10_wide[n&0x7], bignumRepPtr);
for (i = 3; (err == MP_OKAY) && (i <= 7); ++i) {
if (n & (1 << i)) {
mp_mul(bignumRepPtr, pow5+i, bignumRepPtr);
err = mp_mul(bignumRepPtr, pow5+i, bignumRepPtr);
}
}
while (n >= 256) {
mp_mul(bignumRepPtr, pow5+8, bignumRepPtr);
while ((err == MP_OKAY) && (n >= 256)) {
err = mp_mul(bignumRepPtr, pow5+8, bignumRepPtr);
n -= 256;
}
if ((err != MP_OKAY)
mp_mul_2d(bignumRepPtr, (int)(numZeros+1)&~0x7, bignumRepPtr);
mp_add_d(bignumRepPtr, (mp_digit) digit, bignumRepPtr);
|| (mp_mul_2d(bignumRepPtr, (int)(numZeros+1)&~0x7, bignumRepPtr) != MP_OKAY)
|| (mp_add_d(bignumRepPtr, (mp_digit) digit, bignumRepPtr) != MP_OKAY)) {
return 0;
}
}
return 1;
}
/*
*----------------------------------------------------------------------
|
| ︙ | | |
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
|
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
|
-
+
+
+
|
}
/*
* All the easy cases have failed. Promote ths significand to bignum and
* call MakeHighPrecisionDouble to do it the hard way.
*/
mp_init_u64(&significandBig, significand);
if (mp_init_u64(&significandBig, significand) != MP_OKAY) {
return 0.0;
}
retval = MakeHighPrecisionDouble(0, &significandBig, numSigDigs,
exponent);
mp_clear(&significandBig);
/*
* Come here to return the computed value.
*/
|
| ︙ | | |
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
|
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
|
-
+
|
MakeHighPrecisionDouble(
int signum, /* 1=negative, 0=nonnegative */
mp_int *significand, /* Exact significand of the number */
int numSigDigs, /* Number of significant digits */
long exponent) /* Power of 10 by which to multiply */
{
double retval;
int machexp; /* Machine exponent of a power of 10. */
int machexp = 0; /* Machine exponent of a power of 10. */
/*
* With gcc on x86, the floating point rounding mode is double-extended.
* This causes the result of double-precision calculations to be rounded
* twice: once to the precision of double-extended and then again to the
* precision of double. Double-rounding introduces gratuitous errors of 1
* ulp, so we need to change rounding mode to 53-bits.
|
| ︙ | | |
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
|
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
|
+
|
int rteExponent; /* Exponent of the round-to-even result */
int shift; /* Shift count for converting numerator
* and denominator of corrector to floating
* point */
Tcl_WideInt rteSigWide; /* Wide integer version of the significand
* for testing evenness */
int i;
mp_err err = MP_OKAY;
/*
* The first approximation is always low. If we find that it's HUGE_VAL,
* we're done.
*/
if (approxResult == HUGE_VAL) {
|
| ︙ | | |
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
|
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
|
-
+
+
+
-
-
+
+
+
-
-
+
+
+
+
+
-
+
+
-
+
+
+
-
+
+
-
+
-
+
+
+
+
+
-
-
+
+
-
+
+
+
+
+
+
|
* Compute twoMv as 2*M*v, where v is the approximate value.
* This is done by bit-whacking to calculate 2**(M2+1)*significand,
* and then multiplying by 5**M5.
*/
msb = binExponent + M2; /* 1008 */
nDigits = msb / MP_DIGIT_BIT + 1;
mp_init_size(&twoMv, nDigits);
if (mp_init_size(&twoMv, nDigits) != MP_OKAY) {
return approxResult;
}
i = (msb % MP_DIGIT_BIT + 1);
twoMv.used = nDigits;
significand *= SafeLdExp(1.0, i);
while (--nDigits >= 0) {
twoMv.dp[nDigits] = (mp_digit) significand;
significand -= (mp_digit) significand;
significand = SafeLdExp(significand, MP_DIGIT_BIT);
}
for (i = 0; i <= 8; ++i) {
if (M5 & (1 << i)) {
mp_mul(&twoMv, pow5+i, &twoMv);
if (M5 & (1 << i) && (mp_mul(&twoMv, pow5+i, &twoMv) != MP_OKAY)) {
mp_clear(&twoMv);
return approxResult;
}
}
/*
* Compute twoMd as 2*M*d, where d is the exact value.
* This is done by multiplying by 5**(M5+exponent) and then multiplying
* by 2**(M5+exponent+1), which is, of couse, a left shift.
*/
mp_init_copy(&twoMd, exactSignificand);
for (i=0; i<=8; ++i) {
if (mp_init_copy(&twoMd, exactSignificand) != MP_OKAY) {
mp_clear(&twoMv);
return approxResult;
}
for (i = 0; (i <= 8); ++i) {
if ((M5 + exponent) & (1 << i)) {
mp_mul(&twoMd, pow5+i, &twoMd);
err = mp_mul(&twoMd, pow5+i, &twoMd);
}
}
if (err == MP_OKAY) {
mp_mul_2d(&twoMd, M2+exponent+1, &twoMd);
err = mp_mul_2d(&twoMd, M2+exponent+1, &twoMd);
}
/*
* Now let twoMd = twoMd - twoMv, the difference between the exact and
* approximate values.
*/
if (err == MP_OKAY) {
mp_sub(&twoMd, &twoMv, &twoMd);
err = mp_sub(&twoMd, &twoMv, &twoMd);
}
/*
* The result, 2Mv-2Md, needs to be divided by 2M to yield a correction
* term. Because 2M may well overflow a double, we need to scale the
* denominator by a factor of 2**binExponent-mantBits. Place that factor
* times 1/2 ULP into twoMd.
*/
scale = binExponent - mantBits - 1;
mp_set_u64(&twoMv, 1);
for (i=0; i<=8; ++i) {
for (i = 0; (i <= 8) && (err == MP_OKAY); ++i) {
if (M5 & (1 << i)) {
mp_mul(&twoMv, pow5+i, &twoMv);
err = mp_mul(&twoMv, pow5+i, &twoMv);
}
}
multiplier = M2 + scale + 1;
if (err != MP_OKAY) {
mp_clear(&twoMd);
mp_clear(&twoMv);
return approxResult;
if (multiplier > 0) {
mp_mul_2d(&twoMv, multiplier, &twoMv);
} else if (multiplier > 0) {
err = mp_mul_2d(&twoMv, multiplier, &twoMv);
} else if (multiplier < 0) {
mp_div_2d(&twoMv, -multiplier, &twoMv, NULL);
err = mp_div_2d(&twoMv, -multiplier, &twoMv, NULL);
}
if (err != MP_OKAY) {
mp_clear(&twoMd);
mp_clear(&twoMv);
return approxResult;
}
/*
* Will the eventual correction term be less than, equal to, or
* greater than 1/2 ULP?
*/
|
| ︙ | | |
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
|
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
|
-
-
+
+
+
+
+
+
+
+
+
|
/*
* Reduce the numerator and denominator of the corrector term so that
* they will fit in the floating point precision.
*/
shift = mp_count_bits(&twoMv) - FP_PRECISION - 1;
if (shift > 0) {
mp_div_2d(&twoMv, shift, &twoMv, NULL);
mp_div_2d(&twoMd, shift, &twoMd, NULL);
err = mp_div_2d(&twoMv, shift, &twoMv, NULL);
if (err == MP_OKAY) {
err = mp_div_2d(&twoMd, shift, &twoMd, NULL);
}
}
if (err != MP_OKAY) {
mp_clear(&twoMd);
mp_clear(&twoMv);
return approxResult;
}
/*
* Convert the numerator and denominator of the corrector term accurately
* to floating point numbers.
*/
|
| ︙ | | |
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
|
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
|
-
+
+
-
+
-
+
-
+
-
-
+
+
+
|
*
* Side effects:
* Stores base*5**n in result.
*
*----------------------------------------------------------------------
*/
static inline void
static inline mp_err
MulPow5(
mp_int *base, /* Number to multiply. */
unsigned n, /* Power of 5 to multiply by. */
mp_int *result) /* Place to store the result. */
{
mp_int *p = base;
int n13 = n / 13;
int r = n % 13;
mp_err err = MP_OKAY;
if (r != 0) {
mp_mul_d(p, dpow5[r], result);
err = mp_mul_d(p, dpow5[r], result);
p = result;
}
r = 0;
while (n13 != 0) {
while ((err == MP_OKAY) && (n13 != 0)) {
if (n13 & 1) {
mp_mul(p, pow5_13+r, result);
err = mp_mul(p, pow5_13+r, result);
p = result;
}
n13 >>= 1;
++r;
}
if (p != result) {
mp_copy(p, result);
if ((err == MP_OKAY) && (p != result)) {
err = mp_copy(p, result);
}
return err;
}
/*
*----------------------------------------------------------------------
*
* NormalizeRightward --
*
|
| ︙ | | |
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
|
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
|
-
-
+
|
/*
* Compare B and S-m - which is the same as comparing B+m and S - which we
* do by computing b+m and doing a bitwhack compare against
* 2**(MP_DIGIT_BIT*sd)
*/
mp_add(b, m, temp);
if (temp->used <= sd) { /* Too few digits to be > s */
if ((mp_add(b, m, temp) != MP_OKAY) || (temp->used <= sd)) { /* Too few digits to be > s */
return 0;
}
if (temp->used > sd+1 || temp->dp[sd] > 1) {
/* >= 2s */
return 1;
}
for (i = sd-1; i >= 0; --i) {
|
| ︙ | | |
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
|
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
|
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
* converted. */
mp_int mplus, mminus; /* Bounds for roundoff. */
mp_digit digit; /* Current output digit. */
char *s = retval; /* Cursor in the output buffer. */
int i; /* Index in the output buffer. */
mp_int temp;
int r1;
mp_err err = MP_OKAY;
/*
* b = bw * 2**b2 * 5**b5
* mminus = 5**m5
*/
mp_init_u64(&b, bw);
mp_init_set(&mminus, 1);
MulPow5(&b, b5, &b);
mp_mul_2d(&b, b2, &b);
if ((retval == NULL) || (mp_init_u64(&b, bw) != MP_OKAY)) {
return NULL;
}
if (mp_init_set(&mminus, 1) != MP_OKAY) {
mp_clear(&b);
return NULL;
}
err = MulPow5(&b, b5, &b);
if (err == MP_OKAY) {
err = mp_mul_2d(&b, b2, &b);
}
/*
* Adjust if the logarithm was guessed wrong.
*/
if (b.used <= sd) {
mp_mul_d(&b, 10, &b);
if ((err == MP_OKAY) && (b.used <= sd)) {
err = mp_mul_d(&b, 10, &b);
++m2plus; ++m2minus; ++m5;
ilim = ilim1;
--k;
}
/*
* mminus = 5**m5 * 2**m2minus
* mplus = 5**m5 * 2**m2plus
*/
if (err == MP_OKAY) {
mp_mul_2d(&mminus, m2minus, &mminus);
MulPow5(&mminus, m5, &mminus);
if (m2plus > m2minus) {
mp_init_copy(&mplus, &mminus);
mp_mul_2d(&mplus, m2plus-m2minus, &mplus);
}
mp_init(&temp);
err = mp_mul_2d(&mminus, m2minus, &mminus);
}
if (err == MP_OKAY) {
err = MulPow5(&mminus, m5, &mminus);
}
if ((err == MP_OKAY) && (m2plus > m2minus)) {
err = mp_init_copy(&mplus, &mminus);
if (err == MP_OKAY) {
err = mp_mul_2d(&mplus, m2plus-m2minus, &mplus);
}
}
if (err == MP_OKAY) {
err = mp_init(&temp);
}
/*
* Loop through the digits. Do division and mod by s == 2**(sd*MP_DIGIT_BIT)
* by mp_digit extraction.
*/
i = 0;
|
| ︙ | | |
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
|
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
|
+
-
-
-
-
+
+
+
+
+
+
+
-
+
|
break;
}
/*
* Advance to the next digit.
*/
if (err == MP_OKAY) {
mp_mul_d(&b, 10, &b);
mp_mul_d(&mminus, 10, &mminus);
if (m2plus > m2minus) {
mp_mul_2d(&mminus, m2plus-m2minus, &mplus);
err = mp_mul_d(&b, 10, &b);
}
if (err == MP_OKAY) {
err = mp_mul_d(&mminus, 10, &mminus);
}
if ((err == MP_OKAY) && (m2plus > m2minus)) {
err = mp_mul_2d(&mminus, m2plus-m2minus, &mplus);
}
++i;
}
/*
* Endgame - store the location of the decimal point and the end of the
* string.
*/
if (m2plus > m2minus) {
mp_clear(&mplus);
}
mp_clear_multi(&b, &mminus, &temp, NULL);
*s = '\0';
*decpt = k;
if (endPtr) {
*endPtr = s;
}
return retval;
return (err == MP_OKAY) ? retval : NULL;
}
/*
*----------------------------------------------------------------------
*
* StrictBignumConversionPowD --
*
|
| ︙ | | |
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
|
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
|
+
-
-
-
+
+
+
+
+
+
+
-
-
+
+
-
+
|
char *retval = (char *)ckalloc(len + 1);
/* Output buffer. */
mp_int b; /* Numerator of the fraction being
* converted. */
mp_digit digit; /* Current output digit. */
char *s = retval; /* Cursor in the output buffer. */
int i; /* Index in the output buffer. */
mp_err err;
(void)dPtr;
/*
* b = bw * 2**b2 * 5**b5
*/
mp_init_u64(&b, bw);
MulPow5(&b, b5, &b);
mp_mul_2d(&b, b2, &b);
if (mp_init_u64(&b, bw) != MP_OKAY) {
return NULL;
}
err = MulPow5(&b, b5, &b);
if (err == MP_OKAY) {
err = mp_mul_2d(&b, b2, &b);
}
/*
* Adjust if the logarithm was guessed wrong.
*/
if (b.used <= sd) {
mp_mul_d(&b, 10, &b);
if ((err == MP_OKAY) && (b.used <= sd)) {
err = mp_mul_d(&b, 10, &b);
ilim = ilim1;
--k;
}
/*
* Loop through the digits. Do division and mod by s == 2**(sd*MP_DIGIT_BIT)
* by mp_digit extraction.
*/
i = 1;
for (;;) {
while (err == MP_OKAY) {
if (b.used <= sd) {
digit = 0;
} else {
digit = b.dp[sd];
if (b.used > sd+1 || digit >= 10) {
Tcl_Panic("wrong digit!");
}
|
| ︙ | | |
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
|
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
|
-
+
|
break;
}
/*
* Advance to the next digit.
*/
mp_mul_d(&b, 10, &b);
err = mp_mul_d(&b, 10, &b);
++i;
}
/*
* Endgame - store the location of the decimal point and the end of the
* string.
*/
|
| ︙ | | |
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
|
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
|
-
-
+
+
+
|
int r;
mp_int temp;
/*
* Compare b and S-m: this is the same as comparing B+m and S.
*/
mp_init(&temp);
mp_add(b, m, &temp);
if ((mp_init(&temp) != MP_OKAY) || (mp_add(b, m, &temp) != MP_OKAY)) {
return 0;
}
r = mp_cmp_mag(&temp, S);
mp_clear(&temp);
switch(r) {
case MP_LT:
return 0;
case MP_EQ:
return isodd;
|
| ︙ | | |
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
|
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
|
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
-
+
+
-
+
|
mp_int mplus; /* 1/2 ulp above the result. */
mp_int S; /* Denominator of the result. */
mp_int dig; /* Current digit of the result. */
int digit; /* Current digit of the result. */
int minit = 1; /* Fudge factor for when we misguess k. */
int i;
int r1;
mp_err err;
/*
* b = bw * 2**b2 * 5**b5
* S = 2**s2 * 5*s5
*/
mp_init_u64(&b, bw);
mp_mul_2d(&b, b2, &b);
mp_init_set(&S, 1);
MulPow5(&S, s5, &S); mp_mul_2d(&S, s2, &S);
if ((retval == NULL) || (mp_init_u64(&b, bw) != MP_OKAY)) {
return NULL;
}
err = mp_mul_2d(&b, b2, &b);
if (err == MP_OKAY) {
err = mp_init_set(&S, 1);
}
if (err == MP_OKAY) {
err = MulPow5(&S, s5, &S);
}
if (err == MP_OKAY) {
err = mp_mul_2d(&S, s2, &S);
}
/*
* Handle the case where we guess the position of the decimal point wrong.
*/
if (mp_cmp_mag(&b, &S) == MP_LT) {
mp_mul_d(&b, 10, &b);
if ((err == MP_OKAY) && (mp_cmp_mag(&b, &S) == MP_LT)) {
err = mp_mul_d(&b, 10, &b);
minit = 10;
ilim =ilim1;
--k;
}
/*
* mminus = 2**m2minus * 5**m5
*/
if (err == MP_OKAY) {
mp_init_set(&mminus, minit);
mp_mul_2d(&mminus, m2minus, &mminus);
if (m2plus > m2minus) {
mp_init_copy(&mplus, &mminus);
mp_mul_2d(&mplus, m2plus-m2minus, &mplus);
err = mp_init_set(&mminus, minit);
}
if (err == MP_OKAY) {
err = mp_mul_2d(&mminus, m2minus, &mminus);
}
if ((err == MP_OKAY) && (m2plus > m2minus)) {
err = mp_init_copy(&mplus, &mminus);
if (err == MP_OKAY) {
err = mp_mul_2d(&mplus, m2plus-m2minus, &mplus);
}
}
/*
* Loop through the digits.
*/
if (err == MP_OKAY) {
mp_init(&dig);
err = mp_init(&dig);
}
i = 1;
for (;;) {
mp_div(&b, &S, &dig, &b);
while (err == MP_OKAY) {
err = mp_div(&b, &S, &dig, &b);
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
digit = dig.dp[0];
/*
* Does the current digit leave us with a remainder small enough to
* round to it?
*/
r1 = mp_cmp_mag(&b, (m2plus > m2minus)? &mplus : &mminus);
if (r1 == MP_LT || (r1 == MP_EQ && (dPtr->w.word1 & 1) == 0)) {
mp_mul_2d(&b, 1, &b);
err = mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
++digit;
if (digit == 10) {
*s++ = '9';
s = BumpUp(s, retval, &k);
break;
}
|
| ︙ | | |
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
|
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
|
-
-
+
+
-
+
-
-
-
-
+
+
+
+
+
+
+
-
+
+
|
}
/*
* Have we converted all the requested digits?
*/
*s++ = '0' + digit;
if (i == ilim) {
mp_mul_2d(&b, 1, &b);
if ((err == MP_OKAY) && (i == ilim)) {
err = mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
break;
}
/*
* Advance to the next digit.
*/
if (s5 > 0) {
if ((err == MP_OKAY) && (s5 > 0)) {
/*
* Can possibly shorten the denominator.
*/
mp_mul_2d(&b, 1, &b);
mp_mul_2d(&mminus, 1, &mminus);
if (m2plus > m2minus) {
mp_mul_2d(&mplus, 1, &mplus);
err = mp_mul_2d(&b, 1, &b);
if (err == MP_OKAY) {
err = mp_mul_2d(&mminus, 1, &mminus);
}
if ((err == MP_OKAY) && (m2plus > m2minus)) {
err = mp_mul_2d(&mplus, 1, &mplus);
}
if (err == MP_OKAY) {
mp_div_d(&S, 5, &S, NULL);
err = mp_div_d(&S, 5, &S, NULL);
}
--s5;
/*
* IDEA: It might possibly be a win to fall back to int64_t
* arithmetic here if S < 2**64/10. But it's a win only for
* a fairly narrow range of magnitudes so perhaps not worth
* bothering. We already know that we shorten the
|
| ︙ | | |
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
|
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
|
-
-
-
-
-
+
+
+
+
+
+
+
|
* 10**38 12 trips
* 10**39 13 trips
* 10**40 14 trips
* 10**41 15 trips
* 10**42 16 trips
* thereafter no gain.
*/
} else {
mp_mul_d(&b, 10, &b);
mp_mul_d(&mminus, 10, &mminus);
if (m2plus > m2minus) {
mp_mul_2d(&mplus, 10, &mplus);
} else if (err == MP_OKAY) {
err = mp_mul_d(&b, 10, &b);
if (err == MP_OKAY) {
err = mp_mul_d(&mminus, 10, &mminus);
}
if ((err == MP_OKAY) && (m2plus > m2minus)) {
err = mp_mul_2d(&mplus, 10, &mplus);
}
}
++i;
}
/*
|
| ︙ | | |
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
|
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
|
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
-
-
+
-
+
-
+
-
-
+
+
+
+
-
+
+
-
+
+
-
-
+
+
-
-
+
|
char *s = retval; /* Cursor in the return value. */
mp_int b; /* Numerator of the result. */
mp_int S; /* Denominator of the result. */
mp_int dig; /* Current digit of the result. */
int digit; /* Current digit of the result. */
int g; /* Size of the current digit ground. */
int i, j;
mp_err err;
(void)dPtr;
/*
* b = bw * 2**b2 * 5**b5
* S = 2**s2 * 5*s5
*/
if (mp_init(&dig) != MP_OKAY) {
mp_init_multi(&dig, NULL);
mp_init_u64(&b, bw);
mp_mul_2d(&b, b2, &b);
mp_init_set(&S, 1);
MulPow5(&S, s5, &S); mp_mul_2d(&S, s2, &S);
return NULL;
}
if (mp_init_u64(&b, bw) != MP_OKAY) {
mp_clear(&dig);
return NULL;
}
err = mp_mul_2d(&b, b2, &b);
if (err == MP_OKAY) {
err = mp_init_set(&S, 1);
}
if (err == MP_OKAY) {
err = MulPow5(&S, s5, &S);
if (err == MP_OKAY) {
err = mp_mul_2d(&S, s2, &S);
}
}
/*
* Handle the case where we guess the position of the decimal point wrong.
*/
if (mp_cmp_mag(&b, &S) == MP_LT) {
if ((mp_cmp_mag(&b, &S) == MP_LT) && (mp_mul_d(&b, 10, &b) == MP_OKAY)) {
mp_mul_d(&b, 10, &b);
ilim =ilim1;
--k;
}
/*
* Convert the leading digit.
*/
i = 0;
mp_div(&b, &S, &dig, &b);
err = mp_div(&b, &S, &dig, &b);
if (dig.used > 1 || dig.dp[0] >= 10) {
Tcl_Panic("wrong digit!");
}
digit = dig.dp[0];
/*
* Is a single digit all that was requested?
*/
*s++ = '0' + digit;
if (++i >= ilim) {
mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
if ((mp_mul_2d(&b, 1, &b) == MP_OKAY) && ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
} else {
for (;;) {
while (err == MP_OKAY) {
/*
* Shift by a group of digits.
*/
g = ilim - i;
if (g > DIGIT_GROUP) {
g = DIGIT_GROUP;
}
if (s5 >= g) {
mp_div_d(&S, dpow5[g], &S, NULL);
err = mp_div_d(&S, dpow5[g], &S, NULL);
s5 -= g;
} else if (s5 > 0) {
mp_div_d(&S, dpow5[s5], &S, NULL);
mp_mul_d(&b, dpow5[g - s5], &b);
err = mp_div_d(&S, dpow5[s5], &S, NULL);
if (err == MP_OKAY) {
err = mp_mul_d(&b, dpow5[g - s5], &b);
}
s5 = 0;
} else {
mp_mul_d(&b, dpow5[g], &b);
err = mp_mul_d(&b, dpow5[g], &b);
}
if (err == MP_OKAY) {
mp_mul_2d(&b, g, &b);
err = mp_mul_2d(&b, g, &b);
}
/*
* As with the shortening bignum conversion, it's possible at this
* point that we will have reduced the denominator to less than
* 2**64/10, at which point it would be possible to fall back to
* to int64_t arithmetic. But the potential payoff is tremendously
* less - unless we're working in F format - because we know that
* three groups of digits will always suffice for %#.17e, the
* longest format that doesn't introduce empty precision.
*
* Extract the next group of digits.
*/
mp_div(&b, &S, &dig, &b);
if (dig.used > 1) {
if ((err != MP_OKAY) || (mp_div(&b, &S, &dig, &b) != MP_OKAY) || (dig.used > 1)) {
Tcl_Panic("wrong digit!");
}
digit = dig.dp[0];
for (j = g-1; j >= 0; --j) {
int t = itens[j];
*s++ = digit / t + '0';
digit %= t;
}
i += g;
/*
* Have we converted all the requested digits?
*/
if (i == ilim) {
mp_mul_2d(&b, 1, &b);
if (ShouldBankerRoundUp(&b, &S, digit&1)) {
if ((mp_mul_2d(&b, 1, &b) == MP_OKAY) && ShouldBankerRoundUp(&b, &S, digit&1)) {
s = BumpUp(s, retval, &k);
}
break;
}
}
}
while (*--s == '0') {
|
| ︙ | | |
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
|
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
|
+
-
+
+
|
double d;
#ifdef IEEE_FLOATING_POINT
union {
double dv;
Tcl_WideUInt iv;
} bitwhack;
#endif
mp_err err = MP_OKAY;
#if defined(__sgi) && defined(_COMPILER_VERSION)
union fpc_csr mipsCR;
mipsCR.fc_word = get_fpc_csr();
mipsCR.fc_struct.flush = 0;
set_fpc_csr(mipsCR.fc_word);
#endif
/*
* Initialize table of powers of 10 expressed as wide integers.
*/
maxpow10_wide = (int)
floor(sizeof(Tcl_WideUInt) * CHAR_BIT * log(2.) / log(10.));
pow10_wide = (Tcl_WideUInt *)ckalloc((maxpow10_wide + 1) * sizeof(Tcl_WideUInt));
pow10_wide = (Tcl_WideUInt *)
ckalloc((maxpow10_wide + 1) * sizeof(Tcl_WideUInt));
u = 1;
for (i = 0; i < maxpow10_wide; ++i) {
pow10_wide[i] = u;
u *= 10;
}
pow10_wide[i] = u;
|
| ︙ | | |
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
|
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
|
-
+
-
+
-
+
-
-
+
+
+
+
+
|
}
/*
* Initialize a table of large powers of five.
*/
for (i=0; i<9; ++i) {
mp_init(pow5 + i);
err = err || mp_init(pow5 + i);
}
mp_set_u64(pow5, 5);
for (i=0; i<8; ++i) {
mp_sqr(pow5+i, pow5+i+1);
err = err || mp_sqr(pow5+i, pow5+i+1);
}
mp_init_u64(pow5_13, 1220703125);
err = err || mp_init_u64(pow5_13, 1220703125);
for (i = 1; i < 5; ++i) {
mp_init(pow5_13 + i);
mp_sqr(pow5_13 + i - 1, pow5_13 + i);
err = err || mp_init(pow5_13 + i);
err = err || mp_sqr(pow5_13 + i - 1, pow5_13 + i);
}
if (err != MP_OKAY) {
Tcl_Panic("out of memory");
}
/*
* Determine the number of decimal digits to the left and right of the
* decimal point in the largest and smallest double, the smallest double
* that differs from zero, and the number of mp_digits needed to represent
* the significand of a double.
|
| ︙ | | |
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
|
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
|
-
+
+
+
-
+
-
-
-
+
+
+
+
+
-
+
+
+
+
|
*----------------------------------------------------------------------
*/
int
Tcl_InitBignumFromDouble(
Tcl_Interp *interp, /* For error message. */
double d, /* Number to convert. */
mp_int *b) /* Place to store the result. */
void *big) /* Place to store the result. */
{
double fract;
int expt;
mp_err err;
mp_int *b = (mp_int *)big;
/*
* Infinite values can't convert to bignum.
*/
if (TclIsInfinite(d)) {
if (interp != NULL) {
const char *s = "integer value too large to represent";
Tcl_SetObjResult(interp, Tcl_NewStringObj(s, -1));
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, NULL);
}
return TCL_ERROR;
}
fract = frexp(d, &expt);
if (expt <= 0) {
mp_init(b);
err = mp_init(b);
mp_zero(b);
} else {
Tcl_WideInt w = (Tcl_WideInt) ldexp(fract, mantBits);
int shift = expt - mantBits;
mp_init_i64(b, w);
if (shift < 0) {
mp_div_2d(b, -shift, b, NULL);
err = mp_init_i64(b, w);
if (err != MP_OKAY) {
/* just skip */
} else if (shift < 0) {
err = mp_div_2d(b, -shift, b, NULL);
} else if (shift > 0) {
mp_mul_2d(b, shift, b);
err = mp_mul_2d(b, shift, b);
}
}
if (err != MP_OKAY) {
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | |
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
|
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
|
-
+
+
+
|
* too large to convert.
*
*----------------------------------------------------------------------
*/
double
TclBignumToDouble(
const mp_int *a) /* Integer to convert. */
const void *big) /* Integer to convert. */
{
mp_int b;
int bits, shift, i, lsb;
double r;
mp_err err;
const mp_int *a = (const mp_int *)big;
/*
* We need a 'mantBits'-bit significand. Determine what shift will
* give us that.
*/
|
| ︙ | | |
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
|
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
|
-
-
-
+
+
+
+
+
-
+
-
-
+
+
-
+
-
+
-
-
-
+
+
+
+
+
-
+
-
+
+
+
+
-
+
|
* in length. If shift < 0, we will need to shift the significand right
* by the requisite number of bits, and round it. If the '1-shift'
* least significant bits are 0, but the 'shift'th bit is nonzero,
* then the significand lies exactly between two values and must be
* 'rounded to even'.
*/
mp_init(&b);
if (shift == 0) {
mp_copy(a, &b);
err = mp_init(&b);
if (err != MP_OKAY) {
/* just skip */
} else if (shift == 0) {
err = mp_copy(a, &b);
} else if (shift > 0) {
mp_mul_2d(a, shift, &b);
err = mp_mul_2d(a, shift, &b);
} else if (shift < 0) {
lsb = mp_cnt_lsb(a);
if (lsb == -1-shift) {
/*
* Round to even
*/
mp_div_2d(a, -shift, &b, NULL);
if (mp_isodd(&b)) {
err = mp_div_2d(a, -shift, &b, NULL);
if ((err == MP_OKAY) && mp_isodd(&b)) {
if (mp_isneg(&b)) {
mp_sub_d(&b, 1, &b);
err = mp_sub_d(&b, 1, &b);
} else {
mp_add_d(&b, 1, &b);
err = mp_add_d(&b, 1, &b);
}
}
} else {
/*
* Ordinary rounding
*/
mp_div_2d(a, -1-shift, &b, NULL);
if (mp_isneg(&b)) {
mp_sub_d(&b, 1, &b);
err = mp_div_2d(a, -1-shift, &b, NULL);
if (err != MP_OKAY) {
/* just skip */
} else if (mp_isneg(&b)) {
err = mp_sub_d(&b, 1, &b);
} else {
mp_add_d(&b, 1, &b);
err = mp_add_d(&b, 1, &b);
}
mp_div_2d(&b, 1, &b, NULL);
err = mp_div_2d(&b, 1, &b, NULL);
}
}
/*
* Accumulate the result, one mp_digit at a time.
*/
if (err != MP_OKAY) {
return 0.0;
}
r = 0.0;
for (i=b.used-1 ; i>=0 ; --i) {
for (i = b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
mp_clear(&b);
/*
* Scale the result to the correct number of bits.
*/
|
| ︙ | | |
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
|
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
|
-
+
+
+
-
-
-
+
+
+
+
+
-
-
+
+
-
-
-
+
+
+
+
+
-
+
-
-
+
+
+
+
+
|
* Returns the floating point number.
*
*----------------------------------------------------------------------
*/
double
TclCeil(
const mp_int *a) /* Integer to convert. */
const void *big) /* Integer to convert. */
{
double r = 0.0;
mp_int b;
mp_err err;
const mp_int *a = (const mp_int *)big;
mp_init(&b);
if (mp_isneg(a)) {
mp_neg(a, &b);
err = mp_init(&b);
if ((err == MP_OKAY) && mp_isneg(a)) {
err = mp_neg(a, &b);
r = -TclFloor(&b);
} else {
int bits = mp_count_bits(a);
if (bits > DBL_MAX_EXP*log2FLT_RADIX) {
r = HUGE_VAL;
} else {
int i, exact = 1, shift = mantBits - bits;
if (err != MP_OKAY) {
/* just skip */
if (shift > 0) {
mp_mul_2d(a, shift, &b);
} else if (shift > 0) {
err = mp_mul_2d(a, shift, &b);
} else if (shift < 0) {
mp_int d;
mp_init(&d);
mp_div_2d(a, -shift, &b, &d);
exact = d.used == 0;
err = mp_init(&d);
if (err == MP_OKAY) {
err = mp_div_2d(a, -shift, &b, &d);
}
exact = mp_iszero(&d);
mp_clear(&d);
} else {
mp_copy(a, &b);
err = mp_copy(a, &b);
}
if (!exact) {
mp_add_d(&b, 1, &b);
if ((err == MP_OKAY) && !exact) {
err = mp_add_d(&b, 1, &b);
}
if (err != MP_OKAY) {
return 0.0;
}
for (i=b.used-1 ; i>=0 ; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
}
|
| ︙ | | |
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
|
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
|
-
+
+
+
-
-
-
+
+
+
-
+
-
+
-
+
+
+
+
|
* Returns the floating point value.
*
*----------------------------------------------------------------------
*/
double
TclFloor(
const mp_int *a) /* Integer to convert. */
const void *big) /* Integer to convert. */
{
double r = 0.0;
mp_int b;
mp_err err;
const mp_int *a = (const mp_int *)big;
mp_init(&b);
if (mp_isneg(a)) {
mp_neg(a, &b);
err = mp_init(&b);
if ((err == MP_OKAY) && mp_isneg(a)) {
err = mp_neg(a, &b);
r = -TclCeil(&b);
} else {
int bits = mp_count_bits(a);
if (bits > DBL_MAX_EXP*log2FLT_RADIX) {
r = DBL_MAX;
} else {
int i, shift = mantBits - bits;
if (shift > 0) {
mp_mul_2d(a, shift, &b);
err = mp_mul_2d(a, shift, &b);
} else if (shift < 0) {
mp_div_2d(a, -shift, &b, NULL);
err = mp_div_2d(a, -shift, &b, NULL);
} else {
mp_copy(a, &b);
err = mp_copy(a, &b);
}
if (err != MP_OKAY) {
return 0.0;
}
for (i=b.used-1 ; i>=0 ; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
r = ldexp(r, bits - mantBits);
}
}
|
| ︙ | | |
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
|
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
|
+
-
+
+
+
-
+
-
+
-
+
+
-
-
+
+
+
|
int *machexp) /* Power of two. */
{
mp_int b;
int bits;
int shift;
int i;
double r;
mp_err err = MP_OKAY;
/*
* Determine how many bits we need, and extract that many from the input.
* Round to nearest unit in the last place.
*/
bits = mp_count_bits(a);
shift = mantBits - 2 - bits;
mp_init(&b);
if (mp_init(&b)) {
return 0.0;
}
if (shift > 0) {
mp_mul_2d(a, shift, &b);
err = mp_mul_2d(a, shift, &b);
} else if (shift < 0) {
mp_div_2d(a, -shift, &b, NULL);
err = mp_div_2d(a, -shift, &b, NULL);
} else {
mp_copy(a, &b);
err = mp_copy(a, &b);
}
/*
* Accumulate the result, one mp_digit at a time.
*/
r = 0.0;
if (err == MP_OKAY) {
for (i=b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
for (i=b.used-1; i>=0; --i) {
r = ldexp(r, MP_DIGIT_BIT) + b.dp[i];
}
}
mp_clear(&b);
/*
* Return the result with the appropriate sign.
*/
|
| ︙ | | |