Bug in struct with float arguments?

Q&A's, tips, howto's
Locked
AVC
Posts: 3
Joined: Sun Mar 28, 2021 4:28 pm

Bug in struct with float arguments?

Post by AVC »

Hello,

I love newLISP and I'm trying to use it for some graphics programming. So I'm writing a FFI module for the raylib library.

I've encountered an odd behavior of struct: If I define a 'struct' with one or more floats and pass it to 'pack', the float values are stored as zeroes. But surprisingly the same struct works well with 'unpack'. Here is an example:

(struct 'Vector2 "float" "float")
(pack Vector2 200 200) => returns "\000\000\000\000\000\000\000\000", which is wrong!
(pack "ff" 200 200) => returns "\000\000HC\000\000HC", which is what 'pack Vector2...' also should return
(unpack Vector2 (pack "ff" 200 200)) => returns (200 200) correctly!

I'm using newLISP 10.7.5 64-bit on Arch Linux with ffi and UTF-8 support enabled.

Is there a bug or am I doing something wrong?

Thanks in advance
Alfonso

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Bug in struct with float arguments?

Post by ralph.ronnquist »

Yes it's a bug in the handling of the "incoming cell data" for an ffi_type_float at nl-import.c:814.
That current line is

Code: Select all

floatV = (float) *(double *)&cell->aux;
and should be replaced with the following

Code: Select all

floatV = (float) ((cell->type == CELL_FLOAT)? *(double *)&uint64V : uint64V);
which corresponds to how it's handled in the pack function.

AVC
Posts: 3
Joined: Sun Mar 28, 2021 4:28 pm

Re: Bug in struct with float arguments?

Post by AVC »

I've changed that line and recompiled. Bug solved! Thank you very much!

Locked