PDA

View Full Version : Curious case of !sudden! AVs on ARM due to packed records



Chebmaster
26-04-2017, 07:50 AM
In this particular case, Raspberry Pi 2 (ARMv7l / fpc 2.6.4).
I always had my application-wide data organized in a tree of records. *Packed* records.
It always worked.
Then, on RPi2, I loaded GLES2... and suddenly got hit in my face with an AV at the first attempt of float division of two longints.
I was stumped.
I was frustrated.
The cause...? GLES obviously changed some flags and float math stopped working on *unaligned* data.
Comrades @ freepascal.ru looked at me like I was nuts (unaligned records? on ARM? That couldn't work even in theory!)
And yet it was working. At least until... Yeah.
So I solved it using {$packrecords 4} and {$ifndef cpuarm}packed{$endif}

imcold
28-04-2017, 03:05 PM
If there's any padding between record's fields because of differently sized datatypes, you could also try reordering the fields. For example, if you mix booleans with integers (which misaligns them if the record is packed), put the integer fields before all the booleans, etc.

phibermon
29-04-2017, 12:15 AM
You're essentially dealing with two devices, the CPU and the GPU - both access structured/ordered data in their own way - so when transferring data between the two? a factor such as record byte alignment can't be assumed in any case where the GPU and CPU could potentially differ. So in all cases on any platform - I would recommend that knowing and managing data alignment be considered part of standard best practice as is knowing and managing platform endiness.