home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,304 of 4,675   
   Terje Mathisen to Robert Prins   
   Re: Online generation of constants for "   
   13 Mar 18 11:10:46   
   
   From: terje.mathisen@nospicedham.tmsw.no   
      
   Robert Prins wrote:   
   > On 2018-03-12 19:50, Terje Mathisen wrote:   
   >> Multiplication by 41 can be done in several ways:   
   >>   
   >>    imul eax,edx        ;; *41   
   >>   
   >>    lea edx,[eax+eax*4] ;; *5   
   >>    lea eax,[eax+edx*8] ;; *41   
   >>   
   >>    lea edx,[eax+eax*8]     ;; *9   
   >>    shl eax,5        ;; *32   
   >>    add eax,edx   
   >>   
   >> On a cpu where LEA takes two cycles the last version can run in three   
   >> cycles while the dual-LEA version would take four.   
   >   
   > But that takes me back to my earlier question, given that fact that   
   > multiplies are very fast nowadays, how useful are the above LEA sequences?   
      
   With single-cycle LEA and 3-cycle MUL/IMUL LEA still wins.   
      
   >   
   > The 41/4096 is neat, but do you know what you can do with these six   
   > digits, 1, 1, 3, 3, 5, and 5?   
   >   
   >   
   > Well, quite remarkably, 1 / (113 / 355) is, for two (relatively) tiny   
   > numbers, amazingly close to pi!   
      
   This is in fact quite similar to my 41/4096 approximation, i.e. you use   
   6 digits to get 3.14159292 instead of the real value of 3.1415926535...   
   so you got 7+ accurate digits.   
      
   The first such ratio is of course 22/7 which results in 3.142857, so   
   nearly 4 correct digits.   
      
   41/4096 only needs 4 digits in total (*41, SHR 12) and results in   
   0.01000977, which is close enough to give correct results around each of   
   the century boundaries.   
      
   BTW, I previously misstated the problem!   
      
   Since I first figure out the year number I then have a value in the   
   0..399 range, dividing this by 100 to get the century works easily with   
   a 5-digit accurate reciprocal.   
      
   The day number to year calculation is in fact much more interesting, it   
   turns our that even though there are many formulas to calculate this   
   exactly, it is much faster to simply guess the year using a fast   
   approximation, then calculate how many days would go into those years   
   and adjust the first guess if it was off by one. :-)   
      
   Terje   
      
   --   
   -    
   "almost all programming can be viewed as an exercise in caching"   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca