Skip to content

Commit 9c5affd

Browse files
committed
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.
1 parent f59e49c commit 9c5affd

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/test/bench/shootout/nbody.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,8 @@ mod NBodySystem {
7070
while (i < 5) {
7171
let int j = i+1;
7272
while (j < 5) {
73-
let float dx = bodies.(i).x - bodies.(j).x;
74-
let float dy = bodies.(i).y - bodies.(j).y;
75-
let float dz = bodies.(i).z - bodies.(j).z;
76-
77-
let float dSquared = dx * dx + dy * dy + dz * dz;
78-
79-
let float distance;
80-
rustrt.squareroot(dSquared, distance);
81-
let float mag = dt / (dSquared * distance);
82-
83-
bodies.(i).vx -= dx * bodies.(j).mass * mag;
84-
bodies.(i).vy -= dy * bodies.(j).mass * mag;
85-
bodies.(i).vz -= dz * bodies.(j).mass * mag;
86-
87-
bodies.(j).vx += dx * bodies.(i).mass * mag;
88-
bodies.(j).vy += dy * bodies.(i).mass * mag;
89-
bodies.(j).vz += dz * bodies.(i).mass * mag;
90-
73+
advance_one(bodies.(i), bodies.(j), dt);
9174
j += 1;
92-
9375
}
9476

9577
i += 1;
@@ -106,6 +88,26 @@ mod NBodySystem {
10688
}
10789
}
10890

91+
fn advance_one(&Body.props bi, &Body.props bj, float dt) {
92+
let float dx = bi.x - bj.x;
93+
let float dy = bi.y - bj.y;
94+
let float dz = bi.z - bj.z;
95+
96+
let float dSquared = dx * dx + dy * dy + dz * dz;
97+
98+
let float distance;
99+
rustrt.squareroot(dSquared, distance);
100+
let float mag = dt / (dSquared * distance);
101+
102+
bi.vx -= dx * bj.mass * mag;
103+
bi.vy -= dy * bj.mass * mag;
104+
bi.vz -= dz * bj.mass * mag;
105+
106+
bj.vx += dx * bi.mass * mag;
107+
bj.vy += dy * bi.mass * mag;
108+
bj.vz += dz * bi.mass * mag;
109+
}
110+
109111
fn energy(vec[Body.props] bodies) -> float {
110112
let float dx;
111113
let float dy;

0 commit comments

Comments
 (0)