2015-10-22 18:14+0200, Paolo Bonzini: > On 22/10/2015 18:02, Eric Blake wrote: >> I see a bug in there: > > Of course. You shouldn't have told me what the bug was, I deserved > to look for it myself. :)
It rather seems that you don't want spoilers, :) I see two bugs now. > bool memeqzero4_paolo(const void *data, size_t length) > { > const unsigned char *p = data; > unsigned long word; > > while (__builtin_expect(length & (sizeof(word) - 1), 0)) { > if (*p) > return false; > p++; > length--; > if (!length) > return true; > } > > /* We must always read one byte or word, even if everything is aligned! > * Otherwise, memcmp(data, data, length) is trivially true. > */ > for (;;) { > memcpy(&word, p, sizeof(word)); > if (word) > return false; > if (__builtin_expect(length & (16 - sizeof(word)), 0) == 0) > break; > p += sizeof(word); > length -= sizeof(word); > if (!length) > return true; > } > > /* Now we know that's zero, memcmp with self. */ > return memcmp(data, p, length) == 0; > }