AngusJohnson / Image32

An extensive 2D graphics library written in Delphi Pascal
Boost Software License 1.0
137 stars 31 forks source link

Negative index on array pointer can be unsupported #72

Closed tomwiel closed 3 months ago

tomwiel commented 3 months ago

This issue is found only in latest source (from July 7). Contrary to common practice over the last decades, negative index on static-array pointer (eg. buf[ index]) is unsupported by certain compilers (at least 64bit-FPC): To prevent wrong code generation, Range Checking can't be ignored on static array pointer. for reference: https://github.com/graphics32/graphics32/issues/51

If I reuse the existing type names, then supported alternative types look like this: (Range Checking can stay enabled):

type
{$PointerMath on}  // support of negative index
PStaticByteArray = ^Byte;  // use this instead of PByteArray
PStaticDoubleArray = ^double;
PStaticInt64Array = ^int64; 
PStaticColor32Array = ^TColor32;
{$PointerMath off} 
ahausladen commented 3 months ago

I would have declared those types as you have, but Delphi 7, which is still supported by Image32, doesn't have POINTERMATH. I found the odd/unexpected array-indexing behavior of FPC while testing the code in FPC and changed the index variable from Integer to NativeInt, doing the sign extension for the FPC compiler. (I read through the FPC bug report and well, forcing one to use a workaround, is an interesting solution)

The code could be IFDEF'ed for Delphi 7-2006. I doubt that Image32 will drop those old Delphi versions. Using a PointerMath PByte instead of the "abomination" PByte = PChar would remove all those Ord(b^).