Rust 1.59: stable inline assembly — no more C for low-level

Posted by Arjun Pandava · 2022-02-20

The asm! macro is finally stable. Write bare-metal code and SIMD intrinsics in pure Rust. No C FFI needed. Here's how to read the CPU cycle counter. https://github.com/rust-lang/rust/releases/tag/1.59.0

// Rust 1.59 stable inline assembly
use std::arch::asm;

fn read_tsc() -> u64 {
    let lo: u32;
    let hi: u32;
    unsafe {
        asm!(
            "rdtsc",
            out("eax") lo,
            out("edx") hi,
            options(nomem, nostack)
        );
    }
    ((hi as u64) << 32) | (lo as u64)
}

Explore GitVibes