unit FastcodeMinMax; {Uncomment to Allow Conditional Moves (Pentium Pro and Above)} //{$DEFINE AllowCMOV} interface //Need Functions for ShortInt, SmallInt, Word, Byte function Max(A, B: Integer ): Integer; overload; function Max(A, B: Cardinal): Cardinal; overload; function Max(A, B: Int64 ): Int64; overload; function Max(A, B: Single ): Single; overload; function Max(A, B: Double ): Double; overload; function Max(A, B: Extended): Extended; overload; function Min(A, B: Integer ): Integer; overload; function Min(A, B: Cardinal): Cardinal; overload; function Min(A, B: Int64 ): Int64; overload; function Min(A, B: Single ): Single; overload; function Min(A, B: Double ): Double; overload; function Min(A, B: Extended): Extended; overload; implementation function Max(A, B: Integer): Integer; overload; asm {$IFDEF AllowCMOV} cmp edx, eax cmovg eax, edx {$ELSE} xor ecx, ecx sub edx, eax setl cl sub ecx, 1 and edx, ecx add eax, edx {$ENDIF} end; function Max(A, B: Cardinal): Cardinal; overload; asm {$IFDEF AllowCMOV} cmp edx,eax cmova eax,edx {$ELSE} xor ecx, ecx sub eax, edx adc ecx, -1 and eax, ecx add eax, edx {$ENDIF} end; function Max(A, B: Int64): Int64; overload; asm xor ecx, ecx mov eax, [ebp+$08] mov edx, [ebp+$0C] sub eax, [ebp+$10] sbb edx, [ebp+$14] setl cl mov eax, [ebp+8*ecx+$08] mov edx, [ebp+8*ecx+$0C] end; function Max(A, B: Single): Single; overload; asm fld B fcomp A fstsw ax and eax, $100 shr eax, 6 {eax = 0 or 4} fld dword ptr [ebp+eax+8] end; function Max(A, B: Double): Double; overload; asm fld A fld B fcomi st(0),st(1) jb @@Done ffree st(1) pop ebp ret 16 @@Done: fxch ffree st(1) end; function Max(A, B: Extended): Extended; overload; asm fld A fld B fcomi st(0), st(1) jb @@Done ffree st(1) pop ebp ret $18 @@Done: fxch ffree st(1) end; function Min(A, B: Integer): Integer; overload; asm {$IFDEF AllowCMOV} cmp edx, eax cmovl eax, edx {$ELSE} xor ecx, ecx sub edx, eax setge cl sub ecx, 1 and edx, ecx add eax, edx {$ENDIF} end; function Min(A, B: Cardinal): Cardinal; overload; asm {$IFDEF AllowCMOV} cmp edx,eax cmovb eax,edx {$ELSE} sub edx, eax sbb ecx, ecx and edx, ecx add eax, edx {$ENDIF} end; function Min(A, B: Int64): Int64; overload; asm mov eax, [ebp+$08] xor ecx, ecx mov edx, [ebp+$0C] sub eax, [ebp+$10] sbb edx, [ebp+$14] setge cl mov eax, [ebp+8*ecx+$08] mov edx, [ebp+8*ecx+$0C] end; function Min(A, B : Single): Single; overload; asm fld A fcomp B fstsw ax and eax, $100 shr eax, 6 {eax = 0 or 4} fld dword ptr [ebp+eax+8] end; function Min(A, B : Double): Double; overload; asm fld A fld B fcomi st(0),st(1) ja @@Done ffree st(1) pop ebp ret 16 @@Done: fxch ffree st(1) end; function Min(A, B : Extended): Extended; overload; asm fld A fld B fcomi st(0), st(1) ja @@Done ffree st(1) pop ebp ret $18 @@Done: fxch ffree st(1) end; end.