Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reroll outer loop #30

Merged
merged 2 commits into from
May 19, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 47 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,71 +68,69 @@ const RC: [u64; 24] = [
pub fn keccakf(a: &mut [u64; PLEN]) {
let mut arrays: [[u64; 5]; 24] = [[0; 5]; 24];

unroll! {
for i in 0..24 {
// Theta
unroll! {
for x in 0..5 {
// This looks useless but it gets way slower without it. I tried using
// `mem::uninitialized` for the initialisation of `arrays` but that also makes
// it slower, although not by as much as removing this assignment. Optimisers
// are weird. Maybe a different version of LLVM will react differently, so if
// you see this comment in the future try deleting this assignment and using
// uninit above and see how it affects the benchmarks.
arrays[i][x] = 0;

unroll! {
for y_count in 0..5 {
let y = y_count * 5;
arrays[i][x] ^= a[x + y];
}
for i in 0..24 {
// Theta
unroll! {
for x in 0..5 {
// This looks useless but it gets way slower without it. I tried using
// `mem::uninitialized` for the initialisation of `arrays` but that also makes
// it slower, although not by as much as removing this assignment. Optimisers
// are weird. Maybe a different version of LLVM will react differently, so if
// you see this comment in the future try deleting this assignment and using
// uninit above and see how it affects the benchmarks.
arrays[i][x] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After loop was unrolled you don't need this anymore. This variant has the same speed for me.

Copy link
Contributor Author

@eira-fransham eira-fransham May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, I was really hoping I could get rid of this (as you can see from my comment).

Copy link
Contributor

@dvdplm dvdplm May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw I tried using uninitialised memory yesterday to see if a newer compiler did anything different – it did not (well, it made the whole thing slower just like you said in the comment).


unroll! {
for y_count in 0..5 {
let y = y_count * 5;
arrays[i][x] ^= a[x + y];
}
}
}
}

unroll! {
for x in 0..5 {
unroll! {
for y_count in 0..5 {
let y = y_count * 5;
a[y + x] ^= arrays[i][(x + 4) % 5] ^ arrays[i][(x + 1) % 5].rotate_left(1);
}
unroll! {
for x in 0..5 {
unroll! {
for y_count in 0..5 {
let y = y_count * 5;
a[y + x] ^= arrays[i][(x + 4) % 5] ^ arrays[i][(x + 1) % 5].rotate_left(1);
}
}
}
}

// Rho and pi
let mut last = a[1];
unroll! {
for x in 0..24 {
arrays[i][0] = a[PI[x]];
a[PI[x]] = last.rotate_left(RHO[x]);
last = arrays[i][0];
}
// Rho and pi
let mut last = a[1];
unroll! {
for x in 0..24 {
arrays[i][0] = a[PI[x]];
a[PI[x]] = last.rotate_left(RHO[x]);
last = arrays[i][0];
}
}

// Chi
unroll! {
for y_step in 0..5 {
let y = y_step * 5;
// Chi
unroll! {
for y_step in 0..5 {
let y = y_step * 5;

unroll! {
for x in 0..5 {
arrays[i][x] = a[y + x];
}
unroll! {
for x in 0..5 {
arrays[i][x] = a[y + x];
}
}

unroll! {
for x in 0..5 {
a[y + x] = arrays[i][x] ^ ((!arrays[i][(x + 1) % 5]) & (arrays[i][(x + 2) % 5]));
}
unroll! {
for x in 0..5 {
a[y + x] = arrays[i][x] ^ ((!arrays[i][(x + 1) % 5]) & (arrays[i][(x + 2) % 5]));
}
}
};
}
};

// Iota
a[0] ^= RC[i];
}
// Iota
a[0] ^= RC[i];
}
}

Expand Down