[fpc-devel] [Suggestion] Enumeration range-check intrinsic

J. Gareth Moreton gareth at moreton-family.com
Fri Jul 5 07:11:42 CEST 2019


Note, with "is" or an intrinsic that only does a range check, it can be 
made very fast when platform-specific.  For example, for "Result := 
(Value is TMyEnum);", under x86, if "l" is the Low value and "h" is the 
High value, then the assembly becomes this (remember that l and h are 
constants, so h - l is also constant and determined at compile time):

MOV   REG, Value
SUB   REG, l
CMP   REG, (h - l)
SETNA Result

If l = 0, which is very common for most enumerations, then the assembly 
is even simpler, not requiring a temporary register:

CMP   Value, h
SETNA Result

For "If (Value is TMyEnum) then", the SETNA instruction becomes a 
conditional jump.

In this instance, the intrinsic's function is similar to how "Inc(X);" 
is a shorthand for "X := X + 1;", there for convenience more than 
anything but traditionally able to produce more optimal code.

Gareth aka. Kit


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the fpc-devel mailing list