gpt4 book ai didi

c++ - 将字节数组 (uint8_t) 转换为字数组 (uint16_t),反之亦然

转载 作者:行者123 更新时间:2023-11-28 04:36:50 34 4
gpt4 key购买 nike

我有一段非常关键的代码需要优化,它将字节数组转换为单词数组,反之亦然。该操作用于在 8 位和 16 位图像数据之间进行转换。

数组是 qword 对齐的并且足够大以存储结果。

从字节到字的转换需要乘以257(所以0转换为0,255得到65535)

一个简单的解决方案可能是

void simpleBytesToWords(void *ptr, int pixelCount)
{
for (int i = pixelCount - 1; i >= 0; --i)
reinterpret_cast<uint16_t*>(ptr)[i] = reinterpret_cast<uint8_t*>(ptr)[i] * 0x101;
}

我还尝试通过一次转换 4 个字节来使用 64 位寄存器来提高性能:

void bytesToWords(void *ptr, int pixelCount)
{
const auto fastCount = pixelCount / 4;

if (fastCount > 0)
{
for (int f = fastCount-1; f >= 0; --f)
{
auto bytes = uint64_t{ reinterpret_cast<const uint32_t*>(ptr)[f] };

auto r2 = uint64_t{ bytes & 0xFF };
bytes <<= 8;
r2 |= bytes & 0xFF0000;
bytes <<= 8;
r2 |= bytes & 0xFF00000000ull;
bytes <<= 8;
r2 |= bytes & 0xFF000000000000ull;

r2 *= 0x101;

reinterpret_cast<uint64_t*>(ptr)[f] = r2;
}
}

if (pixelCount % 4)
{
auto source = reinterpret_cast<const uint8_t*>(ptr);
auto target = reinterpret_cast<uint16_t*>(ptr);

for (int i = fastCount * 4; i < pixelCount; ++i)
{
target[i] = (source[i] << 8) | source[i];
}
}

}

它正在运行,并且比简单的解决方案稍微快一些。

另一个方向(字到字节)是用这段代码完成的:

for (int i = 0; i < pixelCount; ++i)
reinterpret_cast<uint8_t*>(bufferPtr)[i] = reinterpret_cast<uint16_t*>(bufferPtr)[i] / 256;

我一直在寻找编译器内在函数来加速这种转换,但没有找到任何有用的东西。是否有任何其他方法可以提高此转换的性能?

最佳答案

编译您的代码后我尝试了两件事(我刚刚重命名了 bytesToWords(),现在是下面的 groupedBytesToWords()):

  • 测试您的两个函数:它们不会产生相同的结果。使用 simpleBytesToWords() 我最终得到一个零填充数组。使用 groupedBytesToWords() 我最终得到有效结果和零的交替。

  • 在不更改它们的情况下,假设错误修复不会改变它们的复杂性,我尝试了我编写的第三个,它使用了预先计算的 uint8_t -> uint16_t最初必须构建的表:

这是这张 table 。它很小,因为它只有 255 个条目,每个可能的 uint8_t:

// Build a precalculation table for each possible uint8_t -> uint16_t conversion 
const size_t sizeTable(std::numeric_limits<uint8_t>::max());

uint16_t * precalc_table = new uint16_t[sizeTable];

for (uint16_t i = 0; i < sizeTable; ++i)
{
precalc_table[i] = i * 0x101;
}

我尝试的第三个功能如下:

void hopefullyFastBytesToWords(uint16_t *ptr, size_t pixelCount, uint16_t const * precalc_table)
{
for (size_t i = 0; i < pixelCount; ++i)
{
ptr[i] = precalc_table[ptr[i]];
}
}

我当然测试了它,它产生的结果看起来与您在原始帖子中所做的描述一致。通过传递与其他两个函数相同的参数以及预先计算的转换表来调用此函数:

hopefullyFastBytesToWords(buffer, sizeBuf, precalc_table);

然后我做了一些比较,使用 500000000 uint16_t 长数组,最初填充了随机 uint8_t 值。这是一个使用您编写的 simpleBytesToWords() 的示例:

fillBuffer(buffer, sizeBuf);
begin = clock();
simpleBytesToWords(buffer, sizeBuf);
end = clock();
std::cout << "simpleBytesToWords(): " << (double(end - begin) / CLOCKS_PER_SEC) << std::endl;

我获得了以下结果(您会看到我使用了一台小而慢的笔记本电脑)。以下是三个示例,但它们都始终如一地产生相似大小的值:

$ Sandbox.exe
simpleBytesToWords(): 0.681
groupedBytesToWords(): 1.2
hopefullyFastBytesToWords(): 0.461

$ Sandbox.exe
simpleBytesToWords(): 0.737
groupedBytesToWords(): 1.251
hopefullyFastBytesToWords(): 0.414

$ Sandbox.exe
simpleBytesToWords(): 0.582
groupedBytesToWords(): 1.173
hopefullyFastBytesToWords(): 0.436

当然,这并不代表真正有效的基准测试,但它表明您的“分组”功能在我的机器上速度较慢,这与您获得的结果不符。它还表明,预先计算乘法而不是动态转换/乘法会有所帮助。

关于c++ - 将字节数组 (uint8_t) 转换为字数组 (uint16_t),反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176899/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com