32-bit Hamming String formation from 32 8-bit comparisons

234 views Asked by At

I am performing a census-transform on an image doing 32 comparisons per pixel. I can efficiently generate a 256-bit vector of 0x0100010100010100... where each 8-bits correspond to 0x00 or 0x01. The vector is identified below as 'comparisons'. I need to collapse this 256-bit vector to generate a 32-bit hamming string. The array 'census' is where I store the 8-bit comparisons. Note that at this point I do not care about the hamming distance, I am only interested in generating the string as quickly as possible. I have AVX2 available to me. My current code:

uint8_t* census = (uint8_t*) _mm_malloc(sizeof(int)*8,32);

...

_mm256_storeu_si256((__m256i*) census, comparisons);

uint32_t hammingString = (uint32_t) (census[0] +
                                    (census[1] << 1)   +
                                    (census[2] << 2)   +
                                    ...
                                    (census[31] << 31));
1

There are 1 answers

1
harold On BEST ANSWER

That's almost exactly what _mm256_movemask_epi8 is for, except it takes the top bits of the bytes instead of the least significant bits. So just shift left by 7 first.

Or, change how you produce those bytes, because you probably made them as 0x00 or 0xFF for false and true respectively, right? At least, normally comparisons result in that sort of thing.