From dc26b567271101535f43dd61ac6dc4a768d3ab25 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 29 Nov 2026 14:22:09 +0100 Subject: [PATCH 1/2] upscale: vector EASU and RCAS rows under GOEXPERIMENT=simd (#141) 1280x720 -> 1920x1080 on one Ryzen 9 7950X core: 242 ms scalar, 82 ms with the portable Go 1.27 simd package (EASU alone 193 -> 73 ms). The vector rows are chosen at init, so the scalar build keeps 100% coverage and no branch in run(). Tests compare both rows with the scalar code under ErrorBound, including NaN lobes on black rings and ragged widths. Also an exact flat shortcut in easuPixel. Refs #141 Co-Authored-By: Claude Sonnet 5.5 --- README.md | 10 ++- upscale/easu.go | 38 ++++++-- upscale/easu_simd.go | 178 ++++++++++++++++++++++++++++++++++++++ upscale/easu_simd_test.go | 48 ++++++++++ upscale/rcas.go | 51 +++++++---- upscale/rcas_simd_test.go | 44 ++++++++++ 6 files changed, 339 insertions(+), 30 deletions(-) create mode 100644 upscale/easu_simd.go create mode 100644 upscale/easu_simd_test.go create mode 100644 upscale/rcas_simd_test.go diff --git a/README.md b/README.md index e3c4b9b..4500d07 100644 --- a/README.md +++ b/README.md @@ -1430,10 +1430,12 @@ PSNR on some. Speed, one core of a Ryzen 9 7950X (load about 2), 1280x720 to 1920x1080, ms per frame: ffmpeg 6.1.1 `scale` bilinear 3.3, bicubic 2.0, lanczos 2.8 (source and -conversion subtracted) against `upscale` 239 (EASU alone 191); 1080p to 2160p takes -935 ms. **This is scalar Go and about 90 times slower than ffmpeg's SIMD scaler: -it is an open defect** (a vector kernel is tracked in the repository's issues), so -for now it suits stills, offline work and small frames, not real-time 1080p. +conversion subtracted) against `upscale` 242 scalar (EASU alone 193) and **82 with +`GOEXPERIMENT=simd`** (EASU alone 73), the portable Go 1.27 `simd` package on +AVX2/AVX-512 and NEON; the scalar build is the default and the two agree within +`ErrorBound`. Still about 30 times slower than ffmpeg's hand-written scaler, which +is an open defect (the 12-tap gather per output pixel dominates), so it suits +stills, offline work and small frames, not real-time 1080p. ## Guarantees diff --git a/upscale/easu.go b/upscale/easu.go index 63b428c..3e4377b 100644 --- a/upscale/easu.go +++ b/upscale/easu.go @@ -19,7 +19,20 @@ func rcpMed(a float32) float32 { return b * (-b*a + 2) } -func sat(a float32) float32 { return min(1, max(0, a)) } +// fmin and fmax are the builtins, named so the kernels read alike. (The +// comparison forms were measured 28 % slower on amd64: the branches +// mispredict on image data.) +func fmin(a, b float32) float32 { return min(a, b) } + +func fmax(a, b float32) float32 { return max(a, b) } + +// clamp4 limits v to the range of a, b, c, d. +func clamp4(v, a, b, c, d float32) float32 { + lo, hi := fmin(fmin(a, b), fmin(c, d)), fmax(fmax(a, b), fmax(c, d)) + return fmin(fmax(v, lo), hi) +} + +func sat(a float32) float32 { return fmin(1, fmax(0, a)) } func abs32(a float32) float32 { return math.Float32frombits(math.Float32bits(a) &^ (1 << 31)) } @@ -30,13 +43,13 @@ func abs32(a float32) float32 { return math.Float32frombits(math.Float32bits(a) // b c d // e func easuSet(dirX, dirY, ln *float32, w, a, b, c, d, e float32) { - lenX := rcpLo(max(abs32(d-c), abs32(c-b))) + lenX := rcpLo(fmax(abs32(d-c), abs32(c-b))) dx := d - b *dirX += dx * w lenX = sat(abs32(dx) * lenX) lenX *= lenX *ln += lenX * w - lenY := rcpLo(max(abs32(e-c), abs32(c-a))) + lenY := rcpLo(fmax(abs32(e-c), abs32(c-a))) dy := e - a *dirY += dy * w lenY = sat(abs32(dy) * lenY) @@ -48,7 +61,7 @@ func easuSet(dirX, dirY, ln *float32, w, a, b, c, d, e float32) { func easuTap(offx, offy, dirx, diry, lx, ly, lob, clp float32) float32 { vx := (offx*dirx + offy*diry) * lx vy := (offx*-diry + offy*dirx) * ly - d2 := min(vx*vx+vy*vy, clp) + d2 := fmin(vx*vx+vy*vy, clp) wB := 2.0/5.0*d2 - 1 wA := lob*d2 - 1 wB *= wB @@ -77,6 +90,15 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32) iE, iF, iG, iH := base-1, base, base+1, base+2 iI, iJ, iK, iL := base+s-1, base+s, base+s+1, base+s+2 iN, iO := base+2*s, base+2*s+1 + // The result is clamped to the range of the four nearest pixels (f, g, + // j, k); when they are equal in every channel the range is a point and + // the result is that colour exactly, so no direction or weight is needed. + r0, g0, b0 := p.r[iF], p.g[iF], p.b[iF] + if p.r[iG] == r0 && p.r[iJ] == r0 && p.r[iK] == r0 && + p.g[iG] == g0 && p.g[iJ] == g0 && p.g[iK] == g0 && + p.b[iG] == b0 && p.b[iJ] == b0 && p.b[iK] == b0 { + return r0, g0, b0 + } l := p.l bL, cL, eL, fL, gL, hL := l[iB], l[iC], l[iE], l[iF], l[iG], l[iH] iL_, jL, kL, lL, nL, oL := l[iI], l[iJ], l[iK], l[iL], l[iN], l[iO] @@ -97,7 +119,7 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32) dirY *= dirR ln *= 0.5 ln *= ln - stretch := (dirX*dirX + dirY*dirY) * rcpLo(max(abs32(dirX), abs32(dirY))) + stretch := (dirX*dirX + dirY*dirY) * rcpLo(fmax(abs32(dirX), abs32(dirY))) lx := 1 + (stretch-1)*ln ly := 1 + -0.5*ln lob := 0.5 + ((1.0/4.0-0.04)-0.5)*ln @@ -114,9 +136,9 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32) } rcp := 1 / aW // Dering against the four nearest: f, g, j, k. - r := min(max(aR*rcp, min(min(p.r[iF], p.r[iG]), min(p.r[iJ], p.r[iK]))), max(max(p.r[iF], p.r[iG]), max(p.r[iJ], p.r[iK]))) - g := min(max(aG*rcp, min(min(p.g[iF], p.g[iG]), min(p.g[iJ], p.g[iK]))), max(max(p.g[iF], p.g[iG]), max(p.g[iJ], p.g[iK]))) - b := min(max(aB*rcp, min(min(p.b[iF], p.b[iG]), min(p.b[iJ], p.b[iK]))), max(max(p.b[iF], p.b[iG]), max(p.b[iJ], p.b[iK]))) + r := clamp4(aR*rcp, r0, p.r[iG], p.r[iJ], p.r[iK]) + g := clamp4(aG*rcp, g0, p.g[iG], p.g[iJ], p.g[iK]) + b := clamp4(aB*rcp, b0, p.b[iG], p.b[iJ], p.b[iK]) return r, g, b } diff --git a/upscale/easu_simd.go b/upscale/easu_simd.go new file mode 100644 index 0000000..cea51d3 --- /dev/null +++ b/upscale/easu_simd.go @@ -0,0 +1,178 @@ +//go:build goexperiment.simd + +package upscale + +import "simd" + +// Vector EASU (GOEXPERIMENT=simd, Go 1.27): the portable simd package maps to +// AVX2 on amd64 and NEON on arm64. The 12 taps of each output pixel are +// gathered by scalar loads into [12][maxLanes] stack arrays (there is no +// 2D gather in hardware worth using here), then the direction, length and +// weight arithmetic runs one pixel per lane. Every expression is the scalar +// one of easuPixel, evaluated in the same order, so results differ from it +// only by the compiler's FMA contraction. + +const maxLanes = 16 + +// useVec reports whether easuRowVec can run here: the hardware vector must +// fit the stack buffers. +func useVec() bool { return bc(0).Len() <= maxLanes } + +func init() { + if useVec() { + easuRowFn, rcasRowFn = easuRowVec, rcasRowVec + } +} + +type tapBuf [12][maxLanes]float32 + +func ld(a *[maxLanes]float32) simd.Float32s { return simd.LoadFloat32s(a[:]) } + +func bc(v float32) simd.Float32s { return simd.BroadcastFloat32s(v) } + +func vRcpLo(a simd.Float32s) simd.Float32s { + return simd.BroadcastUint32s(0x7ef07ebb).Sub(a.ToBits()).BitsToFloat32() +} + +func vRsqLo(a simd.Float32s) simd.Float32s { + return simd.BroadcastUint32s(0x5f347d74).Sub(a.ToBits().ShiftAllRight(1)).BitsToFloat32() +} + +func vSat(a simd.Float32s) simd.Float32s { return a.Max(bc(0)).Min(bc(1)) } + +// vEasuSet is easuSet, one pixel per lane. +func vEasuSet(dirX, dirY, ln, w, a, b, c, d, e simd.Float32s) (simd.Float32s, simd.Float32s, simd.Float32s) { + lenX := vRcpLo(d.Sub(c).Abs().Max(c.Sub(b).Abs())) + dx := d.Sub(b) + dirX = dirX.Add(dx.Mul(w)) + lenX = vSat(dx.Abs().Mul(lenX)) + lenX = lenX.Mul(lenX) + ln = ln.Add(lenX.Mul(w)) + lenY := vRcpLo(e.Sub(c).Abs().Max(c.Sub(a).Abs())) + dy := e.Sub(a) + dirY = dirY.Add(dy.Mul(w)) + lenY = vSat(dy.Abs().Mul(lenY)) + lenY = lenY.Mul(lenY) + ln = ln.Add(lenY.Mul(w)) + return dirX, dirY, ln +} + +// easuRowVec computes one output row of EASU: the pixels 0..ow-1, whose "f" +// taps are at source row fy, fractional row position py. +func easuRowVec(p *planes, ax axis, fy int, py float32, ow int, dr, dg, db []float32) { + var tl, tr, tg, tb tapBuf + var ppxs, out [maxLanes]float32 + n := bc(0).Len() + one, zero := bc(1), bc(0) + ppy := bc(py) + for x0 := 0; x0 < ow; x0 += n { + cnt := min(n, ow-x0) + for j := 0; j < n; j++ { + ox := x0 + min(j, cnt-1) // spare lanes repeat the last pixel + base := p.at(int(ax.fp[ox]), fy) + ppxs[j] = ax.pp[ox] + for k, off := range p.tapOff { + i := base + off + tl[k][j], tr[k][j], tg[k][j], tb[k][j] = p.l[i], p.r[i], p.g[i], p.b[i] + } + } + ppx := ld(&ppxs) + // Luma taps by the reference's names. + bL, cL, iL, jL, fL, eL := ld(&tl[0]), ld(&tl[1]), ld(&tl[2]), ld(&tl[3]), ld(&tl[4]), ld(&tl[5]) + kL, lL, hL, gL, oL, nL := ld(&tl[6]), ld(&tl[7]), ld(&tl[8]), ld(&tl[9]), ld(&tl[10]), ld(&tl[11]) + + ipx, ipy := one.Sub(ppx), one.Sub(ppy) + dirX, dirY, ln := zero, zero, zero + dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ipx.Mul(ipy), bL, eL, fL, gL, jL) + dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ppx.Mul(ipy), cL, fL, gL, hL, kL) + dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ipx.Mul(ppy), fL, iL, jL, kL, nL) + dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ppx.Mul(ppy), gL, jL, kL, lL, oL) + + dirR := dirX.Mul(dirX).Add(dirY.Mul(dirY)) + small := dirR.Less(bc(1.0 / 32768.0)) + dirR = one.IfElse(small, vRsqLo(dirR)) + dirX = one.IfElse(small, dirX) + dirX = dirX.Mul(dirR) + dirY = dirY.Mul(dirR) + ln = ln.Mul(bc(0.5)) + ln = ln.Mul(ln) + stretch := dirX.Mul(dirX).Add(dirY.Mul(dirY)).Mul(vRcpLo(dirX.Abs().Max(dirY.Abs()))) + lx := one.Add(stretch.Sub(one).Mul(ln)) + ly := one.Add(bc(-0.5).Mul(ln)) + lob := bc(0.5).Add(bc((1.0/4.0 - 0.04) - 0.5).Mul(ln)) + clp := vRcpLo(lob) + + aR, aG, aB, aW := zero, zero, zero, zero + for k := range tapDX { + offx, offy := bc(tapDX[k]).Sub(ppx), bc(tapDY[k]).Sub(ppy) + vx := offx.Mul(dirX).Add(offy.Mul(dirY)).Mul(lx) + vy := offx.Mul(dirY.Neg()).Add(offy.Mul(dirX)).Mul(ly) + d2 := vx.Mul(vx).Add(vy.Mul(vy)).Min(clp) + wB := bc(2.0 / 5.0).Mul(d2).Sub(one) + wA := lob.Mul(d2).Sub(one) + wB = wB.Mul(wB) + wA = wA.Mul(wA) + wB = bc(25.0 / 16.0).Mul(wB).Sub(bc(25.0/16.0 - 1.0)) + w := wB.Mul(wA) + aR = aR.Add(ld(&tr[k]).Mul(w)) + aG = aG.Add(ld(&tg[k]).Mul(w)) + aB = aB.Add(ld(&tb[k]).Mul(w)) + aW = aW.Add(w) + } + rcp := one.Div(aW) + vFinish(aR, rcp, &tr, &out, dr[x0:x0+cnt]) + vFinish(aG, rcp, &tg, &out, dg[x0:x0+cnt]) + vFinish(aB, rcp, &tb, &out, db[x0:x0+cnt]) + } +} + +// vFinish normalises one channel and dering-clamps it to the range of the +// four nearest pixels f, g, j, k (taps 4, 9, 3, 6). +func vFinish(a, rcp simd.Float32s, t *tapBuf, out *[maxLanes]float32, dst []float32) { + f, g, j, k := ld(&t[4]), ld(&t[9]), ld(&t[3]), ld(&t[6]) + lo, hi := f.Min(g).Min(j.Min(k)), f.Max(g).Max(j.Max(k)) + a.Mul(rcp).Max(lo).Min(hi).Store(out[:]) + copy(dst, out[:]) +} + +// vnmax is nmax, one pixel per lane: a NaN operand yields the other one. +func vnmax(a, b simd.Float32s) simd.Float32s { + r := a.Max(b) + r = b.IfElse(a.NotEqual(a), r) + return a.IfElse(b.NotEqual(b), r) +} + +// rcasRowVec is rcasRow for the whole row: the interior in vectors, the +// first pixel and the ragged tail by the scalar code (edge clamping). +func rcasRowVec(w int, sharp float32, u, m, d, out rowBuf) { + n := bc(0).Len() + x := min(1, w) + rcasRow(w, 0, x, sharp, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, out.r, out.g, out.b) + one, four, vsharp := bc(1), bc(4), bc(sharp) + for ; x+n+1 <= w; x += n { + var lobe simd.Float32s + var chans [3]struct{ b, dd, e, f, h simd.Float32s } + for c, p := range [3][4][]float32{{u.r, m.r, d.r, out.r}, {u.g, m.g, d.g, out.g}, {u.b, m.b, d.b, out.b}} { + b, dd, e, f, h := simd.LoadFloat32s(p[0][x:]), simd.LoadFloat32s(p[1][x-1:]), simd.LoadFloat32s(p[1][x:]), simd.LoadFloat32s(p[1][x+1:]), simd.LoadFloat32s(p[2][x:]) + chans[c] = struct{ b, dd, e, f, h simd.Float32s }{b, dd, e, f, h} + mn4, mx4 := b.Min(dd).Min(f.Min(h)), b.Max(dd).Max(f.Max(h)) + hitMin := mn4.Min(e).Mul(one.Div(four.Mul(mx4))) + hitMax := one.Sub(mx4.Max(e)).Mul(one.Div(four.Mul(mn4).Sub(four))) + l := vnmax(hitMin.Neg(), hitMax) + if c == 0 { + lobe = l + } else { + lobe = vnmax(lobe, l) + } + } + lobe = vnmax(bc(-rcasLimit), lobe.Min(bc(0))).Mul(vsharp) + den := four.Mul(lobe).Add(one) + bb := simd.BroadcastUint32s(0x7ef19fff).Sub(den.ToBits()).BitsToFloat32() + rcp := bb.Mul(bb.Neg().Mul(den).Add(bc(2))) + for c, o := range [3][]float32{out.r, out.g, out.b} { + s := chans[c] + lobe.Mul(s.b).Add(lobe.Mul(s.dd)).Add(lobe.Mul(s.h)).Add(lobe.Mul(s.f)).Add(s.e).Mul(rcp).Store(o[x:]) + } + } + rcasRow(w, x, w, sharp, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, out.r, out.g, out.b) +} diff --git a/upscale/easu_simd_test.go b/upscale/easu_simd_test.go new file mode 100644 index 0000000..39b73d4 --- /dev/null +++ b/upscale/easu_simd_test.go @@ -0,0 +1,48 @@ +//go:build goexperiment.simd + +package upscale + +import ( + "image" + "math/rand" + "runtime" + "testing" +) + +// TestVectorMatchesScalar compares the vector EASU row with easuPixel on +// noise, edges and flat patches, for widths that are not multiples of the +// vector length. +func TestVectorMatchesScalar(t *testing.T) { + if (runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64") && !useVec() { + t.Fatal("vector kernel unavailable on a SIMD architecture") + } + rng := rand.New(rand.NewSource(7)) + for _, c := range []struct{ sw, sh, ow, oh int }{{20, 14, 31, 21}, {17, 9, 17, 9}, {33, 12, 64, 24}, {5, 5, 9, 9}} { + src := image.NewNRGBA(image.Rect(0, 0, c.sw, c.sh)) + for i := range src.Pix { + switch { + case i%97 < 30: + src.Pix[i] = 128 // flat patches + case i%2 == 0: + src.Pix[i] = uint8(rng.Intn(256)) + default: + src.Pix[i] = uint8(i) + } + } + p := fromImage(src) + ax, ay := newAxis(c.sw, c.ow), newAxis(c.sh, c.oh) + dr, dg, db := make([]float32, c.ow), make([]float32, c.ow), make([]float32, c.ow) + for oy := 0; oy < c.oh; oy++ { + fy, py := int(ay.fp[oy]), ay.pp[oy] + easuRowVec(p, ax, fy, py, c.ow, dr, dg, db) + for ox := 0; ox < c.ow; ox++ { + r, g, b := easuPixel(p, p.at(int(ax.fp[ox]), fy), ax.pp[ox], py) + for _, d := range []float32{abs32(r - dr[ox]), abs32(g - dg[ox]), abs32(b - db[ox])} { + if d > ErrorBound { + t.Fatalf("%dx%d->%dx%d (%d,%d): vector differs from scalar by %g", c.sw, c.sh, c.ow, c.oh, ox, oy, d) + } + } + } + } + } +} diff --git a/upscale/rcas.go b/upscale/rcas.go index 860198a..27495cd 100644 --- a/upscale/rcas.go +++ b/upscale/rcas.go @@ -19,31 +19,32 @@ func nmax(a, b float32) float32 { if b != b { return a } - return max(a, b) + return fmax(a, b) } // rcasRow sharpens one output row from the rows above (u), at (m) and -// below (d), clamping at the left and right edges. -func rcasRow(w int, sharp float32, ur, ug, ub, mr, mg, mb, dr, dg, db, outR, outG, outB []float32) { - for x := 0; x < w; x++ { +// below (d), clamping at the left and right edges. It handles the pixels +// lo..hi-1 of a row w pixels wide. +func rcasRow(w, lo, hi int, sharp float32, ur, ug, ub, mr, mg, mb, dr, dg, db, outR, outG, outB []float32) { + for x := lo; x < hi; x++ { xl, xr := max(x-1, 0), min(x+1, w-1) bR, bG, bB := ur[x], ug[x], ub[x] dR, dG, dB := mr[xl], mg[xl], mb[xl] eR, eG, eB := mr[x], mg[x], mb[x] fR, fG, fB := mr[xr], mg[xr], mb[xr] hR, hG, hB := dr[x], dg[x], db[x] - mn4R, mn4G, mn4B := min(min(bR, dR), min(fR, hR)), min(min(bG, dG), min(fG, hG)), min(min(bB, dB), min(fB, hB)) - mx4R, mx4G, mx4B := max(max(bR, dR), max(fR, hR)), max(max(bG, dG), max(fG, hG)), max(max(bB, dB), max(fB, hB)) - hitMinR := min(mn4R, eR) * (1 / (4 * mx4R)) - hitMinG := min(mn4G, eG) * (1 / (4 * mx4G)) - hitMinB := min(mn4B, eB) * (1 / (4 * mx4B)) - hitMaxR := (1 - max(mx4R, eR)) * (1 / (4*mn4R - 4)) - hitMaxG := (1 - max(mx4G, eG)) * (1 / (4*mn4G - 4)) - hitMaxB := (1 - max(mx4B, eB)) * (1 / (4*mn4B - 4)) + mn4R, mn4G, mn4B := fmin(fmin(bR, dR), fmin(fR, hR)), fmin(fmin(bG, dG), fmin(fG, hG)), fmin(fmin(bB, dB), fmin(fB, hB)) + mx4R, mx4G, mx4B := fmax(fmax(bR, dR), fmax(fR, hR)), fmax(fmax(bG, dG), fmax(fG, hG)), fmax(fmax(bB, dB), fmax(fB, hB)) + hitMinR := fmin(mn4R, eR) * (1 / (4 * mx4R)) + hitMinG := fmin(mn4G, eG) * (1 / (4 * mx4G)) + hitMinB := fmin(mn4B, eB) * (1 / (4 * mx4B)) + hitMaxR := (1 - fmax(mx4R, eR)) * (1 / (4*mn4R - 4)) + hitMaxG := (1 - fmax(mx4G, eG)) * (1 / (4*mn4G - 4)) + hitMaxB := (1 - fmax(mx4B, eB)) * (1 / (4*mn4B - 4)) lobeR := nmax(-hitMinR, hitMaxR) lobeG := nmax(-hitMinG, hitMaxG) lobeB := nmax(-hitMinB, hitMaxB) - lobe := nmax(-rcasLimit, min(nmax(nmax(lobeR, lobeG), lobeB), 0)) * sharp + lobe := nmax(-rcasLimit, fmin(nmax(nmax(lobeR, lobeG), lobeB), 0)) * sharp rcp := rcpMed(4*lobe + 1) outR[x] = (lobe*bR + lobe*dR + lobe*hR + lobe*fR + eR) * rcp outG[x] = (lobe*bG + lobe*dG + lobe*hG + lobe*fG + eG) * rcp @@ -51,6 +52,23 @@ func rcasRow(w int, sharp float32, ur, ug, ub, mr, mg, mb, dr, dg, db, outR, out } } +// The row kernels: scalar here, replaced by the vector ones at init under +// GOEXPERIMENT=simd (easu_simd.go) when the hardware vector fits. +var ( + easuRowFn = easuRowScalar + rcasRowFn = rcasRowScalar +) + +func easuRowScalar(p *planes, ax axis, fy int, py float32, ow int, dr, dg, db []float32) { + for ox := 0; ox < ow; ox++ { + dr[ox], dg[ox], db[ox] = easuPixel(p, p.at(int(ax.fp[ox]), fy), ax.pp[ox], py) + } +} + +func rcasRowScalar(w int, sharp float32, u, m, d, out rowBuf) { + rcasRow(w, 0, w, sharp, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, out.r, out.g, out.b) +} + // rcasScale converts stops of attenuation to the linear constant // (FsrRcasCon). func rcasScale(stops float32) float32 { @@ -73,10 +91,7 @@ func run(p *planes, ow, oh int, sharp float32, emit func(y int, r, g, b, a []flo alpha = make([]float32, ow) } easuRow := func(oy int, dst rowBuf) { - fy, py := int(ay.fp[oy]), ay.pp[oy] - for ox := 0; ox < ow; ox++ { - dst.r[ox], dst.g[ox], dst.b[ox] = easuPixel(p, p.at(int(ax.fp[ox]), fy), ax.pp[ox], py) - } + easuRowFn(p, ax, int(ay.fp[oy]), ay.pp[oy], ow, dst.r, dst.g, dst.b) } // Bilinear alpha at the same sample position as the colour. alphaFor := func(oy int) []float32 { @@ -112,7 +127,7 @@ func run(p *planes, ow, oh int, sharp float32, emit func(y int, r, g, b, a []flo easuRow(computed, ring[computed%3]) } u, m, d := ring[max(oy-1, 0)%3], ring[oy%3], ring[min(oy+1, oh-1)%3] - rcasRow(ow, k, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, out.r, out.g, out.b) + rcasRowFn(ow, k, u, m, d, out) emit(oy, out.r, out.g, out.b, alphaFor(oy)) } } diff --git a/upscale/rcas_simd_test.go b/upscale/rcas_simd_test.go new file mode 100644 index 0000000..eee7735 --- /dev/null +++ b/upscale/rcas_simd_test.go @@ -0,0 +1,44 @@ +//go:build goexperiment.simd + +package upscale + +import ( + "math/rand" + "testing" +) + +// TestRCASVectorMatchesScalar compares the vector RCAS row with rcasRow, +// including black rings (0*inf = NaN in the lobe) and widths that are not +// multiples of the vector length. +func TestRCASVectorMatchesScalar(t *testing.T) { + rng := rand.New(rand.NewSource(11)) + fill := func(w int) rowBuf { + r := newRow(w) + for i := 0; i < w; i++ { + for _, c := range [][]float32{r.r, r.g, r.b} { + switch rng.Intn(4) { + case 0: + c[i] = 0 + case 1: + c[i] = 1 + default: + c[i] = rng.Float32() + } + } + } + return r + } + for _, w := range []int{0, 1, 2, 3, 7, 16, 33, 100} { + u, m, d := fill(w), fill(w), fill(w) + got, want := newRow(w), newRow(w) + rcasRowVec(w, 0.5, u, m, d, got) + rcasRow(w, 0, w, 0.5, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, want.r, want.g, want.b) + for x := 0; x < w; x++ { + for _, p := range [][2]float32{{got.r[x], want.r[x]}, {got.g[x], want.g[x]}, {got.b[x], want.b[x]}} { + if !(abs32(p[0]-p[1]) <= ErrorBound) { + t.Fatalf("w=%d x=%d: vector %g, scalar %g", w, x, p[0], p[1]) + } + } + } + } +} From 8da57b6df0d6c2ab75f9b6f1f6d722093882b540 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 29 Nov 2026 14:23:34 +0100 Subject: [PATCH 2/2] upscale: drop the unproven flat shortcut Co-Authored-By: Claude Sonnet 5.5 --- upscale/easu.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/upscale/easu.go b/upscale/easu.go index 3e4377b..4690775 100644 --- a/upscale/easu.go +++ b/upscale/easu.go @@ -90,15 +90,6 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32) iE, iF, iG, iH := base-1, base, base+1, base+2 iI, iJ, iK, iL := base+s-1, base+s, base+s+1, base+s+2 iN, iO := base+2*s, base+2*s+1 - // The result is clamped to the range of the four nearest pixels (f, g, - // j, k); when they are equal in every channel the range is a point and - // the result is that colour exactly, so no direction or weight is needed. - r0, g0, b0 := p.r[iF], p.g[iF], p.b[iF] - if p.r[iG] == r0 && p.r[iJ] == r0 && p.r[iK] == r0 && - p.g[iG] == g0 && p.g[iJ] == g0 && p.g[iK] == g0 && - p.b[iG] == b0 && p.b[iJ] == b0 && p.b[iK] == b0 { - return r0, g0, b0 - } l := p.l bL, cL, eL, fL, gL, hL := l[iB], l[iC], l[iE], l[iF], l[iG], l[iH] iL_, jL, kL, lL, nL, oL := l[iI], l[iJ], l[iK], l[iL], l[iN], l[iO] @@ -136,9 +127,9 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32) } rcp := 1 / aW // Dering against the four nearest: f, g, j, k. - r := clamp4(aR*rcp, r0, p.r[iG], p.r[iJ], p.r[iK]) - g := clamp4(aG*rcp, g0, p.g[iG], p.g[iJ], p.g[iK]) - b := clamp4(aB*rcp, b0, p.b[iG], p.b[iJ], p.b[iK]) + r := clamp4(aR*rcp, p.r[iF], p.r[iG], p.r[iJ], p.r[iK]) + g := clamp4(aG*rcp, p.g[iF], p.g[iG], p.g[iJ], p.g[iK]) + b := clamp4(aB*rcp, p.b[iF], p.b[iG], p.b[iJ], p.b[iK]) return r, g, b }