vendredi 28 avril 2017

Fastest way to check if a value is all zeros (64-bit processor)

I want to write a function that can determine if any value is made completely of zeroes.

Here was my initial thought:

bool isZero(T* value) {
    char* ptr = reinterpret_cast<char*>(value);
    char sum = 0;
    for (size_t i = 0; i < sizeof(T); ++i) {
        sum |= ptr[i];
    }
    return sum == 0;
}

But then I thought that maybe on a 64-bit processor it will be faster to check 8 bytes at a time, instead of checking them one-by-one.

So I came up with this:

bool isZero(T* value) {
    uintptr_t* ptr = reinterpret_cast<uintptr_t*>(value);
    uintptr_t sum = 0;
    size_t i = 0;
    for (; (i + 1) * sizeof(uintptr_t) <= sizeof(T); ++i) {
        sum |= ptr[i];
    }
    uintptr_t leftover = ptr[i] >> (8 * ((i + 1) * sizeof(uintptr_t) - sizeof(T)));
    return (sum | leftover) == 0;
}

Can anybody shine some light on which of these might be faster? I'm not experienced enough to know how to properly benchmark this.

Aucun commentaire:

Enregistrer un commentaire