Drop a multiply to the step below rather than rounding it
Bryan's call, 2026-09-16, taken for the cycles: a share now lands a thousandth of a pixel short of its row instead of on it, which is less than an even number of pixels draws. `Fixed::mul` is a widening multiply and a shift, with the sign branch and the half-step add gone. The two short-circuits priced against the old multiply go with it: `UiSpan::within`'s test for a span that is the whole of its parent, and `Fixed::scaled`'s test for nothing scaled by something, which was the whole of `scaled` -- both cases come out of the truncating multiply unchanged, and the bodies the comparisons cost were what kept the inliner from taking `within` at all. `nm` is the check: `<UiSpan>::within` is a symbol in the rounding head and in neither the float head nor this one. `Holds::through` inverts the multiply, so its widening is re-derived: each rounding now drops a whole step where it dropped half of one, which doubles the allowance for the two routes to a length, and the multiply on the way in drops only downward, so its own step goes at the top of the range alone. The derived allowance for one truncation either side is measurably too narrow -- it excludes boxes drawings were made in, in eleven generated cases -- because each route is a chain of multiplies rather than one. Measured on the fixed-shape fixture (`Edits::fixed_branches`), seed 1 depth 8, 500 frames of `many`, medians of 25 runs of uninstrumented release binaries with this VM's garbage `perf` readings dropped: | | instructions | cycles | IPC | | --- | ---: | ---: | ---: | | `5ed9e87`, the float head | 1,761M | 688M | 2.561 | | `60367d8`, rounding | 1,915M | 777M | 2.465 | | this | 1,800M | 715M | 2.516 | -6.0% instructions and -8.0% cycles against `60367d8`, whose twenty-five work counters are identical to this one's, so that pair is the same work at a different speed. It leaves +2.2% and +3.9% against the float head, from +8.7% and +12.9% -- but the float head draws 100 widgets to this one's 97 and writes 4,272 primitives to 3,951, so that pair is not, and the remainder is not all arithmetic. Checked: fmt, clippy, 80 suite tests and 18 core unit tests, the release oracle at 100 seeds, all fifteen shrinker cases at 400 seeds of depth 5 (seed 288 on `region-node` still failing, unchanged), and depth-6 oracle seeds 18 and 190 passing with 326 still failing. `view`, `minimal`, `text`, `random` and the tab replay render byte-identical at 1920x1200; `tabs` differs on 4,664 of 2,304,000 pixels, single-pixel-wide runs along 80 columns of one band of rounded rects, which is an antialiased edge moved less than a pixel. Three tests say what changed rather than being relaxed: a multiply drops on both sides of zero, a division cannot put back what it dropped, and an unevenly nested row's shares stay contiguous and end at its edge with each edge on the even division or one step below. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
60367d806e
commit
08c9d5aa32
5 files changed
+101
-73
No files matched your search
+30
-23
@@ -10,10 +10,10 @@ use std::{
|
||||
/// chain, and the same box summed from what its children asked for -- and has
|
||||
/// to decide whether the two are the same place. In floats they land a few
|
||||
/// bits apart, which is a defect wherever the answer changes what is drawn
|
||||
/// rather than where. Here adding and subtracting are exact and only a
|
||||
/// multiply or a conversion rounds, back onto the same steps, so two routes
|
||||
/// that come within half a step land on one number and everything downstream
|
||||
/// compares for equality instead of for nearness.
|
||||
/// rather than where. Here adding and subtracting are exact, a multiply
|
||||
/// drops to the step below, and a conversion between grids takes the nearest
|
||||
/// one, so two routes to one place land on one number and everything
|
||||
/// downstream compares for equality instead of for nearness.
|
||||
///
|
||||
/// `SHIFT` is the number of fractional bits, which is what makes the steps
|
||||
/// divide a whole number: a power of two also converts to `f32` without
|
||||
@@ -31,9 +31,9 @@ use std::{
|
||||
)]
|
||||
pub struct Fixed<const SHIFT: u32>(i32);
|
||||
|
||||
/// A length or a coordinate in pixels, to a sixty-fourth. Finer than anything
|
||||
/// a display can show, and exact in `f32` up to 262,144 px, which is what lets
|
||||
/// the same number reach the GPU.
|
||||
/// A length or a coordinate in pixels, in steps of `1/1024`. Finer than
|
||||
/// anything a display can show, and exact in `f32` up to 16,384 px, which is
|
||||
/// what lets the same number reach the GPU.
|
||||
pub type Px = Fixed<PX_SHIFT>;
|
||||
|
||||
/// How many bits of a pixel a [`Px`] keeps. One place, because [`PxVec2`]
|
||||
@@ -142,18 +142,16 @@ impl<const SHIFT: u32> Fixed<SHIFT> {
|
||||
/// Scaled by a number on any grid, which is how a length takes a fraction
|
||||
/// of itself and keeps being a length: the product is measured in the
|
||||
/// receiver's steps.
|
||||
///
|
||||
/// Dropped to the step below rather than taken to the nearest one
|
||||
/// (Bryan, 2026-09-16), which costs a share a thousandth of a pixel of
|
||||
/// its row -- less than an even number of pixels draws. Toward negative
|
||||
/// infinity on both sides of zero, since that is a shift and nothing
|
||||
/// else: a value and its negation therefore land different distances
|
||||
/// from where they came, so a flipped span can sit a step from its
|
||||
/// mirror image.
|
||||
pub const fn mul<const BY: u32>(self, by: Fixed<BY>) -> Self {
|
||||
Self(shift_round(self.0 as i64 * by.0 as i64, BY) as i32)
|
||||
}
|
||||
|
||||
/// A part of a span that is often nothing: no part of nothing is
|
||||
/// nothing, for the cost of a comparison rather than a widening
|
||||
/// multiply and a rounding.
|
||||
pub const fn scaled<const BY: u32>(self, by: Fixed<BY>) -> Self {
|
||||
match self.0 == 0 {
|
||||
true => self,
|
||||
false => self.mul(by),
|
||||
}
|
||||
Self(((self.0 as i64 * by.0 as i64) >> BY) as i32)
|
||||
}
|
||||
|
||||
/// Repeated a whole number of times, which no grid rounds.
|
||||
@@ -198,7 +196,7 @@ impl<const SHIFT: u32> Fixed<SHIFT> {
|
||||
/// `from` and `to` a fraction of the way apart, the fraction being the
|
||||
/// receiver -- the argument order [`crate::util::LerpUtil`] already uses.
|
||||
pub const fn lerp<const OF: u32>(self, from: Fixed<OF>, to: Fixed<OF>) -> Fixed<OF> {
|
||||
from.add(to.sub(from).scaled(self))
|
||||
from.add(to.sub(from).mul(self))
|
||||
}
|
||||
|
||||
pub const fn min(self, other: Self) -> Self {
|
||||
@@ -465,19 +463,28 @@ mod tests {
|
||||
assert_eq!(Px::from_int(100) * Rel::ZERO, Px::ZERO);
|
||||
}
|
||||
|
||||
/// Toward negative infinity on both sides of zero, which is what makes
|
||||
/// it a shift rather than a shift and a sign branch -- and what makes a
|
||||
/// value and its negation land different distances from where they came,
|
||||
/// so a flipped span can sit a step from its mirror image.
|
||||
#[test]
|
||||
fn halves_round_away_from_zero_either_side() {
|
||||
fn a_multiply_drops_to_the_step_below_on_both_sides_of_zero() {
|
||||
// A step and a half of one, which has no step of its own.
|
||||
let step_and_a_half = Rel::from_f32(1.5).div_int(Px::ONE.raw());
|
||||
assert_eq!(Px::ONE * step_and_a_half, Px::from_raw(2));
|
||||
assert_eq!(Px::ONE * step_and_a_half, Px::from_raw(1));
|
||||
assert_eq!(Px::ONE.neg() * step_and_a_half, Px::from_raw(-2));
|
||||
}
|
||||
|
||||
/// A division rounds to the nearest step, so it cannot put back the
|
||||
/// steps a truncating multiply dropped: a round trip comes back short,
|
||||
/// never long, and by the few steps the two operations gave up.
|
||||
#[test]
|
||||
fn dividing_by_a_fraction_undoes_multiplying_by_it() {
|
||||
fn dividing_by_a_fraction_cannot_undo_a_truncating_multiply() {
|
||||
let third = Rel::ONE / Rel::from_int(3);
|
||||
let len = Px::from_int(300);
|
||||
assert_eq!(len * third / third, len);
|
||||
let back = len * third / third;
|
||||
assert!(back <= len, "{back:?} is longer than {len:?}");
|
||||
assert!(len - back <= Px::from_raw(3), "{back:?} against {len:?}");
|
||||
assert_eq!(Px::from_int(100) / Rel::from_f32(0.5), Px::from_int(200));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user