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
//! Greatest Common Divisor algorithms that use bit manipulation.
//!
//! Note: this is not intended to provide _the_ fastest GCD implementation
//! possible, but only to provide those algorithms that are implemented using
//! bit manipulation.
//!
//! A fast GCD implementation will probably want to switch algorithms depending
//! on the size of the input, and will involve other algorithms like Lehmer's
//! and probably parallelization. The `benches/gcd.rs` benchmarks might be
//! useful for making these decisions.
use word::Word;
pub mod test_util {
use super::Word;
pub fn invariant<T: Word>(x: T, y: T, gcd: T) -> bool {
if x == T::zero() {
gcd == y
} else if y == T::zero() {
gcd == x
} else {
x % gcd == T::zero() && y % gcd == T::zero()
}
}
pub fn run_u8<F: Fn(u8, u8) -> u8>(from: u8, to: u8, f: F) {
(from..to)
.map(|x| {
let xs: [u8; 1] = [x];
(from..to)
.zip(xs.iter().cycle())
.map(|(x, &y)| {
let gcd = f(x, y);
assert!(invariant(x, y, gcd))
})
.count();
})
.count();
}
pub fn run_u16<F: Fn(u16, u16) -> u16>(from: u16, to: u16, f: F) {
(from..to)
.map(|x| {
let xs: [u16; 1] = [x];
(from..to)
.zip(xs.iter().cycle())
.map(|(x, &y)| {
let gcd = f(x, y);
assert!(invariant(x, y, gcd))
})
.count();
})
.count();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_u8() {
test_util::run_u8(0, u8::max_value(), euclid::recursive);
test_util::run_u8(0, u8::max_value(), euclid::iterative);
test_util::run_u8(0, u8::max_value(), steins::recursive);
test_util::run_u8(0, u8::max_value(), steins::iterative);
test_util::run_u8(0, u8::max_value(), steins::iterative_xor);
}
}
pub mod euclid {
//! Recursive implementation of the GCD algorithm.
use word::Word;
#[inline]
pub fn recursive<T: Word>(x: T, y: T) -> T {
if y == T::zero() {
x
} else {
recursive(y, x % y)
}
}
#[inline]
pub fn iterative<T: Word>(x: T, y: T) -> T {
let mut x = x;
let mut y = y;
while y != T::zero() {
let t = y;
y = x % y;
x = t;
}
x
}
}
pub mod steins {
use std;
use word::{Word, IsEven, IsOdd};
#[inline] pub fn recursive<T: Word>(x: T, y: T) -> T {
match (x, y) {
(x, y) if (x == y) => x,
(x, y) if (x == T::zero()) => y,
(x, y) if (y == T::zero()) => x,
(x, y) => {
match (x.is_odd(), y.is_odd()) {
(false, false) => recursive(x >> T::one(), y >> T::one()) << T::one(),
(false, true) => recursive(x >> T::one(), y),
(true, false) => recursive(x, y >> T::one()),
(true, true) => {
if x >= y {
recursive((x - y) >> T::one(), x)
} else {
recursive((y - x) >> T::one(), x)
}
}
}
}
}
}
#[inline]pub fn iterative<T: Word>(x: T, y: T) -> T {
if x == T::zero() { return y; }
if y == T::zero() { return x; }
let mut x = x;
let mut y = y;
let mut shift = T::zero();
while (x | y).is_even() {
x = x >> T::one();
y = y >> T::one();
shift = shift + T::one();
}
while x.is_even() {
x = x >> T::one();
}
loop {
while y.is_even() {
y = y >> T::one();
}
if x > y {
std::mem::swap(&mut x, &mut y);
}
y = y - x;
if y == T::zero() { break; }
}
x << shift
}
#[inline]pub fn iterative_xor<T: Word>(x: T, y: T) -> T {
let mut x = x;
let mut y = y;
while y != T::zero() {
x = x % y;
y = y ^ x;
x = x ^ y;
y = y ^ x;
}
x
}
}
/*
/// Greatest Common Divisor (GCD) of `x` and `y`.
///
///
/// # Keywords:
///
/// Greatest Common Divisor, GCD.
///
/// # Examples
///
/// ```
/// use bitwise::word::*;
///
/// ```
#[inline]
pub fn greatest_common_divisor<T: Word>(x: T, y: T) -> T {
}
/// Method version of [`greatest_common_divisor`](fn.greatest_common_divisor.html).
pub trait GCD {
#[inline]
fn greatest_common_divisor(self, Self) -> Self;
}
impl<T: Word> GCD for T {
#[inline]
fn greatest_common_divisor(self, y: Self) -> Self {
greatest_common_divisor(self, y)
}
}
*/