1
+ //! n-body benchmark from the [benchmarks game][bg].
2
+ //!
3
+ //! [bg]: https://benchmarksgame.alioth.debian.org/u64q/nbody-description.
4
+ //! html#nbody
5
+
1
6
#![ cfg_attr( feature = "strict" , deny( warnings) ) ]
2
7
#![ feature( cfg_target_feature) ]
3
8
#![ feature( target_feature) ]
9
+ #![ cfg_attr( feature = "cargo-clippy" ,
10
+ allow( similar_names, missing_docs_in_private_items,
11
+ shadow_reuse, print_stdout) ) ]
4
12
5
13
extern crate stdsimd;
6
14
use self :: stdsimd:: simd;
7
15
use simd:: f64x2;
8
16
9
- const PI : f64 = 3.141592653589793 ;
17
+ const PI : f64 = std :: f64 :: consts :: PI ;
10
18
const SOLAR_MASS : f64 = 4.0 * PI * PI ;
11
19
const DAYS_PER_YEAR : f64 = 365.24 ;
12
20
@@ -29,7 +37,7 @@ impl Frsqrt for f64x2 {
29
37
f32x4:: new ( t. extract ( 0 ) , t. extract ( 1 ) , 0. , 0. ) ,
30
38
) . as_f64x4 ( )
31
39
} ;
32
- f64x2 :: new ( u. extract ( 0 ) , u. extract ( 1 ) )
40
+ Self :: new ( u. extract ( 0 ) , u. extract ( 1 ) )
33
41
}
34
42
#[ cfg( all( any( target_arch = "arm" , target_arch = "aarch64" ) ,
35
43
target_feature = "neon" ) ) ]
@@ -61,8 +69,8 @@ struct Body {
61
69
impl Body {
62
70
fn new (
63
71
x0 : f64 , x1 : f64 , x2 : f64 , v0 : f64 , v1 : f64 , v2 : f64 , mass : f64
64
- ) -> Body {
65
- Body {
72
+ ) -> Self {
73
+ Self {
66
74
x : [ x0, x1, x2] ,
67
75
_fill : 0.0 ,
68
76
v : [ v0, v1, v2] ,
@@ -103,8 +111,8 @@ fn advance(bodies: &mut [Body; N_BODIES], dt: f64) {
103
111
104
112
i = 0 ;
105
113
while i < N {
106
- for m in 0 .. 3 {
107
- dx [ m ] = f64x2:: new ( r[ i] [ m] , r[ i + 1 ] [ m] ) ;
114
+ for ( m , dx ) in dx . iter_mut ( ) . enumerate ( ) {
115
+ * dx = f64x2:: new ( r[ i] [ m] , r[ i + 1 ] [ m] ) ;
108
116
}
109
117
110
118
dsquared = dx[ 0 ] * dx[ 0 ] + dx[ 1 ] * dx[ 1 ] + dx[ 2 ] * dx[ 2 ] ;
@@ -144,11 +152,10 @@ fn energy(bodies: &[Body; N_BODIES]) -> f64 {
144
152
e += bi. mass
145
153
* ( bi. v [ 0 ] * bi. v [ 0 ] + bi. v [ 1 ] * bi. v [ 1 ] + bi. v [ 2 ] * bi. v [ 2 ] )
146
154
/ 2.0 ;
147
- for j in i + 1 ..N_BODIES {
148
- let bj = & bodies[ j] ;
155
+ for bj in bodies. iter ( ) . take ( N_BODIES ) . skip ( i + 1 ) {
149
156
let mut dx = [ 0.0 ; 3 ] ;
150
- for k in 0 .. 3 {
151
- dx [ k ] = bi. x [ k] - bj. x [ k] ;
157
+ for ( k , dx ) in dx . iter_mut ( ) . enumerate ( ) {
158
+ * dx = bi. x [ k] - bj. x [ k] ;
152
159
}
153
160
let mut distance = 0.0 ;
154
161
for & d in & dx {
@@ -210,7 +217,7 @@ fn main() {
210
217
. nth ( 1 )
211
218
. expect ( "need one arg" )
212
219
. parse ( )
213
- . unwrap ( ) ;
220
+ . expect ( "argument should be a usize" ) ;
214
221
215
222
offset_momentum ( & mut bodies) ;
216
223
println ! ( "{:.9}" , energy( & bodies) ) ;
0 commit comments