@@ -235,6 +235,7 @@ typedef union { double d; ULong L[2]; } U;
235235#define Bias 1023
236236#define Emax 1023
237237#define Emin (-1022)
238+ #define Etiny (-1074) /* smallest denormal is 2**Etiny */
238239#define Exp_1 0x3ff00000
239240#define Exp_11 0x3ff00000
240241#define Ebits 11
@@ -244,7 +245,6 @@ typedef union { double d; ULong L[2]; } U;
244245#define Bletch 0x10
245246#define Bndry_mask 0xfffff
246247#define Bndry_mask1 0xfffff
247- #define LSB 1
248248#define Sign_bit 0x80000000
249249#define Log2P 1
250250#define Tiny0 0
@@ -622,6 +622,15 @@ mult(Bigint *a, Bigint *b)
622622 ULong z2 ;
623623#endif
624624
625+ if ((!a -> x [0 ] && a -> wds == 1 ) || (!b -> x [0 ] && b -> wds == 1 )) {
626+ c = Balloc (0 );
627+ if (c == NULL )
628+ return NULL ;
629+ c -> wds = 1 ;
630+ c -> x [0 ] = 0 ;
631+ return c ;
632+ }
633+
625634 if (a -> wds < b -> wds ) {
626635 c = a ;
627636 a = b ;
@@ -820,6 +829,9 @@ lshift(Bigint *b, int k)
820829 Bigint * b1 ;
821830 ULong * x , * x1 , * xe , z ;
822831
832+ if (!k || (!b -> x [0 ] && b -> wds == 1 ))
833+ return b ;
834+
823835 n = k >> 5 ;
824836 k1 = b -> k ;
825837 n1 = n + b -> wds + 1 ;
@@ -1019,6 +1031,76 @@ b2d(Bigint *a, int *e)
10191031 return dval (& d );
10201032}
10211033
1034+ /* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
1035+ except that it accepts the scale parameter used in _Py_dg_strtod (which
1036+ should be either 0 or 2*P), and the normalization for the return value is
1037+ different (see below). On input, d should be finite and nonnegative, and d
1038+ / 2**scale should be exactly representable as an IEEE 754 double.
1039+
1040+ Returns a Bigint b and an integer e such that
1041+
1042+ dval(d) / 2**scale = b * 2**e.
1043+
1044+ Unlike d2b, b is not necessarily odd: b and e are normalized so
1045+ that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
1046+ and e == Etiny. This applies equally to an input of 0.0: in that
1047+ case the return values are b = 0 and e = Etiny.
1048+
1049+ The above normalization ensures that for all possible inputs d,
1050+ 2**e gives ulp(d/2**scale).
1051+
1052+ Returns NULL on failure.
1053+ */
1054+
1055+ static Bigint *
1056+ sd2b (U * d , int scale , int * e )
1057+ {
1058+ Bigint * b ;
1059+
1060+ b = Balloc (1 );
1061+ if (b == NULL )
1062+ return NULL ;
1063+
1064+ /* First construct b and e assuming that scale == 0. */
1065+ b -> wds = 2 ;
1066+ b -> x [0 ] = word1 (d );
1067+ b -> x [1 ] = word0 (d ) & Frac_mask ;
1068+ * e = Etiny - 1 + (int )((word0 (d ) & Exp_mask ) >> Exp_shift );
1069+ if (* e < Etiny )
1070+ * e = Etiny ;
1071+ else
1072+ b -> x [1 ] |= Exp_msk1 ;
1073+
1074+ /* Now adjust for scale, provided that b != 0. */
1075+ if (scale && (b -> x [0 ] || b -> x [1 ])) {
1076+ * e -= scale ;
1077+ if (* e < Etiny ) {
1078+ scale = Etiny - * e ;
1079+ * e = Etiny ;
1080+ /* We can't shift more than P-1 bits without shifting out a 1. */
1081+ assert (0 < scale && scale <= P - 1 );
1082+ if (scale >= 32 ) {
1083+ /* The bits shifted out should all be zero. */
1084+ assert (b -> x [0 ] == 0 );
1085+ b -> x [0 ] = b -> x [1 ];
1086+ b -> x [1 ] = 0 ;
1087+ scale -= 32 ;
1088+ }
1089+ if (scale ) {
1090+ /* The bits shifted out should all be zero. */
1091+ assert (b -> x [0 ] << (32 - scale ) == 0 );
1092+ b -> x [0 ] = (b -> x [0 ] >> scale ) | (b -> x [1 ] << (32 - scale ));
1093+ b -> x [1 ] >>= scale ;
1094+ }
1095+ }
1096+ }
1097+ /* Ensure b is normalized. */
1098+ if (!b -> x [1 ])
1099+ b -> wds = 1 ;
1100+
1101+ return b ;
1102+ }
1103+
10221104/* Convert a double to a Bigint plus an exponent. Return NULL on failure.
10231105
10241106 Given a finite nonzero double d, return an odd Bigint b and exponent *e
@@ -1028,7 +1110,6 @@ b2d(Bigint *a, int *e)
10281110 If d is zero, then b == 0, *e == -1010, *bbits = 0.
10291111 */
10301112
1031-
10321113static Bigint *
10331114d2b (U * d , int * e , int * bits )
10341115{
@@ -1299,45 +1380,29 @@ static int
12991380bigcomp (U * rv , const char * s0 , BCinfo * bc )
13001381{
13011382 Bigint * b , * d ;
1302- int b2 , bbits , d2 , dd , i , nd , nd0 , odd , p2 , p5 ;
1383+ int b2 , d2 , dd , i , nd , nd0 , odd , p2 , p5 ;
13031384
13041385 dd = 0 ; /* silence compiler warning about possibly unused variable */
13051386 nd = bc -> nd ;
13061387 nd0 = bc -> nd0 ;
13071388 p5 = nd + bc -> e0 ;
1308- if (rv -> d == 0. ) {
1309- /* special case because d2b doesn't handle 0.0 */
1310- b = i2b (0 );
1311- if (b == NULL )
1312- return -1 ;
1313- p2 = Emin - P + 1 ; /* = -1074 for IEEE 754 binary64 */
1314- bbits = 0 ;
1315- }
1316- else {
1317- b = d2b (rv , & p2 , & bbits );
1318- if (b == NULL )
1319- return -1 ;
1320- p2 -= bc -> scale ;
1321- }
1322- /* now rv/2^(bc->scale) = b * 2**p2, and b has bbits significant bits */
1323-
1324- /* Replace (b, p2) by (b << i, p2 - i), with i the largest integer such
1325- that b << i has at most P significant bits and p2 - i >= Emin - P +
1326- 1. */
1327- i = P - bbits ;
1328- if (i > p2 - (Emin - P + 1 ))
1329- i = p2 - (Emin - P + 1 );
1330- /* increment i so that we shift b by an extra bit; then or-ing a 1 into
1331- the lsb of b gives us rv/2^(bc->scale) + 0.5ulp. */
1332- b = lshift (b , ++ i );
1389+ b = sd2b (rv , bc -> scale , & p2 );
13331390 if (b == NULL )
13341391 return -1 ;
1392+
13351393 /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
13361394 case, this is used for round to even. */
1337- odd = b -> x [0 ] & 2 ;
1395+ odd = b -> x [0 ] & 1 ;
1396+
1397+ /* left shift b by 1 bit and or a 1 into the least significant bit;
1398+ this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
1399+ b = lshift (b , 1 );
1400+ if (b == NULL )
1401+ return -1 ;
13381402 b -> x [0 ] |= 1 ;
1403+ p2 -- ;
13391404
1340- p2 -= p5 + i ;
1405+ p2 -= p5 ;
13411406 d = i2b (1 );
13421407 if (d == NULL ) {
13431408 Bfree (b );
@@ -1425,8 +1490,8 @@ bigcomp(U *rv, const char *s0, BCinfo *bc)
14251490double
14261491_Py_dg_strtod (const char * s00 , char * * se )
14271492{
1428- int bb2 , bb5 , bbe , bd2 , bd5 , bbbits , bs2 , c , dsign , e , e1 , error ;
1429- int esign , i , j , k , lz , nd , nd0 , sign ;
1493+ int bb2 , bb5 , bbe , bd2 , bd5 , bs2 , c , dsign , e , e1 , error ;
1494+ int esign , i , j , k , lz , nd , nd0 , odd , sign ;
14301495 const char * s , * s0 , * s1 ;
14311496 double aadj , aadj1 ;
14321497 U aadj2 , adj , rv , rv0 ;
@@ -1786,13 +1851,17 @@ _Py_dg_strtod(const char *s00, char **se)
17861851 goto failed_malloc ;
17871852 }
17881853 Bcopy (bd , bd0 );
1789- bb = d2b (& rv , & bbe , & bbbits ); /* rv = bb * 2^bbe */
1854+ bb = sd2b (& rv , bc . scale , & bbe ); /* srv = bb * 2^bbe */
17901855 if (bb == NULL ) {
17911856 Bfree (bd );
17921857 Bfree (bd0 );
17931858 goto failed_malloc ;
17941859 }
1795- /* tdv = bd * 10^e; srv = bb * 2^(bbe - scale) */
1860+ /* Record whether lsb of bb is odd, in case we need this
1861+ for the round-to-even step later. */
1862+ odd = bb -> x [0 ] & 1 ;
1863+
1864+ /* tdv = bd * 10**e; srv = bb * 2**bbe */
17961865 bs = i2b (1 );
17971866 if (bs == NULL ) {
17981867 Bfree (bb );
@@ -1813,44 +1882,28 @@ _Py_dg_strtod(const char *s00, char **se)
18131882 bb2 += bbe ;
18141883 else
18151884 bd2 -= bbe ;
1816-
1817- /* At this stage e = bd2 - bb2 + bbe = bd5 - bb5, so:
1818-
1819- tdv = bd * 2^(bbe + bd2 - bb2) * 5^(bd5 - bb5)
1820- srv = bb * 2^(bbe - scale).
1821-
1822- Compute j such that
1823-
1824- 0.5 ulp(srv) = 2^(bbe - scale - j)
1825- */
1826-
18271885 bs2 = bb2 ;
1828- j = bbe - bc .scale ;
1829- i = j + bbbits - 1 ; /* logb(rv) */
1830- if (i < Emin ) /* denormal */
1831- j += P - Emin ;
1832- else
1833- j = P + 1 - bbbits ;
1886+ bb2 ++ ;
1887+ bd2 ++ ;
18341888
1835- /* Now we have:
1889+ /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
1890+ and bs == 1, so:
18361891
1837- M * tdv = bd * 2^(bd2 + scale + j ) * 5^ bd5
1838- M * srv = bb * 2^( bb2 + j) * 5^bb5
1839- M * 0.5 ulp(srv) = 2^bs2 * 5^bb5
1892+ tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2 ) * 5**( bd5 - bb5)
1893+ srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
1894+ 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
18401895
1841- where M is the constant (currently) represented by 2^(j + scale +
1842- bb2 - bbe) * 5^bb5.
1843- */
1896+ It follows that:
18441897
1845- bb2 += j ;
1846- bd2 += j ;
1847- bd2 += bc . scale ;
1898+ M * tdv = bd * 2**bd2 * 5**bd5
1899+ M * srv = bb * 2**bb2 * 5**bb5
1900+ M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
18481901
1849- /* After the adjustments above, tdv, srv and 0.5 ulp(srv) are
1850- proportional to: bd * 2^bd2 * 5^bd5, bb * 2^bb2 * 5^bb5, and
1851- bs * 2^bs2 * 5^bb5, respectively. */
1902+ for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
1903+ this fact is not needed below.)
1904+ */
18521905
1853- /* Remove excess powers of 2. i = min(bb2, bd2, bs2). */
1906+ /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
18541907 i = bb2 < bd2 ? bb2 : bd2 ;
18551908 if (i > bs2 )
18561909 i = bs2 ;
@@ -2028,12 +2081,12 @@ _Py_dg_strtod(const char *s00, char **se)
20282081 word1 (& rv ) = 0xffffffff ;
20292082 break ;
20302083 }
2031- if (!( word1 ( & rv ) & LSB ) )
2084+ if (!odd )
20322085 break ;
20332086 if (dsign )
2034- dval (& rv ) += ulp (& rv );
2087+ dval (& rv ) += sulp (& rv , & bc );
20352088 else {
2036- dval (& rv ) -= ulp (& rv );
2089+ dval (& rv ) -= sulp (& rv , & bc );
20372090 if (!dval (& rv )) {
20382091 if (bc .nd > nd )
20392092 break ;
0 commit comments