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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright © 2016–2018 University of Malta

// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.

//! Aribtrary-precision integers.
//!
//! This module provides support for arbitrary-precision integers of
//! type [`Integer`](../struct.Integer.html). Instances of `Integer`
//! always have a heap allocation for the bit data; if you want a
//! temporary small integer without heap allocation, you can use the
//! [`SmallInteger`](struct.SmallInteger.html) type.
//!
//! # Examples
//!
//! ```rust
//! use rug::{Assign, Integer};
//! use rug::integer::SmallInteger;
//! let mut int = Integer::from(10);
//! assert_eq!(int, 10);
//! let small = SmallInteger::from(-15);
//! // `small` behaves like an `Integer` in the following line:
//! int.assign(small.abs_ref());
//! assert_eq!(int, 15);
//! ```

mod arith;
pub(crate) mod big;
mod cmp;
mod division;
#[cfg(feature = "serde")]
mod serde;
pub(crate) mod small;
mod traits;

pub use integer::big::{IsPrime, ParseIntegerError, ValidInteger};
pub use integer::small::SmallInteger;

#[cfg(test)]
mod tests {
    use {Assign, Integer};
    use gmp_mpfr_sys::gmp;
    use ops::NegAssign;
    use std::{f32, f64, i32, i64, u32, u64};
    use std::mem;

    #[test]
    fn check_int_conversions() {
        let mut i = Integer::from(-1);
        assert_eq!(i.to_u32_wrapping(), u32::MAX);
        assert_eq!(i.to_i32_wrapping(), -1);
        i.assign(0xff000000u32);
        i <<= 4;
        assert_eq!(i.to_u32_wrapping(), 0xf0000000u32);
        assert_eq!(i.to_i32_wrapping(), 0xf0000000u32 as i32);
        i = i.clone() << 32 | i;
        assert_eq!(i.to_u32_wrapping(), 0xf0000000u32);
        assert_eq!(i.to_i32_wrapping(), 0xf0000000u32 as i32);
        i.neg_assign();
        assert_eq!(i.to_u32_wrapping(), 0x10000000u32);
        assert_eq!(i.to_i32_wrapping(), 0x10000000i32);
    }

