3

I looked into the source of Rust compiler, looking for the sin() and cos() functions of the f64 type and I cannot find it. It looks like there is just a reference to an intrinsic function, which does not have any body. So, is sin() implemented as a CPU instruction? Please guide me here.

ArekBulski
  • 4,520
  • 4
  • 39
  • 61
  • It's system dependent. You should read [here](https://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions?rq=1). – ImajinDevon Jul 07 '22 at 20:19
  • 2
    I think that's the part that passes it through to LLVM as an intrinsic: https://github.com/rust-lang/rust/blob/3e51277fe638dc0c8ceb6d1d3acc5aa247277c29/compiler/rustc_codegen_llvm/src/intrinsic.rs#L36 Just a wild guess, though, I'm not an expert in how the Rust compiler works. – Finomnis Jul 07 '22 at 20:35

1 Answers1

4

f32::sin compiles to the llvm.sin.f32 intrinsic, which compiles to a call to the sinf function in the C standard library.

cos and tan, and their f64 equivalents, likely do something similar.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85