我有两个向量。大小为a
的双精度N
的向量和大小为b
的无符号字符ceil(N/8)
的向量。目的是计算a
的某些值的乘积。将b
逐位读取,其中每个位指示是否要在产品中考虑从double
给定的a
。
// Let's create some data
unsigned nbBits = 1e7;
unsigned nbBytes = nbBits / 8;
unsigned char nbBitsinlastByte = nbBits % 8;
assert(nbBits == nbBytes * 8 + nbBitsinlastByte);
std::vector<double> a(nbBits,0.999999); // In practice a values will vary. It is just an easy to build example I am showing here
std::vector<unsigned char> b(nbBytes,false); // I am not using `vector<bool>` nor `bitset`. I've got my reasons!
assert(a.size() == b.size() * 8);
// Set a few bits to true
for (unsigned byte = 0 ; byte < (nbBytes-1) ; byte+=2)
{
b[byte] |= 1 << 2; // set second (zero-based counting) bit to 'true'
b[byte] |= 1 << 7; // set last bit to 'true'
// ^ This is the bit index
}
如上所述,我的目标是在a
为真时计算b
中值的乘积。可以通过
// Initialize the variable we want to compute
double product = 1.0;
// Product for the first nbByts-1 bytes
for (unsigned byte = 0 ; byte < (nbBytes-1) ; ++byte)
{
for (unsigned bit = 0 ; bit < 8 ; ++bit) // inner loop could be manually unrolled
{
if((b[byte] >> bit) & 1) // gets the bit value
product *= a[byte*8+bit];
}
}
// Product for the last byte
for (unsigned bit = 0 ; bit < nbBitsinlastByte ; ++bit)
{
if((b[nbBytes-1] >> bit) & 1) // gets the bit value
product *= a[(nbBytes-1)*8+bit];
}
此乘积计算是我代码中速度较慢的部分。我想知道是否显式向量化(SIMD)这个过程在这里是否有帮助?我一直在看'xmmintrin.h'中提供的功能,但对SIMD知之甚少,未能找到有帮助的东西。你能帮我吗?