Rust and SPIR-V float rounding semantics are different
Nessuno ha ancora preso questa issue.
- Lingua principale
- Rust
- Stelle
- 3.4k
- Fork
- 126
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
There is a footgun when trying to share code between CPU and GPU. Rust and SPIR-V have different semantics when rounding floats:
Rust:
- Rust’s built-in conversion using
astruncates the fractional part, effectively rounding toward zero. - For example, 1743028479.999... becomes 1743028479.
Rust Language Reference
SPIR-V:
- SPIR-V uses the
OpConvertFToSinstruction for converting floating-point values to integers. Here is where it happens in rust-gpu. - It rounds using round-to-nearest-even, which can round 1743028479.999... up to 1743028480.
SPIR-V Specification
fn main() {
let x = 1743028480i32;
let y = (x as f64) * (1.0 / (i32::MAX as f64));
// Without explicit rounding, differs:
let without = (y * (i32::MAX as f64)) as i32;
// Using `trunc()` forces Rust’s truncation:
let with = (y * (i32::MAX as f64)).trunc() as i32;
println!("x = {}. Without rounding = {}. With trunc() = {}.", x, without, with);
}
I'm not sure which should be the default. If it is Rust's we'll want to polyfill on the spir-v side. Might be good to have this user controlled in any case.
Originally posted by @LegNeato in https://github.com/Rust-GPU/rust-gpu/discussions/228#discussioncomment-12784276
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia da crates/rustc_codegen_spirv/src/builder/builder_methods.rs intorno alla generazione referenziata di OpConvertFToS, quindi confronta il riferimento Rust e la specifica SPIR-V collegati nell’issue. Determina il comportamento predefinito previsto o quello controllabile dall’utente per la conversione da virgola mobile a intero e convalidalo rispetto all’esempio fornito. Il lavoro è concluso quando la semantica scelta è concordata esplicitamente e il comportamento di CPU/GPU è allineato o reso deliberatamente configurabile.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rust
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100