Recent

Author Topic: CORDIC algorithms  (Read 2408 times)

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
CORDIC algorithms
« on: December 05, 2023, 12:34:43 am »
Does anyone know where I can find examples of CORDIC algorithms written in Pascal?

Thaddy

  • Hero Member
  • *****
  • Posts: 16193
  • Censorship about opinions does not belong here.
Re: CORDIC algorithms
« Reply #1 on: December 05, 2023, 09:00:18 am »
I used a CORDIC implementation in UCSD Pascal on an Apple 2e while teaching computer science introduction in programming at University in 1982 ... So a little over 41 years ago.
I also wrote one in TurboPascal in 1989 based on the UCSD code.
I do no have the code anymore, but I can write a CORDIC easily, since it is not very difficult. So if you can not find it, ping me.

I think you can find a CORDIC for Pascal anyway since it is quite common to use.
edit
On Rosetta code is a quite good example in C that will easily translate. It is not a full task yet. Google Rosetta code cordic. The C code will translate in minutes, not hours.It uses look/up tables for some of the trigonometric functions so it is fast.
This afternoon I have some time left so I will add it here if there isn´t a link or code yet.
« Last Edit: December 05, 2023, 09:13:31 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

avra

  • Hero Member
  • *****
  • Posts: 2532
    • Additional info
Re: CORDIC algorithms
« Reply #2 on: December 05, 2023, 03:44:28 pm »
I did Taylor series for AvrCo fixed point lib, so if needed ping me
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

BlueIcaro

  • Hero Member
  • *****
  • Posts: 804
    • Blog personal
Re: CORDIC algorithms
« Reply #3 on: December 05, 2023, 04:21:02 pm »
Hello, I found this C code. https://people.math.sc.edu/Burkardt/c_src/fn/fn.html
It has a lot of comments. I think I't we easy port to Pascal

/BlueIcaro

Thaddy

  • Hero Member
  • *****
  • Posts: 16193
  • Censorship about opinions does not belong here.
Re: CORDIC algorithms
« Reply #4 on: December 05, 2023, 06:21:11 pm »
I am close to clean up the C  translated code from Rosetta code and will post it tomorrow. Just need to fix the example, basically Pascalify it, but the core function  is ready and did not take too much time, as I expected.

BTW the UCSD version from 41 years ago showed up in Google....
For good measure, though.
I used the ucsd version for teaching purposes, but did not write it.
I wrote a TurboPascal version

A good reader would have understood that.
« Last Edit: December 05, 2023, 06:31:55 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

AlexTP

  • Hero Member
  • *****
  • Posts: 2488
    • UVviewsoft
Re: CORDIC algorithms
« Reply #5 on: December 05, 2023, 06:28:45 pm »
@Thaddy,
1. let's create the wiki page, and post examples there. Easy for future users to google it.

2. what function is discussed: sin, cos, tan, sqrt, other?

Thaddy

  • Hero Member
  • *****
  • Posts: 16193
  • Censorship about opinions does not belong here.
Re: CORDIC algorithms
« Reply #6 on: December 05, 2023, 06:38:27 pm »
I will open a wiki subject and when verified also post it to Rosetta code.
Strange to visit the subject after 40 odd years.
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8035
Re: CORDIC algorithms
« Reply #7 on: December 05, 2023, 09:34:57 pm »
Strange to visit the subject after 40 odd years.

Maybe, maybe not. We're all of us getting older, and that sometimes leads us to revisit and tidy up- or at least explore- "loose ends".

http://www.hp9825.com/html/osborne_s_story.html

https://hackaday.com/2013/08/30/ken-shirriff-completely-reverse-engineers-the-1974-sinclair-scientific-calculator/

We might not do it for the right reasons, but at least we do it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MaxCuriosus

  • Full Member
  • ***
  • Posts: 136
Re: CORDIC algorithms
« Reply #8 on: December 06, 2023, 12:13:46 am »
1) Publishing CORDIC algorithms in Pascal is a good idea. It is one more opportunity to highlight the excellence of the Pascal language.

2) The reason of my original post is to see the algorithm to compute the square root and to compare it with the various non-CORDIC based ones.

3) Is there a snowball chance that somebody know or remember the square root algorithm type used in the math package (assembler) used in DEC PDP11? In the late 70s I saw the source printout of that routine but I don't remember the algorithm type. 

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: CORDIC algorithms
« Reply #9 on: December 06, 2023, 02:12:34 am »
Well I know how to do it on paper and its been a while since I have done it.

But I remember you had to add 2 zeros to each remainder and go from there.





The only true wisdom is knowing you know nothing

MathMan

  • Sr. Member
  • ****
  • Posts: 405
Re: CORDIC algorithms
« Reply #10 on: December 06, 2023, 10:00:16 am »
...

2) The reason of my original post is to see the algorithm to compute the square root and to compare it with the various non-CORDIC based ones.

3) Is there a snowball chance that somebody know or remember the square root algorithm type used in the math package (assembler) used in DEC PDP11? In the late 70s I saw the source printout of that routine but I don't remember the algorithm type.

