使用位操作可降低缓存开销 [英] Using bits manipulation reduce cache expense

查看:104
本文介绍了使用位操作可降低缓存开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a raw12 file(4096 * 3072 ) where RGGB(odd rows RGRG..And even GBGB...) Pixels are stored ,12 bit for each .I have to  use only 5 bit MSB data from each 12 bit data , I have to take 64 bit at a time for cache optimization and use bit manipulation to get 5 MSB bits for each 12 bit pixel,I am not getting , How should I achieve this ? 
 This is code I wrote and working properly but rather than taking 8 bit at a time(buffer is 8bit pointer) I have to take 64 bits .  In buffer I have pointed complete file. *This code is to make a histogram.

Taking LITTLE ENDIAN into consideration

code is in C .





< b>我尝试了什么:





What I have tried:

for(int i = 0; i < length; i += 3){
       if (i % (3 * 2 * num_cols / 2) < num_cols * (3) / 2) {       /* ODD ROW */
          hist[0][buffer[i] >> 3]++;                                /* CH[0] */
          hist[1][buffer[i+1] & 0x0F << 1 |                         /* CH[1] */
                   buffer[i+2] >> 7]++;
        } else {                                                    /* EVEN ROW*/
          hist[2][buffer[i] >> 3]++;                                /* CH[2] */
          hist[3][buffer[i+1] & 0x0F << 1 |                         /* CH[3] */
                buffer[i+2] >> 7]++ ;
        }
    }

推荐答案

我会先尝试让事情更简单,更实用解决速度问题。当代码首先不起作用时,速度没有任何意义。尝试这样的事情:
I would try to make things simpler and functional first and then address the speed. Speed means nothing when the code doesn't work in the first place. Try something like this :
const int width = 4096;
 typedef union
 {
     UCHAR byte;
     struct nibble
     {
         UCHAR first : 4;
         UCHAR second : 4;
     } nibs;
 } bytenib;

 bytenib byte2;
 UCHAR byte1;
 UCHAR byte3;
 USHORT pixel1;
 USHORT pixel2;
 const int width = 4096;

 bool oddrow = row % 2;

 int i = 0;
 while( i < width )
 {
     byte1 = image[row][ i ];
     byte2.byte = image[row][ i + 1];
     byte3 = image[row][ i ];

     pixel1 = byte1 << 4;
     pixel1 |= byte2.nibs.second;
     pixel2 = byte3;
     pixel2 |= ( byte2.nibs.first << 8 );

     i += 3;
 }

这是从三个字节中获取两个十二位像素的部分代码。它没有考虑事物的奇/偶方面。它还显示了一种非常简单的方法来确定行是奇数还是偶数。



关键是有一个字节和两个位字段的并集是一个蚕食每个(半个字节)或四个比特。这是将一个字节剥离一半的一种非常简单的方法。

This is partial code to get two twelve-bit pixels from three bytes. It doesn't take into account the odd/even aspect of things. It also shows a very easy way to determine whether a row is odd or even.

The key thing there is the union of a byte and two bit fields that are one nibble each (half a byte) or four bits. This is a very easy way to peel a byte in half.


这篇关于使用位操作可降低缓存开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