Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

16.8 KB
Raw Blame
export function h4x2(v: i16x8): i16x8 {
  const p = v.swizzle([1, 0, 3, 2, 5, 4, 7, 6])
  const t = i16x8.select(i16x8(-1, 0, -1, 0, -1, 0, -1, 0), v + p, p - v)
  const q = t.swizzle([2, 3, 0, 1, 6, 7, 4, 5])
  return i16x8.select(i16x8(-1, -1, 0, 0, -1, -1, 0, 0), t + q, q - t)
}

function h8(v: i16x8): i16x8 {
  const u = h4x2(v)
  const r = u.swizzle([4, 5, 6, 7, 0, 1, 2, 3])
  return i16x8.select(i16x8(-1, -1, -1, -1, 0, 0, 0, 0), u + r, r - u)
}

function h4(v: i32x4): i32x4 {
  const p = v.swizzle([1, 0, 3, 2])
  const t = i32x4.select(i32x4(-1, 0, -1, 0), v + p, p - v)
  const q = t.swizzle([2, 3, 0, 1])
  return i32x4.select(i32x4(-1, -1, 0, 0), t + q, q - t)
}

function satdPair(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  const d0 = i16x8.loadU8(a, ao) - i16x8.loadU8(b, bo)
  const d1 = i16x8.loadU8(a, ao + as) - i16x8.loadU8(b, bo + bs)
  const d2 = i16x8.loadU8(a, ao + 2 * as) - i16x8.loadU8(b, bo + 2 * bs)
  const d3 = i16x8.loadU8(a, ao + 3 * as) - i16x8.loadU8(b, bo + 3 * bs)
  const s01 = d0 + d1
  const m01 = d0 - d1
  const s23 = d2 + d3
  const m23 = d2 - d3
  const v = h4x2(s01 + s23).abs() + h4x2(s01 - s23).abs() + h4x2(m01 + m23).abs() + h4x2(m01 - m23).abs()
  return v.sum() >> 1
}

export function satd4(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  const d0 = i32x4.loadU8(a, ao) - i32x4.loadU8(b, bo)
  const d1 = i32x4.loadU8(a, ao + as) - i32x4.loadU8(b, bo + bs)
  const d2 = i32x4.loadU8(a, ao + 2 * as) - i32x4.loadU8(b, bo + 2 * bs)
  const d3 = i32x4.loadU8(a, ao + 3 * as) - i32x4.loadU8(b, bo + 3 * bs)
  const s01 = d0 + d1
  const m01 = d0 - d1
  const s23 = d2 + d3
  const m23 = d2 - d3
  const v = h4(s01 + s23).abs() + h4(s01 - s23).abs() + h4(m01 + m23).abs() + h4(m01 - m23).abs()
  return v.sum() >> 1
}

export function h4x4(v: i16x16): i16x16 {
  const p = v.swizzle([1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14])
  const t = i16x16.select(i16x16(-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0), v + p, p - v)
  const q = t.swizzle([2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13])
  return i16x16.select(i16x16(-1, -1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 0), t + q, q - t)
}

function h4x4AbsMax(v: i16x16): i16x16 {
  const p = v.swizzle([1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14])
  const t = i16x16.select(i16x16(-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0), v + p, p - v).abs()
  return t.max(t.swizzle([2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13]))
}

export function satd16(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  // The last butterfly stage folds into |a + b| + |a - b| = 2 * max(|a|, |b|), and each pair's max sits in both of
  // its lanes. A lane adds half of eight coefficients of one 4x4 block per band, at most sqrt(8) * 16 * 255 / 2 < 5800
  // (Cauchy-Schwarz with Parseval), so the four bands stay within 16 bits.
  let acc = i16x16.splat(0)
  for (let by = 0; by < 16; by += 4) {
    const oa = ao + by * as
    const ob = bo + by * bs
    const d0 = i16x16.loadU8(a, oa) - i16x16.loadU8(b, ob)
    const d1 = i16x16.loadU8(a, oa + as) - i16x16.loadU8(b, ob + bs)
    const d2 = i16x16.loadU8(a, oa + 2 * as) - i16x16.loadU8(b, ob + 2 * bs)
    const d3 = i16x16.loadU8(a, oa + 3 * as) - i16x16.loadU8(b, ob + 3 * bs)
    const s01 = d0 + d1
    const m01 = d0 - d1
    const s23 = d2 + d3
    const m23 = d2 - d3
    acc += h4x4AbsMax(s01 + s23) + h4x4AbsMax(s01 - s23) + h4x4AbsMax(m01 + m23) + h4x4AbsMax(m01 - m23)
  }
  return acc.sum() >> 1
}

