Recent

Author Topic: cdecl - conventions instructions?  (Read 1205 times)

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
cdecl - conventions instructions?
« on: March 03, 2021, 02:05:38 am »
There's a nice C module, well documented, debugged, etc.[1]

I was thinking of translating it to Pascal, not difficult, but a good chance of screwup.  Then I'd have to translate the test code too ... with a good chance of ...

It would be less risky (I believe) to call it directly.  Maybe even save time.

I see a lot of discussions around cdecl, but no "reference manual" to it.  Is there such?

IAC the target is ARM 64 bit, Linux (Raspberry Pi OS to be specific).

[1] https://github.com/MartinWeigel/Quaternion is the code I wish to interface to.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2900
  • Compiler Developer
Re: cdecl - conventions instructions?
« Reply #1 on: March 03, 2021, 08:55:27 am »
I see a lot of discussions around cdecl, but no "reference manual" to it.  Is there such?

The cdecl calling convention adheres to the target's default C calling convention thus you should check the target's ABI information (and in case of i386 that calling convention is officially called cdecl).

For ARM64/AArch64 there is a rough overview here with more details in the references of that part of the article.

avra

  • Hero Member
  • *****
  • Posts: 2144
    • Additional info
Re: cdecl - conventions instructions?
« Reply #2 on: March 03, 2021, 10:36:34 am »
I was thinking of translating it to Pascal
If you are just after simple interpolation without vectors, then take a look at this: https://bitbucket.org/avra/interpolate1234d

As for converting C headers, these links might help:
http://rvelthuis.de/articles/articles-convert.html#cconvs
https://wiki.freepascal.org/C_to_Pascal
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Zvoni

  • Hero Member
  • *****
  • Posts: 518
Re: cdecl - conventions instructions?
« Reply #3 on: March 03, 2021, 11:24:23 am »
There you go.

It...... compiles.......  :-\

No Idea about optimizations, correctness and whatever......



One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: cdecl - conventions instructions?
« Reply #4 on: March 03, 2021, 01:47:41 pm »
Thanks everyone and Zvoni, you really shouldn't feed my laziness.

That said, I'll compile it (for x86_64) -al to see if the assembler looks right (for an _86 anyway - I don't know much about the ARM code).

And of course throw some tests at it.

CORRECTION:  I assumed, Zvoni, you would go the cdecl route, not translate.  I'll have to examine it carefully.  For instance where you mention the need for a local vars, I agree it would seem not.

I'll also be looking at that pointer notation in C that I don't fully get ( such as result.v[0] = (q1->v[0] * 0.5 + q2->v[0] * 0.5); ).

Did you use a tool for the translation?  I had found a few online but was putting off using them.
« Last Edit: March 03, 2021, 02:15:00 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: cdecl - conventions instructions?
« Reply #5 on: March 03, 2021, 02:04:01 pm »
@Avra: I'll be integrating 3-axis gyro and accelerometers with GPS to constrain the velocity.  (Gyros integrate well, accelerometers, not so much).
« Last Edit: March 03, 2021, 02:08:41 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

Zvoni

  • Hero Member
  • *****
  • Posts: 518
Re: cdecl - conventions instructions?
« Reply #6 on: March 03, 2021, 02:10:43 pm »
No tool. Everything "by hand"
Had to look up some functions (what they do in C, and what's the equivalent in FPC), and write my own "copysign" (i found graeme's function on github, but that's not in FPC320?!?!?!)
Also pay attention to comment in the "...printf"-Function.

As for the cdecl-way: basically, you would just have to append a "cdecl;external functionname blablabla" to each Function in the Interface section, and define the libname and where it is
It's the reason why i used conditional usage of the ctypes-unit.

I'm loath to use an external library that's so small and actually pretty easy to translate.

But as i said: No idea, if i did everything correct.
Wouldn't mind a second opinion
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: cdecl - conventions instructions?
« Reply #7 on: March 03, 2021, 02:19:12 pm »
@Zvoni: I really appreciate the effort, but please don't haul my water.  I'm too lazy as it is!

(Also now I really have to test the crap out of it!   ::) ).

As to the printf, I'll be changing that to suit my needs in any case.

Not sure when I'll get to this, I have other hardware coming in this week that I want to test, but I should be able to throw some vectors at it and see if it works.  (And a real job too ...).
« Last Edit: March 03, 2021, 02:24:39 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

