如何从不进行字节对齐(移位)的源复制内存

发布于 2024-09-24 02:26:06 字数 135 浏览 1 评论 0原文

我可以想到一些令人讨厌的低效方法来完成这项任务,但我想知道最好的方法是什么。

例如,我想复制一个字节中从第 3 位开始的 10 个字节,并像往常一样复制到指针。

有没有比一次复制一个移位字节更好的方法?

谢谢

I can think of some nasty inefficient ways to accomplish this task, but I'm wondering what the best way is.

For example I want to copy 10 bytes starting at the 3rd bit in a byte and copy to a pointer as usual.

Is there a better way than to copy one shifted byte at a time?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔女人霸气范 2024-10-01 02:26:06

一般方法是尽可能高效地读取源缓冲区,并在写入目标缓冲区时根据需要进行移位。

您不必执行字节操作,您始终可以通过在开头执行最多三个字节并类似地处理结尾来使源读取与大部分操作对齐,因为您不应该不要尝试读取超过规定的源缓冲区长度。

根据读取的值,您可以根据需要进行移位以获得所需的位对齐并组装完成的字节以写入目标。您还可以对写入进行相同的优化,以达到尽可能宽的对齐字大小。

如果您深入研究广泛使用可变宽度标记(zlib、MPEG、TIFF 和 JPEG)的压缩工具或库的源代码,您可能会发现将输入或输出缓冲区视为需要考虑一些实现想法的比特流。

The general approach is to read the source buffer as efficiently as possible, and shift it as required on the way to writing the destination buffer.

You don't have to do byte operations, you can always get the source reads long aligned for the bulk of the operation by doing up to three bytes at the beginning, and similarly handling the end since you shouldn't attempt to read past the stated source buffer length.

From the values read, you shift as required to get the bit alignment desired and assemble finished bytes for writing to the destination. You can also do the same optimization of writes to the widest aligned word size you can.

If you dig around in the source to a compression tool or library that makes extensive use of variable-width tokens (zlib, MPEG, TIFF, and JPEG all leap to mind) you will likely find sample code that treats an input or output buffer as a stream of bits that will have some implementation ideas to think about.

不即不离 2024-10-01 02:26:06

在 x86 上,您可以访问的最小单位是字节。但是,您可以一次访问 4 个字节,并且一次使用 4 个字节而不是一个字节。为了获得更快的速度,您可以使用 pslldq (SSE2)。当然,请确保您的副本对齐以获得最佳性能。

On x86 you the smallest unit you can access is a byte. However you can access 4 bytes at a time and work with 4 bytes at a time instead of one byte. For greater speeds you can use pslldq (SSE2). Of course, make sure you copies are aligned for maximum performance.

我不会写诗 2024-10-01 02:26:06

这是我编码并开始使用的解决方案。

void RightShiftMemCopy(uchar * pSource, uchar * pDest ,ushort len,uchar shiftOffset)
{
    ushort i=0;

    pDest+=(len-1);
    pSource+=(len-1);

    for(i=len-1;i != 0 ;--i)
    {
        *pDest = (*(pSource - 1) << 8 | *pSource) >> shiftOffset;

        --pDest;
        --pSource;
    }

    *pDest = *pSource >> shiftOffset;

}

This is the solution I coded, and started using.

void RightShiftMemCopy(uchar * pSource, uchar * pDest ,ushort len,uchar shiftOffset)
{
    ushort i=0;

    pDest+=(len-1);
    pSource+=(len-1);

    for(i=len-1;i != 0 ;--i)
    {
        *pDest = (*(pSource - 1) << 8 | *pSource) >> shiftOffset;

        --pDest;
        --pSource;
    }

    *pDest = *pSource >> shiftOffset;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文