Skip to content

Consider removing fast inverse square root #5

@solonovamax

Description

@solonovamax

Currently, Quake 3's Fast inverse square root algorithm is used in AlgebraFunctions.invSqrt().
However, just using 1.0 / Math.sqrt(a) should (I think) actually be faster than the quake 3 implementation.

See: https://www.linkedin.com/pulse/fast-inverse-square-root-still-armin-kassemi-langroodi/

Doing an extremely crude test with openjdk 21 on my laptop, the following method:

public static double invSqrt(double a) {
    return 1.0 / Math.sqrt(a);
}

emits the following instructions:

  0x00007f725c5e7c7c:   vsqrtsd %xmm0,%xmm0,%xmm0
  0x00007f725c5e7c80:   vmovsd -0x68(%rip),%xmm1        # 0x00007f725c5e7c20
                                                            ;   {section_word}
  0x00007f725c5e7c88:   vdivsd %xmm0,%xmm1,%xmm1

and it's using sqrtsd followed by a divsd (double equivalents of sqrtss and divss), so based on that post I linked it should be faster, I think?
either way, it's worth doing a quick benchmark to verify whether the fast inverse square root is actually faster than the version with Math.sqrt, and if it's not remove it.

See also: https://groovy.apache.org/blog/quake3-inverse-square-root#_results

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions