hilbert_c2rust/
lib.rs

1
2#![allow(dead_code,
3         mutable_transmutes,
4         non_camel_case_types,
5         non_snake_case,
6         non_upper_case_globals,
7         unused_mut)]
8extern crate libc;
9//rotate/flip a quadrant appropriately
10unsafe extern "C" fn rot(mut n: libc::c_uint, mut x: *mut libc::c_uint,
11                             mut y: *mut libc::c_uint, mut _rx: libc::c_uint,
12                             mut _ry: libc::c_uint) -> () {
13    if _ry == 0i32 as libc::c_uint {
14        if _rx == 1i32 as libc::c_uint {
15            *x = n.wrapping_sub(1i32 as libc::c_uint).wrapping_sub(*x);
16            *y = n.wrapping_sub(1i32 as libc::c_uint).wrapping_sub(*y)
17        }
18        //Swap x and y
19        let mut t: libc::c_uint = *x;
20        *x = *y;
21        *y = t
22    };
23}
24//convert (x,y) to d
25unsafe extern "C" fn _xy2d(mut n: libc::c_uint, mut x: libc::c_uint,
26                              mut y: libc::c_uint) -> libc::c_uint {
27    let mut _rx: libc::c_uint = 0;
28    let mut _ry: libc::c_uint = 0;
29    let mut _s: libc::c_uint = 0;
30    let mut d: libc::c_uint = 0i32 as libc::c_uint;
31    _s = n.wrapping_div(2i32 as libc::c_uint);
32    while _s > 0i32 as libc::c_uint {
33        _rx = (x & _s > 0i32 as libc::c_uint) as libc::c_int as libc::c_uint;
34        _ry = (y & _s > 0i32 as libc::c_uint) as libc::c_int as libc::c_uint;
35        d =
36            d.wrapping_add(_s.wrapping_mul(_s).wrapping_mul((3i32 as
37                                                               libc::c_uint).wrapping_mul(_rx)
38                                                              ^ _ry));
39        rot(_s, &mut x, &mut y, _rx, _ry);
40        _s = _s.wrapping_div(2i32 as libc::c_uint)
41    }
42    return d;
43}
44//convert d to (x,y)
45unsafe extern "C" fn _d2xy(mut n: libc::c_uint, mut d: libc::c_uint,
46                              mut x: *mut libc::c_uint,
47                              mut y: *mut libc::c_uint) -> () {
48    let mut _rx: libc::c_uint = 0;
49    let mut _ry: libc::c_uint = 0;
50    let mut _s: libc::c_uint = 0;
51    let mut t: libc::c_uint = d;
52    *y = 0i32 as libc::c_uint;
53    *x = *y;
54    _s = 1i32 as libc::c_uint;
55    while _s < n {
56        _rx = 1i32 as libc::c_uint & t.wrapping_div(2i32 as libc::c_uint);
57        _ry = 1i32 as libc::c_uint & (t ^ _rx);
58        rot(_s, x, y, _rx, _ry);
59        *x = (*x).wrapping_add(_s.wrapping_mul(_rx));
60        *y = (*y).wrapping_add(_s.wrapping_mul(_ry));
61        t = t.wrapping_div(4i32 as libc::c_uint);
62        _s = _s.wrapping_mul(2i32 as libc::c_uint)
63    };
64}
65
66pub fn xy2d(x: libc::c_uint, y: libc::c_uint, n: libc::c_uint) -> libc::c_uint {
67    unsafe {
68        return _xy2d(x, y, n);
69    }
70}
71
72pub fn d2xy(n: libc::c_uint, d: libc::c_uint) -> (libc::c_uint, libc::c_uint) {
73    unsafe {
74        let mut x: libc::c_uint = 0;
75        let mut y: libc::c_uint = 0;
76        _d2xy(n, d, &mut x as *mut libc::c_uint, &mut y as *mut libc::c_uint);
77        return (x, y);
78    }
79}
80
81#[test]
82fn xy2d_simple() {
83    assert_eq!(xy2d(1,1,1), 0);
84    assert_eq!(xy2d(1,1,2), 0);
85    assert_eq!(xy2d(2,1,4), 3);
86}
87
88#[test]
89fn d2xy_simple() {
90    assert_eq!(d2xy(2, 0), (0, 0));
91    assert_eq!(d2xy(2, 1), (0, 1));
92    assert_eq!(d2xy(2, 2), (1, 1));
93    assert_eq!(d2xy(2, 3), (1, 0));
94}