您当前的位置: 首页 >  rust

mutourend

暂无认证

  • 3浏览

    0关注

    661博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Rust asm

mutourend 发布时间:2019-09-05 11:47:32 ,浏览量:3

目前未stable,使用前需要#![feature(asm)],调用asm宏的格式为:

asm!(assembly template
   : output operands
   : input operands
   : clobbers
   : options
   );

https://github.com/dalek-cryptography/subtle/blob/master/src/lib.rs中的volatile是一个类型修饰符(type specifier),作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。

/// This function is a best-effort attempt to prevent the compiler
/// from knowing anything about the value of the returned `u8`, other
/// than its type.
///
/// Uses inline asm when available, otherwise it's a no-op.
#[cfg(all(feature = "nightly", not(any(target_arch = "asmjs", target_arch = "wasm32"))))]
fn black_box(input: u8) -> u8 {
    debug_assert!((input == 0u8) | (input == 1u8));

    // Pretend to access a register containing the input.  We "volatile" here
    // because some optimisers treat assembly templates without output operands
    // as "volatile" while others do not.
    unsafe { asm!("" :: "r"(&input) :: "volatile") }

    input
}

参考资料: [1] https://doc.rust-lang.org/1.12.0/book/inline-assembly.html

关注
打赏
1664532908
查看更多评论
立即登录/注册

微信扫码登录

0.0383s