Re: Str-Kat

new topic     » goto parent     » topic index » view thread      » older message » newer message

Jim Brown wrote:
> 
> Kat wrote:
> > 
> > ken mortenson wrote:
> > > 
> > > Kat wrote:
> > > 
> > > > It would also be easier if Eu returned unused
> > > > memeory to the OS via some command or other
> > > 
> > > I believe it does Kat.  I believe I remember seeing free() routine?
> > 
> > So it's only a matter of using allocate() and free()? No wonder it's not
> > been
> > done yet! Where is the magic spell that makes compare() and equal() and
> > string[2]
> > and length(string) work, after you get allocate() and free() typed out?
> > 
> > Kat
> 
> --string.e
> ---------------------
> namespace string
> 
> global function compare(atom a, atom b)
> integer i, ac, bc
> i = 0
> ac = peek(a)
> bc = peek(b)
> while 1 do
> if ac > bc then
> return 1
> elsif ac < bc then
> return -1
> elsif (ac = bc) and (ac = 0) then
> return 0
> else
> i = i + 1
> ac = peek(a+i)
> bc = peek(b+i)
> end if
> end while
> end function
> 
> global function equal(atom a, atom b)
> return compare(a,b) = 0
> end function
> 
> global function length(atom a)
> integer i
> i = 0
> while peek(a+i) != 0 do
> i = i + 1
> end while
> return i
> end function
> 
> --string[2] is impossible to do currently, but there is this workaround
> global function slice(atom a, integer i)
> return peek(a+i)
> end function

On an Intel CPU, the length function needs only be this:
push edi
push ecx
xor eax,eax
xor ecx,ecx
mov edi,[esp+4]
cld
repnz scasb
jecxz ret
sub eax,ecx
pop ecx
pop edi
ret

A whopping 20 bytes. You can shave 2 more if ecx is discardable. The jecxz is
optional too, becuse machines with 4Go RAM are still hard to find. That comes to
16 bytes, which nicely fits into a cache line.

Oh, and the string address needs not to be on the stack if ecx is discardable.
Can still shave some cycles.

How much slower would the string: code above be? I'd bet between 10 and 30
times.

CChris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu