String compare in words?

qznc via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 29 11:15:16 PDT 2016


On Sunday, 29 May 2016 at 17:38:17 UTC, Jonathan M Davis wrote:
> And if you're not simply comparing for equality, what are you 
> looking to figure out? Without more information about what 
> you're trying to do, it's kind of hard to help you.

If I write the comparison naively, the assembly clearly shows a 
"movzbl" [0]. It loads a single byte! The other single byte load 
is encoded in the address mode of "cmp". Implementation:

bool stringcmp(string x, string y) {
   foreach(i; 0..x.length) {
     if (x[i] != y[i]) // byte compare
       return false;
   }
   return true;
}

It makes no sense to load single bytes here. Since we only want 
to check for equality, we could load two full words and compare 
four or eight bytes in one go.

This example is simplified and far-fetched. Actually, this is 
about the find algorithm [1].

[0] http://goo.gl/ttybAB
[1] 
http://forum.dlang.org/post/vdjraubhtoqtxeshjvqt@forum.dlang.org


More information about the Digitalmars-d-learn mailing list