Skip to content
Merged
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
110 changes: 95 additions & 15 deletions assembly/Histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,45 @@ function laneSub<U>(a: v128, b: v128): v128 {
}
}

/**
* Largest count a U-wide cell can hold. u64 cells never overflow in practice, so
* their checks are compiled out (guarded by sizeof<U>() < 8 at each call site).
*/
// @ts-ignore: decorator
@inline
function countMax<U>(): u64 {
return (<u64>1 << (sizeof<U>() << 3)) - 1;
}

// Aborts with the message (AssemblyScript's throw of an Error carries no message in the
// minimal runtime, so the JS wrapper would only see the location).

/** Aborts for a count that would overflow a narrow U cell, naming the width. */
function overflowAbort<U>(count: u64): void {
abort(count.toString() + " would overflow " + (sizeof<U>() << 3).toString() + "-bit integer count");
}

/** Aborts for a count that would underflow a narrow U cell, naming the width. */
function underflowAbort<U>(count: i64): void {
abort(count.toString() + " would underflow " + (sizeof<U>() << 3).toString() + "-bit integer count");
}

/** Lane-wise unsigned a < b. u64 lanes flip the sign bit so the signed compare orders them unsigned. */
// @ts-ignore: decorator
@inline
function laneLtU<U>(a: v128, b: v128): v128 {
if (sizeof<U>() == 1) {
return i8x16.lt_u(a, b);
} else if (sizeof<U>() == 2) {
return i16x8.lt_u(a, b);
} else if (sizeof<U>() == 4) {
return i32x4.lt_u(a, b);
} else {
const bias = i64x2.splat(i64.MIN_VALUE);
return i64x2.lt_s(v128.xor(a, bias), v128.xor(b, bias));
}
}

export default class Histogram<T, U> extends AbstractHistogramBase<T, U> {
// "Hot" accessed fields (used in the the value recording code path) are bunched here, such
// that they will have a good chance of ending up in the same cache line as the totalCounts and
Expand Down Expand Up @@ -582,6 +621,19 @@ export default class Histogram<T, U> extends AbstractHistogramBase<T, U> {
return index == 0 ? this.lowestNonZeroCountsIndex() : index + 1;
}

/**
* Entry point for count arguments that arrive as f64 (the JS boundary). A negative
* count is not representable in any U cell (and would saturate to 0 in the u64 cast,
* dropping the record silently), so it aborts for every width.
*/
recordCountAtValueFromF64(count: f64, value: f64): void {
if (count < 0) {
underflowAbort<U>(<i64>count);
}
this.recordCountAtValue(<u64>count, <u64>value);
}

// @ts-ignore: decorator
@inline
recordCountAtValue(count: u64, value: u64): void {
const countsIndex = this.countsArrayIndex(value);
Expand Down Expand Up @@ -870,18 +922,23 @@ export default class Histogram<T, U> extends AbstractHistogramBase<T, U> {
return;
}
// @ts-ignore
const currentCount = unchecked(this.counts[index]);
const newCount = currentCount + 1;
if (newCount < 0) {
throw new Error(
newCount.toString() + " would overflow short integer count"
);
const newCount = <u64>unchecked(this.counts[index]) + 1;
if (sizeof<U>() < 8 && newCount > countMax<U>()) {
overflowAbort<U>(newCount);
}
// @ts-ignore
unchecked((this.counts[index] = newCount));
unchecked((this.counts[index] = <U>newCount));
}

setCountAtIndex(index: i32, value: u64): void {
if (sizeof<U>() < 8) {
if (<i64>value < 0) {
underflowAbort<U>(<i64>value);
}
if (value > countMax<U>()) {
overflowAbort<U>(value);
}
}
// @ts-ignore
unchecked((this.counts[index] = <U>value));
}
Expand All @@ -895,13 +952,24 @@ export default class Histogram<T, U> extends AbstractHistogramBase<T, U> {
return;
}
// @ts-ignore
const currentCount = unchecked(this.counts[index]);
const newCount = currentCount + value;
if (newCount < 0) {
throw newCount + " would overflow short integer count";
const currentCount = <u64>unchecked(this.counts[index]);
if (<i64>value >= 0) {
const newCount = currentCount + value;
if (sizeof<U>() < 8 && newCount > countMax<U>()) {
overflowAbort<U>(newCount);
}
// @ts-ignore
unchecked((this.counts[index] = <U>newCount));
} else {
// Negative values arrive as two's complement (subtract); the magnitude is their negation.
// A cell of U (u64 included) cannot go below zero, so the check applies to every width.
const decrement = (<u64>0) - value;
if (decrement > currentCount) {
underflowAbort<U>(<i64>currentCount - <i64>decrement);
}
// @ts-ignore
unchecked((this.counts[index] = <U>(currentCount - decrement)));
}
// @ts-ignore
unchecked((this.counts[index] = <U>newCount));
}

incrementTotalCount(): void {
Expand Down Expand Up @@ -988,8 +1056,15 @@ export default class Histogram<T, U> extends AbstractHistogramBase<T, U> {
for (; i + step <= end; i += step) {
const offset = <usize>i << alignof<U>();
const other = v128.load(src + offset);
const current = v128.load(dst + offset);
const sum = laneAdd<U>(current, other);
if (sizeof<U>() < 8 && v128.any_true(laneLtU<U>(sum, current))) {
// A lane would wrap: leave this block to the checked scalar loop below,
// which reports the exact cell and width.
break;
}
acc = i64x2.add(acc, widenSum<W>(other));
v128.store(dst + offset, laneAdd<U>(v128.load(dst + offset), other));
v128.store(dst + offset, sum);
}
observedOtherTotalCount += horizontalSum(acc);
}
Expand Down Expand Up @@ -1176,8 +1251,13 @@ export default class Histogram<T, U> extends AbstractHistogramBase<T, U> {
for (; i + step <= end; i += step) {
const offset = <usize>i << alignof<U>();
const other = v128.load(src + offset);
const current = v128.load(dst + offset);
if (v128.any_true(laneLtU<U>(current, other))) {
// A lane would go negative: leave this block to the checked scalar loop below.
break;
}
acc = i64x2.add(acc, widenSum<W>(other));
v128.store(dst + offset, laneSub<U>(v128.load(dst + offset), other));
v128.store(dst + offset, laneSub<U>(current, other));
}
observedOtherTotalCount += horizontalSum(acc);
}
Expand Down
44 changes: 44 additions & 0 deletions assembly/__tests__/Histogram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,47 @@ describe("Histogram scans bounded by recorded min and max", () => {
);
});
});

