1
Fork 0

Use std::iter::range_step

Use the iterator version instead of the old uint::/int::range_step
functions.
This commit is contained in:
blake2-ppc 2013-09-15 02:46:51 +02:00 committed by Daniel Micay
parent 66c2965b04
commit ad74fde62f
7 changed files with 57 additions and 77 deletions

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use std::uint; use std::iter::range_step;
use cryptoutil::{write_u32_le, read_u32v_le, FixedBuffer, FixedBuffer64, StandardPadding}; use cryptoutil::{write_u32_le, read_u32v_le, FixedBuffer, FixedBuffer64, StandardPadding};
use digest::Digest; use digest::Digest;
@ -86,46 +86,42 @@ impl Md5State {
read_u32v_le(data, input); read_u32v_le(data, input);
// round 1 // round 1
do uint::range_step(0, 16, 4) |i| { for i in range_step(0u, 16, 4) {
a = op_f(a, b, c, d, data[i] + C1[i], 7); a = op_f(a, b, c, d, data[i] + C1[i], 7);
d = op_f(d, a, b, c, data[i + 1] + C1[i + 1], 12); d = op_f(d, a, b, c, data[i + 1] + C1[i + 1], 12);
c = op_f(c, d, a, b, data[i + 2] + C1[i + 2], 17); c = op_f(c, d, a, b, data[i + 2] + C1[i + 2], 17);
b = op_f(b, c, d, a, data[i + 3] + C1[i + 3], 22); b = op_f(b, c, d, a, data[i + 3] + C1[i + 3], 22);
true }
};
// round 2 // round 2
let mut t = 1; let mut t = 1;
do uint::range_step(0, 16, 4) |i| { for i in range_step(0u, 16, 4) {
a = op_g(a, b, c, d, data[t & 0x0f] + C2[i], 5); a = op_g(a, b, c, d, data[t & 0x0f] + C2[i], 5);
d = op_g(d, a, b, c, data[(t + 5) & 0x0f] + C2[i + 1], 9); d = op_g(d, a, b, c, data[(t + 5) & 0x0f] + C2[i + 1], 9);
c = op_g(c, d, a, b, data[(t + 10) & 0x0f] + C2[i + 2], 14); c = op_g(c, d, a, b, data[(t + 10) & 0x0f] + C2[i + 2], 14);
b = op_g(b, c, d, a, data[(t + 15) & 0x0f] + C2[i + 3], 20); b = op_g(b, c, d, a, data[(t + 15) & 0x0f] + C2[i + 3], 20);
t += 20; t += 20;
true }
};
// round 3 // round 3
t = 5; t = 5;
do uint::range_step(0, 16, 4) |i| { for i in range_step(0u, 16, 4) {
a = op_h(a, b, c, d, data[t & 0x0f] + C3[i], 4); a = op_h(a, b, c, d, data[t & 0x0f] + C3[i], 4);
d = op_h(d, a, b, c, data[(t + 3) & 0x0f] + C3[i + 1], 11); d = op_h(d, a, b, c, data[(t + 3) & 0x0f] + C3[i + 1], 11);
c = op_h(c, d, a, b, data[(t + 6) & 0x0f] + C3[i + 2], 16); c = op_h(c, d, a, b, data[(t + 6) & 0x0f] + C3[i + 2], 16);
b = op_h(b, c, d, a, data[(t + 9) & 0x0f] + C3[i + 3], 23); b = op_h(b, c, d, a, data[(t + 9) & 0x0f] + C3[i + 3], 23);
t += 12; t += 12;
true }
};
// round 4 // round 4
t = 0; t = 0;
do uint::range_step(0, 16, 4) |i| { for i in range_step(0u, 16, 4) {
a = op_i(a, b, c, d, data[t & 0x0f] + C4[i], 6); a = op_i(a, b, c, d, data[t & 0x0f] + C4[i], 6);
d = op_i(d, a, b, c, data[(t + 7) & 0x0f] + C4[i + 1], 10); d = op_i(d, a, b, c, data[(t + 7) & 0x0f] + C4[i + 1], 10);
c = op_i(c, d, a, b, data[(t + 14) & 0x0f] + C4[i + 2], 15); c = op_i(c, d, a, b, data[(t + 14) & 0x0f] + C4[i + 2], 15);
b = op_i(b, c, d, a, data[(t + 21) & 0x0f] + C4[i + 3], 21); b = op_i(b, c, d, a, data[(t + 21) & 0x0f] + C4[i + 3], 21);
t += 28; t += 28;
true }
};
self.s0 += a; self.s0 += a;
self.s1 += b; self.s1 += b;

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use std::uint; use std::iter::range_step;
use cryptoutil::{write_u64_be, write_u32_be, read_u64v_be, read_u32v_be, add_bytes_to_bits, use cryptoutil::{write_u64_be, write_u32_be, read_u64v_be, read_u32v_be, add_bytes_to_bits,
add_bytes_to_bits_tuple, FixedBuffer, FixedBuffer128, FixedBuffer64, StandardPadding}; add_bytes_to_bits_tuple, FixedBuffer, FixedBuffer128, FixedBuffer64, StandardPadding};
@ -111,7 +111,7 @@ impl Engine512State {
// Putting the message schedule inside the same loop as the round calculations allows for // Putting the message schedule inside the same loop as the round calculations allows for
// the compiler to generate better code. // the compiler to generate better code.
do uint::range_step(0, 64, 8) |t| { for t in range_step(0u, 64, 8) {
schedule_round!(t + 16); schedule_round!(t + 16);
schedule_round!(t + 17); schedule_round!(t + 17);
schedule_round!(t + 18); schedule_round!(t + 18);
@ -129,10 +129,9 @@ impl Engine512State {
sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5); sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6); sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7); sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7);
true }
};
do uint::range_step(64, 80, 8) |t| { for t in range_step(64u, 80, 8) {
sha2_round!(a, b, c, d, e, f, g, h, K64, t); sha2_round!(a, b, c, d, e, f, g, h, K64, t);
sha2_round!(h, a, b, c, d, e, f, g, K64, t + 1); sha2_round!(h, a, b, c, d, e, f, g, K64, t + 1);
sha2_round!(g, h, a, b, c, d, e, f, K64, t + 2); sha2_round!(g, h, a, b, c, d, e, f, K64, t + 2);
@ -141,8 +140,7 @@ impl Engine512State {
sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5); sha2_round!(d, e, f, g, h, a, b, c, K64, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6); sha2_round!(c, d, e, f, g, h, a, b, K64, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7); sha2_round!(b, c, d, e, f, g, h, a, K64, t + 7);
true }
};
self.H0 += a; self.H0 += a;
self.H1 += b; self.H1 += b;
@ -527,7 +525,7 @@ impl Engine256State {
// Putting the message schedule inside the same loop as the round calculations allows for // Putting the message schedule inside the same loop as the round calculations allows for
// the compiler to generate better code. // the compiler to generate better code.
do uint::range_step(0, 48, 8) |t| { for t in range_step(0u, 48, 8) {
schedule_round!(t + 16); schedule_round!(t + 16);
schedule_round!(t + 17); schedule_round!(t + 17);
schedule_round!(t + 18); schedule_round!(t + 18);
@ -545,10 +543,9 @@ impl Engine256State {
sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5); sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6); sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7); sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7);
true }
};
do uint::range_step(48, 64, 8) |t| { for t in range_step(48u, 64, 8) {
sha2_round!(a, b, c, d, e, f, g, h, K32, t); sha2_round!(a, b, c, d, e, f, g, h, K32, t);
sha2_round!(h, a, b, c, d, e, f, g, K32, t + 1); sha2_round!(h, a, b, c, d, e, f, g, K32, t + 1);
sha2_round!(g, h, a, b, c, d, e, f, K32, t + 2); sha2_round!(g, h, a, b, c, d, e, f, K32, t + 2);
@ -557,8 +554,7 @@ impl Engine256State {
sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5); sha2_round!(d, e, f, g, h, a, b, c, K32, t + 5);
sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6); sha2_round!(c, d, e, f, g, h, a, b, K32, t + 6);
sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7); sha2_round!(b, c, d, e, f, g, h, a, K32, t + 7);
true }
};
self.H0 += a; self.H0 += a;
self.H1 += b; self.H1 += b;

