Fast 1/X division (reciprocal)

后端 未结 6 1300
忘掉有多难
忘掉有多难 2020-12-29 22:46

Is there some way to improve reciprocal (division 1 over X) with respect to speed, if the precision is not crucial?

So, I need to calculate 1/X. Is there so

6条回答
  •  梦谈多话
    2020-12-29 23:28

    I’ve bench tested these methods on Arduino NANO for speed and 'accuracy'.
    Basic calculation was to setup variables, Y = 15,000,000 and Z = 65,535
    (in my real case, Y is a constant and Z can vary between 65353 and 3000 so a useful test)
    Calc time on Arduino was established by putting pin low, then high as calc made and then low again and comparing on times with logic analyser. FOR 100 CYCLES. With variables as unsigned integers:-

    Y * Z takes 0.231 msec
    Y / Z takes  3.867 msec.  
    With variables as floats:-  
    Y * Z takes  1.066 msec
    Y / Z takes  4.113 msec.  
    Basic Bench Mark  and ( 15,000,000/65535 = 228.885 via calculator.) 
    

    Using {Jack Giffin’s} float reciprocal algorithm:

    Y * reciprocal(Z)  takes  1.937msec  which is a good improvement, but accuracy less so 213.68.  
    

    Using {nimig18’s} float inv_fast:

    Y* inv_fast(Z)  takes  5.501 msec  accuracy 228.116  with single iteration  
    Y* inv_fast(Z)  takes  7.895 msec  accuracy 228.883  with second iteration 
    

    Using Wikipedia's Q_rsqrt (pointed to by {Jack Giffin})

    Y * Q*rsqrt(Z) takes  6.104 msec  accuracy   228.116  with single iteration  
    All entertaining but ultimately disappointing!
    

提交回复
热议问题