Using sqrt(), cos(), sin(), atan(), acos()... in no_std


#1

Hi!
I’m trying to port one of my crates with no dependencies to work on no-std targets. I’ve managed to port everything, and I even found out that the powi() methods in floats are unstable (why?).

But I cannot figure out how to use sqrt(), cos(), sin(), atan() and acos() methods in f32/f64. I see they use unsafe intrinsics, but I don’t really understand why. I’m guessing they require allocation. Can they be replaced by other implementations?

Thanks in advance!


#2

They probably don’t require allocation, but they require linking to a C math library (e.g., libm) because

  • hardware does not usually provide instructions that compute these transcendental functions (at least not with decent accuracy)
  • software implementations (that are fast & decently accurate) are rather tricky, so there’s no implementation in libcore

The intrinsics are just used instead of direct calls to the C functions to help the optimizer a bit (e.g., libm functions set errno which Rust does not care for).