有没有将char扩展为uint64_t的更有效的方法? [英] Is there a more efficient way of expanding a char to an uint64_t?

查看:121
本文介绍了有没有将char扩展为uint64_t的更有效的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将每个位重复8次来将unsigned char膨胀为uint64_t.例如

I want to inflate an unsigned char to an uint64_t by repeating each bit 8 times. E.g.

char -> uint64_t
0x00 -> 0x00
0x01 -> 0xFF
0x02 -> 0xFF00
0x03 -> 0xFFFF
0xAA -> 0xFF00FF00FF00FF00

我目前有以下实现,使用移位测试是否设置了位,以实现此目的:

I currently have the following implementation, using bit shifts to test if a bit is set, to accomplish this:

#include <stdint.h>
#include <inttypes.h>   

#define BIT_SET(var, pos) ((var) & (1 << (pos)))

static uint64_t inflate(unsigned char a)
{
    uint64_t MASK = 0xFF;
    uint64_t result = 0;
    for (int i = 0; i < 8; i++) {
        if (BIT_SET(a, i))
            result |= (MASK << (8 * i));    
    }

    return result;
} 

但是,我对C还是很陌生,所以对单个位的摆弄使我略有不同,可能会有更好(即更有效)的方式来做到这一点.

However, I'm fairly new to C, so this fiddling with individual bits makes me a little vary that there might be a better (i.e. more efficient) way of doing this.

编辑添加
好的,因此在尝试了表查找解决方案之后,这里是结果.但是,请记住,我不是直接测试例程,而是作为更大函数的一部分(准确地说是二进制矩阵的乘法),因此这可能会影响结果的生成方式.因此,在我的计算机上,将一百万个8x8矩阵相乘,并使用以下命令进行编译:

EDIT TO ADD
Ok, so after trying out the table lookup solution, here are the results. However, keep in mind that I didn't test the routine directly, but rather as part of bigger function (a multiplication of binary matrices to be precise), so this might have affected how the results turned out. So, on my computer, when multiplying a million 8x8 matrices, and compiled with:

  gcc -O2 -Wall -std=c99 foo.c

我知道了

./a.out original
real    0m0.127s
user    0m0.124s
sys     0m0.000s

./a.out table_lookup
real    0m0.012s
user    0m0.012s
sys     0m0.000s

因此,至少在我的机器(我应该提到的64位Linux Mint虚拟机)上,表查找方法似乎提供了大约10倍的加速,所以我将其作为答案.

So at least on my machine (a virtual machine 64 bit Linux Mint I should mention), the table lookup approach seems to provide a roughly 10-times speed-up, so I will accept that as the answer.

推荐答案

如果要提高效率,请使用查找表:256个条目的静态数组,每个条目已经保存了所需的结果.您可以使用上面的代码来生成它.

If you're looking for efficiency use a lookup table: a static array of 256 entries, each already holding the required result. You can use your code above to generate it.

这篇关于有没有将char扩展为uint64_t的更有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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