在SSE中浮点到uchar转换问题

时间:2014-10-16 14:03:46

标签: c sse

您好,
关于我之前的帖子,我解决了SSE中的比较操作 但是在得到输出后我发现我的输出是浮点数而我的预期输出是在uchar中 例如,我期望输出为8,输出以浮点格式显示为8.0(32位浮点格式)。转换后的值为1字节无符号,远远不同于8 ..
PFB我在C中的原始代码及其在SSE中的对应代码:

C代码:

unsigned char *destination_buff = (unsigned char *)malloc(sizeof(unsigned char)*height*width);
float *d1 = inputbuffer;
float *d2 = d1 + width;
float *d3 = d2 + width;

for(int i=1;i<height;i++)
{
    for(int j=1;j<width;j++)
    {
       int val = d2[j];
       int temp1 = 0x00FF;
       int temp2 = 0;   
       if(val <= d1[j-1]) temp2 += 0x80;
       if(val <= d1[j])   temp2 += 0x40;
       if(val <= d1[j+1]) temp2 += 0x20;    }
       if(val <= d2[j-1]) temp2 += 0x10;}
       if(val <= d2[j+1]) temp2 += 0x08;
       if(val <= d3[j-1]) temp2 += 0x04;
       if(val <= d3[j])   temp2 += 0x02;
       if(val <= d3[j+1]) temp2 ++;    
       temp1 &= (~temp2);
       destination_buff[j-1] = temp1;       
    }
        d1 += width;
        d2 += width;
        d3 += width;

        destination_buff += (width);
}   

这是我的SSE代码:

float *destination_buff = (float *)malloc(sizeof(float)*height*width);

uchar *dst_d = outputbuffer; //Pointer to the destination buffer which is already present and need to fill the output data in this
float *CT_image_0 = m_dat;
float *CT_image_1 = CT_image_0 + width;
float *CT_image_2 = CT_image_1 + width;

for(int i=1;i<height;++i)
{
    for(int j=1;j<width;j+=4)
    {
      __m128 CT_current_00 = _mm_loadu_ps((CT_image_0+j-1));
      __m128 CT_current_10 = _mm_loadu_ps((CT_image_1+j-1));
      __m128 CT_current_20 = _mm_loadu_ps((CT_image_2+j-1));

      __m128 CT_current_01 = _mm_loadu_ps(((CT_image_0+1)+j-1));
      __m128 CT_current_11 = _mm_loadu_ps(((CT_image_1+1)+j-1));
      __m128 CT_current_21 = _mm_loadu_ps(((CT_image_2+1)+j-1));

      __m128 CT_current_02 = _mm_loadu_ps(((CT_image_0+2)+j-1));
      __m128 CT_current_12 = _mm_loadu_ps(((CT_image_1+2)+j-1));
      __m128 CT_current_22 = _mm_loadu_ps(((CT_image_2+2)+j-1));

      __m128 val    =  CT_current_11;

      __m128 t1 = _mm_set1_ps(0x80);
      __m128 t2 = _mm_set1_ps(0x40);
      __m128 t3 = _mm_set1_ps(0x20);
      __m128 t4 = _mm_set1_ps(0x10);
      __m128 t5 = _mm_set1_ps(0x08);
      __m128 t6 = _mm_set1_ps(0x04);
      __m128 t7 = _mm_set1_ps(0x02);
      __m128 t8 = _mm_set1_ps(0x01);

      __m128 out = _mm_setzero_ps();                 // init output flags to all zeroes


      __m128 sample = _mm_cmple_ps(val,CT_current_00);
             sample = _mm_and_ps(sample,t1);
               out  = _mm_or_ps(out,sample);
             sample = _mm_cmple_ps(val,CT_current_01);
             sample = _mm_and_ps(sample,t2);
               out  = _mm_or_ps(out,sample);
            sample = _mm_cmple_ps(val,CT_current_02);
            sample = _mm_and_ps(sample,t3);
              out  = _mm_or_ps(out,sample);

            sample = _mm_cmple_ps(val,CT_current_10);
            sample = _mm_and_ps(sample,t4);
              out  = _mm_or_ps(out,sample);
            sample = _mm_cmple_ps(val,CT_current_12);
            sample = _mm_and_ps(sample,t5);
              out  = _mm_or_ps(out,sample);

            sample = _mm_cmple_ps(val,CT_current_20);
            sample = _mm_and_ps(sample,t6);
              out  = _mm_or_ps(out,sample);
            sample = _mm_cmple_ps(val,CT_current_21);
            sample = _mm_and_ps(sample,t7);
              out  = _mm_or_ps(out,sample);
            sample = _mm_cmple_ps(val,CT_current_22);
            sample = _mm_and_ps(sample,t8);
              out  = _mm_or_ps(out,sample);

            _mm_storeu_ps((destination_buff+(j-1)),out);
            dst_d =  (uchar *)destination_buff;

        }

    CT_image_0  += width;
    CT_image_1  += width;
    CT_image_2  += width;

    dst_d += (width);

}

所有商店操作都在float和__m128i。如何将结果存储到uchar中?

1 个答案:

答案 0 :(得分:0)

您可以执行packed-compare来获取掩码,但是然后将该掩码与整数操作一起使用。 _mm_set1_ps(0x80)表示你正在做一些奇怪的事情。您可能不应该将两个功率位掩码转换为浮点数,因为使用_mm_add_ps添加它们比将它们与_mm_or_si128组合要慢得多。

对于某些偏移负载,您可能更适合palignr,以平衡加载端口和ALU端口之间的代码。