2 ^ N指数的计算确实比位位移效率较低? [英] are 2^n exponent calculations really less efficient than bit-shifts?

查看:218
本文介绍了2 ^ N指数的计算确实比位位移效率较低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我做的:

int x = 4;
pow(2, x);

是不是真的有效不止做少得多:

Is that really that much less efficient than just doing:

1 << 4

推荐答案

是的。一个简单的方法,以显示这是编译下面的两个函数做同样的事情,然后看看拆卸。

Yes. An easy way to show this is to compile the following two functions that do the same thing and then look at the disassembly.

#include <stdint.h>
#include <math.h>

uint32_t foo1(uint32_t shftAmt) {
    return pow(2, shftAmt);
}

uint32_t foo2(uint32_t shftAmt) {
    return (1 << shftAmt);
}

CC -arch的ARMv7 -O3 -S -o - shift.c (我偶然发现ARM汇编更容易阅读,但如果你想86只是删除牌坊标志)

cc -arch armv7 -O3 -S -o - shift.c (I happen to find ARM asm easier to read but if you want x86 just remove the arch flag)

    _foo1:
@ BB#0:
    push    {r7, lr}
    vmov    s0, r0
    mov r7, sp
    vcvt.f64.u32    d16, s0
    vmov    r0, r1, d16
    blx _exp2
    vmov    d16, r0, r1
    vcvt.u32.f64    s0, d16
    vmov    r0, s0
    pop {r7, pc}

_foo2:
@ BB#0:
    movs    r1, #1
    lsl.w   r0, r1, r0
    bx  lr

您可以看到 foo2的只需要2条指令VS foo1 这需要几个指令。它必须将数据移动到FP硬件寄存器( VMOV ),整数转换为浮动( vcvt.f64.u32 )调用 EXP 函数,然后转换的答案回一个UINT( vcvt.u32.f64 )从FP HW将其移回GP的寄存器。

You can see foo2 only takes 2 instructions vs foo1 which takes several instructions. It has to move the data to the FP HW registers (vmov), convert the integer to a float (vcvt.f64.u32) call the exp function and then convert the answer back to an uint (vcvt.u32.f64) and move it from the FP HW back to the GP registers.

这篇关于2 ^ N指数的计算确实比位位移效率较低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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