[fpc-devel] Internal "Signed wrap" function

J. Gareth Moreton gareth at moreton-family.com
Sat Dec 2 08:58:13 CET 2023


Hi everyone,

I'm just fixing a bugged optimisation, and I came across a situation 
where I need to essentially do a "signed wrap" of a constant.  At one 
point I'm optimising "subl $-12,%eax", "addl $1,%eax" into a single 
instruction, but to make sure (untrapped) overflows are handled 
correctly, the calculated constant has to be wrapped modulo 32 (since 
%eax is a 32-bit constant).  Normally this is as simple as performing a 
bitwise AND against $FFFFFFFF, but it produces the number 4294967285 
rather than the more compact -11 (this is because the operands are 
stored as 64-bit integers within the compiler).  Storing the smaller 
negative number will most likely result in a smaller number of bytes in 
the final machine code as well as avoid any potential assembler errors.

Currently I'm just using a local function to perform the sign extension 
of arbitrary length (since I have to extend either an 8-bit, 16-bit or 
32-bit value depending on the operand size). Does the compiler contain 
such a function already in a utility unit, and if not, would one be 
welcomed?

Kit



More information about the fpc-devel mailing list