strcmp() and drivers

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: strcmp() and drivers

Post by piranha »

Solar: I have to say, that is disturbingly elegant. And it is tested OK, I was bored.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: strcmp() and drivers

Post by NickJohnson »

I think I have a better way, that only requires one possible branch - it may be a bit faster (but it's untested):

Code: Select all

int strcmp(const char *s1, const char *s2)
{
    while (~((*s1)^(*s2))&((*s1)&(*s2)))
    {
        ++s1;
        ++s2;
    }
    
    return (*s1 - *s2);
}
It should be equivalent to ( !((s1*) != (*s2)) && (*s1) ), but more bit-twiddling-y. It only works to use ~ instead of ! because you only check common bits between *s1 and *s2, which is what makes it only one branch. I have no idea if four logical instructions and one branch is faster than two branches though.
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

Re: strcmp() and drivers

Post by AUsername »

Edit:

It works!

Turns out we didn't have /ALIGN:512. D:

But it works! :D
Post Reply