Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:12:28+00:00 2026-06-09T22:12:28+00:00

Hullo, I am not too much experienced in asm and I would like to

  • 0

Hullo, I am not too much experienced in asm and I would like
to rewrite carmack’s inverted square root c routine in assembly

    ;   float InvSqrt (float x){
    ;
    @173:
    push      ebp
    mov       ebp,esp
    add       esp,-8
    ;
    ;       float xhalf = 0.5f*x;
    ;
    fld       dword ptr [@174]
    fmul      dword ptr [ebp+8]
    fstp      dword ptr [ebp-4]
    ;
    ;       int i = *(int*)&x;
    ;
    mov       eax,dword ptr [ebp+8]
    mov       dword ptr [ebp-8],eax
    ;
    ;       i = 0x5f3759df - (i>>1);
    ;
    mov       edx,dword ptr [ebp-8]
    sar       edx,1
    mov       ecx,1597463007
    sub       ecx,edx
    mov       dword ptr [ebp-8],ecx
    ;
    ;       x = *(float*)&i;
    ;
    mov       eax,dword ptr [ebp-8]
    mov       dword ptr [ebp+8],eax
    ;
    ;       x = x*(1.5f - xhalf*x*x);
    ;
   fld       dword ptr [ebp-4]
   fmul      dword ptr [ebp+8]
   fmul      dword ptr [ebp+8]
   fsubr     dword ptr [@174+4]
   fmul      dword ptr [ebp+8]
   fstp      dword ptr [ebp+8]
   ;
   ;        return x;
   ;
   fld       dword ptr [ebp+8]
   ;
   ;    }
   ;
   @176:
   @175:
    pop       ecx
    pop       ecx
    pop       ebp
    ret

here was what compiler generated, but I would like to optimise it
and rewrite to asm routine

(this code generated is far from optimal i thing – mixing fpu with integer
operations, maybe some revrite by conscious person would much improve it)

how it can be optymized?

edit:

as to answer @harold

there is an improvement:

  • 1.0/sqrt(100.0) takes 140 cycles on my old machine

  • InvSqrt – c version – takes 44 cycles (though accuracy is not stunning)

  • ansver below in asm works same as c version and it takes 29 cycles

(measurments may be somewhat approximate but genarlly seem be ok IMO,
done by rtdsc 1000x for loop then resulting 140000/1000 = 140cycles
29000/1000 = 29cycles and so)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:12:29+00:00Added an answer on June 9, 2026 at 10:12 pm

    Many of those moves to/from memory aren’t really necessary. This probably isn’t too much of an improvement though (especially not compared to not doing any of this in the first place and just using SSE).

    Not tested:

    ; i = 0x5f3759df - (reinterpret_cast<int32>(number) >> 1)
    mov eax, dword ptr [ebp+8]
    sar eax,1
    mov edx, 0x5f3759df
    sub edx, eax
    mov dword ptr [ebp-4], edx
    ; y = reinterpret_cast<float>(i)
    fld dword ptr [ebp-4]
    ; x2 = numer * 0.5f
    fld dword ptr [ebp+8]
    fmul dword ptr [half]
    ; (x2 * y) * y
    fmul st(0), st(1)
    fmul st(0), st(1)
    ; 1.5f - (stuff)
    fld dword ptr [threehalfs]
    fsubrp st(1), st(0)
    ; y * (stuff)
    fmulp st(1), st(0)
    

    It shouldn’t really be too hard to follow, but I’ll make some stack diagrams if you want them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be easy but i've spent way too much time
First, apologies,this should be simple but I've had too much coffee and cannot wrap
My question might not be too correct... What I mean is: class MyClass {
I'm not too good with C++, however; my code compiled, but the function crashes
I have some code in the form of a string and would like to
Hello I would like to create a circular image witch has a small border
Hello not sure why Im getting this error. Basically I get it in these
Hello everybody my linq to sql selection does not working on dateTime field for
Hello friends I have tried many times but I am not successful than please
hello its really weird that a if and else statement is not working in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.