forked from edwinyzh/Delphi-64-bit-compiler-RTL-speed-up_2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD64IPP.pas
206 lines (184 loc) · 5.37 KB
/
D64IPP.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
unit D64IPP;
// 22 Febr 2019 Roberto Della Pasqua www.dellapasqua.com
// 24 March 2021 updated to latest stable Intel Performance Primitives branch
interface
uses
Winapi.Windows;
{$IFDEF WIN64}
const
DPSubset = 'D64IPP.DLL';
{$ENDIF}
{$IFDEF WIN32}
const
DPSubset = 'D32IPP.DLL';
{$ENDIF}
function DZero(Pdst: PByte; Len: NativeUint): Integer; cdecl; external DPSubset name 'ippsZero_8u';
function DCopy(const Psrc: PByte; Pdst: PByte; Len: NativeUint): Integer; cdecl; external DPSubset name 'ippsCopy_8u';
function DMove(const Psrc: PByte; Pdst: PByte; Len: NativeUint): Integer; cdecl; external DPSubset name 'ippsMove_8u';
function DSet(Val: Byte; Pdst: PByte; Len: NativeUint): Integer; cdecl; external DPSubset name 'ippsSet_8u';
function DFind(const Psrc: PByte; Len: NativeUint; const Pfind: PByte; Lenfind: NativeUint; Pindex: PNativeUint): Integer; cdecl; external DPSubset name 'ippsFind_8u';
function DCompare(const Psrc1: PByte; const Psrc2: PByte; Len: NativeInt; Presult: PNativeInt): Integer; cdecl; external DPSubset name 'ippsCompare_8u';
function DUpperCase(const PSrcDst: PByte; Len: NativeUint): Integer; cdecl; external DPSubset name 'ippsUppercaseLatin_8u_I';
function DReplace(const Psrc: PByte; Pdst: PByte; Len: NativeUint; oldVal: Byte; ipp8u: Byte): Integer; cdecl; external DPSubset name 'ippsReplaceC_8u';
implementation
type
TByteArray = array [0 .. 32767] of Byte;
PByteArray = ^TByteArray;
_ansichr = Ansichar;
_WideStr = WideString;
_RawByteStr = RawByteString;
procedure PatchCode(Old, New: Pointer; Size: Integer);
var
NP, IR: DWORD;
I: Integer;
begin
if VirtualProtect(Old, Size, PAGE_EXECUTE_READWRITE, NP) then
begin
for I := 0 to Size - 1 do
PByteArray(Old)^[I] := PByteArray(New)^[I];
VirtualProtect(Old, Size, NP, IR);
FlushInstructionCache(GetCurrentProcess, Old, Size);
end;
end;
procedure RedirectCode(Func, RedirectFunc: Pointer);
var
NewJump: packed record Code: Byte;
Distance: Integer;
end;
begin
if (Func = nil) or (RedirectFunc = nil) then Exit;
NewJump.Code := $E9;
NewJump.Distance := Integer(NativeUint(RedirectFunc) - NativeUint(Func) - Sizeof(NewJump));
PatchCode(Func, @NewJump, Sizeof(NewJump));
end;
procedure Fillchar2(var Dest; Count: NativeUint; Value: _ansichr); inline;
begin
if (Value = #0) then
DZero(@Dest, Count)
else
DSet(Byte(Value), @Dest, Count);
end;
procedure Move2(const Source; var Dest; Count: NativeInt); inline;
begin
if Count > 0 then
DMove(@Source, @Dest, Count);
end;
type
TPosRawFunc = function(const SubStr, Str: _RawByteStr; Offset: Integer = 1): Integer;
TPosWideFunc = function(const SubStr, Str: _WideStr; Offset: Integer = 1): Integer;
TPosUnicodeFunc = function(const SubStr, Str: UnicodeString; Offset: Integer = 1): Integer;
function RetrievePosRawAddr: Pointer;
var
f: TPosRawFunc;
begin
f := Pos;
Result := @f;
end;
function RetrievePosWideAddr: Pointer;
var
f: TPosWideFunc;
begin
f := Pos;
Result := @f;
end;
function RetrievePosUnicodeAddr: Pointer;
var
f: TPosUnicodeFunc;
begin
f := Pos;
Result := @f;
end;
function PosUnicode(const SubStr, Str: UnicodeString; Offset: Integer = 1): Integer;
var
Idx: PByte;
LenR: Integer;
LenS: Integer;
LenB: Integer;
begin
LenS := Length(Str);
LenB := Length(SubStr);
if (LenS < LenB) or (Offset > LenS) or (Offset < 1) then
Result := 0
else
begin
Idx := PByte(Str);
Inc(Idx, (Offset - 1) * 2);
DFind(Idx, (LenS - (Offset - 1)) * 2, PByte(SubStr), LenB * 2, @LenR);
if LenR = -1 then
Result := 0
else
begin
Inc(LenR, (LenS * 2) - ((LenS - Offset + 1) * 2));
Result := Trunc(LenR / 2) + 1;
end;
end;
end;
function PosWide(const SubStr, Str: _WideStr; Offset: Integer = 1): Integer;
var
Idx: PByte;
LenR: Integer;
LenS: Integer;
LenB: Integer;
begin
LenS := Length(Str);
LenB := Length(SubStr);
if (LenS < LenB) or (Offset > LenS) or (Offset < 1) then
Result := 0
else
begin
Idx := PByte(Str);
Inc(Idx, (Offset - 1) * 2);
DFind(Idx, (LenS - (Offset - 1)) * 2, PByte(SubStr), LenB * 2, @LenR);
if LenR = -1 then
Result := 0
else
begin
Inc(LenR, (LenS * 2) - ((LenS - Offset + 1) * 2));
Result := Trunc(LenR / 2) + 1;
end;
end;
end;
function PosRaw(const SubStr, Str: _RawByteStr; Offset: Integer = 1): Integer;
var
Idx: PByte;
LenR: Integer;
LenS: Integer;
LenB: Integer;
begin
LenS := Length(Str);
LenB := Length(SubStr);
if (LenS < LenB) or (Offset > LenS) or (Offset < 1) then
Result := 0
else
begin
Idx := PByte(Str);
Inc(Idx, Offset - 1);
DFind(Idx, LenS - Offset + 1, PByte(SubStr), LenB, @LenR);
if LenR = -1 then
Result := 0
else
begin
Inc(LenR, LenS - (LenS - Offset + 1));
Result := LenR + 1;
end;
end;
end;
{$IFDEF WIN64}
function OrigFillchar: Pointer;
asm
mov rax,offset System.@FillChar
end;
{$ENDIF}
procedure PatchRTL64;
begin
RedirectCode(@System.Move, @Move2);
{$IFDEF WIN64}
RedirectCode(OrigFillchar, @Fillchar2);
{$ENDIF}
RedirectCode(RetrievePosRawAddr, @PosRaw);
RedirectCode(RetrievePosWideAddr, @PosWide);
RedirectCode(RetrievePosUnicodeAddr, @PosUnicode);
end;
initialization
PatchRTL64;
end.