-
Notifications
You must be signed in to change notification settings - Fork 4
/
recursion.rs
56 lines (52 loc) · 1.85 KB
/
recursion.rs
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
use crate::common::*;
use typenum::Unsigned;
mod binary_gcd {
use super::*;
typ! {
fn BinaryGcd<lhs, rhs>(lhs: Unsigned, rhs: Unsigned) -> Unsigned {
if lhs == rhs {
lhs
} else if lhs == 0u {
rhs
} else if rhs == 0u {
lhs
} else {
if lhs % 2u == 1u {
if rhs % 2u == 1u {
if lhs > rhs {
let sub: Unsigned = lhs - rhs;
BinaryGcd(sub, rhs)
} else {
let sub: Unsigned = rhs - lhs;
BinaryGcd(sub, lhs)
}
} else {
let div: Unsigned = rhs / 2u;
BinaryGcd(lhs, div)
}
} else {
if rhs % 2u == 1u {
let div: Unsigned = lhs / 2u;
BinaryGcd(div, rhs)
} else {
let ldiv: Unsigned = lhs / 2u;
let rdiv: Unsigned = rhs / 2u;
BinaryGcd(ldiv, rdiv) * 2u
}
}
}
}
}
#[test]
fn binary_gcd() {
use typenum::consts::*;
let _: AssertSameOp<BinaryGcdOp<U3, U0>, U3> = ();
let _: AssertSameOp<BinaryGcdOp<U0, U1>, U1> = ();
let _: AssertSameOp<BinaryGcdOp<U2, U4>, U2> = ();
let _: AssertSameOp<BinaryGcdOp<U6, U3>, U3> = ();
let _: AssertSameOp<BinaryGcdOp<U4, U4>, U4> = ();
let _: AssertSameOp<BinaryGcdOp<U7, U17>, U1> = ();
let _: AssertSameOp<BinaryGcdOp<U58, U11>, U1> = ();
let _: AssertSameOp<BinaryGcdOp<tyuint!(624129), tyuint!(2061517)>, tyuint!(18913)> = ();
}
}