Search code examples
c++cordic

CORDIC for square roots


I have been looking at the CORDIC algorithm in hyperbolic rotation mode to find the square root of a variable. I am unsure what my initial variables should be (x0, y0, z0) to find the square root. I have read some papers, citing that to find the sqrt(a), initial values should be set to a+1,a-1,0 for x0,y0,and z0 respectively. Others says it should be a+0.25,a-0.25,0. I am very confused by this

Can anyone help?

double x = (64.0+1);
double y = (64.0-1);
double z = 0; 


double k = 3;
double n = 1;
while(n <= 20 ){

    double xn = pow(2.0,-1.0*n) * x;
    double yn = pow(2.0,-1.0*n) * y;

    if(y < 0){ 
        x = x + xn;
        y = y + yn;
        z = z - atanh(pow(2.0,-1.0*n));
    }
    else
    {
        x = x - xn;
        y = y - yn;
        z = z + atanh(pow(2.0,-1*n));

    }

    if(k > 0){
        k = k-1;
    }
    else{
        k = 3;
        if(y < 0){ 
            x = x + xn;
            y = y + yn;
            z = z - atanh(pow(2.0,-1.0*n));
        }
        else
        {
            x = x - xn;
            y = y - yn;
            z = z + atanh(pow(2.0,-1.0*n));

        }
    }
    n++;
    cout << "x: " << x << " y: " << y << " z: " << z << endl;
}

EDIT* Along with compensating for 3j+1 repeats, CORDIC requires to execute the loop twice in instances such as n = 4,13,40, ... I have updated my code to compensate for that, but it still does not work. I am using hyperbolic rotation in vectoring mode, which the variable d should be based on sign of y

EDIT* Turns out that CORDIC can fail when computing larger square root values, so you have to normalize the number you are trying to find the square root of to 0.5 to 2 range, then scale back answer back up.


Solution

  • You should normalize the number you are performing the square root on to [0.5,2) range, then scale back up accordingly.