function avgRow(p: Uint8Array, po: int, q: Uint8Array, qo: int): i16x16 {
  return i16x16.bitcast(u16x16.loadU8(p, po).avg(u16x16.loadU8(q, qo)))
}

export function satdAvg16(a: Uint8Array, ao: int, as: int, p: Uint8Array, po: int, q: Uint8Array, qo: int, ps: int): int {
  let acc = i16x16.splat(0)
  for (let by = 0; by < 16; by += 4) {
    const oa = ao + by * as
    const op = po + by * ps
    const oq = qo + by * ps
    const d0 = i16x16.loadU8(a, oa) - avgRow(p, op, q, oq)
    const d1 = i16x16.loadU8(a, oa + as) - avgRow(p, op + ps, q, oq + ps)
    const d2 = i16x16.loadU8(a, oa + 2 * as) - avgRow(p, op + 2 * ps, q, oq + 2 * ps)
    const d3 = i16x16.loadU8(a, oa + 3 * as) - avgRow(p, op + 3 * ps, q, oq + 3 * ps)
    const s01 = d0 + d1
    const m01 = d0 - d1
    const s23 = d2 + d3
    const m23 = d2 - d3
    acc += h4x4AbsMax(s01 + s23) + h4x4AbsMax(s01 - s23) + h4x4AbsMax(m01 + m23) + h4x4AbsMax(m01 - m23)
  }
  return acc.sum() >> 1
}

export function satd8(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  return satdPair(a, ao, as, b, bo, bs) + satdPair(a, ao + 4 * as, as, b, bo + 4 * bs, bs)
}

export function sa8d(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  const r0 = i16x8.loadU8(a, ao) - i16x8.loadU8(b, bo)
  const r1 = i16x8.loadU8(a, ao + as) - i16x8.loadU8(b, bo + bs)
  const r2 = i16x8.loadU8(a, ao + 2 * as) - i16x8.loadU8(b, bo + 2 * bs)
  const r3 = i16x8.loadU8(a, ao + 3 * as) - i16x8.loadU8(b, bo + 3 * bs)
  const r4 = i16x8.loadU8(a, ao + 4 * as) - i16x8.loadU8(b, bo + 4 * bs)
  const r5 = i16x8.loadU8(a, ao + 5 * as) - i16x8.loadU8(b, bo + 5 * bs)
  const r6 = i16x8.loadU8(a, ao + 6 * as) - i16x8.loadU8(b, bo + 6 * bs)
  const r7 = i16x8.loadU8(a, ao + 7 * as) - i16x8.loadU8(b, bo + 7 * bs)
  const a0 = r0 + r1
  const a1 = r0 - r1
  const a2 = r2 + r3
  const a3 = r2 - r3
  const a4 = r4 + r5
  const a5 = r4 - r5
  const a6 = r6 + r7
  const a7 = r6 - r7
  const b0 = a0 + a2
  const b1 = a1 + a3
  const b2 = a0 - a2
  const b3 = a1 - a3
  const b4 = a4 + a6
  const b5 = a5 + a7
  const b6 = a4 - a6
  const b7 = a5 - a7
  const lo = u16x8.bitcast(h8(b0 + b4).abs()) + u16x8.bitcast(h8(b1 + b5).abs()) +
    u16x8.bitcast(h8(b2 + b6).abs()) + u16x8.bitcast(h8(b3 + b7).abs())
  const hi = u16x8.bitcast(h8(b0 - b4).abs()) + u16x8.bitcast(h8(b1 - b5).abs()) +
    u16x8.bitcast(h8(b2 - b6).abs()) + u16x8.bitcast(h8(b3 - b7).abs())
  return (lo.sum() + hi.sum() + 2) >> 2
}

