Skip to content
Browse files

shootout: Hoist out the vector indexing on nbody; don't rely on LICM,…

… which isn't working for some reason (insufficient alias info?) Speeds up nbody a bit.
  • Loading branch information...
1 parent f59e49c commit 9c5affda1a17532109f919df954ee743b850544e @pcwalton pcwalton committed Mar 26, 2011
Showing with 21 additions and 19 deletions.
  1. +21 −19 src/test/bench/shootout/nbody.rs
View
40 src/test/bench/shootout/nbody.rs
@@ -70,26 +70,8 @@ mod NBodySystem {
while (i < 5) {
let int j = i+1;
while (j < 5) {
- let float dx = bodies.(i).x - bodies.(j).x;
- let float dy = bodies.(i).y - bodies.(j).y;
- let float dz = bodies.(i).z - bodies.(j).z;
-
- let float dSquared = dx * dx + dy * dy + dz * dz;
-
- let float distance;
- rustrt.squareroot(dSquared, distance);
- let float mag = dt / (dSquared * distance);
-
- bodies.(i).vx -= dx * bodies.(j).mass * mag;
- bodies.(i).vy -= dy * bodies.(j).mass * mag;
- bodies.(i).vz -= dz * bodies.(j).mass * mag;
-
- bodies.(j).vx += dx * bodies.(i).mass * mag;
- bodies.(j).vy += dy * bodies.(i).mass * mag;
- bodies.(j).vz += dz * bodies.(i).mass * mag;
-
+ advance_one(bodies.(i), bodies.(j), dt);
j += 1;
-
}
i += 1;
@@ -106,6 +88,26 @@ mod NBodySystem {
}
}
+ fn advance_one(&Body.props bi, &Body.props bj, float dt) {
+ let float dx = bi.x - bj.x;
+ let float dy = bi.y - bj.y;
+ let float dz = bi.z - bj.z;
+
+ let float dSquared = dx * dx + dy * dy + dz * dz;
+
+ let float distance;
+ rustrt.squareroot(dSquared, distance);
+ let float mag = dt / (dSquared * distance);
+
+ bi.vx -= dx * bj.mass * mag;
+ bi.vy -= dy * bj.mass * mag;
+ bi.vz -= dz * bj.mass * mag;
+
+ bj.vx += dx * bi.mass * mag;
+ bj.vy += dy * bi.mass * mag;
+ bj.vz += dz * bi.mass * mag;
+ }
+
fn energy(vec[Body.props] bodies) -> float {
let float dx;
let float dy;

0 comments on commit 9c5affd

Please sign in to comment.
Something went wrong with that request. Please try again.