Skip to content

Commit

Permalink
FpSketch
Browse files Browse the repository at this point in the history
  • Loading branch information
linxuanm committed Dec 8, 2024
1 parent 131dbef commit 7fe2a77
Showing 1 changed file with 0 additions and 23 deletions.
23 changes: 0 additions & 23 deletions lib/util/FpSketch.v3
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// words and an exponent, this class has just barely enough arithmetic
// support to properly compute and round large bit strings.
// Always mutable; arithmetic is done in-place.
def trace = false; // XXX remove tracing altogether
def XXX: Terminal;
class FpSketch {
var words = Array<u32>.new(1); // base 2^32 digits.
var exp: int;
Expand Down Expand Up @@ -173,8 +171,6 @@ class FpSketch {
// Divide this number by {di} in place, producing at least {qmin} quotient words.
// Returns {true} if the divison was exact, {false} otherwise.
def div(di: FpSketch, qmin: u31) -> bool {
if (trace) XXX.put3("%q / %q, qmin=%d\n", this.render, di.render, qmin);

// Single word divisor has a faster method.
var dwords = di.words.length, dextra = dwords - 2;
if (dwords == 1) {
Expand Down Expand Up @@ -206,28 +202,15 @@ class FpSketch {
for (npos = ri.words.length -2; npos >= dextra; npos--) {
var q: u32;
var nd = ri.u64at(npos);
if (trace) XXX.put1(" npos=%d", npos);
if (trace) XXX.put3(", qi=%q, ri=%q, rlast=%d\n", qi.render, ri.render, rlast);
if (rlast == 0) { // 64x64 bit divide
var qe = nd / dh2;
q = u32.view(qe);
if (trace) TerminalBuffer.new()
.puts(" nd=").putd(long.view(nd))
.puts(" / dh2=").putd(long.view(dh2))
.puts(" = ").putx(q)
.outln();
} else { // 96x64 bit divide; shift in rlast bits
var shift = 1 + Longs.log(rlast);
if (trace) XXX.put2(" rlast=%d, shift=%d\n", rlast, shift);
var sdh2 = dh2 >> u6.view(shift);
var snd = nd >> u6.view(shift) | (u64.view(rlast) << u6.view(64 - shift));
var qe = snd / sdh2;
q = if(qe > 0xFFFFFFFFu, 0xFFFFFFFFu, u32.view(qe)); // +1 or +2 overestimate
if (trace) TerminalBuffer.new()
.puts(" snd=").putd(long.view(snd))
.puts(" / sdh2=").putd(long.view(sdh2))
.puts(" = ").putx(q)
.outln();
}
while (q > 0) {
var si = if(q == 1, di, di.copyInto(mi).mulAdd32(q, 0));
Expand All @@ -241,7 +224,6 @@ class FpSketch {
qi.words[npos] = q;
}
var exact = ri.isZero() && rlast == 0;
if (trace) XXX.put3(" qi=%q, ri=%q, rlast=%d\n", qi.render, ri.render, rlast);
return exact;
}
// Divide this number by {d} in place, producing at least {qmin} quotient words.
Expand All @@ -262,19 +244,15 @@ class FpSketch {
// Long-division starting at highest word; grade-school algorithm.
var rem = 0u;
for (npos = ri.words.length -1; npos >= 0; npos--) {
if (trace) XXX.put3(" npos=%d, qi=%q, ri=%q", npos, qi.render, ri.render);
if (trace) XXX.put1(", rem=%d\n", rem);
var nd = (u64.view(rem) << 32) | ri.words[npos];
rem = u32.view(nd % d);
qi.words[npos] = u32.view(nd / d);
}
if (trace) XXX.put2(" qi=%q, ri=%q\n", qi.render, ri.render);
return rem == 0;
}
// Subtract {num * 2^(32*pos)} from this number, in place, returning the borrow
// from the {maxPos} position.
def subRange(num: FpSketch, pos: int, maxPos: int) -> u32 {
if (trace) XXX.put3(" subRange(s=%q, pos=%d, max=%d)\n", num.render, pos, maxPos);
var nd = num.words, borrow = 0u;
for (i < nd.length) {
var p = pos + i;
Expand All @@ -293,7 +271,6 @@ class FpSketch {
// Add {num * 2^(32*pos)} to this number, in place, returning the carry
// out from the highest order digit of {num}.
def addRange(num: FpSketch, pos: int, maxPos: int) -> u32 {
if (trace) XXX.put2(" addRange(s=%q, pos=%d)\n", num.render, pos);
var nd = num.words, carry = 0u;
for (i < nd.length) {
var p = pos + i;
Expand Down

0 comments on commit 7fe2a77

Please sign in to comment.