1. Phix: float32 return value
- Posted by andreasWagner 1 month ago
- 458 views
Hello,
It's me again, even at the risk of annoying you all. After a year, I've started working on a wrapper for Raylib again. (Or rather, I've started programming again in general)
So far, I've been able to find a workaround/solution for everything. But now I'm stuck on something that is (in my opinion) relatively simple. More specifically, this:
constant xGetMouseWheelMove = define_c_func(ray,GetMouseWheelMove,{},C_FLOAT)
Under Phix64bit, calling the function returns incorrect values, but under Phix32bit it works. My goal is 64bit.(also because my workarounds/solutions for passing structures and receiving them as return values only work with 64-bit).
And, to be perfectly honest, I don't feel like making my code even more unreadable than it already is with if 32bit then do this else do that.
To make it easier to investigate and visualize the whole thing, I have built a test system.
A DLL:
// File: test_ffi.c
// compile with:- m32 for 32bit dll
//gcc -shared -m64 -o test_ffi64.dll test_ffi.c
//or
//gcc -shared -m64 -s -o test_ffi64.dll test_ffi.c -Wl,--out-implib,libtest_ffi.a
#include <windows.h>
// Funktioniert in GCC/MinGW
__attribute__((dllexport)) float GetTestFloat() {
return 123.456f;
}
__attribute__((dllexport)) float MultiplyFloats(float a, float b) {
return a * b;
}
And a test program:
sequence dll={"test_ffi64.dll","test_ffi32.dll"} atom result atom x=2.5,y=3.5 atom lib=open_dll(dll) constant GetTestFloat= define_c_func(lib,"GetTestFloat",{},C_FLOAT) constant MultiplyFloats=define_c_func(lib,"MultiplyFloats",{C_FLOAT,C_FLOAT},C_FLOAT) printf(1,"%d Bit %s\n",{machine_bits(),"System"}) result=c_func(GetTestFloat,{}) printf(1,"%f should be 123.456\n",result) result=c_func(MultiplyFloats,{x,y}) printf(1,"%f should be %f\n",{result,x*y}) wait_key()
As I said, it works with phix32 bit but not with phix64:
My working system is Windows11 64-bit Phix1.0.5
2. Re: Phix: float32 return value
- Posted by petelomax 1 month ago
- 453 views
Oh my, that is an absolutely perfect error report, can I just say well done and thankyou!
The fix itself is pretty straightforward, pcfunc.e ~line 2219 should end up as
cmp rdx,0x03000004 -- (C_FLOAT)
-- je :cstorexmm0
jne @f
sub rsp,8
movss dword[rsp],xmm0
fld dword[rsp]
jmp :cstorest0
@@:
cmp rdx,0x03000008 -- (C_DOUBLE)
jne @f
-- ::cstorexmm0
-- 14/2/16: (certainly C_DOUBLE, not necessarily C_FLOAT?) [25/2, I think it's the same]
sub rsp,8
movsd qword[rsp],xmm0
fld qword[rsp]
::cstorest0
add rsp,8
However, 1.0.5 does not implement movss, which needs, before making the above changes:
pttree.e line 1289:
global constant T_swi = 6060 tt_stringF("swi",T_swi)
global constant T_movss = 6068 tt_stringF("movss",T_movss)
pilasm.e line 4333:
-- elsif ttidx=T_movsd then
elsif ttidx=T_movsd
or ttidx=T_movss then
op = iff(ttidx=T_movsd?0o362:0o363)
and ten/twenty lines on:
-- s5 &= {0o362,0o017,0o020}
s5 &= {op,0o017,0o020}
-- s5 &= {0o362,0o017,0o021}
s5 &= {op,0o017,0o021}
You'll then need to run p -c p, then make the changes to pcfunc.e, then run p -c p again.
Actually, probably best if you just change pttree.e first and quickly check by running "p p"
whether you get anything like "movss should be 6068(not 6064)", if not kill it and carry on.
Finally, a quick "p -test" should reassure you nothing else got broken.
Thanks again, I would probably never have found or fixed that without your help.
3. Re: Phix: float32 return value
- Posted by andreasWagner 1 month ago
- 436 views
First of all, thank you for your quick reply.
Actually, probably best if you just change pttree.e first and quickly check by running "p p"
whether you get anything like "movss should be 6068(not 6064)", if not kill it and carry on.
okay, i end here:
D:\testpool\ray_workplace\Phix>p p
swi should be 5956(not 6060)
and its more like line 1271 not 1289 in pttree.e where the constants end.
Okay, I'll take another look tomorrow.
4. Re: Phix: float32 return value
- Posted by Icy_Viking 1 month ago
- 437 views
I do have a Raylib wrapper for Euphoria 4.1.0 Beta 2 [https://github.com/gAndy50/EuRayLib5]
It uses the FFI library to help with wrapping structs. It might help with making a Phix wrapper.
Forked into: Raylib Phix reasing.e for Icy_Viking
5. Re: Phix: float32 return value
- Posted by petelomax 1 month ago
- 426 views
First of all, thank you for your quick reply.
Actually, probably best if you just change pttree.e first and quickly check by running "p p"
whether you get anything like "movss should be 6068(not 6064)", if not kill it and carry on.
okay, i end here:
D:\testpool\ray_workplace\Phix>p p
swi should be 5956(not 6060)
and its more like line 1271 not 1289 in pttree.e where the constants end.
Okay, I'll take another look tomorrow.
Right, the improvements list for 1.0.6 is currently at 156 lines long, so my pttree.e is probably a bit mangled vs yours.
The intention was not to change T_swi (which should be the very last call to tt_stringF() in that file) but to "add 8" or
whatever it actually needs, so I'd take a (blind) stab at 5964 next. No biggie. If you really get stuck you'll have to wait
until 1.0.6 ships, but that's at least a few months off yet.
6. Re: Phix: float32 return value
- Posted by andreasWagner 1 month ago
- 429 views
Hallo,
Right, the improvements list for 1.0.6 is currently at 156 lines long, so my pttree.e is probably a bit mangled vs yours.
The intention was not to change T_swi (which should be the very last call to tt_stringF() in that file) but to "add 8" or
whatever it actually needs, so I'd take a (blind) stab at 5964 next. No biggie. If you really get stuck you'll have to wait
until 1.0.6 ships, but that's at least a few months off yet.
i did this, T_swi is not in pttree.e (from the 1.0.5 release):
pttree.e
begining with line 1271 this:
global constant T_swi = 5956 tt_stringF("swi",T_swi)
global constant T_movss = 5964 tt_stringF("movss",T_movss)
p p worked.
then this in pilasm.e (also from the 1.0.5 release):
pilasm.e
line 4337:
-- elsif ttidx=T_movsd then
elsif ttidx=T_movsd
or ttidx=T_movss then
op = iff(ttidx=T_movsd?0o362:0o363) -- 64-bit move (as per movsb, movsw, movsd)
line 4301: -- s5 &= {0o362,0o017,0o020}
s5 &= {op,0o017,0o020}
line 4311 -- s5 &= {0o362,0o017,0o021}
s5 &= {op,0o017,0o021}
p p now gives this.
D:\testpool\ray_workplace\Phix\pilasm.e:4338
elsif ttidx=T_movsd or ttidx=T_movss then op = iff(ttidx=T_movsd?..
^ duplicate case value [3364, il offset:2791]
I think your local copy of Phix is probably very different from the release version. But thanks for the quick response. It shows that Phix is still alive and being maintained.
Edit:I have found a temporary workaround. I have changed raylib; the return value is now int. That is sufficient for the two examples I have ported for phix and which use the function. Now I can continue until the next problems arise.
7. Re: Phix: float32 return value
- Posted by petelomax 1 month ago
- 399 views
My bad, I shd've gotten line nos from 1.0.5 srcs... maybe next time.
D:\testpool\ray_workplace\Phix\pilasm.e:4338
elsif ttidx=T_movsd or ttidx=T_movss then op = iff(ttidx=T_movsd?..
^ duplicate case value [3364, il offset:2791]
That error means it has converted the if to a switch (for performance reasons) but hit a snag in that
there are two (or more) branches for 3364 (T_movsd) - there is one on the-1.0.5-line 4291 (ie where I should have pointed you).
I'll have a think about making the error say "duplicate case value [3364 on lines 4338 and 4291]".
Edit:I have found a temporary workaround. the return value is now int.
C_DOUBLE instead of C_FLOAT return types would also be hassle-free (as long as the C code is returning a 64-bit float and not a 32-bit float), if that helps or you need the fractions.
Should you want to leave it as-is for now, I'm happy if you`re happy.
(undo if you can, but having non-compilable sources for the compiler itself lying around is not really going to hurt you, unless/until some other snag has no similar workaround)
8. Re: Phix: float32 return value
- Posted by andreasWagner 3 weeks ago
- 337 views
Should you want to leave it as-is for now, I'm happy if you`re happy.
No, I'm not really happy. A float32 return value is of course needed more often, especially in raymath. And I wasn't able to apply the patch myself.
Perhaps the easiest thing would be for you to update your GitHub repo. Maybe even with the older patches, thank you.
I had an unpleasant experience. I referred a friend to my https://github.com/andizk4kx/ArduLink. Unfortunately, the program only seems to work with the phix version on my computer and probably yours. It doesn't work with the git repo or the official download. It was something to do with assert https://openeuphoria.org/forum/138715.wc#138715. In the case with my friend, I simply copied everything onto a USB stick and drove over to his place (he trusts me not to bring any viruses with me).
In addition, return values of type c_bool are unstable (I don't know how else to put it). You can try bunnymark test and https://openeuphoria.org/forum/139277.wc?last_id=0
and remove the and_bits() here
global function IsMouseButtonDown(atom button) return and_bits(c_func(xIsMouseButtonDown,{button}),1) end function
Thank you
and apologies for the grumbling and complaining
9. Re: Phix: float32 return value
- Posted by petelomax 3 weeks ago
- 271 views
No, I'm not really happy.
OK, I've made a start on releasing 1.0.6.
Perhaps the easiest thing would be for you to update your GitHub repo. Maybe even with the older patches, thank you.
Alas, that rarely works out as well as I'd hope (self-hosted 'n all that, I guess), but GitHub is now updated, feel free to give it a shot.
I had an unpleasant experience. ... apologies for the grumbling and complaining
Good feedback is nice, but negative feedback is actually immensely useful, especially when well written as you have been doing, so keep that up!
In addition, return values of type c_bool are unstable (I don't know how else to put it).
I think it's best I leave that for now (ie ship 1.0.6 as-is), C return "bool" is a law unto itself, instead of the neat safe 1/0 typical in Phix,
true/false can be said or anything/0, not -ve/-ve, +ve/not +ve, +ve/0, 1/not 1, 0/not 0(!), 0/#80[byte|word|dword] bit, you name it.
As you indicated, there are often pretty much unavoidable call-specific workarounds needed. There may well be nowt I can do.
10. Re: Phix: float32 return value
- Posted by andreasWagner 3 weeks ago
- 274 views
Alas, that rarely works out as well as I'd hope (self-hosted 'n all that, I guess), but GitHub is now updated, feel free to give it a shot.
Hi, First of all, thanks. I didn't really expect a response so quickly (I know it takes time and effort).
Of course, I only briefly tested everything I usually do when a new Phix comes out.
So, p -c p works, p64 -c p works too.
..\..\p -c edita.exw, okay
Edix looks promising.
The float32 bug is gone
The old bugs I remember are also gone, meaning my old programs are running again with the official version of Phix.
iup.chm and cd.chm don t work for me with Windows HTML Help; I only get the table of contents the individual contents aren t displayed.
However, I can read the files with the Calibre e-book viewer. It does report missing links in both files, though.
Before I forget, the new icon is much better (the old one always reminded me of Turbo Pascal for Windows from the last century)
11. Re: Phix: float32 return value
- Posted by petelomax 2 weeks ago
- 271 views
iup.chm and cd.chm don t work for me with Windows HTML Help; I only get the table of contents the individual contents aren t displayed.
However, I can read the files with the Calibre e-book viewer. It does report missing links in both files, though.
Ah, those were never meant for public release, they are not in the official zip/installer download and are probably quite outdated,
should be better covered within phix.chm with a Phix slant, and when/if not you may be better off looking at the official/latest IUP
docs they (once) came from, ie https://www.tecgraf.puc-rio.br/iup/ and https://www.tecgraf.puc-rio.br/cd/. I've deleted them now.
UPDATE:
So, p -c p works, p64 -c p works too.
Oh, OK, if it's alright with you I'll put the release on hold then, and press on with other things (such as further investigating bool returns).
12. Re: Phix: float32 return value
- Posted by andreasWagner 6 days ago
- 66 views
Oh, OK, if it's alright with you I'll put the release on hold then, and press on with other things (such as further investigating bool returns).
Just a few thoughts...
Unfortunately, I had to delve deeply into the MS-ABI (not the MS Win32 API) specifically the AMD64 MS-ABI.
Of course, whether you like it or not, the C programming language is the deciding factor here. The 'bool' data type is unknown in C and was therefore represented as an integer: 0 = false, 1 = true. With C99, a fixed definition was introduced via stdbool.h that did exactly that. This replaced the internal keyword _Bool.
stdbool.h has been deprecated since C23. I don't know at the moment how things will proceed from here.
A simple definition of bool (used in many PLC systems) is that if the least significant bit is 1, it is true; if it is 0, it is false.
The problem with all ABIs is that it's not guaranteed that the irrelevant bits will be cleared. A bool can be 1 byte, 2 bytes, 4 bytes, or even 8 bytes in 64-bit systems. But as far as I've seen, there's no specification anywhere that requires clearing the irrelevant bits.
I think 'and_bits(result, 1)' is the best solution available.
However, this also means that if 'result' were 2, the result would be false.
And many checks like 'if (result) then' would, understandably, no longer work.
These are just my thoughts.
And, since I use a translation tool, it's really a pain that the forum crashes every time I use a Unicode character.

