Some checks failed
standards workspaces build + test / jouleclaw-rs (pull_request) Successful in 2m13s
standards workspaces build + test / aac-rs (pull_request) Successful in 1m58s
standards workspaces build + test / joule-code-rs (pull_request) Successful in 50s
standards workspaces build + test / joulecontract-rs (pull_request) Successful in 34s
standards workspaces build + test / map-rs (pull_request) Successful in 31s
standards workspaces build + test / mesh-rs (pull_request) Successful in 31s
no-vanity-metrics / Scan committed Markdown for vanity metrics (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no comparison (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no company or product (pull_request) Has been cancelled
standards workspaces build + test / arl-rs (pull_request) Has been cancelled
standards workspaces build + test / eoc-rs (pull_request) Has been cancelled
standards workspaces build + test / jcp-rs (pull_request) Has been cancelled
standards workspaces build + test / joule-ui-rs (pull_request) Has been cancelled
standards workspaces build + test / jouledesk-rs (pull_request) Has been cancelled
standards workspaces build + test / joulehook-rs (pull_request) Has been cancelled
standards workspaces build + test / proof-rs (pull_request) Has been cancelled
standards workspaces build + test / sandbox-rs (pull_request) Has been cancelled
standards workspaces build + test / sense-rs (pull_request) Has been cancelled
standards workspaces build + test / smart-byte-rs (pull_request) Has been cancelled
standards workspaces build + test / macOS energy backends (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · macos (pull_request) Has been cancelled
WAI conformance / quantum core and corpus under wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
WAI conformance / v2 vocabulary · manifests validate as stated (pull_request) Has been cancelled
WAI conformance / independent verifiers · claims, JWP, key discovery and integer payloads (Python) (pull_request) Has been cancelled
WAI conformance / delivery telemetry · CMCD and CMSD against an independent library (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · macos (pull_request) Has been cancelled
WAI conformance / C2PA manifest emission · signed, read back, validated (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux-arm64 (pull_request) Has been cancelled
WAI energy receipts / energy receipts · macos (pull_request) Has been cancelled
WAI section references / cited sections exist (pull_request) Has been cancelled
wai-vector vector graphics / linux-aarch64 (pull_request) Has been cancelled
wai-vector vector graphics / linux-x86_64 (pull_request) Has been cancelled
wai-vector vector graphics / macos-arm64 (pull_request) Has been cancelled
wai-vector vector graphics / wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
wai-vector vector graphics / wasm32 under Node (pull_request) Has been cancelled
wai-vector vector graphics / fuzz, peak allocation, comparison count (pull_request) Has been cancelled
workflow hygiene / every workflow supersedes its own queue (pull_request) Has been cancelled
standards workspaces build + test / openpay (pull_request) Has been cancelled
The reference crate of wai.graphics.vector (no_std + alloc, no dependencies; SHA-256 is wai-ops's, by #[path]): - WOS1 parsed with §2.1's load checks in order, and written; - WVG1 split (steps 1-3, nothing allocated before the exact length), the record pass (step 4), the cross-record checks (step 5(a)-(h)), the sets by digest with at most length + 1 bytes read (step 6), the glyphs (7), the interval check (8) and the per-frame cost (9); - the clock with the frame range by division, track evaluation, and placement; - the JSON authoring form, one member per field, packing back to the same bytes; - the CLI: check, cost, pack, unpack, vectors gen and verify; - a wasm32 module and verify.mjs for Node. The corpus wai/vector-conformance/ (arith, canonical, refusals, geometry, cost) with its README's provenance line and a sha256.json sidecar naming the div255 table; wai-vector.yml with native, wasip2, node and fuzz jobs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
79 lines
2.8 KiB
Rust
79 lines
2.8 KiB
Rust
//! §3.0, §3.9, §2.9: `div255`, `R_rat`, `R_half_up` and the straight-alpha channel,
|
|
//! against their definitions as exact rationals.
|
|
|
|
use wai_vector::arith::*;
|
|
|
|
/// `⌊n/d⌋` of the exact rational, in `i128`.
|
|
fn floor_q(n: i128, d: i128) -> i128 {
|
|
n.div_euclid(d)
|
|
}
|
|
|
|
#[test]
|
|
fn div255_over_every_input() {
|
|
for x in 0..=65_025u32 {
|
|
let want = floor_q(2 * i128::from(x) + 255, 510);
|
|
assert_eq!(i128::from(div255(x)), want, "div255({x}) is the half-up rounding of x/255");
|
|
// The rq form: pre-bias 128, multiplier 257, shift 16, floor.
|
|
assert_eq!(div255(x), ((x + 128) * 257) >> 16, "rq form at {x}");
|
|
}
|
|
assert_eq!(div255(65_025), 255);
|
|
for c in 0..=255u32 {
|
|
assert_eq!(div255(255 * c), c, "div255(255c) = c");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn r_rat_is_half_up_of_the_exact_rational() {
|
|
for d in 1i64..=300 {
|
|
for n in -3_000i64..=3_000 {
|
|
let want = floor_q(2 * i128::from(n) + i128::from(d), 2 * i128::from(d));
|
|
assert_eq!(i128::from(r_rat(n, d)), want, "R_rat({n}, {d})");
|
|
}
|
|
}
|
|
let big = (1i64 << 58) - 1;
|
|
for d in [1i64, 2, 3, 1 << 20, (1 << 28) - 1, 1 << 28] {
|
|
for n in [big, -big, big - 7, -big + 7, 0, d / 2, -(d / 2), (d + 1) / 2, -(d + 1) / 2] {
|
|
let want = floor_q(2 * i128::from(n) + i128::from(d), 2 * i128::from(d));
|
|
assert_eq!(i128::from(r_rat(n, d)), want, "R_rat({n}, {d})");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn r_half_up_is_the_rounded_shift() {
|
|
for s in 0u32..=20 {
|
|
for p in (-(1i64 << 21)..=(1 << 21)).step_by(13) {
|
|
let want = if s == 0 { i128::from(p) } else { floor_q(i128::from(p) + (1i128 << (s - 1)), 1i128 << s) };
|
|
assert_eq!(i128::from(r_half_up(p, s)), want, "R_half_up({p}, {s})");
|
|
}
|
|
}
|
|
for p in [-(1i64 << 47), (1i64 << 47) - 1, -32_768, 32_768, -1, 1] {
|
|
assert_eq!(i128::from(r_half_up(p, 16)), floor_q(i128::from(p) + (1 << 15), 1 << 16));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn straight_alpha_is_r_rat_over_every_c_le_a() {
|
|
for a in 1..=255u8 {
|
|
for c in 0..=a {
|
|
let v = unpremul(c, a);
|
|
assert_eq!(i64::from(v), r_rat(255 * i64::from(c), i64::from(a)), "({c}, {a})");
|
|
assert_eq!(u32::from(v), (255 * u32::from(c) + u32::from(a) / 2) / u32::from(a));
|
|
}
|
|
}
|
|
assert_eq!(unpremul(0, 0), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn quantisers_are_even_and_bounded() {
|
|
for v in -(1i32 << 19)..=(1 << 19) {
|
|
let (nz, eo) = (alpha_nonzero(v), alpha_evenodd(v));
|
|
assert_eq!(nz, alpha_nonzero(-v));
|
|
assert_eq!(eo, alpha_evenodd(-v));
|
|
}
|
|
assert_eq!(alpha_nonzero(1 << 17), 255);
|
|
assert_eq!(alpha_nonzero(1 << 16), 128);
|
|
assert_eq!(alpha_evenodd(1 << 17), 255);
|
|
assert_eq!(alpha_evenodd(1 << 18), 0);
|
|
assert_eq!(alpha_nonzero(i32::MIN), 255);
|
|
}
|