od: refactor sign_extend

easier to understand algoritm which does not use unsafe
This commit is contained in:
Wim Hueskes 2016-07-21 10:20:42 +02:00
parent e0b7ff1953
commit 3e143217a9

View file

@ -424,14 +424,8 @@ fn print_item_hex(p: u64, itembytes: usize) {
fn sign_extend(item: u64, itembytes: usize) -> i64{ fn sign_extend(item: u64, itembytes: usize) -> i64{
// https://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend let shift = 64 - itembytes * 8;
unsafe{ (item << shift) as i64 >> shift
let b = 8 * itembytes; // number of bits representing the number in p
let m = mem::transmute::<u64,i64>(1u64 << (b - 1));
let x = mem::transmute::<u64,i64>(item) & (mem::transmute::<u64,i64>(1u64 << b) - 1);
let r = (x ^ m) - m;
r
}
} }