View file

@ -12,7 +12,7 @@
use cast::transmute; use cast::transmute;
use option::{None, Option, Some}; use option::{None, Option, Some};
use i32; use iter::{Iterator, range_step};
use str::StrSlice; use str::StrSlice;
use unicode::{derived_property, general_category, decompose}; use unicode::{derived_property, general_category, decompose};
use to_str::ToStr; use to_str::ToStr;
@ -286,15 +286,14 @@ pub fn escape_unicode(c: char, f: &fn(char)) {
(c <= '\uffff') { f('u'); 4 } (c <= '\uffff') { f('u'); 4 }
_ { f('U'); 8 } _ { f('U'); 8 }
); );
do i32::range_step(4 * (pad - 1), -1, -4) |offset| { for offset in range_step::<i32>(4 * (pad - 1), -1, -4) {
unsafe { unsafe {
match ((c as i32) >> offset) & 0xf { match ((c as i32) >> offset) & 0xf {
i @ 0 .. 9 => { f(transmute('0' as i32 + i)); } i @ 0 .. 9 => { f(transmute('0' as i32 + i)); }
i => { f(transmute('a' as i32 + (i - 10))); } i => { f(transmute('a' as i32 + (i - 10))); }
} }
} }
true }
};
} }
/// ///

View file

@ -48,7 +48,7 @@ use clone::Clone;
use cmp; use cmp;
use container::Container; use container::Container;
use int; use int;
use iter::{Iterator, range}; use iter::{Iterator, range, range_step};
use local_data; use local_data;
use num; use num;
use prelude::*; use prelude::*;
@ -748,7 +748,7 @@ impl IsaacRng {
if use_rsl { if use_rsl {
macro_rules! memloop ( macro_rules! memloop (
($arr:expr) => {{ ($arr:expr) => {{
do u32::range_step(0, RAND_SIZE, 8) |i| { for i in range_step(0u32, RAND_SIZE, 8) {
a+=$arr[i ]; b+=$arr[i+1]; a+=$arr[i ]; b+=$arr[i+1];
c+=$arr[i+2]; d+=$arr[i+3]; c+=$arr[i+2]; d+=$arr[i+3];
e+=$arr[i+4]; f+=$arr[i+5]; e+=$arr[i+4]; f+=$arr[i+5];
@ -758,22 +758,20 @@ impl IsaacRng {
self.mem[i+2]=c; self.mem[i+3]=d; self.mem[i+2]=c; self.mem[i+3]=d;
self.mem[i+4]=e; self.mem[i+5]=f; self.mem[i+4]=e; self.mem[i+5]=f;
self.mem[i+6]=g; self.mem[i+7]=h; self.mem[i+6]=g; self.mem[i+7]=h;
true }
};
}} }}
); );
memloop!(self.rsl); memloop!(self.rsl);
memloop!(self.mem); memloop!(self.mem);
} else { } else {
do u32::range_step(0, RAND_SIZE, 8) |i| { for i in range_step(0u32, RAND_SIZE, 8) {
mix!(); mix!();
self.mem[i ]=a; self.mem[i+1]=b; self.mem[i ]=a; self.mem[i+1]=b;
self.mem[i+2]=c; self.mem[i+3]=d; self.mem[i+2]=c; self.mem[i+3]=d;
self.mem[i+4]=e; self.mem[i+5]=f; self.mem[i+4]=e; self.mem[i+5]=f;
self.mem[i+6]=g; self.mem[i+7]=h; self.mem[i+6]=g; self.mem[i+7]=h;
true }
};
} }
self.isaac(); self.isaac();
@ -794,7 +792,7 @@ impl IsaacRng {
}); });
macro_rules! rngstep( macro_rules! rngstep(
($j:expr, $shift:expr) => {{ ($j:expr, $shift:expr) => {{
let base = base + $j; let base = $j;
let mix = if $shift < 0 { let mix = if $shift < 0 {
a >> -$shift as uint a >> -$shift as uint
} else { } else {
@ -813,13 +811,12 @@ impl IsaacRng {
let r = [(0, MIDPOINT), (MIDPOINT, 0)]; let r = [(0, MIDPOINT), (MIDPOINT, 0)];
for &(mr_offset, m2_offset) in r.iter() { for &(mr_offset, m2_offset) in r.iter() {
do uint::range_step(0, MIDPOINT, 4) |base| { for i in range_step(0u, MIDPOINT, 4) {
rngstep!(0, 13); rngstep!(i + 0, 13);
rngstep!(1, -6); rngstep!(i + 1, -6);
rngstep!(2, 2); rngstep!(i + 2, 2);
rngstep!(3, -16); rngstep!(i + 3, -16);
true }
};
} }
self.a = a; self.a = a;

View file

@ -520,6 +520,7 @@ pub fn check_integrity<T>(trie: &TrieNode<T>) {
mod test_map { mod test_map {
use super::*; use super::*;
use prelude::*; use prelude::*;
use iter::range_step;
use uint; use uint;
#[test] #[test]
@ -538,21 +539,19 @@ mod test_map {
#[test] #[test]
fn test_step() { fn test_step() {
let mut trie = TrieMap::new(); let mut trie = TrieMap::new();
let n = 300; let n = 300u;
do uint::range_step(1, n, 2) |x| { for x in range_step(1u, n, 2) {
assert!(trie.insert(x, x + 1)); assert!(trie.insert(x, x + 1));
assert!(trie.contains_key(&x)); assert!(trie.contains_key(&x));
check_integrity(&trie.root); check_integrity(&trie.root);
true }
};
do uint::range_step(0, n, 2) |x| { for x in range_step(0u, n, 2) {
assert!(!trie.contains_key(&x)); assert!(!trie.contains_key(&x));
assert!(trie.insert(x, x + 1)); assert!(trie.insert(x, x + 1));
check_integrity(&trie.root); check_integrity(&trie.root);
true }
};
for x in range(0u, n) { for x in range(0u, n) {
assert!(trie.contains_key(&x)); assert!(trie.contains_key(&x));
@ -560,19 +559,17 @@ mod test_map {
check_integrity(&trie.root); check_integrity(&trie.root);
} }
do uint::range_step(1, n, 2) |x| { for x in range_step(1u, n, 2) {
assert!(trie.remove(&x)); assert!(trie.remove(&x));
assert!(!trie.contains_key(&x)); assert!(!trie.contains_key(&x));
check_integrity(&trie.root); check_integrity(&trie.root);
true }
};
do uint::range_step(0, n, 2) |x| { for x in range_step(0u, n, 2) {
assert!(trie.contains_key(&x)); assert!(trie.contains_key(&x));
assert!(!trie.insert(x, x + 1)); assert!(!trie.insert(x, x + 1));
check_integrity(&trie.root); check_integrity(&trie.root);
true }
};
} }
#[test] #[test]
@ -715,11 +712,10 @@ mod test_map {
let value = 42u; let value = 42u;
let mut map : TrieMap<uint> = TrieMap::new(); let mut map : TrieMap<uint> = TrieMap::new();
do uint::range_step(0u, last, step as int) |x| { for x in range_step(0u, last, step) {
assert!(x % step == 0); assert!(x % step == 0);
map.insert(x, value); map.insert(x, value);
true }
};
for i in range(0u, last - step) { for i in range(0u, last - step) {
let mut lb = map.lower_bound_iter(i); let mut lb = map.lower_bound_iter(i);

View file

@ -28,11 +28,11 @@ fn int_range_rev(hi: int, lo: int, it: &fn(int) -> bool) -> bool {
} }
fn int_range_step(a: int, b: int, step: int, it: &fn(int) -> bool) -> bool { fn int_range_step(a: int, b: int, step: int, it: &fn(int) -> bool) -> bool {
int::range_step(a, b, step, it) std::iter::range_step(a, b, step).advance(it)
} }
fn uint_range_step(a: uint, b: uint, step: int, it: &fn(uint) -> bool) -> bool { fn uint_range_step(a: uint, b: uint, step: int, it: &fn(uint) -> bool) -> bool {
uint::range_step(a, b, step, it) std::iter::range_step(a, b, step).advance(it)
} }

View file

@ -20,11 +20,11 @@ fn int_range(lo: int, hi: int, it: &fn(int) -> bool) -> bool {
} }
fn int_range_step(a: int, b: int, step: int, it: &fn(int) -> bool) -> bool { fn int_range_step(a: int, b: int, step: int, it: &fn(int) -> bool) -> bool {
int::range_step(a, b, step, it) std::iter::range_step(a, b, step).advance(it)
} }
fn uint_range_step(a: uint, b: uint, s: int, it: &fn(uint) -> bool) -> bool { fn uint_range_step(a: uint, b: uint, s: int, it: &fn(uint) -> bool) -> bool {
uint::range_step(a, b, s, it) std::iter::range_step(a, b, s).advance(it)
} }
pub fn main() { pub fn main() {
@ -98,35 +98,31 @@ pub fn main() {
// range_step_inclusive will never pass stop element, and may skip it. // range_step_inclusive will never pass stop element, and may skip it.
let mut saw21 = false; let mut saw21 = false;
do uint::range_step_inclusive(0, 21, 4) |x| { for x in std::iter::range_step_inclusive(0, 21, 4) {
assert!(x <= 21); assert!(x <= 21);
if x == 21 { saw21 = true; } if x == 21 { saw21 = true; }
true }
};
assert!(!saw21); assert!(!saw21);
let mut saw21 = false; let mut saw21 = false;
do int::range_step_inclusive(0, 21, 4) |x| { for x in std::iter::range_step_inclusive(0, 21, 4) {
assert!(x <= 21); assert!(x <= 21);
if x == 21 { saw21 = true; } if x == 21 { saw21 = true; }
true }
};
assert!(!saw21); assert!(!saw21);
// range_step_inclusive will never pass stop element, but may visit it. // range_step_inclusive will never pass stop element, but may visit it.
let mut saw21 = false; let mut saw21 = false;
do uint::range_step_inclusive(0, 21, 3) |x| { for x in std::iter::range_step_inclusive(0, 21, 3) {
assert!(x <= 21); assert!(x <= 21);
printfln!("saw: %u", x); printfln!("saw: %u", x);
if x == 21 { saw21 = true; } if x == 21 { saw21 = true; }
true }
};
assert!(saw21); assert!(saw21);
let mut saw21 = false; let mut saw21 = false;
do int::range_step_inclusive(0, 21, 3) |x| { for x in std::iter::range_step_inclusive(0, 21, 3) {
assert!(x <= 21); assert!(x <= 21);
if x == 21 { saw21 = true; } if x == 21 { saw21 = true; }
true }
};
assert!(saw21); assert!(saw21);
} }