Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libm and math stuff #19

Open
mrhota opened this issue Feb 18, 2017 · 3 comments
Open

libm and math stuff #19

mrhota opened this issue Feb 18, 2017 · 3 comments

Comments

@mrhota
Copy link
Collaborator

mrhota commented Feb 18, 2017

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
Copy link
Owner

anp commented Feb 18, 2017

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
Copy link
Collaborator Author

mrhota commented Feb 19, 2017

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
Copy link
Owner

anp commented Feb 19, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants