简体   繁体   中英

How to multiply __m128i register by float factor using SSE?

I have I problem with multiplying two registers (or just register by float constant). One register is __m128i type and contains one channel of RGBA pixel color from 16 pixels (the array with 16 pixels is sending as a parameter to CPP dll). I want to multiply this register by constant to get grayscale value for this channel and do this operation also for other channels stored in __m128i registers.

I think that a good idea to use SIMD for convert image to grayscale is to use this algorithm.

fY(R, G, B) = R x 0.29891 + G x 0.58661 + B x 0.11448

I have this following code and now it's only decomposing the image to channels and pack it together to return as an src vector. Now I need to make it for grayscale :)
The src variable is a pointer to unsigned char array.

__m128i vecSrc = _mm_loadu_si128((__m128i*) &src[srcIndex]);

    __m128i maskR = _mm_setr_epi16(1, 0, 0, 0, 1, 0, 0, 0);
    __m128i maskG = _mm_setr_epi16(0, 1, 0, 0, 0, 1, 0, 0);
    __m128i maskB = _mm_setr_epi16(0, 0, 1, 0, 0, 0, 1, 0);
    __m128i maskA = _mm_setr_epi16(0, 0, 0, 1, 0, 0, 0, 1);

    // Creating factors.
    const __m128i factorR = _mm_set1_epi16((short)(0.29891 * 0x10000));  //8 coefficients - R scale factor.
    const __m128i factorG = _mm_set1_epi16((short)(0.58661 * 0x10000));  //8 coefficients - G scale factor.
    const __m128i factorB = _mm_set1_epi16((short)(0.11448 * 0x10000));  //8 coefficients - B scale factor.

    __m128i zero = _mm_setzero_si128();

    // Shifting higher part of src register to lower.
    __m128i vectSrcLowInHighPart = _mm_cvtepu8_epi16(vecSrc);
    __m128i vectSrcHighInHighPart = _mm_unpackhi_epi8(vecSrc, zero); 

    // Multiply high parts of 16 x uint8 vectors by channels masks and save lower half. Getting each channels separatly (in two parts H and L)
    __m128i vecR_L = _mm_mullo_epi16(vectSrcLowInHighPart, maskR);
    __m128i vecG_L = _mm_mullo_epi16(vectSrcLowInHighPart, maskG);
    __m128i vecB_L = _mm_mullo_epi16(vectSrcLowInHighPart, maskB);
    __m128i vecA_L = _mm_mullo_epi16(vectSrcLowInHighPart, maskA);

    // Multiply lower parts of 16 x uint8 vectors by channels masks and save lower half.
    __m128i vecR_H = _mm_mullo_epi16(vectSrcHighInHighPart, maskR);
    __m128i vecG_H = _mm_mullo_epi16(vectSrcHighInHighPart, maskG);
    __m128i vecB_H = _mm_mullo_epi16(vectSrcHighInHighPart, maskB);
    __m128i vecA_H = _mm_mullo_epi16(vectSrcHighInHighPart, maskA);

    // Lower and high masks using to packing.
    __m128i maskLo = _mm_set_epi8(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 14, 12, 10, 8, 6, 4, 2, 0);
    __m128i maskHi = _mm_set_epi8(14, 12, 10, 8, 6, 4, 2, 0, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80);

    // Packed the High and Lowe part of register into one 16 x 8bit registers of each channels.
    __m128i R = _mm_or_si128(_mm_shuffle_epi8(vecR_L, maskLo), _mm_shuffle_epi8(vecR_H, maskHi));
    __m128i G = _mm_or_si128(_mm_shuffle_epi8(vecG_L, maskLo), _mm_shuffle_epi8(vecG_H, maskHi));
    __m128i B = _mm_or_si128(_mm_shuffle_epi8(vecB_L, maskLo), _mm_shuffle_epi8(vecB_H, maskHi));
    __m128i A = _mm_or_si128(_mm_shuffle_epi8(vecA_L, maskLo), _mm_shuffle_epi8(vecA_H, maskHi));

    // Added all sub vectors to get in result one 128-bit vector with all edited channels.
    __m128i resultVect = _mm_add_epi8(_mm_add_epi8(R, G), _mm_add_epi8(B, A));

    // Put result vector into array to return as src pointer.
    _mm_storel_epi64((__m128i*)&src[srcIndex], resultVect);

Thanks for help for you! It's my first program with SIMD (SSE) instructions.

Based on comments to my question, I created a solution. And also a project where I was learning how the registers exactly work when I using SSE instructions.

