Skip to content

Commit 861d264

Browse files
committed
Add no_std polyfill for round_ties_even for f32 and f64, not just f16
1 parent 273072d commit 861d264

File tree

4 files changed

+51
-26
lines changed

4 files changed

+51
-26
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,11 @@ jobs:
301301
302302
# check with no features
303303
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-types --no-default-features
304+
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p naga --no-default-features
304305
305306
# Check with all features except "std".
306307
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-types --no-default-features --features strict_asserts,fragile-send-sync-non-atomic-wasm,serde,counters
308+
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p naga --no-default-features --features dot-out,compact
307309
308310
# Building for native platforms with standard tests.
309311
- name: Check native

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ Bottom level categories:
4242

4343
### New Features
4444

45+
#### Naga
46+
47+
- Added `no_std` support with default features disabled. By @Bushrat011899 in [#7585](https://github.com/gfx-rs/wgpu/pull/7585).
48+
4549
#### General
4650

4751
- Add support for rendering to slices of 3D texture views and single layered 2D-Array texture views (this requires `VK_KHR_maintenance1` which should be widely available on newer drivers). By @teoxoy in [#7596](https://github.com/gfx-rs/wgpu/pull/7596)

naga/src/back/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ pub type NeedBakeExpressions = crate::FastHashSet<crate::Handle<crate::Expressio
4646
///
4747
/// [`Expression`]: crate::Expression
4848
/// [`Handle`]: crate::Handle
49+
#[cfg_attr(
50+
not(any(glsl_out, hlsl_out, msl_out, wgsl_out)),
51+
allow(
52+
dead_code,
53+
reason = "shared helpers can be dead if none of the enabled backends need it"
54+
)
55+
)]
4956
struct Baked(crate::Handle<crate::Expression>);
5057

5158
impl core::fmt::Display for Baked {

naga/src/proc/constant_evaluator.rs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,34 +1171,46 @@ impl<'a> ConstantEvaluator<'a> {
11711171
component_wise_float!(self, span, [arg], |e| { Ok([e.floor()]) })
11721172
}
11731173
crate::MathFunction::Round => {
1174-
component_wise_float(self, span, [arg], |e| match e {
1175-
Float::Abstract([e]) => Ok(Float::Abstract([e.round_ties_even()])),
1176-
Float::F32([e]) => Ok(Float::F32([e.round_ties_even()])),
1177-
Float::F16([e]) => {
1178-
// TODO: `round_ties_even` is not available on `half::f16` yet.
1179-
//
1180-
// This polyfill is shamelessly [~~stolen from~~ inspired by `ndarray-image`][polyfill source],
1181-
// which has licensing compatible with ours. See also
1182-
// <https://github.com/rust-lang/rust/issues/96710>.
1183-
//
1184-
// [polyfill source]: https://github.com/imeka/ndarray-ndimage/blob/8b14b4d6ecfbc96a8a052f802e342a7049c68d8f/src/lib.rs#L98
1185-
fn round_ties_even(x: f64) -> f64 {
1186-
let i = x as i64;
1187-
let f = (x - i as f64).abs();
1188-
if f == 0.5 {
1189-
if i & 1 == 1 {
1190-
// -1.5, 1.5, 3.5, ...
1191-
(x.abs() + 0.5).copysign(x)
1192-
} else {
1193-
(x.abs() - 0.5).copysign(x)
1194-
}
1195-
} else {
1196-
x.round()
1197-
}
1198-
}
1174+
/// Rounds to the nearest integer, with ties biasing towards an even result.
1175+
///
1176+
/// # TODO
1177+
///
1178+
/// - `round_ties_even` is not available on `half::f16` yet.
1179+
/// - It is also not available on `no_std`
1180+
///
1181+
/// This polyfill is shamelessly [~~stolen from~~ inspired by `ndarray-image`][polyfill source],
1182+
/// which has licensing compatible with ours. See also
1183+
/// <https://github.com/rust-lang/rust/issues/96710>.
1184+
///
1185+
/// [polyfill source]: https://github.com/imeka/ndarray-ndimage/blob/8b14b4d6ecfbc96a8a052f802e342a7049c68d8f/src/lib.rs#L98
1186+
fn round_ties_even<T: num_traits::float::FloatCore>(x: T) -> T {
1187+
let half = (T::one() + T::one()).recip();
1188+
1189+
if x.fract().abs() != half {
1190+
x.round()
1191+
} else {
1192+
let i = x.abs().trunc();
11991193

1200-
Ok(Float::F16([(f16::from_f64(round_ties_even(f64::from(e))))]))
1194+
let value = if (i * half).fract() == half {
1195+
// -1.5, 1.5, 3.5, ...
1196+
x.abs() + half
1197+
} else {
1198+
// -0.5, 0.5, 2.5, ...
1199+
x.abs() - half
1200+
};
1201+
1202+
if x.signum() != value.signum() {
1203+
-value
1204+
} else {
1205+
value
1206+
}
12011207
}
1208+
}
1209+
1210+
component_wise_float(self, span, [arg], |e| match e {
1211+
Float::Abstract([e]) => Ok(Float::Abstract([round_ties_even(e)])),
1212+
Float::F32([e]) => Ok(Float::F32([round_ties_even(e)])),
1213+
Float::F16([e]) => Ok(Float::F16([round_ties_even(e)])),
12021214
})
12031215
}
12041216
crate::MathFunction::Fract => {

0 commit comments

Comments
 (0)