forked from JBontes/FastCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastCompare.pas
2432 lines (2292 loc) · 58.8 KB
/
FastCompare.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit FastCompare;
interface
uses
System.SysUtils;
//Uncomment if you want to test purepascal versions.
// {$define purepascal}
const
DefaultHashSeed = 0;
function CompareFast(const Left, Right: byte): integer; overload; inline;
function CompareFast(const Left, Right: int8): integer; overload; inline;
function CompareFast(const Left, Right: word): integer; overload; inline;
function CompareFast(const Left, Right: int16): integer; overload; inline;
function CompareFast(const Left, Right: cardinal): integer; overload; inline;
function CompareFast(const Left, Right: integer): integer; overload; inline;
function CompareFast(const Left, Right: UInt64): integer; overload; inline;
function CompareFast(const Left, Right: Int64): integer; overload; inline;
function CompareFast(const Left, Right: NativeInt): integer; overload; inline;
function CompareFast(const Left, Right: NativeUInt): integer; overload; inline;
function CompareFast(const Left, Right: AnsiChar): integer; overload; inline;
function CompareFast(const Left, Right: WideChar): integer; overload; inline;
function CompareFast(const Left, Right: UCS4Char): integer; overload; inline;
function CompareFast(const Left, Right: single): integer; overload; inline;
function CompareFast(const Left, Right: Real48): integer; overload; inline;
function CompareFast(const Left, Right: double): integer; overload; inline;
function CompareFast(const Left, Right: extended): integer; overload; inline;
function CompareFast(const Left, Right: Comp): integer; overload; inline;
function CompareFast(const Left, Right: Currency): integer; overload; inline;
function CompareFast(const Left, Right: boolean): integer; overload; inline;
function CompareFast(const Left, Right: wordbool): integer; overload; inline;
function CompareFast(const Left, Right: longbool): integer; overload; inline;
function CompareFast(const Left, Right: bytebool): integer; overload; inline;
function CompareFast(const Left, Right: ShortString): integer; overload; inline;
function CompareFast(const Left, Right: AnsiString): integer; overload; inline;
function CompareFast(const Left, Right: UnicodeString): integer; overload; inline;
function CompareFast(const Left, Right: WideString): integer; overload; inline;
function CompareFast(const Left, Right: RawByteString): integer; overload; inline;
function CompareFast(const Left, Right: UTF8String): integer; overload; inline;
function CompareFast(const Left, Right: pointer): integer; overload; inline;
function CompareFast(const Left, Right: IInterface): integer; overload; inline;
function CompareFast(const Left, Right: TObject): integer; overload; inline;
function Compare_Variant(const Left, Right: pointer): integer;
function BinaryCompare(const Left, Right; Size: integer): integer;
function FastBinaryCompare(const Left, Right; Size: integer): integer;
function BinaryCompare4(const Left, Right: cardinal): integer;
{$IFDEF purepascal}
function BinaryCompare8(const Left, Right: pointer): integer;
{$ELSEIF DEFINED(CPUX64)}
function BinaryCompare8(const Left, Right: UInt64): integer;
{$ELSE !CPUX64}
function BinaryCompare8(const Left, Right: pointer): integer;
{$ENDIF}
function BinaryCompare3(const Left, Right: pointer): integer;
function Compare_DynArray(const Left, Right: pointer; ElementSize: integer): NativeInt;
function Compare_PSn(const Left, Right: OpenString): integer;
{$IFDEF purepascal}inline;{$ELSE}{$IFNDEF CPUX64}inline;{$ENDIF}{$ENDIF}
// For testing purposes
function PascalMurmurHash3(const HashData; Len, Seed: integer): integer;
function MurmurHash3(const HashData; Len: integer; Seed: integer = 0)
: integer;{$IFDEF purepascal}inline;{$ENDIF}
/// FNV1a_Hash_Meiyan is about 30% faster than murmurhash3
function FNV1A_Hash_Meiyan(const HashData; Len: integer; Seed: integer = DefaultHashSeed): integer;
{$IFDEF purepascal}inline;{$ENDIF}
function PascalFNV1A_Hash_Meiyan(const HashData; Len: integer; Seed: integer = DefaultHashSeed): integer;
function BobJenkinsHash(const HashData; Len, Seed: integer): integer; inline;
function xxHash32Calc(const HashData; Len: integer; Seed: integer = 0): integer;
function AsmxxHash32Calc(const HashData; Len: integer; Seed: integer = 0): integer;
function CompareWideStr(const S1, S2: WideString): integer;
function CompareUnicodeStr(const S1, S2: string): integer;
function CompareAnsiStr(const S1, S2: AnsiString): integer;
function SameUnicodeStr(const S1, S2: string): boolean;
function SameWideStr(const S1, S2: WideString): boolean;
function BinaryEquals(const Left, Right: pointer; Size: integer): boolean;
function DynLen(const Arr: pointer): NativeInt; inline;
function Normalize(const X: single): single; overload;
//function Normalize(const X: Real48): Real48; overload; //Real48 is always normalized
function Normalize(const X: double): double; overload;
{$if SizeOf(Extended) = 10}
function Normalize(const X: extended): extended; overload;
{$endif}
implementation
uses
System.Variants, System.Math, System.AnsiStrings, System.Generics.Defaults;
type
PExtRec = ^TExtRec;
TExtRec = packed record
Mant: Int64;
Exp: word
end;
const
SingleExpBits: Integer = $7F800000; { 8 bits }
SingleMantBits: Integer = $007FFFFF;
DoubleExpBits: Int64 = $7FF0000000000000; { 11 bits }
DoubleMantBits: Int64 = $000FFFFFFFFFFFFF;
ExtendedExpBits: word = $7FFF; {15 bits}
ExtendedMantBits: Int64 = -1;
//function IsInfinite(const Value: double): boolean;
//begin
// Result:= ((PInt64(@Value)^ and $7FF0000000000000) = $7FF0000000000000)
// and ((PInt64(@Value)^ and $000FFFFFFFFFFFFF) = $0000000000000000)
//end;
Function IsNANSingle (const Value: single): boolean; inline;
begin
Result:= ((PInteger(@Value)^ and SingleExpBits) = SingleExpBits)
and ((PInteger(@Value)^ and SingleExpBits) <> 0);
end;
function IsNanDouble(const Value: double): boolean; inline;
begin
Result:= ((PInt64(@Value)^ and $7FF0000000000000) = $7FF0000000000000)
and ((PInt64(@Value)^ and $000FFFFFFFFFFFFF) <> 0)
end;
{$if SizeOf(Extended) = 10}
function IsNanExtended(const Value: extended): boolean; inline;
begin
Result:= ((PExtRec(@Value)^.Exp and ExtendedExpBits)= ExtendedExpBits)
and ((PExtRec(@Value)^.Mant and ExtendedMantBits) <> 0);
end;
{$endif}
function IsZeroSingle(const Value: single): boolean; inline;
begin
Result:= (PInteger(@Value)^ and $80000000) = 0;
end;
function IsZeroDouble(const Value: Double): boolean; inline;
begin
Result:= (PInt64(@Value)^ and $000FFFFFFFFFFFFF = 0)
or (PInt64(@Value)^ and $000FFFFFFFFFFFFF = $0008000000000000);
end;
{$if SizeOf(Extended) = 10}
function IsZeroExtended(const Value: Extended): boolean; inline;
begin
Result:= (PExtRec(@Value)^.Exp = 0)
or (PExtRec(@Value)^.Exp = $8000000000000000);
end;
{$endif}
function Normalize(const X: single): single; overload;
begin
if IsNanSingle(x) then Result:= NaN
else if IsZeroSingle(x) then Result:= 0.0
else Result:= x;
end;
//The Real48 type cannot store denormals, NaNs, and infinities (Inf).
//Denormals become zero when stored in a Real48,
//while NaNs and infinities produce an overflow error if an attempt is made
//to store them in a Real48.
//function Normalize(const X: Real48): Real48; overload;
function Normalize(const X: double): double; overload;
begin
if IsNanDouble(x) then Result:= NaN
else if IsZeroDouble(x) then Result:= 0.0
else Result:= x;
end;
{$if SizeOf(Extended) = 10}
function Normalize(const X: extended): extended; overload;
begin
if IsNanExtended(x) then Result:= NaN
else if IsZeroExtended(x) then Result:= 0.0
else Result:= x;
end;
{$endif}
function Compare_Variant(const Left, Right: pointer): integer;
var
l, r: Variant;
lAsString, rAsString: string;
varTypeLeft, varTypeRight: integer;
begin
Result:= 0; // Avoid warning.
l:= PVariant(Left)^;
r:= PVariant(Right)^;
try
case System.Variants.VarCompareValue(l, r) of
vrEqual: Exit(0);
vrLessThan: Exit( -1);
vrGreaterThan: Exit(1);
vrNotEqual: begin
if VarIsEmpty(l) or VarIsNull(l) then Exit(1)
else Exit( -1);
end;
end;
except // if comparison failed with exception, compare as string.
try
// Prevent type conversions
varTypeLeft:= VarType(l) and varTypeMask;
varTypeRight:= VarType(r) and varTypeMask;
if ((varTypeLeft = varUString) or (varTypeRight = varUString)) or
((varTypeLeft = varOleStr) or (varTypeRight = varOleStr)) then begin
Result:= CompareWideStr(WideString(l), WideString(r));
end else if (varTypeLeft = varString) or (varTypeRight = varString) then begin
Result:= CompareAnsiStr(AnsiString(l), AnsiString(r));
end else begin
lAsString:= PVariant(Left)^;
rAsString:= PVariant(Right)^;
Result:= CompareUnicodeStr(lAsString, rAsString);
end;
except // if comparison fails again, compare bytes.
Result:= FastBinaryCompare(Left, Right, SizeOf(Variant));
end;
end;
end;
function BinaryCompare4(const Left, Right: cardinal): integer;
{$IFDEF purepascal}
var
i: integer;
begin
Result:= (integer(Left > Right) - integer(Left < Right));
end;
{$ELSE !PurePascal}
{$IFDEF CPUX64}
asm
.NOFRAME
// Left: RCX
// Right: RDX
// preserve:RBX, RBP, RDI, RSI, R12-R15
// bswap ECX
// bswap EDX
cmp ECX,EDX
sbb EAX,EAX
cmp EDX,ECX
adc EAX,0
end;
{$ENDIF}
{$IFDEF CPUX86}
asm
// Left: EAX
// Right: EDX
// Size: ECX
cmp EAX,EDX
seta AL
movzx EAX,AL
sbb EAX,0
end;
{$ENDIF}
{$ENDIF !PurePascal}
{$IFDEF purepascal}
function BinaryCompare8(const Left, Right: pointer): integer;
var
pl, pr: PByte;
i: integer;
begin
pl:= Left;
pr:= Right;
for i:= 0 to 7 do begin
Result:= pl^ - pr^;
if Result <> 0 then Exit;
Inc(pl);
Inc(pr);
end;
Result:= 0;
end;
{$ELSE !PurePascal}
{$IFDEF CPUX64}
function BinaryCompare8(const Left, Right: UInt64): integer;
asm
.NOFRAME
// Left: RCX
// Right: RDX
// preserve:RBX, RBP, RDI, RSI, R12-R15
bswap RCX
bswap RDX
xor EAX,EAX
cmp RCX,RDX
seta AL
sbb EAX,0
end;
{$ENDIF}
{$IFDEF CPUX86}
function BinaryCompare8(const Left, Right: pointer): integer;
asm
// Left: EAX
// Right: EDX
// Size: ECX
push EBX
mov EBX, [EAX]
mov ECX, [EDX]
bswap EBX
bswap ECX
cmp EBX, ECX
jnz @done
mov EBX, [EAX+4]
mov ECX, [EDX+4]
bswap EBX
bswap ECX
cmp EBX, ECX
@done:
sbb EAX,EAX
cmp ECX,EBX
adc EAX,0
pop EBX
end;
{$ENDIF}
{$ENDIF !PurePascal}
function BinaryCompare3(const Left, Right: pointer): integer;
{$IFDEF PurePascal}
var
pl, pr: PByte;
i: integer;
begin
pl:= Left;
pr:= Right;
for i:= 0 to 2 do begin
Result:= pl^ - pr^;
if Result <> 0 then Exit;
Inc(pl);
Inc(pr);
end;
Result:= 0;
end;
{$ELSE !PurePascal}
{$IFDEF CPUX64}
asm // Left RCX, Right RDX
movzx R8, word ptr [RCX]
shl R8, 16
mov R8b, byte ptr [RCX+2]
bswap R8
movzx R9, word ptr [RCX]
shl R9, 16
mov R9b, byte ptr [RCX+2]
bswap R9
cmp R8, R9
sbb EAX, EAX
cmp R9, R8
adc EAX, 0
end;
{$ELSE CPUX86}
asm
push EBX
movzx EBX, word ptr [EAX]
shl EBX, 16
mov BL, byte ptr [EAX+2]
bswap EBX
movzx ECX, word ptr [EDX]
shl ECX, 16
mov CL, byte ptr [EDX+2]
bswap ECX
cmp EBX, ECX
sbb EAX, EAX
cmp ECX, EBX
adc EAX, 0
pop EBX
end;
{$ENDIF}{$ENDIF}
// Size is not 1,2,3,4 or 8
function BinaryCompare(const Left, Right; Size: integer): integer;
{$IFDEF purepascal} // pure pascal
type
TBSwap = record
case integer of
1: (Arr: array[0 .. SizeOf(NativeInt)- 1] of byte);
2: (ni: NativeInt);
3: (i: integer);
end;
var
Same: boolean;
i: integer;
Lq, Rq: PNativeUInt;
Li, Ri: PInteger;
SwapL, SwapR: TBSwap;
label
DifferentInteger, DifferentNativeInt;
begin
if (@Left = @Right) then Exit(0)
else if @Left = nil then Exit(-1)
else if @Right = nil then Exit(1);
case Size of
0: Result:= 0;
1: Result:= PByte(@Left)^ - PByte(@Right)^;
2, 3: begin
for i:= 0 to Size - 1 do begin
Result:= PByte(@Left)[i] - PByte(@Right)[i];
if Result <> 0 then Exit;
end;
end
else begin
if (SizeOf(NativeUInt) > SizeOf(integer)) and (Size < SizeOf(NativeUInt)) then begin
Li:= @Left;
Ri:= @Right;
for i:= 0 to (Size div SizeOf(integer)) - 1 do begin
Same:= Li^ = Ri^;
if not(Same) then goto DifferentInteger;
Inc(Li);
Inc(Ri);
end;
// Unaligned test for few remaining bytes
NativeInt(Li):= NativeInt(Li) + (Size and (SizeOf(integer)- 1)) - SizeOf(integer);
NativeInt(Ri):= NativeInt(Ri) + (Size and (SizeOf(integer)- 1)) - SizeOf(integer);
if (Li^ = Ri^) then Result:= 0
else begin
DifferentInteger:
SwapL.i:= Li^;
SwapR.i:= Ri^;
for i:= 0 to 3 do begin
Result:= SwapL.Arr[i] - SwapR.Arr[i];
if Result <> 0 then Exit;
end;
end;
end else begin
Lq:= @Left;
Rq:= @Right;
for i:= 0 to (Size div SizeOf(NativeUInt)) - 1 do begin
Same:= Lq^ = Rq^;
if not(Same) then goto DifferentNativeInt;
Inc(Lq);
Inc(Rq);
end;
// Unaligned test for few remaining bytes
NativeUInt(Lq):= NativeUInt(Lq) + (Size and (SizeOf(NativeUInt)- 1)) - SizeOf(NativeUInt);
NativeUInt(Rq):= NativeUInt(Rq) + (Size and (SizeOf(NativeUInt)- 1)) - SizeOf(NativeUInt);
if (Lq^ = Rq^) then Result:= 0
else begin
DifferentNativeInt:
SwapL.ni:= Lq^;
SwapR.ni:= Rq^;
for i:= 0 to SizeOf(NativeInt)- 1 do begin
Result:= SwapL.Arr[i] - SwapR.Arr[i];
if Result <> 0 then Exit;
end;
end;
end; { else }
end; { case else }
end; { case }
end;
{$ELSE !PurePascal}
{$IFDEF CPUX64}
asm
.NOFRAME
cmp RCX,RDX
jz @Equal
test RCX,RDX
jz @PossibleNilPointer
@NoNil:
xchg RSI,RAX
// Left: RCX
// Right: RDX
// Size: R8 preserve:RBX, RBP, RDI, RSI, R12-R15
mov RSI, RCX
xchg RDI, RDX
mov RCX, R8
repe cmpsb
xchg RSI, RAX
xchg RDI, RDX
@Equal:
seta AL
movzx EAX,AL
sbb EAX,0
ret
@PossibleNilPointer:
lea R9, [RCX-1]
lea R10,[RDX-1]
mov R11,R10
or R11,R9
jns @NoNil
cmp RCX,RDX
seta AL
movzx EAX,AL
sbb EAX,0
end;
{$ENDIF}
{$IFDEF CPUX86}
asm
// Left: EAX
// Right: EDX
// Size: ECX
cmp EAX,EDX
jz @equal
test EAX,EDX
jz @PossibleNilPointer
@NoNil:
xchg ESI,EAX
xchg EDI,EDX
repe cmpsb
xchg ESI,EAX
xchg EDI,EDX
@equal:
seta AL
movzx EAX,AL
sbb EAX,0
ret
@PossibleNilPointer:
or EAX,EAX
jnz @FirstParamNotNil
dec EAX
ret
@FirstParamNotNil:
or EDX,EDX
jnz @NoNil
lea EAX,[EDX+1]
ret
end;
{$ENDIF}
{$ENDIF !PurePascal}
// Cannot be used for comparisons shorter than 4 bytes.
function BinaryEquals(const Left, Right: pointer; Size: integer): boolean;
{$IFDEF purepascal}
var
i: integer;
Lq, Rq: PNativeUInt;
Li, Ri: PInteger;
begin
if (SizeOf(NativeUInt) > SizeOf(integer)) and (Size < SizeOf(NativeUInt)) then begin
Li:= Left;
Ri:= Right;
if (Li = Ri) then Exit(True)
else if (Li = nil) or (Ri = nil) then Exit(False);
for i:= 0 to (Size div SizeOf(integer)) - 1 do begin
Result:= Li^ = Ri^;
if not(Result) then Exit;
Inc(Li);
Inc(Ri);
end;
// Unaligned test for few remaining bytes
NativeInt(Li):= NativeInt(Li) + (Size and (SizeOf(integer)- 1)) - SizeOf(integer);
NativeInt(Ri):= NativeInt(Ri) + (Size and (SizeOf(integer)- 1)) - SizeOf(integer);
Result:= Li^ = Ri^;
end else begin
Lq:= Left;
Rq:= Right;
if (Lq = Rq) then Exit(True)
else if (Lq = nil) or (Rq = nil) then Exit(False);
for i:= 0 to (Size div SizeOf(NativeUInt)) - 1 do begin
Result:= Lq^ = Rq^;
if not(Result) then Exit;
Inc(Lq);
Inc(Rq);
end;
// Unaligned test for few remaining bytes
NativeUInt(Lq):= NativeUInt(Lq) + (Size and (SizeOf(NativeUInt)- 1)) - SizeOf(NativeUInt);
NativeUInt(Rq):= NativeUInt(Rq) + (Size and (SizeOf(NativeUInt)- 1)) - SizeOf(NativeUInt);
Result:= Lq^ = Rq^;
end;
end;
{$ELSE !PurePascal}
{$IFDEF CPUX64}
// RCX: Left
// RDC: Right
// R8: size
asm
.NOFRAME
cmp RCX,RDX
jz @equal
test RCX,RDX
jz @PossibleNilPointer
@NoNil:
cmp R8,4
jns @MoreThan4bytes
xor RAX,RAX
xor R9,R9
dec R8
jz @Compare1
movzx EAX, word ptr [RCX+R8-1]
movzx R9d, word ptr [RDX+R8-1]
dec R8
jz @Confront
shl EAX,16
shl R9d,16
@compare1:
mov AL, [RCX]
mov R9b,[RDX]
@Confront:
xor EAX,R9d
setz AL
ret
@MoreThan4Bytes:
neg R8
// jz @equal
sub RCX, R8
sub RDX, R8
@loop8:
add R8,8
jns @check4
mov RAX,[RCX+R8-8]
xor RAX,[RDX+R8-8]
jz @loop8
@different:
xor EAX,EAX
ret
@check4:
sub R8,4
jg @smaller
mov EAX,[RCX+R8-4]
xor EAX,[RDX+R8-4]
jnz @different
@smaller:
and R8,-4
jz @equal
mov EAX,[RCX+R8]
xor EAX,[RDX+R8]
setz AL
ret
@equal:
mov EAX,1
ret
@PossibleNilPointer:
lea R9, [RCX-1]
lea R10,[RDX-1]
mov R11,R10
or R11,R9
jns @NoNil
xor EAX,EAX
end;
{$ELSE !CPUX64}
// EAX: Left
// EDX: Right
// ECX: size
asm
cmp EAX,EDX
jnz @Skip
mov AL,1
ret
@Skip:
test EAX,EDX
jz @PossibleNilPointer
@NoNil:
push EBX
cmp ECX,3
jg @MoreThan3Bytes
push ESI
dec ECX
jz @OneMoreByte
movzx EBX, word ptr [EAX+ECX-1]
movzx ESI, word ptr [EDX+ECX-1]
dec ECX
xchg ESI,ECX
jz @DoneLoading
shl EBX,8
shl ECX,8
@OneMoreByte:
mov BL, [EAX]
mov CL, [EDX]
@DoneLoading:
xor EBX,ECX
setz AL
pop ESI
pop EBX
ret
@MoreThan3Bytes:
neg ECX
// jz @equal
sub EAX, ECX
sub EDX, ECX
@loop8:
add ECX,8
jns @check4
mov EBX,[EAX+ECX-8]
xor EBX,[EDX+ECX-8]
jnz @different
mov EBX,[EAX+ECX-8+4]
xor EBX,[EDX+ECX-8+4]
jz @loop8
@different:
xor EAX,EAX
pop EBX
ret
@check4:
sub ECX,4
jg @smaller
mov EBX,[EAX+ECX-4]
xor EBX,[EDX+ECX-4]
jnz @different
@smaller:
and ECX,-4
jz @equal
mov EBX,[EAX+ECX]
xor EBX,[EDX+ECX]
jnz @different
@equal:
mov AL,1
pop EBX
ret
@PossibleNilPointer:
or EAX,EAX
jnz @FirstParamNotNil
ret
@FirstParamNotNil:
or EDX,EDX
jnz @NoNil
mov EAX,EDX
end;
{$ENDIF}
{$ENDIF}
function FastBinaryCompare(const Left, Right; Size: integer): integer;
{$IFDEF purepascal}
var
i: integer;
begin
for i:= 0 to Size - 1 do begin
Result:= PByte(Left)[i] - PByte(Right)[i];
if Result <> 0 then Exit;
end;
end;
{$ELSE !PurePascal}
{$IFDEF CPUX64}
asm
.NOFRAME
// Left: RCX
// Right: RDX
// Size: R8 preserve:RBX, RBP, RDI, RSI, R12-R15
xor RAX, RAX
mov R11, R8
and R11, $7
xor R8, R11
jz @loop2
neg R8
sub RCX, R8
sub RDX, R8
@loop:
mov RAX, [RCX+R8]
bswap RAX
mov R9, [RDX+R8]
bswap R9
sub RAX,R9
jnz @different
add R8, 8
jnz @loop
@loop2:
dec R11
js @same
movzx RAX, byte ptr [RCX+R8]
movzx R9, byte ptr [RDX+R8]
inc R8
sub RAX,R9
jz @loop2
@different:
sbb RAX,RAX
sbb RAX,-1
@same:
end;
{$ENDIF}
{$IFDEF CPUX86}
asm
// //Left: EAX
// //Right: EDX
// //Size: ECX
push EBX
push EDI
push ESI
xor ESI, ESI // In case Size = 0 and fallthrough to @same occurs.
mov EBX, ECX
and EBX, $3
xor ECX, EBX
jz @loop2
neg ECX
sub EAX, ECX
sub EDX, ECX
@loop:
mov ESI, [EAX+ECX]
bswap ESI
mov EDI, [EDX+ECX]
bswap EDI
sub ESI,EDI
jnz @different
add ECX, 4
jnz @loop
@loop2:
dec EBX
js @same
movzx ESI, byte ptr [EAX+ECX]
movzx EDI, byte ptr [EDX+ECX]
inc ECX
sub ESI,EDI
jz @loop2
@different:
sbb ESI,ESI
sbb ESI,-1
@same:
mov EAX,ESI
pop ESI
pop EDI
pop EBX
end;
{$ENDIF CPUX86}
{$ENDIF !PurePascal}
function DynLen(const Arr: pointer): NativeInt; inline;
begin
if Arr = nil then Result:= 0
else Result:= PNativeInt(PByte(Arr) - SizeOf(NativeInt))^;
end;
function Compare_DynArray(const Left, Right: pointer; ElementSize: integer): NativeInt;
var
LenL, LenR, lenDiff: NativeInt;
begin
LenL:= DynLen(Left);
LenR:= DynLen(Right);
lenDiff:= LenL - LenR;
LenL:= Min(LenL, LenR);
Result:= FastBinaryCompare(Left^, Right^, ElementSize * LenL);
if Result = 0 then Result:= lenDiff;
end;
function Compare_PSn(const Left, Right: OpenString): integer;
{$IFDEF purepascal}
begin
Result:= (integer(Left > Right) - integer(Left < Right));
end;
{$ELSE}
{$IFDEF CPUX86}
begin
Result:= (integer(Left > Right) - integer(Left < Right));
end;
{$ELSE}
asm
{ ->RCX = Pointer to left string }
{ RDX = Pointer to right string }
XCHG RBX,R8 // Push RBX
MOV R9,RCX
MOV R10,RDX
// Are both S1 and S2 dword aligned?
OR ECX,EDX
TEST ECX,$3
JZ @AlignedStrings
@UnAlignedStrings:
MOVZX EAX, byte ptr [R9] // Length S1
MOVZX ECX, byte ptr [R10] // Length S2
INC R9
INC R10
// ECX:= min(len1, len2)
SUB EAX,ECX { Diff = len1 - len2 }
MOV R11,RAX //save difference
SBB EBX,EBX //if len1 > len2 then EBX = 0 else EBX = -1
AND EAX,EBX //if len1 > len2 then Diff:= 0
ADD ECX,EAX //Diff:= Diff + len2 (a-b+b = a; 0+b=b
MOV RAX,R11
NEG RCX // Count down
SUB R9, RCX
SUB R10,RCX
CMP RCX,-3
JS @@LongLoop
@ShortString:
CMP ECX,0
JZ @@Exit
CMP ECX,-1
JNZ @LengthNot1
MOVZX EDX, byte ptr [R9+ RCX]
MOVZX EBX, byte ptr [R10+RCX]
JMP @@CompareRest
@LengthNot1:
CMP ECX,-2
JNZ @LengthIs3
MOVZX EDX, word ptr [R9+ RCX]
MOVZX EBX, word ptr [R10+RCX]
JMP @@CompareRest
@LengthIs3:
MOV EDX,[R9+ RCX-1]
MOV EBX,[R10+RCX-1]
SHR EDX,8
SHR EBX,8
JMP @@CompareRest
@AlignedStrings:
MOV EDX, [R9]
MOV EBX, [R10]
MOVZX EAX,DL
MOVZX ECX,BL
SUB EAX,ECX { EAX = len1 - len2 }
JA @@Skip1
ADD ECX,EAX { ECX = len2 + (len1 - len2) = len1 }
@@Skip1:
SHR EBX,8
SHR EDX,8
NEG RCX
// JZ @@Exit
CMP EBX,EDX
JNE @@MisMatch
ADD RCX,3
SUB R9, RCX
SUB R10,RCX
// MOV R11,RCX
@@longLoop:
MOV EDX,[R9 +RCX+4]
MOV EBX,[R10+RCX+4]
CMP EDX,EBX
JNE @@misMatch
ADD RCX,4
JNS @@exit
MOV EDX,[R9 +RCX+4]
MOV EBX,[R10+RCX+4]
CMP EDX,EBX
JNE @@misMatch
ADD RCX,4
JS @@longloop
@@exit:
XCHG RBX,R8
RET
@@compareRest:
// RCX = 4/0 -> done
// RCX = 3 -> 1 byte
// RCX = 2 -> 2 bytes
// RCX = 1 -> 3 bytes
TEST RCX,$3
JZ @@Exit
LEA ECX,[ECX*8]
SHL EDX,CL
SHL EBX,CL
BSWAP EDX
BSWAP EBX
CMP EDX,EBX
JNE @@Different
// if Same, then EAX is the difference in length
XCHG RBX,R8
RET
@@misMatch:
ADD RCX,4
JG @@CompareRest
BSWAP EDX
BSWAP EBX
CMP EDX,EBX
@@Different:
SBB EAX,EAX
SBB EAX,-1
XCHG RBX,R8
end;
{$ENDIF}{$ENDIF}
function CompareWideStr(const S1, S2: WideString): integer; overload;
// {$ifdef PurePascal}
var
i: NativeInt;
c1, c2: Char;
begin
if pointer(S1) = pointer(S2) then Exit(0);
if pointer(S1) = nil then Exit( -1);
if pointer(S2) = nil then Exit(1);
i:= low(S1);
while (True) do begin
c1:= S1[i];
c2:= S2[i];
Result:= integer(c1 > c2) - integer(c1 < c2);
if (integer(Result = 0) and integer(c1 <> #0) and integer(c2 <> #0)) = 0 then Exit;
Inc(i);
end;
end;
// {$else !PurePascal}
// {$ifdef CPUX64}
// asm
//
// end;
// {$else !CPUX64}
// asm
//
// end;
// {$endif}{$endif}
function SameWideStr(const S1, S2: WideString): boolean; overload;
{$IFDEF PurePascal}
const
Forever = False;
var
i: integer;
begin
Result:= pointer(S1) = pointer(S2);
if Result then Exit;
Result:= not((pointer(S1) = nil) or (pointer(S2) = nil));
if not(Result) then Exit;
for i:= low(S1) to high(integer) do begin
Result:= S1[i] = S2[i];
if not(Result) or (S1[i] = #0) then Exit;
end;
end;
{$ELSE}
{$IFDEF CPUX64}
asm
.NOFRAME
cmp RCX, RDX
jz @done // pointers are the same