SSE/AVX: absolute value of difference of unsigned integers

Author:Wojciech Muła
Added on:2018-03-11

With signed numbers it is really easy. We can subtract two numbers (instruction psub) and then calculate abs directly, as all Intel's SIMD instruction sets support the abs operation.

To calculate the abs of difference of two unsigned numbers we can use the saturated arithmetic. The saturated subtract (instructions psubusX) is equivalent to max(a - b, 0); it means that whenever subtraction would yield a negative result, the final result is zero.

We need to calculate two saturated subtracts, one for a - b, another for b - a; then merge them with bitwise or -- it's safe, because one of the subtract results is zero.

Below is an SSE implementation; full source code is available.

__m128i abs_sub_epu8(const __m128i a, const __m128i b) {

    const __m128i ab = _mm_subs_epu8(a, b);
    const __m128i ba = _mm_subs_epu8(b, a);

    return _mm_or_si128(ab, ba);