Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: HW Accelerated Square Root

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HW Accelerated Square Root

    Hi,

    I found i need to use square root in a program I am writing.
    As it would cause heavy usage and I understood that Math.sqrt is rather heavy duty I looked around for optimized, albeit less accurate, methods but found in the end that Math.sqrt is faster than everything.

    I then saw that it is delegated to StrictMath.sqrt with the following comment:

    // default impl. delegates to StrictMath
    // Note that hardware sqrt instructions
    // frequently can be directly used by JITs
    // and should be much faster than doing
    // Math.sqrt in software.

    Does this mean that on my machine it is done in hardware?
    Is there a way to know in code if this is so so that I can use the faster alternatives on other machines, where needed?

    Thank you


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: HW Accelerated Square Root

    Does this mean that on my machine it is done in hardware?
    Who knows? I would say that this comment in java.lang.StrictMath would be authoritative:
    To help ensure portability of Java programs, the definitions of some of the numeric functions in this package require that they produce the same results as certain published algorithms.
    I would understand that to mean that if your hardware sqrt result doesn't agree with "certain published algorithms", then it won't be used, but if there are partial results that are usable, then some hw delegation may still occur.

    If processing on hardware is important to you, don't use Java. If you want 'Write Once Run Anywhere', do use Java - but it means you have to let the JVM implementers make all your hw optimisation choices for you.

    found in the end that Math.sqrt is faster than everything
    They're probably quite good at what they do.

    Is there a way to know in code
    Unlikely from Java code, I would have thought. Some performance-critical software (jvisualvm is an example that springs to mind) performs metrics when they start up. Could you measure sqrt performance of your available options and make some decisions that way?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HW Accelerated Square Root

    Quote Originally Posted by Sean4u View Post
    They're probably quite good at what they do.
    Actually this whole concern started when i found several sites claiming Math.sqrt is quite slow.
    They offered some code that does good enough approximations and that's supposed to be faster.
    But when I tried it myself i found the Java version to be faster. So either it really does use HW acceleration or the site was old or just plain wrong. Who knows...

    Quote Originally Posted by Sean4u View Post
    Could you measure sqrt performance of your available options and make some decisions that way?
    I guess I could if this ever becomes a real issue.
    I was asking more out of curiosity than anything else.

    It doesn't seem like a real problem anyway since it really is very fast.

    Thanks

Similar Threads

  1. [SOLVED] Taking a square root in Java, answer appears as 0.0. Why?
    By r19ecua in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 30th, 2012, 11:47 AM
  2. Java Calculator Square Root Function
    By laser1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2011, 09:34 AM
  3. Square root not working
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 06:25 PM
  4. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  5. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM