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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
//! This implements the integer trait which denotes a 32 bit unsigned integer
use super::binary::*;
use super::hex::*;
use std::marker::PhantomData;
mod impls;
use impls::*;
/// A struct which generics represents an unique integer from 0 to 2 ** 32 - 1
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
/// // a has type TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>
/// ```
#[derive(Clone, Copy)]
pub struct TypedInteger<H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex> {
_m0: PhantomData<H0>,
_m1: PhantomData<H1>,
_m2: PhantomData<H2>,
_m3: PhantomData<H3>,
_m4: PhantomData<H4>,
_m5: PhantomData<H5>,
_m6: PhantomData<H6>,
_m7: PhantomData<H7>
}
impl<H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex> TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> {
pub const fn new() -> Self {
TypedInteger {
_m0: PhantomData,
_m1: PhantomData,
_m2: PhantomData,
_m3: PhantomData,
_m4: PhantomData,
_m5: PhantomData,
_m6: PhantomData,
_m7: PhantomData
}
}
/// Returns the value of the Typed integer
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
/// assert_eq!(a.number(), 3);
/// ```
pub const fn number() -> u32 {
H0::NUMBER + 16 * H1::NUMBER + 256 * H2::NUMBER + 4096 * H3::NUMBER + 65536 * H4::NUMBER + 1048576 * H5::NUMBER + 16777216 * H6::NUMBER + 268435456 * H7::NUMBER
}
}
/// A trait that denotes whether something is an integer
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a is 3
/// fn is_3<T>(_a: T) where
/// T: IsInteger,
/// T: TypedAssertEqual<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>>
/// {}
///
/// is_3(a);
/// ```
pub trait IsInteger: Copy {
type Hex0: Hex; type Hex1: Hex; type Hex2: Hex; type Hex3: Hex; type Hex4: Hex; type Hex5: Hex; type Hex6: Hex; type Hex7: Hex;
fn number() -> u32;
}
impl<H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex> IsInteger for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> {
type Hex0 = H0; type Hex1 = H1; type Hex2 = H2; type Hex3 = H3; type Hex4 = H4; type Hex5 = H5; type Hex6 = H6; type Hex7 = H7;
fn number() -> u32 {
H0::NUMBER + 16 * H1::NUMBER + 256 * H2::NUMBER + 4096 * H3::NUMBER + 65536 * H4::NUMBER + 1048576 * H5::NUMBER + 16777216 * H6::NUMBER + 268435456 * H7::NUMBER
}
}
/// A trait that asserts two integers are equal
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a is 3
/// fn is_3<T>(_a: T) where
/// T: IsInteger,
/// T: TypedAssertEqual<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>>
/// {}
///
/// is_3(a);
/// ```
pub trait TypedAssertEqual<N: IsInteger> {}
impl<N: IsInteger> TypedAssertEqual<N> for TypedInteger<N::Hex0, N::Hex1, N::Hex2, N::Hex3, N::Hex4, N::Hex5, N::Hex6, N::Hex7> {}
/// A trait that returns a binary depending on whether two integers are equal
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a <= 5 but not a < 3
/// fn example<T, R, S>(_a: T) where
/// T: IsInteger,
/// R: Binary,
/// S: Binary,
/// T: TypedEqual<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>, Output = R>,
/// R: AssertTrue,
/// T: TypedEqual<TypedInteger<_5, _0, _0, _0, _0, _0, _0, _0>, Output = S>,
/// S: AssertFalse
/// {}
///
/// example(a);
/// ```
pub trait TypedEqual<N: IsInteger> { type Output: Binary; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: Binary> TypedEqual<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
N: _Equal<TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>, Output = R> {
type Output = R;
}
/// A trait that returns a binary depending on whether a < b
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a <= 5 but not a < 3
/// fn example<T, R, S>(_a: T) where
/// T: IsInteger,
/// R: Binary,
/// S: Binary,
/// T: TypedLessThan<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>, Output = R>,
/// R: AssertFalse,
/// T: TypedLessThan<TypedInteger<_5, _0, _0, _0, _0, _0, _0, _0>, Output = S>,
/// S: AssertTrue
/// {}
///
/// example(a);
/// ```
pub trait TypedLessThan<N: IsInteger> { type Output: Binary; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: Binary> TypedLessThan<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _LessThan<N, Output = R> {
type Output = R;
}
/// A trait that returns a binary depending on whether two integers are less or equal
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a <= 5 but not a < 3
/// fn example<T, R, S>(_a: T) where
/// T: IsInteger,
/// R: Binary,
/// S: Binary,
/// T: TypedLeq<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>, Output = R>,
/// R: AssertTrue,
/// T: TypedLeq<TypedInteger<_5, _0, _0, _0, _0, _0, _0, _0>, Output = S>,
/// S: AssertTrue
/// {}
///
/// example(a);
/// ```
pub trait TypedLeq<N: IsInteger> { type Output: Binary; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: Binary> TypedLeq<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _Leq<N, Output = R> {
type Output = R;
}
/// A trait that returns a binary depending on whether two integers are greater or equal
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a <= 5 but not a < 3
/// fn example<T, R, S>(_a: T) where
/// T: IsInteger,
/// R: Binary,
/// S: Binary,
/// T: TypedGeq<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>, Output = R>,
/// R: AssertTrue,
/// T: TypedGeq<TypedInteger<_5, _0, _0, _0, _0, _0, _0, _0>, Output = S>,
/// S: AssertFalse
/// {}
///
/// example(a);
/// ```
pub trait TypedGeq<N: IsInteger> { type Output: Binary; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: Binary> TypedGeq<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _Geq<N, Output = R> {
type Output = R;
}
/// A trait that returns a binary depending on whether a > b
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
///
/// // This verifies that a <= 5 but not a < 3
/// fn example<T, R, S>(_a: T) where
/// T: IsInteger,
/// R: Binary,
/// S: Binary,
/// T: TypedGreaterThan<TypedInteger<_3, _0, _0, _0, _0, _0, _0, _0>, Output = R>,
/// R: AssertFalse,
/// T: TypedGreaterThan<TypedInteger<_1, _0, _0, _0, _0, _0, _0, _0>, Output = S>,
/// S: AssertTrue
/// {}
///
/// example(a);
/// ```
pub trait TypedGreaterThan<N: IsInteger> { type Output: Binary; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: Binary> TypedGreaterThan<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _GreaterThan<N, Output = R> {
type Output = R;
}
/// Denotes integer addition. If this says C7 does not implement HexAssertEq, this means it overflowed.
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(3);
/// let b = parse_integer!(5);
/// let c = parse_integer!(8);
///
/// // This verifies that 3 + 5 = 8
/// fn example<P, Q, R>(_p: P, _q: Q, _r: R) where
/// P: IsInteger,
/// Q: IsInteger,
/// R: IsInteger,
/// P: TypedAdd<Q, Output = R>
/// {}
///
/// example(a, b, c);
///
/// // This is another way of implementing the above
/// fn example2<P, Q, R, S, T>(_p: P, _q: Q, _r: R) where
/// P: IsInteger,
/// Q: IsInteger,
/// R: IsInteger,
/// S: IsInteger,
/// T: Binary,
/// P: TypedAdd<Q, Output = S>,
/// R: TypedEqual<S, Output = T>,
/// T: AssertTrue {}
/// example2(a, b, c);
/// ```
pub trait TypedAdd<N: IsInteger> { type Output: IsInteger; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: IsInteger> TypedAdd<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _Add<N, Output = R> {
type Output = R;
}
/// Denotes integer subtraction. If this says C7 does not implement HexAssertEq, this means it underflowed.
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(7);
/// let b = parse_integer!(4);
/// let c = parse_integer!(3);
///
/// // This verifies that 7 - 4 = 3
/// fn example<P, Q, R>(_p: P, _q: Q, _r: R) where
/// P: IsInteger,
/// Q: IsInteger,
/// R: IsInteger,
/// P: TypedSub<Q, Output = R>
/// {}
///
/// example(a, b, c);
/// ```
pub trait TypedSub<N: IsInteger> { type Output: IsInteger; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: IsInteger> TypedSub<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _Sub<N, Output = R> {
type Output = R;
}
/// A multiplication of 32 bit number and 32 bit number can be stored safely in a 64 bit number. We represent them as lower 32 bits and upper 32 bits
///
/// Example
/// ```
/// use const_arithmetic::*;
/// let a = parse_integer!(6);
/// let b = parse_integer!(4);
/// let c = parse_integer!(24);
///
/// // This verifies that 6 * 4 = 24
/// fn example<P, Q, R>(_p: P, _q: Q, _r: R) where
/// P: IsInteger,
/// Q: IsInteger,
/// R: IsInteger,
/// P: TypedMul<Q, Output = R>
/// {}
///
/// example(a, b, c);
///
/// // If we are handling really big integers we can get the overflowed bits as follows
/// // 1234567890 * 987654321 = 1219326311126352690 = 283896529 * (2**32) + 3623437106
/// let a = parse_integer!(1234567890);
/// let b = parse_integer!(987654321);
/// let overflow = parse_integer!(283896529);
/// let result = parse_integer!(3623437106);
///
/// // This verifies that 7 - 4 = 3
/// fn example2<P, Q, R, S>(_p: P, _q: Q, _r: R, _s: S) where
/// P: IsInteger,
/// Q: IsInteger,
/// R: IsInteger,
/// S: IsInteger,
/// P: TypedMul<Q, Output = R, Overflow = S>
/// {}
///
/// example2(a, b, result, overflow);
/// ```
pub trait TypedMul<N: IsInteger> { type Output: IsInteger; type Overflow: IsInteger; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: IsInteger, O: IsInteger> TypedMul<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _Mul<N, Output = R, Overflow = O> {
type Output = R;
type Overflow = O;
}
// TODO: We can actually implement bitshift to optimize division
/// Returns the Quotient of H/K for H: Div<K, Output: ...>
///
/// Note about implementation detail: This is an expanded version of long division - it takes O(1) steps but the constant is quite big unfortunately
/// (to be precise its 32 multiplications plus a negligible amount of addition and subtraction and other stuff)
/// If there were many divisions, the compile time increases quite a bit
///
/// Example
/// ```
/// // (This doc test takes about 5 seconds to complete on my computer)
/// use const_arithmetic::*;
/// let a = parse_integer!(25);
/// let b = parse_integer!(4);
/// let quotient = parse_integer!(6);
/// let modulus = parse_integer!(1);
///
/// // This verifies that 25/4 = 6 ... 1
/// fn example<P, Q, R, S>(_p: P, _q: Q, _r: R, _s: S) where
/// P: IsInteger,
/// Q: IsInteger,
/// R: IsInteger,
/// S: IsInteger,
/// P: TypedDiv<Q, Output = R, Remainder = S>
/// {}
///
/// example(a, b, quotient, modulus);
pub trait TypedDiv<K: IsInteger> { type Output: IsInteger; type Remainder: IsInteger; }
impl <N: IsInteger, H0: Hex, H1: Hex, H2: Hex, H3: Hex, H4: Hex, H5: Hex, H6: Hex, H7: Hex, R: IsInteger, O: IsInteger> TypedDiv<N> for TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7> where
TypedInteger<H0, H1, H2, H3, H4, H5, H6, H7>: _Div<N, Output = R, Remainder = O> {
type Output = R;
type Remainder = O;
}