about summary refs log tree commit diff
path: root/src/libcore/uint-template/uint.rs
blob: 1c635db2f5423d5b1170b29467296cd6aae0cf4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Operations and constants for `uint`

pub use inst::{
    div_ceil, div_round, div_floor, iterate,
    next_power_of_two
};

mod inst {
    pub type T = uint;

    #[cfg(target_arch = "x86")]
    #[cfg(target_arch = "arm")]
    pub const bits: uint = 32;

    #[cfg(target_arch = "x86_64")]
    pub const bits: uint = 64;

    /**
    * Divide two numbers, return the result, rounded up.
    *
    * # Arguments
    *
    * * x - an integer
    * * y - an integer distinct from 0u
    *
    * # Return value
    *
    * The smallest integer `q` such that `x/y <= q`.
    */
    pub pure fn div_ceil(x: uint, y: uint) -> uint {
        let div = x / y;
        if x % y == 0u { div }
        else { div + 1u }
    }

    /**
    * Divide two numbers, return the result, rounded to the closest integer.
    *
    * # Arguments
    *
    * * x - an integer
    * * y - an integer distinct from 0u
    *
    * # Return value
    *
    * The integer `q` closest to `x/y`.
    */
    pub pure fn div_round(x: uint, y: uint) -> uint {
        let div = x / y;
        if x % y * 2u  < y { div }
        else { div + 1u }
    }

    /**
    * Divide two numbers, return the result, rounded down.
    *
    * Note: This is the same function as `div`.
    *
    * # Arguments
    *
    * * x - an integer
    * * y - an integer distinct from 0u
    *
    * # Return value
    *
    * The smallest integer `q` such that `x/y <= q`. This
    * is either `x/y` or `x/y + 1`.
    */
    pub pure fn div_floor(x: uint, y: uint) -> uint { return x / y; }

    /**
    * Iterate over the range [`lo`..`hi`), or stop when requested
    *
    * # Arguments
    *
    * * lo - The integer at which to start the loop (included)
    * * hi - The integer at which to stop the loop (excluded)
    * * it - A block to execute with each consecutive integer of the range.
    *        Return `true` to continue, `false` to stop.
    *
    * # Return value
    *
    * `true` If execution proceeded correctly, `false` if it was interrupted,
    * that is if `it` returned `false` at any point.
    */
    pub pure fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
        let mut i = lo;
        while i < hi {
            if (!it(i)) { return false; }
            i += 1u;
        }
        return true;
    }

    /// Returns the smallest power of 2 greater than or equal to `n`
    #[inline(always)]
    pub fn next_power_of_two(n: uint) -> uint {
        let halfbits: uint = sys::size_of::<uint>() * 4u;
        let mut tmp: uint = n - 1u;
        let mut shift: uint = 1u;
        while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
        return tmp + 1u;
    }

    #[test]
    fn test_next_power_of_two() {
        assert (uint::next_power_of_two(0u) == 0u);
        assert (uint::next_power_of_two(1u) == 1u);
        assert (uint::next_power_of_two(2u) == 2u);
        assert (uint::next_power_of_two(3u) == 4u);
        assert (uint::next_power_of_two(4u) == 4u);
        assert (uint::next_power_of_two(5u) == 8u);
        assert (uint::next_power_of_two(6u) == 8u);
        assert (uint::next_power_of_two(7u) == 8u);
        assert (uint::next_power_of_two(8u) == 8u);
        assert (uint::next_power_of_two(9u) == 16u);
        assert (uint::next_power_of_two(10u) == 16u);
        assert (uint::next_power_of_two(11u) == 16u);
        assert (uint::next_power_of_two(12u) == 16u);
        assert (uint::next_power_of_two(13u) == 16u);
        assert (uint::next_power_of_two(14u) == 16u);
        assert (uint::next_power_of_two(15u) == 16u);
        assert (uint::next_power_of_two(16u) == 16u);
        assert (uint::next_power_of_two(17u) == 32u);
        assert (uint::next_power_of_two(18u) == 32u);
        assert (uint::next_power_of_two(19u) == 32u);
        assert (uint::next_power_of_two(20u) == 32u);
        assert (uint::next_power_of_two(21u) == 32u);
        assert (uint::next_power_of_two(22u) == 32u);
        assert (uint::next_power_of_two(23u) == 32u);
        assert (uint::next_power_of_two(24u) == 32u);
        assert (uint::next_power_of_two(25u) == 32u);
        assert (uint::next_power_of_two(26u) == 32u);
        assert (uint::next_power_of_two(27u) == 32u);
        assert (uint::next_power_of_two(28u) == 32u);
        assert (uint::next_power_of_two(29u) == 32u);
        assert (uint::next_power_of_two(30u) == 32u);
        assert (uint::next_power_of_two(31u) == 32u);
        assert (uint::next_power_of_two(32u) == 32u);
        assert (uint::next_power_of_two(33u) == 64u);
        assert (uint::next_power_of_two(34u) == 64u);
        assert (uint::next_power_of_two(35u) == 64u);
        assert (uint::next_power_of_two(36u) == 64u);
        assert (uint::next_power_of_two(37u) == 64u);
        assert (uint::next_power_of_two(38u) == 64u);
        assert (uint::next_power_of_two(39u) == 64u);
    }

    #[test]
    fn test_overflows() {
        assert (uint::max_value > 0u);
        assert (uint::min_value <= 0u);
        assert (uint::min_value + uint::max_value + 1u == 0u);
    }

    #[test]
    fn test_div() {
        assert(uint::div_floor(3u, 4u) == 0u);
        assert(uint::div_ceil(3u, 4u)  == 1u);
        assert(uint::div_round(3u, 4u) == 1u);
    }
}