export function sa8dPair(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  const r0 = i16x16.loadU8(a, ao) - i16x16.loadU8(b, bo)
  const r1 = i16x16.loadU8(a, ao + as) - i16x16.loadU8(b, bo + bs)
  const r2 = i16x16.loadU8(a, ao + 2 * as) - i16x16.loadU8(b, bo + 2 * bs)
  const r3 = i16x16.loadU8(a, ao + 3 * as) - i16x16.loadU8(b, bo + 3 * bs)
  const r4 = i16x16.loadU8(a, ao + 4 * as) - i16x16.loadU8(b, bo + 4 * bs)
  const r5 = i16x16.loadU8(a, ao + 5 * as) - i16x16.loadU8(b, bo + 5 * bs)
  const r6 = i16x16.loadU8(a, ao + 6 * as) - i16x16.loadU8(b, bo + 6 * bs)
  const r7 = i16x16.loadU8(a, ao + 7 * as) - i16x16.loadU8(b, bo + 7 * bs)
  const t = sa8dMax2(r0, r1, r2, r3, r4, r5, r6, r7).addPairs()
  return ((t.low().sum() + 1) >> 1) + ((t.high().sum() + 1) >> 1)
}

export const T16_LO = [0, 8, 1, 9, 2, 10, 3, 11]
export const T16_HI = [4, 12, 5, 13, 6, 14, 7, 15]
export const T32_LO = [0, 1, 8, 9, 2, 3, 10, 11]
export const T32_HI = [4, 5, 12, 13, 6, 7, 14, 15]
export const T64_LO = [0, 1, 2, 3, 8, 9, 10, 11]
export const T64_HI = [4, 5, 6, 7, 12, 13, 14, 15]
const W16_LO = [0, 16, 1, 17, 2, 18, 3, 19, 8, 24, 9, 25, 10, 26, 11, 27]
const W16_HI = [4, 20, 5, 21, 6, 22, 7, 23, 12, 28, 13, 29, 14, 30, 15, 31]
const W32_LO = [0, 1, 16, 17, 2, 3, 18, 19, 8, 9, 24, 25, 10, 11, 26, 27]
const W32_HI = [4, 5, 20, 21, 6, 7, 22, 23, 12, 13, 28, 29, 14, 15, 30, 31]
const W64_LO = [0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27]
const W64_HI = [4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31]

