1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
4 // contributed by the Rust Project Developers
6 // Copyright (c) 2011-2014 The Rust Project Developers
8 // All rights reserved.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
14 // - Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
17 // - Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in
19 // the documentation and/or other materials provided with the
22 // - Neither the name of "The Computer Language Benchmarks Game" nor
23 // the name of "The Computer Language Shootout Benchmarks" nor the
24 // names of its contributors may be used to endorse or promote
25 // products derived from this software without specific prior
26 // written permission.
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 // OF THE POSSIBILITY OF SUCH DAMAGE.
43 const PI: f64 = 3.141592653589793;
44 const SOLAR_MASS: f64 = 4.0 * PI * PI;
45 const YEAR: f64 = 365.24;
46 const N_BODIES: uint = 5;
48 static BODIES: [Planet;N_BODIES] = [
51 x: 0.0, y: 0.0, z: 0.0,
52 vx: 0.0, vy: 0.0, vz: 0.0,
57 x: 4.84143144246472090e+00,
58 y: -1.16032004402742839e+00,
59 z: -1.03622044471123109e-01,
60 vx: 1.66007664274403694e-03 * YEAR,
61 vy: 7.69901118419740425e-03 * YEAR,
62 vz: -6.90460016972063023e-05 * YEAR,
63 mass: 9.54791938424326609e-04 * SOLAR_MASS,
67 x: 8.34336671824457987e+00,
68 y: 4.12479856412430479e+00,
69 z: -4.03523417114321381e-01,
70 vx: -2.76742510726862411e-03 * YEAR,
71 vy: 4.99852801234917238e-03 * YEAR,
72 vz: 2.30417297573763929e-05 * YEAR,
73 mass: 2.85885980666130812e-04 * SOLAR_MASS,
77 x: 1.28943695621391310e+01,
78 y: -1.51111514016986312e+01,
79 z: -2.23307578892655734e-01,
80 vx: 2.96460137564761618e-03 * YEAR,
81 vy: 2.37847173959480950e-03 * YEAR,
82 vz: -2.96589568540237556e-05 * YEAR,
83 mass: 4.36624404335156298e-05 * SOLAR_MASS,
87 x: 1.53796971148509165e+01,
88 y: -2.59193146099879641e+01,
89 z: 1.79258772950371181e-01,
90 vx: 2.68067772490389322e-03 * YEAR,
91 vy: 1.62824170038242295e-03 * YEAR,
92 vz: -9.51592254519715870e-05 * YEAR,
93 mass: 5.15138902046611451e-05 * SOLAR_MASS,
98 x: f64, y: f64, z: f64,
99 vx: f64, vy: f64, vz: f64,
103 impl Copy for Planet {}
105 fn advance(bodies: &mut [Planet;N_BODIES], dt: f64, steps: int) {
106 for _ in range(0, steps) {
107 let mut b_slice = bodies.as_mut_slice();
109 let bi = match shift_mut_ref(&mut b_slice) {
113 for bj in b_slice.iter_mut() {
114 let dx = bi.x - bj.x;
115 let dy = bi.y - bj.y;
116 let dz = bi.z - bj.z;
118 let d2 = dx * dx + dy * dy + dz * dz;
119 let mag = dt / (d2 * d2.sqrt());
121 let massj_mag = bj.mass * mag;
122 bi.vx -= dx * massj_mag;
123 bi.vy -= dy * massj_mag;
124 bi.vz -= dz * massj_mag;
126 let massi_mag = bi.mass * mag;
127 bj.vx += dx * massi_mag;
128 bj.vy += dy * massi_mag;
129 bj.vz += dz * massi_mag;
138 fn energy(bodies: &[Planet;N_BODIES]) -> f64 {
140 let mut bodies = bodies.iter();
142 let bi = match bodies.next() {
146 e += (bi.vx * bi.vx + bi.vy * bi.vy + bi.vz * bi.vz) * bi.mass / 2.0;
147 for bj in bodies.clone() {
148 let dx = bi.x - bj.x;
149 let dy = bi.y - bj.y;
150 let dz = bi.z - bj.z;
151 let dist = (dx * dx + dy * dy + dz * dz).sqrt();
152 e -= bi.mass * bj.mass / dist;
158 fn offset_momentum(bodies: &mut [Planet;N_BODIES]) {
162 for bi in bodies.iter() {
163 px += bi.vx * bi.mass;
164 py += bi.vy * bi.mass;
165 pz += bi.vz * bi.mass;
167 let sun = &mut bodies[0];
168 sun.vx = - px / SOLAR_MASS;
169 sun.vy = - py / SOLAR_MASS;
170 sun.vz = - pz / SOLAR_MASS;
174 let n = if std::os::getenv("RUST_BENCH").is_some() {
177 std::os::args().as_slice().get(1)
178 .and_then(|arg| arg.parse())
181 let mut bodies = BODIES;
183 offset_momentum(&mut bodies);
184 println!("{:.9}", energy(&bodies));
186 advance(&mut bodies, 0.01, n);
188 println!("{:.9}", energy(&bodies));
191 /// Pop a mutable reference off the head of a slice, mutating the slice to no
192 /// longer contain the mutable reference. This is a safe operation because the
193 /// two mutable borrows are entirely disjoint.
194 fn shift_mut_ref<'a, T>(r: &mut &'a mut [T]) -> Option<&'a mut T> {
198 if r.len() == 0 { return None }
200 let mut raw = r.repr();
201 let ret = raw.data as *mut T;
202 raw.data = raw.data.offset(1);
204 *r = mem::transmute(raw);
205 Some(unsafe { &mut *ret })