Did you know that `TypeInfo(T)` is an intrinsic since XE7?
Did you know that `TypeInfo(T)` is an intrinsic since XE7?
The following code:
program Project45;
{$APPTYPE CONSOLE}
var
A,B: byte;
type
TX = type byte;
TTest = class
class function A(const a,b: byte): boolean; static; inline;
end;
class function TTest.a(const A,B: byte): boolean;
begin
if TypeInfo(T) = TypeInfo(byte) then result:= (a > b)
else if TypeInfo(T) = TypeInfo(shortint) then result:= (a < b)
else if GetTypeKind(T) = tkInteger then result:= (a = b)
else Result:= false;
end;
begin
a:= 1; b:= 2;
TTest.a(a,b);
TTest.a(a,b);
TTest.a(a,b);
end.
Generates this:
Project45.dpr.21: a:= 1; b:= 2;
004050D4 B001 mov al,$01
004050D6 B202 mov dl,$02
Project45.dpr.22: TTest.a(a,b);
004050D8 3AD0 cmp dl,al
004050DA 0F92C1 setb cl
Project45.dpr.23: TTest.a(a,b);
004050DD 3AD0 cmp dl,al
004050DF 0F97C1 setnbe cl
Project45.dpr.24: TTest.a(a,b);
004050E2 3AD0 cmp dl,al
004050E4 0F94C0 setz al
Project45.dpr.25: end.
004050E7 E870E5FFFF call @Halt0
No calls to TypeInfo or GetTypeKind in sight.
However subclassed types are not equal to built-in ones.
Only GetTypeKind can probe them. And GetTypeKind fails to differentiate between signed and unsigned types :-(.
Still this is a win for generic inlining.
The following code:
program Project45;
{$APPTYPE CONSOLE}
var
A,B: byte;
type
TX = type byte;
TTest
class function A(const a,b: byte): boolean; static; inline;
end;
class function TTest
begin
if TypeInfo(T) = TypeInfo(byte) then result:= (a > b)
else if TypeInfo(T) = TypeInfo(shortint) then result:= (a < b)
else if GetTypeKind(T) = tkInteger then result:= (a = b)
else Result:= false;
end;
begin
a:= 1; b:= 2;
TTest
TTest
TTest
end.
Generates this:
Project45.dpr.21: a:= 1; b:= 2;
004050D4 B001 mov al,$01
004050D6 B202 mov dl,$02
Project45.dpr.22: TTest
004050D8 3AD0 cmp dl,al
004050DA 0F92C1 setb cl
Project45.dpr.23: TTest
004050DD 3AD0 cmp dl,al
004050DF 0F97C1 setnbe cl
Project45.dpr.24: TTest
004050E2 3AD0 cmp dl,al
004050E4 0F94C0 setz al
Project45.dpr.25: end.
004050E7 E870E5FFFF call @Halt0
No calls to TypeInfo or GetTypeKind in sight.
However subclassed types are not equal to built-in ones.
Only GetTypeKind can probe them. And GetTypeKind fails to differentiate between signed and unsigned types :-(.
Still this is a win for generic inlining.
Comments
Post a Comment