Skip to content

Instantly share code, notes, and snippets.

@brainstorm
Last active January 16, 2020 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainstorm/f76da194a03730a98e23766134d54d7b to your computer and use it in GitHub Desktop.
Save brainstorm/f76da194a03730a98e23766134d54d7b to your computer and use it in GitHub Desktop.
SAMv1 specification: C, C2Rust and Rust source code for computing bin number and overlapping bins
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case,
non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
pub type __uint16_t = libc::c_ushort;
pub type uint16_t = __uint16_t;
#[no_mangle]
pub unsafe extern "C" fn reg2bins(mut beg: libc::c_int, mut end: libc::c_int,
mut list: *mut uint16_t) -> libc::c_int {
let mut i: libc::c_int = 0 as libc::c_int;
let mut k: libc::c_int = 0;
end -= 1;
let fresh0 = i;
i = i + 1;
*list.offset(fresh0 as isize) = 0 as libc::c_int as uint16_t;
k = 1 as libc::c_int + (beg >> 26 as libc::c_int);
while k <= 1 as libc::c_int + (end >> 26 as libc::c_int) {
let fresh1 = i;
i = i + 1;
*list.offset(fresh1 as isize) = k as uint16_t;
k += 1
}
k = 9 as libc::c_int + (beg >> 23 as libc::c_int);
while k <= 9 as libc::c_int + (end >> 23 as libc::c_int) {
let fresh2 = i;
i = i + 1;
*list.offset(fresh2 as isize) = k as uint16_t;
k += 1
}
k = 73 as libc::c_int + (beg >> 20 as libc::c_int);
while k <= 73 as libc::c_int + (end >> 20 as libc::c_int) {
let fresh3 = i;
i = i + 1;
*list.offset(fresh3 as isize) = k as uint16_t;
k += 1
}
k = 585 as libc::c_int + (beg >> 17 as libc::c_int);
while k <= 585 as libc::c_int + (end >> 17 as libc::c_int) {
let fresh4 = i;
i = i + 1;
*list.offset(fresh4 as isize) = k as uint16_t;
k += 1
}
k = 4681 as libc::c_int + (beg >> 14 as libc::c_int);
while k <= 4681 as libc::c_int + (end >> 14 as libc::c_int) {
let fresh5 = i;
i = i + 1;
*list.offset(fresh5 as isize) = k as uint16_t;
k += 1
}
return i;
}
#include <stdlib.h>
#include <stdio.h>
#define MAX_BIN (((1<<18)-1)/7)
void reg2bins(int beg, int end, u_int16_t list[MAX_BIN])
{
int i = 0, k;
--end;
list[i++] = 0;
for (k = 1 + (beg>>26); k <= 1 + (end>>26); ++k) list[i++] = k;
for (k = 9 + (beg>>23); k <= 9 + (end>>23); ++k) list[i++] = k;
for (k = 73 + (beg>>20); k <= 73 + (end>>20); ++k) list[i++] = k;
for (k = 585 + (beg>>17); k <= 585 + (end>>17); ++k) list[i++] = k;
for (k = 4681 + (beg>>14); k <= 4681 + (end>>14); ++k) list[i++] = k;
}
int main() {
u_int16_t list[MAX_BIN];
reg2bins(1, 10, &list[MAX_BIN]);
// View what's in there
for (int i=0; i<sizeof(list)/sizeof(u_int16_t); i++) {
printf("%d\n", list[i]);
}
}
// This is the SAM spec C code, oxidized by hand.
pub fn reg2bins(beg: u64, mut end: u64) -> Vec<u64> {
let mut k: u64;
end -= 1;
let mut list = Vec::<u64>::new();
k = 1 + (beg >> 26);
while k <= 1 + (end >> 26) { list.push(k); k += 1 }
k = 9 + (beg >> 23);
while k <= 9 + (end >> 23) { list.push(k); k += 1 }
k = 73 + (beg >> 20);
while k <= 73 + (end >> 20) { list.push(k); k += 1 }
k = 585 + (beg >> 17);
while k <= 585 + (end >> 17) { list.push(k); k += 1 }
k = 4681 + (beg >> 14);
while k <= 4681 + (end >> 14) { list.push(k); k += 1 }
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment