I wasn't aware of the 'absolute' keyword
Be wary it doesn't always work. If you have register variables optimization on, the compiler sometimes gets confused.
Example: my attempt to implement Carmack's fast inverse square root.
Code:
function FastInverseSquareRoot(a: float): float; inline;
var
i: longint absolute Result;
begin
Result:= a;
i:= $5f3759df - (i shr 1);
Result*= 1.5 - (a * 0.5 * Result * Result);
Result*= 1.5 - (a * 0.5 * Result * Result);
end;
- it failed craptacularly. The compiler did not give an error but generated totally rubbish code seqence (I turned on ASM output to check).
So I had to settle on
Code:
// https://en.wikipedia.org/wiki/Fast_inverse_square_root
function FastInverseSquareRoot(a: float): float; inline;
var
i: longint;// absolute Result; //code generator FAILS to marry SSE2 and general-purpose registers
begin
//Result:= a;
i:= longint(pointer(@a)^);
i:= $5f3759df - (i shr 1);
Result:= float(pointer(@i)^);
Result*= 1.5 - (a * 0.5 * Result * Result);
Result*= 1.5 - (a * 0.5 * Result * Result);
end;
P.S. So I'd group absolute together with goto: necessary for backward compatibility but not recommended to use unless you have a very good understanding of processes involved.
Bookmarks