Rust and SPIR-V float rounding semantics are different
まだ誰も着手していません。
- 主要言語
- Rust
- スター
- 3.4k
- フォーク
- 126
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
crates/rustc_codegen_spirv/src/builder/builder_methods.rs の、参照されている OpConvertFToS の生成箇所付近から始め、issue にリンクされている Rust リファレンスと SPIR-V 仕様を比較します。浮動小数点数から整数への変換について、意図されたデフォルトの動作またはユーザーが制御できる動作を特定し、提示された例に対して検証します。選択したセマンティクスについて明示的に合意され、CPU/GPU の動作が一致するか、意図的に設定可能になっていれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- rust
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 25/100