// Function displaying only registers with 16 x uInt8. And message.
void printRegister(__m128i registerToprint, const string &msg) {
    unsigned char tab_debug[16] = { 0 };
    unsigned char *dest = tab_debug;

    _mm_store_si128((__m128i*)&dest[0], registerToprint);

    cout << msg << endl;
    cout << "\/\/\/\/ LO \/\/\/\/" << endl;

    for (int i = 0; i < 16; i++)
        cout << dec << (unsigned int)dest[i] << endl;

    cout << "/\/\/\/\ HI /\/\/\/" << endl;
}

int main()
{
    // Example array as 128-bit register with 16xuInt8. That represent each channel of pixel in BGRA configuration.
    unsigned char tab[] = { 100,200,250,255, 101,201,251,255, 102,202,252,255, 103,203,253,255 };

    // A pointer to source tab for simulate dll parameters reference.
    unsigned char *src = tab;

    // Start index of src t
    int srcIndex = 0;

    // How to define float numbers as integer of uInt16 type.
    const __m128i r_coef = _mm_set1_epi16((short)(0.2989*32768.0 + 0.5));
    const __m128i g_coef = _mm_set1_epi16((short)(0.5870*32768.0 + 0.5));
    const __m128i b_coef = _mm_set1_epi16((short)(0.1140*32768.0 + 0.5));

    // vecSrc - source vector (BGRA BGRA BGRA BGRA).
    // Load data from tab[] into 128-bit register starting from adress at pointer src. (From 0 index so load all 16 elements x 8bit).
    __m128i vecSrc = _mm_loadu_si128((__m128i*) &src[srcIndex]);

    // Shuffle to configuration A0A1A2A3_R0R1R2R3_G0G1G2G3_B0B1B2B3
    // Not revers so mask is read from left (Lo) to right (Hi). And counting from righ in srcVect (Lo).
    __m128i shuffleMask = _mm_set_epi8(15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0);
    __m128i AAAA_R0RRR_G0GGG_B0BBB = _mm_shuffle_epi8(vecSrc, shuffleMask);

    // Put B0BBB in lower part.
    __m128i B0_XXX = _mm_slli_si128(AAAA_R0RRR_G0GGG_B0BBB, 12);
    __m128i XXX_B0 = _mm_srli_si128(B0_XXX, 12);

    // Put G0GGG in Lower part.
    __m128i G0_B_XX = _mm_slli_si128(AAAA_R0RRR_G0GGG_B0BBB, 8);
    __m128i XXX_G0 = _mm_srli_si128(G0_B_XX, 12);

    // Put R0RRR in Lower part.
    __m128i R0_G_XX = _mm_slli_si128(AAAA_R0RRR_G0GGG_B0BBB, 4);
    __m128i XXX_R0 = _mm_srli_si128(R0_G_XX, 12);

    // Unpack uint8 elements to uint16 elements.
    // The sequence in uInt8 is like (Hi) XXXX XXXX XXXX XXXX (Lo) where X represent uInt8.
    // In uInt16 is like (Hi) X_X_ X_X_ X_X_ X_X_ (Lo)
    __m128i B0BBB = _mm_cvtepu8_epi16(XXX_B0);
    __m128i G0GGG = _mm_cvtepu8_epi16(XXX_G0);
    __m128i R0RRR = _mm_cvtepu8_epi16(XXX_R0);

    // Multiply epi16 registers.
    __m128i B0BBB_mul = _mm_mulhrs_epi16(B0BBB, b_coef);
    __m128i G0GGG_mul = _mm_mulhrs_epi16(G0GGG, g_coef);
    __m128i R0RRR_mul = _mm_mulhrs_epi16(R0RRR, r_coef);

    __m128i BGR_gray = _mm_add_epi16(_mm_add_epi16(B0BBB_mul, G0GGG_mul), R0RRR_mul);

    __m128i grayMsk = _mm_setr_epi8(0, 0, 0, 0, 2, 2, 2, 2, 4, 4, 4, 4, 6, 6, 6, 6);
    __m128i vectGray = _mm_shuffle_epi8(BGR_gray, grayMsk);

    printRegister(vectGray, "Gray");
}

How it's work

The unsigned char tab[] contains 16 x uInt8 elements to fill one 128-bit register. This array is simulating a 8 pixels which channels is on BGRA configuration.

void printRegister(__m128i registerToprint, const string &msg);

This function is using to print as a decimal registers value sending as a parameter in console.

If someone wants to test it the full project is available at gitHub: Full project demo gitHub

I hope that all comments are valid if no, please correct me :) Thanks for the support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM