From bdf6f3d5f18289400d2cdcca4df40027d36a48b9 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Tue, 25 Mar 2025 06:18:00 +0200 Subject: [PATCH] Add `no_std` compatible `ceil` method (#18498) # Objective [`f32::ceil`](https://doc.rust-lang.org/std/primitive.f32.html#method.ceil) is not available in `core`. We have `floor` in `bevy_math::ops`, but no equivalent for `floor`. ## Solution Add `ops::ceil` for `no_std` compatibility. --- crates/bevy_math/clippy.toml | 1 + crates/bevy_math/src/ops.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/bevy_math/clippy.toml b/crates/bevy_math/clippy.toml index 0fb122e4dc..c1f67e044d 100644 --- a/crates/bevy_math/clippy.toml +++ b/crates/bevy_math/clippy.toml @@ -34,5 +34,6 @@ disallowed-methods = [ { path = "f32::copysign", reason = "use ops::copysign instead for no_std compatibility" }, { path = "f32::round", reason = "use ops::round instead for no_std compatibility" }, { path = "f32::floor", reason = "use ops::floor instead for no_std compatibility" }, + { path = "f32::ceil", reason = "use ops::ceil instead for no_std compatibility" }, { path = "f32::fract", reason = "use ops::fract instead for no_std compatibility" }, ] diff --git a/crates/bevy_math/src/ops.rs b/crates/bevy_math/src/ops.rs index e9d27ac54a..6d39bbbfd4 100644 --- a/crates/bevy_math/src/ops.rs +++ b/crates/bevy_math/src/ops.rs @@ -510,6 +510,14 @@ mod libm_ops_for_no_std { libm::floorf(x) } + /// Returns the smallest integer greater than or equal to `x`. + /// + /// Precision is specified when the `libm` feature is enabled. + #[inline(always)] + pub fn ceil(x: f32) -> f32 { + libm::ceilf(x) + } + /// Returns the fractional part of `x`. /// /// This function always returns the precise result. @@ -581,6 +589,14 @@ mod std_ops_for_no_std { f32::floor(x) } + /// Returns the smallest integer greater than or equal to `x`. + /// + /// This function always returns the precise result. + #[inline(always)] + pub fn ceil(x: f32) -> f32 { + f32::ceil(x) + } + /// Returns the fractional part of `x`. /// /// This function always returns the precise result.