首页 > 解决方案 > Rust 汇编输出中的明显无操作?

问题描述

我正在研究以下功能的 x86_64 程序集:

/// Returns the greatest power of two less than or equal to `self`, or 0 otherwise.
pub const fn prev_power_of_two(n: u64) -> u64 {
    // n = 0 gives highest_bit_set_idx = 0.
    let highest_bit_set_idx = 63 - (n|1).leading_zeros();
    // Binary AND of highest bit with n is a no-op, except zero gets wiped.
    (1 << highest_bit_set_idx) & n
}

编译时,-C opt=level=3我们在 nightly 和 1.50.0 上得到以下程序集:

example::prev_power_of_two:
        mov     rax, rdi
        or      rax, 1
        bsr     rcx, rax
        xor     ecx, 63
        xor     cl, 63
        mov     eax, 1
        shl     rax, cl
        and     rax, rdi
        ret

一切似乎都很好,除了我无法解释这两个指令:

    xor     ecx, 63
    xor     cl, 63

据我所知,这些都是无操作的。为什么会生成它们?

标签: assemblyrustbit-manipulationx86-64

解决方案


推荐阅读