// Rows are transformed, transposed and transformed again; the last butterfly folds into |a + b| + |a - b| =
// 2 * max(|a|, |b|), so a lane holds half of the |coefficients| of eight positions, at most 4 * 32 * 255 < 32768.
export function sa8dMax(r0: i16x8, r1: i16x8, r2: i16x8, r3: i16x8, r4: i16x8, r5: i16x8, r6: i16x8, r7: i16x8): u16x8 {
  const a0 = r0 + r1
  const a1 = r0 - r1
  const a2 = r2 + r3
  const a3 = r2 - r3
  const a4 = r4 + r5
  const a5 = r4 - r5
  const a6 = r6 + r7
  const a7 = r6 - r7
  const b0 = a0 + a2
  const b1 = a1 + a3
  const b2 = a0 - a2
  const b3 = a1 - a3
  const b4 = a4 + a6
  const b5 = a5 + a7
  const b6 = a4 - a6
  const b7 = a5 - a7
  const c0 = b0 + b4
  const c1 = b1 + b5
  const c2 = b2 + b6
  const c3 = b3 + b7
  const c4 = b0 - b4
  const c5 = b1 - b5
  const c6 = b2 - b6
  const c7 = b3 - b7
  const t0 = i16x8.shuffle(c0, c1, T16_LO)
  const t1 = i16x8.shuffle(c0, c1, T16_HI)
  const t2 = i16x8.shuffle(c2, c3, T16_LO)
  const t3 = i16x8.shuffle(c2, c3, T16_HI)
  const t4 = i16x8.shuffle(c4, c5, T16_LO)
  const t5 = i16x8.shuffle(c4, c5, T16_HI)
  const t6 = i16x8.shuffle(c6, c7, T16_LO)
  const t7 = i16x8.shuffle(c6, c7, T16_HI)
  const u0 = i16x8.shuffle(t0, t2, T32_LO)
  const u1 = i16x8.shuffle(t0, t2, T32_HI)
  const u2 = i16x8.shuffle(t1, t3, T32_LO)
  const u3 = i16x8.shuffle(t1, t3, T32_HI)
  const u4 = i16x8.shuffle(t4, t6, T32_LO)
  const u5 = i16x8.shuffle(t4, t6, T32_HI)
  const u6 = i16x8.shuffle(t5, t7, T32_LO)
  const u7 = i16x8.shuffle(t5, t7, T32_HI)
  const v0 = i16x8.shuffle(u0, u4, T64_LO)
  const v1 = i16x8.shuffle(u0, u4, T64_HI)
  const v2 = i16x8.shuffle(u1, u5, T64_LO)
  const v3 = i16x8.shuffle(u1, u5, T64_HI)
  const v4 = i16x8.shuffle(u2, u6, T64_LO)
  const v5 = i16x8.shuffle(u2, u6, T64_HI)
  const v6 = i16x8.shuffle(u3, u7, T64_LO)
  const v7 = i16x8.shuffle(u3, u7, T64_HI)
  const e0 = v0 + v1
  const e1 = v0 - v1
  const e2 = v2 + v3
  const e3 = v2 - v3
  const e4 = v4 + v5
  const e5 = v4 - v5
  const e6 = v6 + v7
  const e7 = v6 - v7
  const f0 = e0 + e2
  const f1 = e1 + e3
  const f2 = e0 - e2
  const f3 = e1 - e3
  const f4 = e4 + e6
  const f5 = e5 + e7
  const f6 = e4 - e6
  const f7 = e5 - e7
  return u16x8.bitcast(f0.abs().max(f4.abs()) + f1.abs().max(f5.abs()) + f2.abs().max(f6.abs()) + f3.abs().max(f7.abs()))
}

export function sa8dMax2(r0: i16x16, r1: i16x16, r2: i16x16, r3: i16x16, r4: i16x16, r5: i16x16, r6: i16x16, r7: i16x16): u16x16 {
  const a0 = r0 + r1
  const a1 = r0 - r1
  const a2 = r2 + r3
  const a3 = r2 - r3
  const a4 = r4 + r5
  const a5 = r4 - r5
  const a6 = r6 + r7
  const a7 = r6 - r7
  const b0 = a0 + a2
  const b1 = a1 + a3
  const b2 = a0 - a2
  const b3 = a1 - a3
  const b4 = a4 + a6
  const b5 = a5 + a7
  const b6 = a4 - a6
  const b7 = a5 - a7
  const c0 = b0 + b4
  const c1 = b1 + b5
  const c2 = b2 + b6
  const c3 = b3 + b7
  const c4 = b0 - b4
  const c5 = b1 - b5
  const c6 = b2 - b6
  const c7 = b3 - b7
  const t0 = i16x16.shuffle(c0, c1, W16_LO)
  const t1 = i16x16.shuffle(c0, c1, W16_HI)
  const t2 = i16x16.shuffle(c2, c3, W16_LO)
  const t3 = i16x16.shuffle(c2, c3, W16_HI)
  const t4 = i16x16.shuffle(c4, c5, W16_LO)
  const t5 = i16x16.shuffle(c4, c5, W16_HI)
  const t6 = i16x16.shuffle(c6, c7, W16_LO)
  const t7 = i16x16.shuffle(c6, c7, W16_HI)
  const u0 = i16x16.shuffle(t0, t2, W32_LO)
  const u1 = i16x16.shuffle(t0, t2, W32_HI)
  const u2 = i16x16.shuffle(t1, t3, W32_LO)
  const u3 = i16x16.shuffle(t1, t3, W32_HI)
  const u4 = i16x16.shuffle(t4, t6, W32_LO)
  const u5 = i16x16.shuffle(t4, t6, W32_HI)
  const u6 = i16x16.shuffle(t5, t7, W32_LO)
  const u7 = i16x16.shuffle(t5, t7, W32_HI)
  const v0 = i16x16.shuffle(u0, u4, W64_LO)
  const v1 = i16x16.shuffle(u0, u4, W64_HI)
  const v2 = i16x16.shuffle(u1, u5, W64_LO)
  const v3 = i16x16.shuffle(u1, u5, W64_HI)
  const v4 = i16x16.shuffle(u2, u6, W64_LO)
  const v5 = i16x16.shuffle(u2, u6, W64_HI)
  const v6 = i16x16.shuffle(u3, u7, W64_LO)
  const v7 = i16x16.shuffle(u3, u7, W64_HI)
  const e0 = v0 + v1
  const e1 = v0 - v1
  const e2 = v2 + v3
  const e3 = v2 - v3
  const e4 = v4 + v5
  const e5 = v4 - v5
  const e6 = v6 + v7
  const e7 = v6 - v7
  const f0 = e0 + e2
  const f1 = e1 + e3
  const f2 = e0 - e2
  const f3 = e1 - e3
  const f4 = e4 + e6
  const f5 = e5 + e7
  const f6 = e4 - e6
  const f7 = e5 - e7
  return u16x16.bitcast(f0.abs().max(f4.abs()) + f1.abs().max(f5.abs()) + f2.abs().max(f6.abs()) + f3.abs().max(f7.abs()))
}

