Ant*_*per 5 c algorithm assembly des
我几乎用C语言实现了DES算法,我想优化我的代码.所以我用过gprof
.以下是报告的一部分:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls us/call us/call name
51.78 9.32 9.32 8000000 1.17 1.17 sboxes
34.71 15.57 6.25 8000000 0.78 0.78 extendRight
9.90 17.35 1.78 500000 3.56 35.96 operation
2.39 17.78 0.43 8000000 0.05 0.05 xorRightAndKey
Run Code Online (Sandbox Code Playgroud)
gprof
表明该sboxes
功能占51.78%的时间.
在sboxes(uchar aucData[6], ...)
,我被给予48位,将它们分成8个插槽,每个插槽为6位.
每个插槽:
将第一位与最后一位组合得到X
;
获得中间4位得到Y
;
做的东西(X, Y)
;
例如,011110
是一个插槽,所以X = 00
和Y = 1111
.
为实现这一点,我在内存中写了MACRO到GET/SET位,这里是相对代码:
#define LOCATE(ptr, index) (((char *)(ptr))[(index) >> 3])
#define GET_BIT(ptr, index) (LOCATE((ptr), (index)) & (((uchar)0x80) >> ((index) % 8)))
Run Code Online (Sandbox Code Playgroud)
以下是获取的代码 (X, Y)
uchar basePos = 0x00;
for (int i = 0; i < 8; ++i) {
x = 0;
y = 0;
basePos = i * 6; // to locate the slot
// combine first bit with last bit
if (0 != GET_BIT(aucData, basePos)) {
x |= 0x02;
}
if (0 != GET_BIT(aucData, basePos + 5)) {
x |= 0x01;
}
// get continuous 4 bits
for (int j = 1; j <= 4; ++j) {
if (0 != GET_BIT(aucData, basePos + j)) {
y |= (0x01 << (4 - j));
}
}
// do something with (x, y)
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,我得到了48位,如何尽快获得中间的4位?
没有查找表:
typedef unsigned long long u64;
void sboxes(uchar aucData[6])
{
u64 v = aucData[0] + (((u64)aucData[1]) << 8)
+ (((u64)aucData[2]) << 16)
+ (((u64)aucData[3]) << 24)
+ (((u64)aucData[4]) << 32)
+ (((u64)aucData[5]) << 40);
for(int i = 0; i < 8; i++)
{
uchar x = ((v & 1) << 1) | ((v >> 5) & 1);
uchar y = ((v >> 1) & 0xF);
// do something with x, y
printf("x: %hhu, y: %hhu\n", x, y);
v >>= 6;
}
}
Run Code Online (Sandbox Code Playgroud)
完整免责声明:我没有进行基准测试。但应该很快。如果仍然太慢,您也许可以更快地打包到 u64 中。
归档时间: |
|
查看次数: |
265 次 |
最近记录: |