Zvoni

  • Hero Member
  • *****
  • Posts: 518
Re: cdecl - conventions instructions?
« Reply #8 on: March 03, 2021, 02:20:23 pm »
@Zvoni: I really appreciate the effort, but please don't haul my water.  I'm too lazy as it is!

(Also now I really have to test the crap out of it!   ::) ).

No Problem!

I took it as an excersize (and not to get rusty).
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

PascalDragon

  • Hero Member
  • *****
  • Posts: 2900
  • Compiler Developer
Re: cdecl - conventions instructions?
« Reply #9 on: March 04, 2021, 09:05:43 am »
I'll also be looking at that pointer notation in C that I don't fully get ( such as result.v[0] = (q1->v[0] * 0.5 + q2->v[0] * 0.5); ).

That is simply a pointer dereferentiation. In C you use either *p if you want the value p points to or p->f if you want to access field f of the struct p points to. In Pascal you simply use p^ for both cases.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9228
  • FPC developer.
Re: cdecl - conventions instructions?
« Reply #10 on: March 04, 2021, 10:29:04 am »
That is simply a pointer dereferentiation. In C you use either *p if you want the value p points to or p->f if you want to access field f of the struct p points to. In Pascal you simply use p^ for both cases.

Pascal still requires the point for field access though, so -> is equal to ^. or just . in pointermath mode.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2900
  • Compiler Developer
Re: cdecl - conventions instructions?
« Reply #11 on: March 04, 2021, 01:10:11 pm »
That is simply a pointer dereferentiation. In C you use either *p if you want the value p points to or p->f if you want to access field f of the struct p points to. In Pascal you simply use p^ for both cases.

Pascal still requires the point for field access though, so -> is equal to ^. or just . in pointermath mode.

Yes, right... :-[

Zvoni

  • Hero Member
  • *****
  • Posts: 518
Re: cdecl - conventions instructions?
« Reply #12 on: March 04, 2021, 03:00:17 pm »
I'll also be looking at that pointer notation in C that I don't fully get ( such as result.v[0] = (q1->v[0] * 0.5 + q2->v[0] * 0.5); ).
btw: i changed that calculation in my "translated" code to
Code: [Select]
res.v[0]:=0.5*(q1^.v[0]+q2^.v[0])
C-Version = 3 Instructions (Multiplication, Multiplication, Addition)
My Version = 2 Instructions (Addition, Multiplication)

As i said: No idea about optimizations, correctness etc.
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: cdecl - conventions instructions?
« Reply #13 on: March 04, 2021, 10:04:16 pm »
That is simply a pointer dereferentiation. In C you use either *p if you want the value p points to or p->f if you want to access field f of the struct p points to. In Pascal you simply use p^ for both cases.
Pascal still requires the point for field access though, so -> is equal to ^. or just . in pointermath mode.

Thx. I thought it would be something like that.  I recall hating C pointer notation and always thought Pascal ^ ^. and @ were the most natural things to use...
« Last Edit: March 04, 2021, 10:06:15 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: cdecl - conventions instructions?
« Reply #14 on: March 04, 2021, 10:12:07 pm »

btw: i changed that calculation in my "translated" code to
Code: [Select]
res.v[0]:=0.5*(q1^.v[0]+q2^.v[0])
C-Version = 3 Instructions (Multiplication, Multiplication, Addition)
My Version = 2 Instructions (Addition, Multiplication)

Looks okay - but I'll check.  I've been thinking of a way to test the Pascal against the C with wrapper programs and a master to call each of them and stimulate each function in each version and cross check using the test code values from the original package and other random numbers.

On that note when I compiled the original C version (Mac gcc) it threw an error over the abs (x) operation, need fabs (x) as x is a real...

Code: Text  [Select][+][-]
  1. ab@Alans-Mac-mini quaternion-master % gcc quaternion.c
  2. quaternion.c:245:9: warning: using integer absolute value function 'abs' when
  3.       argument is of floating point type [-Wabsolute-value]
  4.     if (abs(cosHalfTheta) >= 1.0)
  5.  

Gcc helpfully suggests:
Code: Text  [Select][+][-]
  1. quaternion.c:245:9: note: use function 'fabs' instead
  2.     if (abs(cosHalfTheta) >= 1.0) {
  3.         ^~~
  4.         fabs
  5.  
« Last Edit: March 04, 2021, 10:47:57 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

 

TinyPortal © 2005-2018