Inverse sqrt for fixed point

前端 未结 3 728
栀梦
栀梦 2021-01-06 02:58

I am looking for the best inverse square root algorithm for fixed point 16.16 numbers. The code below is what I have so far(but basically it takes the square root and divide

3条回答
  •  醉话见心
    2021-01-06 03:43

    The trick is to apply Newton's method to the problem x - 1/y^2 = 0. So, given x, solve for y using an iterative scheme.

    Y_(n+1) = y_n * (3 - x*y_n^2)/2
    

    The divide by 2 is just a bit shift, or at worst, a multiply by 0.5. This scheme converges to y=1/sqrt(x), exactly as requested, and without any true divides at all.

    The only problem is that you need a decent starting value for y. As I recall there are limits on the estimate y for the iterations to converge.

提交回复
热议问题