anp / rusl

An experimental (read: DONT USE) musl libc implementation in Rust.
Other
292 stars 12 forks source link

libm and math stuff #19

Open mrhota opened 7 years ago

mrhota commented 7 years ago

musl provides many math functions in assembly. I thought it'd be nice to leave those as-is and provide a step in the build to assemble all the .s files and produce a libm-intermediate.a that we can link to librusl.

One reason we might want to copy over the assembly is because of optimizations like the short jmps in musl/src/math/x86_64/floorl.s. I thought it would be hard to bring optimizations like that over to #[naked] rust functions with asm! blocks.

Thoughts?

anp commented 7 years ago

Cool! I've also thought about using an existing Rust libm for some functions:

https://github.com/nagisa/math.rs https://github.com/japaric/m

I think that using the existing assembly implementations definitely makes sense where they exist.

mrhota commented 7 years ago

oh wow, @japaric's on the ball. Well, since musl has a libm implementation and rusl is a port of musl, an extra dependency like m isn't strictly necessary.

After a bit of testing, it seems like we can do something like this in shell script:

for f in musl/src/math/<arch>/*.s; do
  as -o $(echo $f | sed 's/\.s/\.o/') $f
done
ar -crus libm-intermediate.a musl/src/math/<arch>/*.o

That'll give us a libm-intermediate.a which we can link to rusl like so:

cargo rustc --release -- -l m-intermediate -L musl/src/math/x86_64

And you can verify the expected asm is part of the resulting binary like objdump -d foo | less

anp commented 7 years ago

Can we do this in a buildscript? It'd be nice to have cargo build Just Work. The buildscript can print link args to stdout: http://doc.crates.io/build-script.html#outputs-of-the-build-script. We probaby also need to do platform detection of some kind -- while the port is only x86_64 right now it seems good to me to do platform dispatch where appropriate in case someone ever decides to port another target.

Seems like there's a crate for invoking nasm (https://github.com/medek/nasm-rs), but I'm not familiar enough with assemblers to know whether that can be a drop-in replacement for as.