Re: improve strlen



Hello. I took it as a hobby project to write C++ code that does the
same thing..

int xstrlen(const char* text)
{
const char* p = text;
unsigned int a = reinterpret_cast<unsigned int&>(text);
unsigned int c = ((a + 3) & 0xfffffffc) - a;
unsigned int s = c;

if ( c )
{
for ( ; c >= 0; --c )
{
if ( *p++ == 0 )
return static_cast<int>(p - text - 1);
}
}

const unsigned int* ap = reinterpret_cast<const unsigned int*>(p);
unsigned int d = reinterpret_cast<unsigned int&>(p) + 3;

for ( ;; )
{
unsigned int i = *ap++;
c = (i - 0x01010101) & ~i & 0x80808080;
if ( c )
break;
}

if ( !(c & 0x8080) )
{
c >>= 16;
s += 2;
}

return reinterpret_cast<unsigned int&>(ap) - d - !(c >> 31) + s;
}

Here's the assembly output from MSVC++ (latest version):

push esi
mov esi, OFFSET $SG-28+3
mov eax, OFFSET $SG-28
and esi, -4 ; fffffffcH
sub esi, eax
je SHORT $LN6@xstrlen
$LL8@xstrlen:
mov cl, BYTE PTR [eax]
add eax, 1
test cl, cl
jne SHORT $LL8@xstrlen
sub eax, OFFSET $SG-28
sub eax, 1
pop esi
ret 0
$LN6@xstrlen:
mov eax, OFFSET $SG-28
npad 6
$LL4@xstrlen:
mov edx, DWORD PTR [eax]
lea ecx, DWORD PTR [edx-16843009]
not edx
and ecx, edx
add eax, 4
and ecx, -2139062144 ; 80808080H
je SHORT $LL4@xstrlen
test ecx, 32896 ; 00008080H
jne SHORT $LN1@xstrlen
shr ecx, 16 ; 00000010H
add esi, 2
$LN1@xstrlen:
shr ecx, 31 ; 0000001fH
not ecx
and ecx, 1
sub eax, ecx
sub eax, OFFSET $SG-28+3
add eax, esi
pop esi
ret 0

Is it just me, or does the "innerloop" resemble each other quite a bit?
Why not shouldn't we rely on compilers again? If you time the codes,
they perform +- 10% the same, too.

I still think very much that assembly itself is not optimization tool
anymore, ofcourse if you know it, you write better higher level code,
especially when you know the compiler.. and check assembly *output* to
check the compiler isn't doing something really stupid.

Best use for machine specific instructions, IMHO, is to let machine
generate them. Be this a offline compiler like g++, visual c++, et al..
or realtime code generator like JIT Compiler or something like that.

I'm not "dissing" assembly per-se, I used to be strongly with the
opinion that it is the way for performance.. I started with z80, then
some commodore64 (6508, cant be arsed to google what it was to be
correct), 68000, mips, ppc, x86, etc.etc.. but over the time everyone
learns the old truth about optimizing at the wrong level and at wrong
spots, premature optimization etc.etc.. at the very heart, optimizing
compilers are very interesting topic.

So is assembly, x86 included but if C/C++ compiler can give same
performance from higher level code which is easier to read, write and
generally "see" the flow of the program I prefer that. What I am going
to do next, is to proof-read, rewrite to be portable and then
regression test the code.. as I didn't do this conversion very
carefully, and the variable names are really just register names from
the original assembly code, I shall rename them to reflect the use
better (and cleanup the code in general :)

Then what I do with it, is to archive and never touch or look at it
again. ;-----)

.



Relevant Pages

  • Re: A better algorithm to calculate a leap year?
    ... From reading the assembly output of GCC I saw the ... > same optimization, ... If the compiler can do the optimisation, ...
    (comp.lang.c)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... what experienced programmers do, ... optimization, ... Thugs" ad nauseum fits that a lot more closely than discussing compiler ... be modified outside a loop, and guessing ...
    (comp.programming)
  • long(!) Re: need help on CFLAGS in /etc/make.conf please
    ... For example, MPlayer sets this high on purpose, so GCC will actually ... and the K&R compiler would've known exactly the kind of optimization we wanted. ... >> A msg from Richard Coleman, taken together with the GCC 3.x Known Bugs ...
    (freebsd-questions)
  • Re: SETF and variable issues in Self Similar program.
    ... The way to get a compiler to optimize a tail-call ... call optimization turned on. ... It changes the semantics of the language in a big way. ... In Common Lisp it has to interact with condition handling, ...
    (comp.lang.lisp)
  • Re: Programming languages
    ... >> execution time, ... That's not a good candidate for optimization. ... if there is comething the compiler ... António> programmers and those can't cope with anything else than ...
    (sci.lang)