function rows2(p: Uint8Array, o: int, s: int): u8x32 {
  return u8x32.concat(u8x16.load(p, o), u8x16.load(p, o + s))
}

export function sad16(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int): int {
  let acc = u32x8.splat(0)
  for (let y = 0; y < 16; y += 2) acc += u8x32.sadParts(rows2(a, ao + y * as, as), rows2(b, bo + y * bs, bs))
  return acc.sum()
}

export function sadAvg16(a: Uint8Array, ao: int, as: int, p: Uint8Array, po: int, q: Uint8Array, qo: int, ps: int): int {
  let acc = u32x8.splat(0)
  for (let y = 0; y < 16; y += 2) {
    acc += u8x32.sadParts(rows2(a, ao + y * as, as), rows2(p, po + y * ps, ps).avg(rows2(q, qo + y * ps, ps)))
  }
  return acc.sum()
}

function abs(v: int): int {
  return v < 0 ? -v : v
}

function hadamardRef(inout t: int[], o: int, s: int, n: int) {
  for (let len = 1; len < n; len = len * 2) {
    for (let i = 0; i < n; i += 2 * len) {
      for (let j = i; j < i + len; j++) {
        const x = t[o + j * s]!
        const y = t[o + (j + len) * s]!
        t[o + j * s] = x + y
        t[o + (j + len) * s] = x - y
      }
    }
  }
}

function hadamardSumRef(a: Uint8Array, ao: int, as: int, b: Uint8Array, bo: int, bs: int, n: int): int {
  let t: int[] = []
  for (let y = 0; y < n; y++) {
    for (let x = 0; x < n; x++) t.push(int(a[ao + y * as + x]!) - int(b[bo + y * bs + x]!))
  }
  for (let y = 0; y < n; y++) hadamardRef(&t, y * n, 1, n)
  for (let x = 0; x < n; x++) hadamardRef(&t, x, n, n)
  let s = 0
  for (const v of t) s += abs(v)
  return s
}

function noise(seed: int, n: int): Uint8Array {
  let out: u8[] = []
  let x = seed
  for (let i = 0; i < n; i++) {
    x = (x * 1103515245 + 12345) & 0x7fffffff
    const v = (x >> 9) & 255
    const w = i % 7 === 0 ? 255 - v : v
    out.push(u8(w))
  }
  return new Uint8Array(out)
}

test("satd of identical blocks is zero and of a DC offset is 16*offset/2", () => {
  let a: u8[] = []
  let b: u8[] = []
  for (let i = 0; i < 16; i++) {
    a.push(u8(100))
    b.push(u8(90))
  }
  const ta = new Uint8Array(a)
  const tb = new Uint8Array(b)
  expect(satd4(ta, 0, 4, ta, 0, 4)).toBe(0)
  expect(satd4(ta, 0, 4, tb, 0, 4)).toBe(80)
  expect(sad16(ta, 0, 0, tb, 0, 0)).toBe(2560)
})

