"Skybuck Flying" <BloodyShame@[EMAIL PROTECTED]
> ha scritto nel messaggio
news:fce6$4814e5fd$541983fa$29141@[EMAIL PROTECTED]
> Delphi uses a different calling convention then C by default.
>
> So your supplied assembler routine is not working in Delphi:
>
> I also tried specifieing the calling convention as stdcall, and cdecl,
but
> so far it's not working...
>
> // this code not working correctly because of different calling
> conventions (?)
> // I tried none, cdecl, and stdcall, which both result in access
violation
> ?!?
> function AsmDiv7v3( Value : longword ) : longword; cdecl;
> asm
> mov ecx, dword[esp+4]
> mov eax, $24924925
> mov dword[esp+4], 0
in the little i know it is easy the access violation is for the above
instruction
if i see right the functions "uns32 div7(uns32 )" "uns32 div7m(uns32 )"
should be ok for the C calling convention (is it cdecl?).
the fast one (div7) is a rewrite in assembly of the algoritm that use
the gcc C compiler for divide for 7.
Saluti
-------------------------------------
$ ./cont
2147483648 div7 is ok
2147483648 div7m is ok
D(div7c)=43.000000
D(div7)=25.000000
D(div7cpu)=97.000000
-----------------------
div7 seems to be fast as 2x div7c (the function of compiler here)
that seems to be 2x div7cpu (the one that use the div assembly
instruction)
----------------------------
section DATA
global div7
global div7m
global div7cpu
section TEXT
div7:
mov ecx, [esp+4]
mov eax, 613566757
mul ecx
sub ecx, edx
shr ecx, 1
lea eax, [edx+ecx]
shr eax, 2
ret
div7m:
mov edx, dword[esp+4]
xor ecx, ecx
mov eax, 0x24924925
..0:
cmp edx, 1431655764
jbe .1
sub edx, 1431655764
add ecx, 204522252
jmp short .0
..1:
mul edx
mov eax, edx
add eax, ecx
ret
div7cpu:
mov ecx, 7
xor edx, edx
mov eax, [esp+4]
div ecx
ret
----------------------
#include <stdio.h>
#include <time.h>
unsigned div7(unsigned);
unsigned div7m(unsigned);
unsigned div7cpu(unsigned);
unsigned div7c(unsigned a) {return a/7;}
double tempo( unsigned (*f)(unsigned))
{double r;
time_t i, e;
unsigned a;
i=time(0);
for(a=1; a; ++a)
{//if(a%0x40000000==0) {printf(" %u ", a); fflush(stdout);}
(*f)(a);
}
e=time(0);
r=difftime(e, i);
return r;
}
int check(unsigned (*f)(unsigned))
{unsigned a=1, b, bb, i=0;
l0:;
b = (*f)(a);
bb=a/7;
if(bb!=b)
{++i;
printf("%u/7=%u but it should be %u\n", a, b, bb);
fflush(stdout);
}
if(a%0x80000000==0) {printf(" %u ", a); fflush(stdout);}
if(i>=15) return 0;
++a;
if(a) goto l0;
return 1;
}
int main(void)
{double r;
check(div7)==1 ?
printf("div7 is ok\n"): printf("Error for div7 \n");
check(div7m)==1?
printf("div7m is ok\n"): printf("Error for div7m \n");
r=tempo(div7c); printf("D(div7c)=%f\n", r);
r=tempo(div7); printf("D(div7)=%f\n", r);
r=tempo(div7cpu); printf("D(div7cpu)=%f\n", r);
return 0;
}
----------------------------------
the assembly that i use, in above there are the nasm instructions
-------------------------------------
section DATA
global div7
global div7m
global div7cpu
section TEXT
div7:
c=[s+4]; a=613566757; mul c; c-=r; c>>=1; a=&[r+c]; a>>=2; ret
div7m: r=D[s+4]; c^=c; a=0x24924925;
..0: r<=1431655764#.1; r-=1431655764; c+=204522252; #.0;
..1: mul r; a=r; a+=c; ret
div7cpu: c=7; r^=r; a=[s+4]; div c; ret
------------------------------------------


|