Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert _m128i to an unsigned int with SSE?

I have made a function for posterizing images.

// =(
#define ARGB_COLOR(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))

inline UINT PosterizeColor(const UINT &color, const float &nColors)
{
    __m128 clr = _mm_cvtepi32_ps(  _mm_cvtepu8_epi32((__m128i&)color)  );

    clr = _mm_mul_ps(clr,  _mm_set_ps1(nColors / 255.0f)  );
    clr = _mm_round_ps(clr, _MM_FROUND_TO_NEAREST_INT);
    clr = _mm_mul_ps(clr, _mm_set_ps1(255.0f / nColors)  );

    __m128i iClr = _mm_cvttps_epi32(clr);

    return ARGB_COLOR(iClr.m128i_u8[12],
                      iClr.m128i_u8[8],
                      iClr.m128i_u8[4],
                      iClr.m128i_u8[0]);
}

in the first line, I unpack the color into 4 floats, but I can't find the proper way to do the reverse.

I searched through the SSE docs and could not find the reverse of _mm_cvtepu8_epi32

does one exist?

like image 638
CuriousGeorge Avatar asked Dec 22 '11 02:12

CuriousGeorge


People also ask

Can you cast int to unsigned?

If you mix signed and unsigned int, the signed int will be converted to unsigned (which means a large positive number if the signed int has a negative value).

Is unsigned int unsigned?

There is no difference. unsigned and unsigned int are both synonyms for the same type (the unsigned version of the int type).

How does unsigned integer work?

An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative. If you take an unsigned 0 and subtract 1 from it, the result wraps around, leaving a very large number (2^32-1 with the typical 32-bit integer size).

What is __ m128i?

The __m128i data type can hold sixteen 8-bit, eight 16-bit, four 32-bit, or two 64-bit integer values. The compiler aligns __m128d and __m128i local and global data to 16-byte boundaries on the stack. To align integer, float, or double arrays, you can use the __declspec(align) statement.


1 Answers

A combination of _mm_shuffle_epi8 and _mm_cvtsi128_si32 is what you need:

static const __m128i shuffleMask = _mm_setr_epi8(0,  4,  8, 12, -1, -1, -1, -1,
                                               -1, -1, -1, -1, -1, -1, -1, -1);
UINT color = _mm_cvtsi128_si32(_mm_shuffle_epi8(iClr, shuffleMask));
like image 200
Marat Dukhan Avatar answered Sep 24 '22 18:09

Marat Dukhan