describe("Histogram count range", () => {
// Overflow and underflow abort the module (AssemblyScript has no throw assertion here),
// so these cover the in-range boundaries; the throwing paths are covered by the
// WASM tests in src/Histogram.spec.ts.
test("should accept a count equal to the 8-bit maximum", () => {
// given
const histogram = new Histogram8(1, 9007199254740991, 3);
// when
histogram.setCountAtIndex(5, 255);
// then
expect<u64>(histogram.getCountAtIndex(5)).equal(255);
});

test("should add up to exactly the 16-bit maximum", () => {
// given
const histogram = new Histogram16(1, 1000000, 3);
histogram.setCountAtIndex(7, 65000);
// when
histogram.addToCountAtIndex(7, 535);
// then
expect<u64>(histogram.getCountAtIndex(7)).equal(65535);
});

test("should subtract down to exactly zero without wrapping", () => {
// given
const histogram = new Histogram32(1, 1000000, 3);
histogram.setCountAtIndex(9, 4294967295);
// when
histogram.addToCountAtIndex(9, <u64>0 - 4294967295);
// then
expect<u64>(histogram.getCountAtIndex(9)).equal(0);
});

test("should subtract a 64-bit cell down to exactly zero", () => {
// given
const histogram = new Histogram64(1, 1000000, 3);
histogram.setCountAtIndex(9, 5);
// when
histogram.addToCountAtIndex(9, <u64>0 - 5);
// then
expect<u64>(histogram.getCountAtIndex(9)).equal(0);
});
});
2 changes: 1 addition & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class HistogramAdapter<T, U> {
}

recordValueWithCount(value: f64, count: f64): void {
this._histogram.recordCountAtValue(<u64>count, <u64>value);
this._histogram.recordCountAtValueFromF64(count, value);
}

recordValueWithExpectedInterval(
Expand Down
Loading
Loading