Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,59 @@ final class MutableBigInteger {
static final int KNUTH_POW2_THRESH_LEN = 6;
static final int KNUTH_POW2_THRESH_ZEROS = 3;

static long divideKnuthLongFast(long intCompact, int ql, int scale) {
if (ql <= 0 && ql > -64 && scale > 0 && scale <= 18 && intCompact > 0) {
// (intCompact << -ql) / 10^scale without allocation, when the quotient fits in [2^32, 2^63)
int n = -ql;
long d = NumberUtils.POW10_LONG_VALUES[scale - 1];
long hi = n == 0 ? 0 : intCompact >>> (64 - n);
if (hi < (d >>> 1)) {
long q = divide128(hi, intCompact << n, d);
if ((q >>> 32) != 0) {
return q;
}
}
}
return divideKnuthLong(intCompact, ql, scale);
}

/**
* Unsigned (u1 * 2^64 + u0) / v, for 0 &lt; v &lt; 2^63 and u1 &lt; v (Hacker's Delight, divlu).
*/
private static long divide128(long u1, long u0, long v) {
final long b = 1L << 32;
int s = Long.numberOfLeadingZeros(v);
v <<= s;
long vn1 = v >>> 32;
long vn0 = v & LONG_MASK;
long un32 = (u1 << s) | (u0 >>> (64 - s));
long un10 = u0 << s;
long un1 = un10 >>> 32;
long un0 = un10 & LONG_MASK;

long q1 = Long.divideUnsigned(un32, vn1);
long rhat = un32 - q1 * vn1;
while (q1 >= b || Long.compareUnsigned(q1 * vn0, (rhat << 32) | un1) > 0) {
q1--;
rhat += vn1;
if (rhat >= b) {
break;
}
}

long un21 = (un32 << 32) + un1 - q1 * v;
long q0 = Long.divideUnsigned(un21, vn1);
rhat = un21 - q0 * vn1;
while (q0 >= b || Long.compareUnsigned(q0 * vn0, (rhat << 32) | un0) > 0) {
q0--;
rhat += vn1;
if (rhat >= b) {
break;
}
}
return (q1 << 32) | q0;
}

static long divideKnuthLong(long intCompact, int ql, int scale) {
int[] pow10 = MutableBigInteger.BIG_TEN_POWERS_MAGIC_TABLE[scale];
int[] magic_a, magic_b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3817,7 +3817,7 @@ public static double doubleValue(int signNum, long intCompact, int scale) {
}

int ql = (int) qb - (P_D + 3); // narrowing qb to an int is safe
long i = MutableBigInteger.divideKnuthLong(intCompact, ql, scale);
long i = MutableBigInteger.divideKnuthLongFast(intCompact, ql, scale);

int dq = (Long.SIZE - (P_D + 2)) - Long.numberOfLeadingZeros(i);
int eq = (Q_MIN_D - 2) - ql;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.util;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Tag("util")
public class MutableBigIntegerTest {
@Test
public void divideKnuthLongFast() {
Random random = new Random(1);
for (int i = 0; i < 1_000_000; i++) {
long intCompact = (random.nextLong() >>> 1) >>> random.nextInt(63);
if (intCompact == 0) {
continue;
}
int scale = 1 + random.nextInt(20);
// same ql as TypeUtils.doubleValue
int bitLength = 64 - Long.numberOfLeadingZeros(intCompact);
int ql = bitLength - (int) Math.ceil(scale * 3.321928094887362) - 56;
assertEquals(
MutableBigInteger.divideKnuthLong(intCompact, ql, scale),
MutableBigInteger.divideKnuthLongFast(intCompact, ql, scale),
intCompact + " " + ql + " " + scale);
}
}
}