2) You'll probably be disappointed - FP math libraries have seen tremenduous improvements over the last 20 years.

3) Maybe this - http://history.siam.org/%5C/sup/Fox_bell_subroutine.pdf - it at least references the PDP11 as using it.

Cheers,
MathMan

Thaddy

  • Hero Member
  • *****
  • Posts: 16193
  • Censorship about opinions does not belong here.
Re: CORDIC algorithms
« Reply #11 on: December 06, 2023, 10:20:40 am »
Hello, I found this C code. https://people.math.sc.edu/Burkardt/c_src/fn/fn.html
It has a lot of comments. I think I't we easy port to Pascal

/BlueIcaro
I am impressed by the cleanliness of that code, but I would suggest not to translate it, but compile it into a library and add just a pascal header.
Just compile in C and translate cordic.h with h2pas seems enough at first glance. Looks a bit of overkill to translate cordic.c to Pascal, although that can be done. There should not be many issues with h2pas, except maybe some type declarations like int4 which means a 32 bit integer type. I believe cint is already always 32 bit.

FYI the Rosetta code translation will follow later today, I hope. That is less clean but works.
« Last Edit: December 06, 2023, 10:26:08 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 565
Re: CORDIC algorithms
« Reply #12 on: December 06, 2023, 04:23:32 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 16193
  • Censorship about opinions does not belong here.
Re: CORDIC algorithms
« Reply #13 on: December 07, 2023, 01:20:37 pm »
Here's 'a' version of a CORDIC function, but it still is not what I have in mind. But it does NOT work as advertised and is reasonably compact. It contains a bug that I have to recover from and discovered after posting.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses math;
  3. const
  4.   ArctanTable: array[0..29] of double = (
  5.     0.7853981633974483, 0.4636476090008061, 0.24497866312686414,
  6.     0.12435499454676144, 0.06241880999595735, 0.031239833430268277,
  7.     0.015623728620476831, 0.007812341060101111, 0.0039062301319669718,
  8.     0.0019531225164788188, 0.0009765621895593195, 0.0004882812111948983,
  9.     0.00024414062014936177, 0.00012207031189367021, 6.103515617420877e-05,
  10.     3.0517578115526096e-05, 1.5258789061315762e-05, 7.62939453110197e-06,
  11.     3.814697265606496e-06, 1.907348632810187e-06, 9.536743164059608e-07,
  12.     4.7683715820308884e-07, 2.3841857910155797e-07, 1.1920928955078068e-07,
  13.     5.960464477539055e-08, 2.9802322387695303e-08, 1.4901161193847655e-08,
  14.     7.450580596923828e-09, 3.725290298461914e-09, 1.862645149230957e-09
  15.   );
  16.  
  17. // function CORDIC(var x, y: double;const  n: Integer): double; //todo
  18. function CORDIC(x, y: double;const  n: Integer): double;
  19. var
  20.   i: Integer;
  21.   d, tx, ty: double;
  22. begin
  23.   if x = 0 then
  24.   begin
  25.     Result := y;
  26.     Exit; // get rid of exit
  27.   end;
  28.   if y = 0 then
  29.   begin
  30.     Result := x;
  31.     Exit;// get rid of exit
  32.   end;
  33.   if x < 0 then
  34.   begin
  35.     x := -x;
  36.     y := -y;
  37.   end;
  38.   d := 1;
  39.   tx := x;
  40.   ty := y;
  41.   for i := 0 to n - 1 do
  42.   begin
  43.     if ty < 0 then
  44.     begin
  45.       x := x + d * ty;
  46.       y := y - d * tx;
  47.       ArctanTable[i] := ArctanTable[i] - ArctanTable[i];
  48.     end
  49.     else
  50.     begin
  51.       x := x - d * ty;
  52.       y := y + d * tx;
  53.     end;
  54.     d := d / 2;
  55.     tx := tx / 2;
  56.     ty := ty / 2;
  57.   end;
  58.   Result := x;
  59. end;
  60. begin
  61. end.
Examples will follow, but I think the internals can still be improved.

« Last Edit: December 07, 2023, 01:44:06 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 804
    • Blog personal
Re: CORDIC algorithms
« Reply #14 on: December 07, 2023, 01:22:51 pm »
Hello, I found this C code. https://people.math.sc.edu/Burkardt/c_src/fn/fn.html
It has a lot of comments. I think I't we easy port to Pascal

/BlueIcaro
I am impressed by the cleanliness of that code, but I would suggest not to translate it, but compile it into a library and add just a pascal header.
Just compile in C and translate cordic.h with h2pas seems enough at first glance. Looks a bit of overkill to translate cordic.c to Pascal, although that can be done. There should not be many issues with h2pas, except maybe some type declarations like int4 which means a 32 bit integer type. I believe cint is already always 32 bit.

FYI the Rosetta code translation will follow later today, I hope. That is less clean but works.
Hi, that's true, and better idea than nime.
/BlueIcaro

 

TinyPortal © 2005-2018