How to access float methods like `ceil()` in a `no-std` crate?

I'm pretty new to Rust but AFAICT the basic issue I'm having is that core::primitive::[f32|f64] doesn't expose the methods defined in std::primitive... and I have two main questions:

  1. Is there a workaround? It would be nice to avoid having to define my own basic arithmetic utilities.
  2. Why? core::primitive::u32, for example, does include similar utilities, even experimental ones like div_floor (which I also use).

I'm working on a bare-metal library with the following cargo config:

rustflags = [
  "-C", "link-arg=-Tlink.x",
  "-C", "target-feature=+neon,+strict-align",
  "-C", "target-cpu=cortex-a53",

target = "aarch64-unknown-none"

build-std = ["core", "compiler_builtins", "alloc"]
build-std-features = ["compiler-builtins-mem"]

and the following crate features:


And if it helps, the error I'm getting:

error[E0599]: no method named `ceil` found for type `f32` in the current scope
   --> libboard_zynq_us/src/ddr/
445 |         (time_ps as f32 / self.t_ckavg_min_ps as f32 - 0.01).ceil() as u32
    |                                                              ^^^^ method not found in `f32`

The libm crate has a lot of replacements.

1 Like

These methods were removed back in 2015, to avoid core calling into C's libm:


This isn't what you're asking, but you can do ceiling division with integers. If everything is positive it's just (x + y - 1) / y. I don't know how integer division performs on your hardware but the code in libm looks like it could pretty inefficient (since it has to be written portably). For floating point there are techniques for converting to integers that typically require adding and subtracting a special constant (2^52 for nonnegative f64 values) though how to get the ceiling is contingent on the rounding mode.

1 Like

Yeah, it occurred to me that for this specific calculation I could do it with integers (albeit in a way that doesn't look much like the spec I'm taking the formula from), but I figured I'd ask the question anyway since I have a hard FPU and I'm going to want to use it eventually. Thanks!