Hi!
On Agner Fog's webby (http://www.agner.org/assem/), there is a "subroutine library" with a function called "MinI". What function would be the fastest - C's min() macro or using the "MinI" function written in Assembly?
Regards,
Sebastian Andersson
I'm not sure I did the C correctly, but there doesn't appear to be any big difference. On my P3 Agner Fog's routine takes 7 cycles and the code generated by Pelles C takes 8, both with the call overhead included in the count.
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
include \masm32\include\masm32rt.inc
.586
include timers.asm
MinI PROTO :DWORD,:DWORD
pc_min PROTO C :DWORD,:DWORD
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.data
.code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
LOOP_COUNT EQU 10000000
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
invoke MinI,1,2
counter_end
print ustr$(eax)," cycles",13,10
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
invoke pc_min,1,2
counter_end
print ustr$(eax)," cycles",13,10
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
invoke MinI,2,1
counter_end
print ustr$(eax)," cycles",13,10
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
invoke pc_min,2,1
counter_end
print ustr$(eax)," cycles",13,10
mov eax, input(13,10,"Press enter to exit...")
exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; From Agner Fog's subroutine library, 2004-07-13.
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
MinI proc a:DWORD,b:DWORD
;MinI PROC NEAR
PUBLIC MinI
;PublicAlias _MinI ; Underscore needed when called from Windows
mov eax, [esp+4] ; a
mov ecx, [esp+8] ; b
sub eax, ecx ; a-b
cdq ; -1 if b > a, 0 if b <= a
and eax, edx ; a-b if b > a, 0 if b <= a
add eax, ecx ; min(a,b)
ret
MinI ENDP
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; Code generated by Pelles C, POCC v 3.00.10, see below.
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
pc_min proc C a:DWORD,b:DWORD
push ebx
mov ebx,[esp+8]
mov edx,[esp+0Ch]
cmp ebx,edx
jge loc_00401011
mov ecx,ebx
jmp loc_00401013
loc_00401011:
mov ecx,edx
loc_00401013:
mov eax,ecx
pop ebx
ret
pc_min endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start
#define min(a,b) (((a) < (b)) ? (a) : (b))
int min_(int a,int b)
{
return min(a,b);
}
-Tx86-coff -Ot -Ox -W1 -Gd
00401000 fn_00401000:
00401000 53 push ebx
00401001 8B5C2408 mov ebx,[esp+8]
00401005 8B54240C mov edx,[esp+0Ch]
00401009 39D3 cmp ebx,edx
0040100B 7D04 jge loc_00401011
0040100D 89D9 mov ecx,ebx
0040100F EB02 jmp loc_00401013
00401011 loc_00401011:
00401011 89D1 mov ecx,edx
00401013 loc_00401013:
00401013 89C8 mov eax,ecx
00401015 5B pop ebx
00401016 C3 ret
Thanks a lot! :U