Last active
December 27, 2015 21:09
-
-
Save luqmana/7389412 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(asm, macro_rules)] | |
use std::mem; | |
fn rotl(val: u64, shift: u8) -> u64 { | |
let sz = mem::size_of_val(&val); | |
(val << shift) | (val >> (sz * 8 - shift as uint)) | |
} | |
macro_rules! rotl( | |
($val: expr, $shift: expr) => ( | |
{ | |
let val = $val; | |
let shift = $shift; | |
let sz = mem::size_of_val(&val); | |
(val << shift) | (val >> (sz * 8 - shift as uint)) | |
} | |
); | |
) | |
fn asm_rotl(mut x: u64, a: u8) -> u64 { | |
unsafe { | |
asm!("rolq $2, $0" | |
: "=r"(x) | |
: "0"(x), "{cl}"(a)); | |
} | |
x | |
} | |
fn main() { | |
println!("rotl(32, 2) - {}", rotl(32, 2)); | |
println!("rotl!(32, 2) - {}", rotl!(32, 2)); | |
println!("asm_rotl(32, 2) - {}", asm_rotl(32, 2)); | |
} |
Author
luqmana
commented
Nov 9, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment