cdecl - conventions instructions? lazarus / 21-General ---------------------------------------------------------------------- AlanTheBeast · 2021-03-03 01:05 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. ---------------------------------------------------------------------- PascalDragon · 2021-03-03 07:55 Quote from: AlanTheBeast on March 03, 2021, 02:05:38 amI 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 · 2021-03-03 09:36 Quote from: AlanTheBeast on March 03, 2021, 02:05:38 amI was thinking of translating it to PascalIf 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 ---------------------------------------------------------------------- Zvoni · 2021-03-03 10:24 There you go. It...... compiles.......  No Idea about optimizations, correctness and whatever...... ---------------------------------------------------------------------- AlanTheBeast · 2021-03-03 12:47 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. ---------------------------------------------------------------------- AlanTheBeast · 2021-03-03 13:04 @Avra: I'll be integrating 3-axis gyro and accelerometers with GPS to constrain the velocity.  (Gyros integrate well, accelerometers, not so much). ---------------------------------------------------------------------- Zvoni · 2021-03-03 13:10 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 ---------------------------------------------------------------------- AlanTheBeast · 2021-03-03 13:19 @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 ...). ---------------------------------------------------------------------- Zvoni · 2021-03-03 13:20 Quote from: AlanTheBeast 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!    ). No Problem! I took it as an excersize (and not to get rusty). ---------------------------------------------------------------------- PascalDragon · 2021-03-04 08:05 Quote from: AlanTheBeast on March 03, 2021, 01:47:41 pmI'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 · 2021-03-04 09:29 Quote from: PascalDragon on March 04, 2021, 09:05:43 amThat 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 · 2021-03-04 12:10 Quote from: marcov on March 04, 2021, 10:29:04 amQuote from: PascalDragon on March 04, 2021, 09:05:43 amThat 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 · 2021-03-04 14:00 Quote from: AlanTheBeast on March 03, 2021, 01:47:41 pmI'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. ---------------------------------------------------------------------- AlanTheBeast · 2021-03-04 21:04 Quote from: PascalDragon on March 04, 2021, 09:05:43 amThat 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. Quote from: marcov on March 04, 2021, 10:29:04 amPascal 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... ---------------------------------------------------------------------- AlanTheBeast · 2021-03-04 21:12 Quote from: Zvoni on March 04, 2021, 03:00:17 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ab@Alans- Mac-mini quaternion-master % gcc quaternion.cquaternion.c:245:9: warning: using integer absolute value function 'abs' when      argument is of floating point type [-Wabsolute-value]    if (abs(cosHalfTheta) >= 1.0)  Gcc helpfully suggests: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i quaternion.c:245:9: note: use function 'fabs' instead    if (abs(cosHalfTheta) >= 1.0) {        ^~~        fabs