test("vector satd, sa8d and sad agree with the scalar Hadamard definitions on random blocks", () => {
  let bad = 0
  for (let seed = 1; seed <= 60; seed++) {
    const a = noise(seed, 40 * 40)
    const b = seed % 3 === 0 ? noise(seed * 31 + 7, 40 * 40) : noise(seed + 1000, 40 * 40)
    const ao = seed % 5
    const bo = 41 + seed % 3
    let ref4 = 0
    for (let by = 0; by < 16; by += 4) {
      for (let bx = 0; bx < 16; bx += 4) {
        const r = hadamardSumRef(a, ao + by * 40 + bx, 40, b, bo + by * 40 + bx, 40, 4) >> 1
        if (satd4(a, ao + by * 40 + bx, 40, b, bo + by * 40 + bx, 40) !== r) bad++
        ref4 += r
      }
    }
    if (satd16(a, ao, 40, b, bo, 40) !== ref4) bad++
    const avg = new Uint8Array(16 * 16)
    for (let y = 0; y < 16; y++) {
      for (let x = 0; x < 16; x++) avg[y * 16 + x] = (int(a[bo + y * 40 + x + 1]!) + int(b[bo + y * 40 + x]!) + 1) >> 1
    }
    if (satdAvg16(b, ao, 40, a, bo + 1, b, bo, 40) !== satd16(b, ao, 40, avg, 0, 16)) bad++
    if (sadAvg16(b, ao, 40, a, bo + 1, b, bo, 40) !== sad16(b, ao, 40, avg, 0, 16)) bad++
    let ref8 = 0
    for (let by = 0; by < 8; by += 4) {
      for (let bx = 0; bx < 8; bx += 4) ref8 += hadamardSumRef(a, ao + by * 40 + bx, 40, b, bo + by * 40 + bx, 40, 4) >> 1
    }
    if (satd8(a, ao, 40, b, bo, 40) !== ref8) bad++
    const left = (hadamardSumRef(a, ao, 40, b, bo, 40, 8) + 2) >> 2
    if (sa8d(a, ao, 40, b, bo, 40) !== left) bad++
    if (sa8dPair(a, ao, 40, b, bo, 40) !== left + ((hadamardSumRef(a, ao + 8, 40, b, bo + 8, 40, 8) + 2) >> 2)) bad++
    let s16 = 0
    for (let y = 0; y < 16; y++) {
      for (let x = 0; x < 16; x++) s16 += abs(int(a[ao + y * 40 + x]!) - int(b[bo + y * 40 + x]!))
    }
    if (sad16(a, ao, 40, b, bo, 40) !== s16) bad++
  }
  expect(bad).toBe(0)
})

test("satd16 and sa8dPair equal the per-block sums on extreme 0/255 patterns", () => {
  let bad = 0
  for (let pat = 0; pat < 64; pat++) {
    const a = new Uint8Array(16 * 16)
    const b = new Uint8Array(16 * 16)
    for (let y = 0; y < 16; y++) {
      for (let x = 0; x < 16; x++) {
        const hx = ((x >> (pat & 1)) ^ (x >> ((pat >> 1) & 1))) & 1
        const hy = ((y >> ((pat >> 2) & 1)) ^ (y >> ((pat >> 3) & 1))) & 1
        const on = (pat & 16) !== 0 ? (hx ^ hy) === 0 : (pat & 32) !== 0 ? hy === 0 || hx === 0 : hx === 0
        a[y * 16 + x] = on ? 255 : 0
        b[y * 16 + x] = on ? 0 : 255
      }
    }
    let ref = 0
    for (let by = 0; by < 16; by += 4) {
      for (let bx = 0; bx < 16; bx += 4) ref += satd4(a, by * 16 + bx, 16, b, by * 16 + bx, 16)
    }
    if (satd16(a, 0, 16, b, 0, 16) !== ref) bad++
    for (let by = 0; by < 16; by += 8) {
      const o = by * 16
      if (sa8dPair(a, o, 16, b, o, 16) !== sa8d(a, o, 16, b, o, 16) + sa8d(a, o + 8, 16, b, o + 8, 16)) bad++
    }
  }
  expect(bad).toBe(0)
})