Giter VIP home page Giter VIP logo

Comments (5)

nektro avatar nektro commented on May 30, 2024

do you have docs on what this function is?

from zig.

nektro avatar nektro commented on May 30, 2024

1 / @sqrt(x) should compile to what you seek

from zig.

expikr avatar expikr commented on May 30, 2024

does it always, for vectors as well?

Code

export fn rsqrt(num: f32) f32 {
    return 1/@sqrt(num);
}

Godbolt default

# Compilation provided by Compiler Explorer at https://godbolt.org/
.LCPI0_0:
        .long   0x3f800000
rsqrt:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 4
        vmovaps xmm1, xmm0
        vmovss  dword ptr [rbp - 4], xmm1
        vsqrtss xmm1, xmm0, xmm1
        vmovss  xmm0, dword ptr [rip + .LCPI0_0]
        vdivss  xmm0, xmm0, xmm1
        add     rsp, 4
        pop     rbp
        ret

Godbolt -O ReleaseFast

# Compilation provided by Compiler Explorer at https://godbolt.org/
.LCPI0_0:
        .long   0x3f800000
rsqrt:
        vsqrtss xmm0, xmm0, xmm0
        vmovss  xmm1, dword ptr [rip + .LCPI0_0]
        vdivss  xmm0, xmm1, xmm0
        ret

from zig.

nektro avatar nektro commented on May 30, 2024

at a glance it looks like it should be using (V)RSQRTPS instead

from zig.

castholm avatar castholm commented on May 30, 2024

Somewhat related: #767
At a quick glance there doesn't appear to be an LLVM intrinsic for rsqrt, but I don't think that's necessarily an argument against adding @rsqrt and I can such a builtin being useful in certain cases.

1 / @sqrt(x) should compile to what you seek

Unless @setFloatMode(.optimized) or equivalent compiler flags are in effect I would hope that 1 / @sqrt(x) is never compiled to reciprocal square root, for the same reasons it is undesired for a * b + c to silently compile to fused multiply-adds, divisions to reciprocals, etc. that change the semantics of the expression.

from zig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.