    #[test]
    fn check_option_conversion() {
        let mut i = Integer::new();
        assert_eq!(i.to_u32(), Some(0));
        assert_eq!(i.to_i32(), Some(0));
        assert_eq!(i.to_u64(), Some(0));
        assert_eq!(i.to_i64(), Some(0));
        i -= 1;
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), Some(-1));
        assert_eq!(i.to_u64(), None);
        assert_eq!(i.to_i64(), Some(-1));

        i.assign(i32::MIN);
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), Some(i32::MIN));
        assert_eq!(i.to_u64(), None);
        assert_eq!(i.to_i64(), Some(i32::MIN as i64));
        i -= 1;
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), None);
        assert_eq!(i.to_i64(), Some(i32::MIN as i64 - 1));
        i.assign(i32::MAX);
        assert_eq!(i.to_u32(), Some(i32::MAX as u32));
        assert_eq!(i.to_i32(), Some(i32::MAX));
        assert_eq!(i.to_u64(), Some(i32::MAX as u64));
        assert_eq!(i.to_i64(), Some(i32::MAX as i64));
        i += 1;
        assert_eq!(i.to_u32(), Some(i32::MAX as u32 + 1));
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), Some(i32::MAX as u64 + 1));
        assert_eq!(i.to_i64(), Some(i32::MAX as i64 + 1));
        i.assign(u32::MAX);
        assert_eq!(i.to_u32(), Some(u32::MAX));
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), Some(u32::MAX as u64));
        assert_eq!(i.to_i64(), Some(u32::MAX as i64));
        i += 1;
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), Some(u32::MAX as u64 + 1));
        assert_eq!(i.to_i64(), Some(u32::MAX as i64 + 1));

        i.assign(i64::MIN);
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), None);
        assert_eq!(i.to_i64(), Some(i64::MIN));
        i -= 1;
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), None);
        assert_eq!(i.to_i64(), None);
        i.assign(i64::MAX);
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), Some(i64::MAX as u64));
        assert_eq!(i.to_i64(), Some(i64::MAX));
        i += 1;
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), Some(i64::MAX as u64 + 1));
        assert_eq!(i.to_i64(), None);
        i.assign(u64::MAX);
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), Some(u64::MAX));
        assert_eq!(i.to_i64(), None);
        i += 1;
        assert_eq!(i.to_u32(), None);
        assert_eq!(i.to_i32(), None);
        assert_eq!(i.to_u64(), None);
        assert_eq!(i.to_i64(), None);
    }

    #[test]
    fn check_float_conversions() {
        let mut i = Integer::from(0);
        assert_eq!(i.to_f32(), 0.0);
        assert_eq!(i.to_f64(), 0.0);
        i.assign(0xff);
        assert_eq!(i.to_f32(), 255.0);
        assert_eq!(i.to_f64(), 255.0);
        i <<= 80;
        assert_eq!(i.to_f32(), 255.0 * 2f32.powi(80));
        assert_eq!(i.to_f64(), 255.0 * 2f64.powi(80));
        i = i.clone() << 30 | i;
        assert_eq!(i.to_f32(), 255.0 * 2f32.powi(110));
        assert_eq!(i.to_f64(), 255.0 * (2f64.powi(80) + 2f64.powi(110)));
        i <<= 100;
        assert_eq!(i.to_f32(), f32::INFINITY);
        assert_eq!(i.to_f64(), 255.0 * (2f64.powi(180) + 2f64.powi(210)));
        i <<= 1000;
        assert_eq!(i.to_f32(), f32::INFINITY);
        assert_eq!(i.to_f64(), f64::INFINITY);
        i.assign(-0xff_ffff);
        assert_eq!(i.to_f32(), -0xff_ffff as f32);
        assert_eq!(i.to_f64(), -0xff_ffff as f64);
        i.assign(-0xfff_ffff);
        assert_eq!(i.to_f32(), -0xff_ffff0 as f32);
        assert_eq!(i.to_f64(), -0xff_fffff as f64);
    }

    #[test]
    fn check_from_str() {
        let mut i: Integer = "+134".parse().unwrap();
        assert_eq!(i, 134);
        i.assign_str_radix("-ffFFffffFfFfffffffffffffffffffff", 16)
            .unwrap();
        assert_eq!(i.significant_bits(), 128);
        i -= 1;
        assert_eq!(i.significant_bits(), 129);

        let bad_strings = [
            ("1\0", None),
            ("1_2", None),
            (" 1", None),
            ("+-3", None),
            ("-+3", None),
            ("++3", None),
            ("--3", None),
            ("0+3", None),
            ("0 ", None),
            ("", None),
            ("80", Some(8)),
            ("0xf", Some(16)),
            ("9", Some(9)),
        ];
        for &(s, radix) in bad_strings.into_iter() {
            assert!(Integer::valid_str_radix(s, radix.unwrap_or(10)).is_err());
        }
        let good_strings = [
            ("0", 10, 0),
            ("+0", 16, 0),
            ("-0", 2, 0),
            ("99", 10, 99),
            ("+Cc", 16, 0xcc),
            ("-77", 8, -0o77),
        ];
        for &(s, radix, i) in good_strings.into_iter() {
            assert_eq!(Integer::from_str_radix(s, radix).unwrap(), i);
        }
    }

    #[test]
    fn check_formatting() {
        let i = Integer::from(-11);
        assert_eq!(format!("{}", i), "-11");
        assert_eq!(format!("{:?}", i), "-11");
        assert_eq!(format!("{:b}", i), "-1011");
        assert_eq!(format!("{:#b}", i), "-0b1011");
        assert_eq!(format!("{:o}", i), "-13");
        assert_eq!(format!("{:#o}", i), "-0o13");
        assert_eq!(format!("{:x}", i), "-b");
        assert_eq!(format!("{:X}", i), "-B");
        assert_eq!(format!("{:8x}", i), "      -b");
        assert_eq!(format!("{:08X}", i), "-000000B");
        assert_eq!(format!("{:#08x}", i), "-0x0000b");
        assert_eq!(format!("{:#8X}", i), "    -0xB");
    }

    #[test]
    fn check_assumptions() {
        // we assume no nail bits when we use limbs
        assert_eq!(gmp::NAIL_BITS, 0);
        assert_eq!(gmp::NUMB_BITS, gmp::LIMB_BITS);
        assert_eq!(gmp::NUMB_BITS as usize, 8 * mem::size_of::<gmp::limb_t>());
        // we assume that a limb has 32 or 64 bits.
        assert!(gmp::NUMB_BITS == 32 || gmp::NUMB_BITS == 64);

        // check that target_pointer_width is 32 or 64
        #[cfg(not(any(target_pointer_width = "32",
                      target_pointer_width = "64")))]
        panic!("target_pointer_width is not 32 or 64");
    }
}