Close

CORDIC MATH IN C The

earl-tEARL T wrote 09/21/2025 at 17:19 • 4 min read • Like

THE FOLLOWING WAS INVENTED BY FOLKS FAR SMARTER THEN ME I'M JUST TRYING TO MAKE IT A FUNCTIONAL C LIBRARY THAT DOSEAS MUCH AS I CAN WITH + - BIT SHIFTS AND FIX POINT


  the CORDIC algorithm for sin and cos where z starts as the angle x at 1 y at 0 and i is number of iterations. 
This is a kinda c kinda psudo code version of it  as you can see sin and cos are both found at once atanz is a array of 20 atan( 2^-i)*(1<<20) the 20 bitshift is because this is all done in fix point 32 bit int Q=20 so thats where the decimal falls 20 binary places from the back 
struct sincos  CORDIC_sin_cos( float ang){
int y, x, z, ny, nx ;
    x =1*(1<<20);
    y = 0;
    z=(int32_t) ang * (1<<20);
for ( int i = 0 ; i  < 20 ; i++ ){
     if ( z <0 ){
        nx = x + ( y >> i ) ;
        ny = y - ( x >> i) ;
        z += atanz[ i ] ;
    }else{
        nx = x - ( y >> i ) ;
        ny = y + ( x >> i ) ;
        z -= atanz[ i ] ;
    }
    x = nx ;
    y = ny ;
}
    struct sincos{
        float sin =(float) y / (1<<20) ;
        float cos =(float) x / (1<<20) ;
    }sincos;
return sincos ;
}

 since tan is  sin/cos  thats your basic trig functions asin  can be found by entering the sin  the target and running the formula to push the y  to the target then getting the z which starts at 0 for this . Acos can be done the same way if it is positive if it is negitive  it is the same as asin( abs( input ) ) + pi /2


LOGARITHMIC MATH

BODWITCH NAPIER AND HOW TO MAKE MATH EASY

Ok so here is where I am with the use of log and exp allthou I have a sorta working CORDIC function for both log and exp it's only sorta working and this is kinda way cooler also it works.

Using exp :

There is actually more but that should give you an idea there is also e and the hyperbolic I spelt that right and messed up spelled so bad spell check didn't even understand me left spelling error left because back to the point:f

on the all log exp bitshift version of asinh and acosh I may be a bit off im not sure if the plus and minus 1 are in the right spot atanh is actualy easier:


so after saying all that 


STATE OF MY CORDIC LOG AND EXP


not very great while they work as a unit least to cancel each other out sq kinda works but sqrt has a limit between 4 2.00 is right and 9 2.8125 25 same thing so im hitting an upper I think i got it  well for the broken version EARL-C-T/CORDIC soon to maybe be fixed  but right now buggy as fuck but enjoy if you use make all in the top level it should make everything it may not also if ya just run make it runs make clean  im not great at makefiles and there is no configure at all anyway make all then if ya go to build/x86_64 run ./CORDIC_CAL you will be in a little shell commands are sq sqrt sct for sin tan cos asin acos log exp quit all commands besides quit are to be followed by a float there out put is to stdin and thats all for now help dosnt do anything i havent added a help file for it to print yet  the fix point lib it uses is in libfixpnt wich i have actually installed so ya i forgot. this wont work unless ya install the libfixpnt.so or change the requires compiler or watever way to make it work and libCORDIC contains the rest wich is in a static lib or watever ya wanna call it I'm tired that one should be fine for compiling 
Like

Discussions

Ray Burne wrote 09/26/2025 at 04:27 point

You may wish to look at how the HO calculators managed CORDIC math. Some C++ examples exist.

http://www.google.com/search?q=HP-45+CORDIC+algorithm+rewritten+in+C%2B%2B

  Are you sure? yes | no

EARL T wrote 09/27/2025 at 11:28 point

Thanks I'm working from combo of random materials from papers to code snippets in several different languages a lot dsp hardware verilog stuff and some just math and pseudo code some of it disagrees and some things like the way the scaling factor was implemented  for asin acos in this one paper was to add it to the target every iter and just wouldn't work for me removed it and I got +/- 0.01 accuracy for asin at least acos is still off  but ya idk It looks good I'm gonna look at some of it some more see if I can't just find instead of trying to reason out and trial and error some of my issues some my issues.

  Are you sure? yes | no