Skip to content

Use sqrt intrinsic for floating point sqrt #5686

Closed

Description

@brson

sqrt is still using libc

Activity

  1. huonw commented on Apr 3, 2013

    @huonw
    Contributor

    I'm running tests on a patch that uses intrinsics for sqrt, sin, cos, exp, etc.

  2. thestinger commented on Apr 3, 2013

    @thestinger
    Contributor

    @dbaupp: Nice! It would be awesome to have all of the useful LLVM intrinsics for integers/floats exposed.

  3. huonw commented on Apr 3, 2013

    @huonw
    Contributor

    Yay! 5x faster:

    use core::float::sqrt;
    
    fn main() {
        let mut sum: float = 0.0;
        for core::uint::range(0, 100000000) |i| {
            sum += sqrt(i as float);
        }
        io::println(fmt!("%f", sum));
    }
    # using cmath
    $ rustc --opt-level=3 intrinsics-bench.rs  && time ./intrinsics-bench 
    666666661666.567017
    
    real    0m2.729s
    user    0m2.712s
    sys     0m0.008s
    
    # using intrinsics
    $ x86_64-unknown-linux-gnu/stage2/bin/rustc --opt-level=3 intrinsics-bench.rs  && time ./intrinsics-bench 
    666666661666.567017
    
    real    0m0.513s
    user    0m0.500s
    sys     0m0.008s
    
  4. brson commented on Apr 3, 2013

    @brson
    ContributorAuthor

    @dbaupp mentioned on reddit that using some of these clobbers the stack canary. I hadn't considered that before and we need to think about how these are implemented. How many of the intrinsics that we are already using are just trotting all over the stack red zone? Perhaps LLVM's intrinsics don't emit the proper stack growth prologues.

  5. huonw commented on Apr 3, 2013

    @huonw
    Contributor

    (The very brief reddit thread, for future reference.)

    Do I continue working on this? (i.e. open a pull request, since I've got the code basically organised.)

    The (most) dangerous intrinsics seem to be sin, cos and exp, so I've left sin as libc and added sin_intr for the intrinsic etc.

  6. brson commented on Apr 3, 2013

    @brson
    ContributorAuthor

    @dbaupp I'd like to know what assembly they generate. Do you have a test case that demonstrates all of them?

  7. huonw commented on Apr 3, 2013

    @huonw
    Contributor
  8. pcwalton commented on Apr 20, 2013

    @pcwalton
    Contributor

    @huonw The necessary LLVM changes have been made.

    However, I didn't realize that intrinsics can clobber the stack. I guess we need to write wrappers with the fixedstacksegment attribute for the problematic intrinsics (sin, cos) as well! This can be done entirely in the Rust compiler now, no need to patch LLVM.

  9. huonw commented on Apr 20, 2013

    @huonw
    Contributor

    @pcwalton Does fixed_stack_segment work on extern/intrinsic functions? i.e.

    #[abi = "rust-intrinsic"]
    pub extern "rust-intrinsic" {
       #[fixed_stack_segment]
       pub fn sinf32(x: f32) -> f32;
    }
  10. pcwalton commented on Apr 20, 2013

    @pcwalton
    Contributor

    @huonw Extern functions automatically require fixed stack segments, if the #[fast_ffi] attribute is used. (Eventually this attribute will no longer be necessary.) However, I don't think that it will work for functions with the rust-intrinsic ABI, because I didn't realize that some intrinsic functions would need it.

  11. added a commit that references this issue on Apr 20, 2013
  12. huonw commented on Apr 21, 2013

    @huonw
    Contributor

    @brson #5975 fixed this.

  13. brson commented on Apr 21, 2013

    @brson
    ContributorAuthor

    Thanks @huonw!

  14. added 2 commits that reference this issue on Jun 5, 2020
  15. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyI-slow

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions