From 30e3d93d5716b60ca4fa07319725a74d6c974830 Mon Sep 17 00:00:00 2001 From: Rika Ichinose Date: Fri, 6 Jan 2023 17:46:12 +0300 Subject: [PATCH] Make generic Index* faster and denser. --- rtl/inc/generic.inc | 149 ++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 95 deletions(-) diff --git a/rtl/inc/generic.inc b/rtl/inc/generic.inc index 23d49fd7ee..320531728f 100644 --- a/rtl/inc/generic.inc +++ b/rtl/inc/generic.inc @@ -316,23 +316,18 @@ var psrc,pend : pbyte; begin psrc:=@buf; + pend:=psrc+len; { simulate assembler implementations behaviour, which is expected } { fpc_pchar_to_ansistr in astrings.inc } if (len < 0) or - (psrc+len < psrc) then - pend:=pbyte(high(PtrUInt)-sizeof(byte)) + (pend < psrc) then + pend:=pbyte(high(PtrUInt)-PtrUint(sizeof(byte))); + while (psrcb) do + inc(psrc); + if psrc high(PtrInt)) or - (psrc+len < psrc) then - pend:=pword(high(PtrUInt)-sizeof(word)) - else - pend:=psrc+len; + if not ( + (len >= 0) and + { is this ever false? } + (len <= high(PtrInt))) or + (pend < psrc) then + pend:=pword(high(PtrUInt)-PtrUint(sizeof(word))); {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT} if (ptruint(psrc) mod 2)<>0 then - while psrc=0 so avoid handling of negative values } - result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(word); - exit; - end; - inc(psrc); - end + while (psrcb) do + inc(psrc) else {$endif FPC_REQUIRES_PROPER_ALIGNMENT} - while psrc=0 so avoid handling of negative values } - result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(word); - exit; - end; - inc(psrc); - end; - result:=-1; + while (psrcb) do + inc(psrc); + if psrc=0 so avoid handling of negative values } + result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(word) + else + result:=-1; end; {$endif not FPC_SYSTEM_HAS_INDEXWORD} @@ -387,39 +370,27 @@ var psrc,pend : pdword; begin psrc:=@buf; + pend:=psrc+len; { simulate assembler implementations behaviour, which is expected } { fpc_pchar_to_ansistr in astrings.inc } - if (len < 0) or - (len > high(PtrInt) div 2) or - (psrc+len < psrc) then - pend:=pdword(high(PtrUInt)-PtrUInt(sizeof(dword))) - else - pend:=psrc+len; + if not ( + (len >= 0) and + (len <= high(PtrInt) div 2)) or + (pend < psrc) then + pend:=pdword(high(PtrUInt)-PtrUInt(sizeof(dword))); {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT} if (ptruint(psrc) mod 4)<>0 then - while psrc=0 so avoid handling of negative values } - result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(DWord); - exit; - end; - inc(psrc); - end + while (psrcb) do + inc(psrc) else {$endif FPC_REQUIRES_PROPER_ALIGNMENT} - while psrc=0 so avoid handling of negative values } - result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(DWord); - exit; - end; - inc(psrc); - end; - result:=-1; + while (psrcb) do + inc(psrc); + if psrc=0 so avoid handling of negative values } + result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(DWord) + else + result:=-1; end; {$endif not FPC_SYSTEM_HAS_INDEXDWORD} @@ -430,39 +401,27 @@ var psrc,pend : pqword; begin psrc:=@buf; + pend:=psrc+len; { simulate assembler implementations behaviour, which is expected } { fpc_pchar_to_ansistr in astrings.inc } - if (len < 0) or - (len > high(PtrInt) div 4) or - (psrc+len < psrc) then - pend:=pqword(high(PtrUInt)-PtrUInt(sizeof(qword))) - else - pend:=psrc+len; + if not ( + (len >= 0) and + (len <= high(PtrInt) div 4)) or + (pend < psrc) then + pend:=pqword(high(PtrUInt)-PtrUInt(sizeof(qword))); {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT} if (ptruint(psrc) mod 8)<>0 then - while psrc=0 so avoid handling of negative values } - result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(QWord); - exit; - end; - inc(psrc); - end + while (psrcb) do + inc(psrc) else {$endif FPC_REQUIRES_PROPER_ALIGNMENT} - while psrc=0 so avoid handling of negative values } - result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(QWord); - exit; - end; - inc(psrc); - end; - result:=-1; + while (psrcb) do + inc(psrc); + if psrc=0 so avoid handling of negative values } + result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(QWord) + else + result:=-1; end; {$endif not FPC_SYSTEM_HAS_INDEXQWORD}