Skip to content

Beautify ISAAC's mixing arithmetic #30455

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

Merged
merged 1 commit into from
Dec 20, 2015
Merged
Changes from all commits
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
78 changes: 62 additions & 16 deletions src/librand/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,37 @@ impl IsaacRng {

macro_rules! mix {
() => {{
a=a^(b<<11); d=d+a; b=b+c;
b=b^(c>>2); e=e+b; c=c+d;
c=c^(d<<8); f=f+c; d=d+e;
d=d^(e>>16); g=g+d; e=e+f;
e=e^(f<<10); h=h+e; f=f+g;
f=f^(g>>4); a=a+f; g=g+h;
g=g^(h<<8); b=b+g; h=h+a;
h=h^(a>>9); c=c+h; a=a+b;
a = a ^ (b << 11);
d = d + a;
b = b + c;
Copy link
Member

Choose a reason for hiding this comment

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

Why aren't all of these a ^= b << 11;, d += a;, b += c; etc.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially tried that, but apparently arithmetic assignment operators aren't defined for Wrapping types like these. I imagine they will be eventually - I can't imagine why that wouldn't be simple.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I didn't realize those were Wrapping. I guess that's a bug, because we have the traits now.


b = b ^ (c >> 2);
e = e + b;
c = c + d;

c = c ^ (d << 8);
f = f + c;
d = d + e;

d = d ^ (e >> 16);
g = g + d;
e = e + f;

e = e ^ (f << 10);
h = h + e;
f = f + g;

f = f ^ (g >> 4);
a = a + f;
g = g + h;

g = g ^ (h << 8);
b = b + g;
h = h + a;

h = h ^ (a >> 9);
c = c + h;
a = a + b;
}}
}

Expand Down Expand Up @@ -337,14 +360,37 @@ impl Isaac64Rng {

macro_rules! mix {
() => {{
a=a-e; f=f^(h>>9); h=h+a;
b=b-f; g=g^(a<<9); a=a+b;
c=c-g; h=h^(b>>23); b=b+c;
d=d-h; a=a^(c<<15); c=c+d;
e=e-a; b=b^(d>>14); d=d+e;
f=f-b; c=c^(e<<20); e=e+f;
g=g-c; d=d^(f>>17); f=f+g;
h=h-d; e=e^(g<<14); g=g+h;
a = a - e;
f = f ^ (h >> 9);
h = h + a;

b = b - f;
g = g ^ (a << 9);
a = a + b;

c = c - g;
h = h ^ (b >> 23);
b = b + c;

d = d - h;
a = a ^ (c << 15);
c = c + d;

e = e - a;
b = b ^ (d >> 14);
d = d + e;

f = f - b;
c = c ^ (e << 20);
e = e + f;

g = g - c;
d = d ^ (f >> 17);
f = f + g;

h = h - d;
e = e ^ (g << 14);
g = g + h;
}}
}

Expand Down