@@ -3686,7 +3686,6 @@ public static boolean validateIPv6(String str) {
3686
3686
return colonCount > 0 && colonCount < 8 ;
3687
3687
}
3688
3688
3689
- private static final BigInteger [] BIG_TEN_POWERS_TABLE ;
3690
3689
private static final int [][] BIG_TEN_POWERS_MAGIC_TABLE = {
3691
3690
{1 },
3692
3691
{10 },
@@ -3818,23 +3817,6 @@ public static boolean validateIPv6(String str) {
3818
3817
{59 , 391113267 , -1868644376 , -112737054 , -1389121638 , 2073349066 , -799507145 , -404191822 , -422998195 , -1210336922 , -2147483648 , 0 , 0 , 0 }
3819
3818
};
3820
3819
3821
- static {
3822
- BigInteger [] bigInts = new BigInteger [128 ];
3823
- bigInts [0 ] = BigInteger .ONE ;
3824
- bigInts [1 ] = BigInteger .TEN ;
3825
- long longValue = 10 ;
3826
- for (int i = 2 ; i < 19 ; ++i ) {
3827
- longValue *= 10 ;
3828
- bigInts [i ] = BigInteger .valueOf (longValue );
3829
- }
3830
- BigInteger bigInt = bigInts [18 ];
3831
- for (int i = 19 ; i < 128 ; ++i ) {
3832
- bigInt = bigInt .multiply (BigInteger .TEN );
3833
- bigInts [i ] = bigInt ;
3834
- }
3835
- BIG_TEN_POWERS_TABLE = bigInts ;
3836
- }
3837
-
3838
3820
private static long divideAndRemainder (int [] m , int [] n ) {
3839
3821
MutableBigInteger q = new MutableBigInteger (),
3840
3822
a = new MutableBigInteger (m ),
@@ -3843,6 +3825,14 @@ private static long divideAndRemainder(int[] m, int[] n) {
3843
3825
return q .longValue (1 );
3844
3826
}
3845
3827
3828
+ private static int divideAndRemainderInt (int [] m , int [] n ) {
3829
+ MutableBigInteger q = new MutableBigInteger (),
3830
+ a = new MutableBigInteger (m ),
3831
+ b = new MutableBigInteger (n );
3832
+ a .divideKnuth (b , q , false );
3833
+ return q .intValue ();
3834
+ }
3835
+
3846
3836
private static int [] shiftLeft (int [] mag , int n ) {
3847
3837
int nInts = n >>> 5 ;
3848
3838
int nBits = n & 0x1f ;
@@ -3886,11 +3876,6 @@ public static double doubleValue(int signNum, long intCompact, int scale) {
3886
3876
return signNum * Double .POSITIVE_INFINITY ;
3887
3877
}
3888
3878
3889
- if (scale < 0 ) {
3890
- BigInteger pow10 = BIG_TEN_POWERS_TABLE [-scale ];
3891
- BigInteger w = BigInteger .valueOf (intCompact );
3892
- return signNum * w .multiply (pow10 ).doubleValue ();
3893
- }
3894
3879
if (scale == 0 ) {
3895
3880
return signNum * (double ) intCompact ;
3896
3881
}
@@ -3939,25 +3924,28 @@ public static float floatValue(int signNum, long intCompact, int scale) {
3939
3924
if (qb > Q_MAX_F + P_F + 1 ) { // qb > 129
3940
3925
return signNum * Float .POSITIVE_INFINITY ;
3941
3926
}
3942
- if (scale < 0 ) {
3943
- BigInteger w = BigInteger .valueOf (intCompact );
3944
- return signNum * w .multiply (BIG_TEN_POWERS_TABLE [-scale ]).floatValue ();
3927
+ if (scale == 0 ) {
3928
+ return signNum * (float ) intCompact ;
3945
3929
}
3946
3930
3947
- BigInteger w = BigInteger .valueOf (intCompact );
3948
- int ql = (int ) qb - (P_F + 3 );
3949
- BigInteger pow10 = BIG_TEN_POWERS_TABLE [scale ];
3950
- BigInteger m , n ;
3931
+ int [] magic_w = new int []{
3932
+ (int ) (intCompact >>> 32 ),
3933
+ (int ) intCompact
3934
+ };
3935
+ int [] magic_m , magic_n ;
3936
+ int ql = (int ) qb - (P_F + 3 ); // narrowing qb to an int is safe
3937
+ int [] pow10 = BIG_TEN_POWERS_MAGIC_TABLE [scale ];
3951
3938
if (ql <= 0 ) {
3952
- m = w . shiftLeft (-ql );
3953
- n = pow10 ;
3939
+ magic_m = shiftLeft (magic_w , -ql );
3940
+ magic_n = pow10 . clone () ;
3954
3941
} else {
3955
- m = w ;
3956
- n = pow10 . shiftLeft (ql );
3942
+ magic_m = magic_w ;
3943
+ magic_n = shiftLeft (pow10 , ql );
3957
3944
}
3958
- BigInteger [] qr = m .divideAndRemainder (n );
3959
- int i = qr [0 ].intValue ();
3960
- int sb = qr [1 ].signum ();
3945
+
3946
+ int i = divideAndRemainderInt (magic_m , magic_n );
3947
+
3948
+ int sb = intCompact == 0 ? 0 : 1 ;
3961
3949
int dq = (Integer .SIZE - (P_F + 2 )) - Integer .numberOfLeadingZeros (i );
3962
3950
int eq = (Q_MIN_F - 2 ) - ql ;
3963
3951
if (dq >= eq ) {
0 commit comments