mov dword[esp+ 4], 0
mov dword[esp+ 8], 0 ; in ^4 and ^8 thre is the last element >
and <
cmp dword[esp+ 84], 0
je .e
cmp dword[esp+ 88], 0
je .e
mov edi, dword[esp+ 92]
mov ebx, dword[esp+ 100]
cmp dword[esp+ 96], 0
jle .e
cmp edi, 0
je .nf
mov esi, 0
jl .e
cmp ebx, 0
je .e
dec edi
jmp short .2
.nf: xor eax, eax
clc
jmp short .z
.e: xor eax, eax
dec eax
stc
jmp short .z
.2: cmp esi, edi
jg .nf
mov ebp, esi
add ebp, edi
shr ebp, 1 ; k=(i+j)/2
mov eax, dword[esp+ 96]
mul ebp
mov ecx, dword[esp+ 84]
cmp edx, 0
jne .e
add eax, dword[esp+ 88]
jc .e
mov dword[esp+ 0], eax
push ecx
push eax
call ebx
add esp, 8
cmp eax, 0
jle .3
dec ebp
mov ecx, dword[esp+ 0]
mov edi, ebp
mov dword[esp+ 4], ecx
jmp short .2
.3: jz .4
inc ebp
mov ecx, dword[esp+ 0]
mov esi, ebp
mov dword[esp+ 8], ecx
jmp short .2
.4: mov eax, dword[esp+ 0]
clc
.z: mov edx, dword[esp+ 4]
mov ecx, dword[esp+ 8]
lea esp, [esp+64]
pop ebp
pop edi
pop esi
pop ebx
ret 20
;0ra, 4P1, 8P2
align 4
cmpi32:
mov eax, dword[esp+ 4]
mov edx, dword[esp+ 8]
mov ecx, [eax]
cmp ecx, [edx]
jge .1
mov eax, -1
jmp short .z
.1: jz .2
mov eax, 1
jmp short .z
.2: mov eax, 0
.z:
ret
;u32 binsearch(i32 arr, u32 len, i32 key)
;0k,4j,8i,12b,16ra, 20P_arr,24P_len,28P_key
align 4
binsearch:
push ebx
push esi
push edi
push ebp
mov eax, dword[esp+ 20]
mov edx, dword[esp+ 24]
lea ecx, dword[esp+ 28]
push cmpi32
push 4
push edx
push eax
push ecx
call binSearchR
jnc .2
.e: mov eax, 0
jmp short .z ; error
.1: mov eax, -1
clc
jmp short .z ; not found
.2: mov ecx, dword[esp+ 20]
cmp eax, 0
je .1
sub eax, ecx
shr eax, 2
clc ; indx=(res-base)/4
.z:
pop ebp
pop edi
pop esi
pop ebx
ret 12
------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define u32 unsigned
#define i32 int
#define u8 unsigned char
#define R return
#define P printf
#define F for
// const, how is smart...
#define cn const
/*
;return (u8*)-1 error, 0 not found, address of the found element in array
;if return 0 [not found]
; in ecx return the address element of the array < than key if exist
; else ecx=0
; in edx return the address element of the array > than key if exist
; else edx=0
would be like the C library one, with the only difference return (u8*)-1 for
error
*/
u8* __stdcall
binSearchR(u8* InPKey, u8* InBase, u32 nElmBase, u32 SizeElmBase,
i32 cmp(cn u8* elm1, cn u8* elm2) );
i32 __stdcall binsearch(int a[], size_t length, int key);
i32 Cbinsearch(int a[], size_t length, int key)
{
size_t low = 0, high = length;
while (high > low) {
size_t middle = low + (high - low)/2;
if (key == a[middle])
return middle;
else if (key > a[middle])
low = middle + 1;
else high = middle;
}
return -1;
}
i32 cmpu32s(const void* elm1, const void* elm2)
{u32 m1, m2;
m1=*(u32*)elm1; m2=*(u32*)elm2;
if(m1<m2) R -1;
else if(m1>m2) R 1;
else R 0;
}
int main(int argc, char** argv)
{time_t in, fin;
double d;
u32 v[]={1, 2, 5, 7, 8, 8, 9, 13, 20}, val=4, *w, *ww, i;
i32 ix, ixx;
u8 *r;
if(argc==2)
{int n=atoi(argv[1]);
int *data = (int *)malloc(n * sizeof *data);
if(data)
{int i, length, step, key;
long int sum;
for(i=0; i<n; i++) data=2*i+1;
// Include one zero-length search for correctness testing
r = binSearchR(data, data, 0, sizeof(data[0]), cmpu32s);
if(r) printf("Error r=%p\n", (void*) r);
sum=0; time(&in);
for(length=1; length<=n; length++)
for(step=1; step<=length; step++)
for(key=0; key<=2*length; key+=step)
{r=binSearchR(&key, data, length, sizeof(data[0]), cmpu32s);
if(r) sum+=(r-(u8*)data)/sizeof(data[0]);
}
time(&fin);
P("myBsearch=%.3f ", (double) d=difftime(fin, in));
printf("sum=%ld\n", sum);
sum=0; time(&in);
for(length=1; length<=n; length++)
for(step=1; step<=length; step++)
for(key=0; key<=2*length; key+=step)
{r=bsearch(&key, data, length, sizeof(data[0]), cmpu32s);
if(r) sum+=(r-(u8*)data)/sizeof(data[0]);
}
time(&fin);
P("libsearch=%.3f ", (double) d=difftime(fin, in));
printf("sum=%ld\n", sum);
free(data);
}
}
if(argc==2)
{int n = atoi(argv[1]);
/* cast so C++ compiled code can timed */
int *data = (int *)malloc(n * sizeof *data);
if (data)
{int i, length, step, key;
long int sum;
for (i = 0; i < n; i++) data= 2*i+1;
/* Include one zero-length search for correctness testing */
time(&in);
sum = binsearch(data, 0, data[0]);
for(length = 1; length <= n; length++)
for(step = 1; step <= length; step++)
for (key = 0; key <= 2 * length; key += step)
sum += binsearch(data, length, key);
time(&fin);
P("AsmBsearc=%.3f sum=%ld\n", (double) d=difftime(fin, in),sum);
time(&in);
sum = binsearch(data, 0, data[0]);
for(length = 1; length <= n; length++)
for(step = 1; step <= length; step++)
for (key = 0; key <= 2 * length; key += step)
sum += Cbinsearch(data, length, key);
time(&fin);
P("C Bsearc=%.3f sum=%ld\n", (double) d=difftime(fin, in),sum);
free(data);
}
}
R 0;
}
--------------
C:>bins 3000
myBsearch=15.000 sum=-1433105604
libsearch=15.000 sum=-1433105604
AsmBsearc=16.000 sum=-1488664059
C Bsearc=8.000 sum=-1488664059
--------------