-
Notifications
You must be signed in to change notification settings - Fork 1
/
bodybodyInteraction_AltiVec.h
148 lines (127 loc) · 3.67 KB
/
bodybodyInteraction_AltiVec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
*
* bodybodyInteraction_AltiVec.h
*
* PowerPC AltiVec implementation of N-body computation.
*
* Copyright (c) 2011-2012, Archaea Software, LLC.
* Copyright (c) 2012-2021, Uplink Laboratories, LLC.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifdef __ALTIVEC__
#include <altivec.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
typedef vector float v4sf;
static const v4sf vec_zero = {0.0f, 0.0f, 0.0f, 0.0f};
typedef union {
float f[4];
v4sf p;
} v4;
#ifndef __VSX__
static inline v4sf
vec_mul(v4sf x, v4sf y)
{
return vec_madd(x, y, vec_zero);
}
#endif
static inline v4sf
_vec_set_ps1(float f)
{
v4 r;
r.f[0] = f;
r.f[1] = f;
r.f[2] = f;
r.f[3] = f;
return r.p;
}
static inline float
_vec_sum(v4sf v)
{
v4 r;
r.p = v;
return r.f[0] + r.f[1] + r.f[2] + r.f[3];
}
static inline v4sf
rcp_sqrt_nr_ps(const v4sf x)
{
const v4sf
nr = vec_rsqrte(x),
muls = vec_mul(vec_mul(nr, nr), x),
beta = vec_mul(_vec_set_ps1(0.5f), nr),
gamma = vec_sub(_vec_set_ps1(3.0f), muls);
return vec_mul(beta, gamma);
}
inline void
bodyBodyInteraction(
v4sf *fx,
v4sf *fy,
v4sf *fz,
const v4sf x0,
const v4sf y0,
const v4sf z0,
const v4sf x1,
const v4sf y1,
const v4sf z1,
const v4sf mass1,
const v4sf softeningSquared )
{
// r_01 [3 FLOPS]
const v4sf dx = vec_sub( x1, x0 );
const v4sf dy = vec_sub( y1, y0 );
const v4sf dz = vec_sub( z1, z0 );
// d^2 + e^2 [6 FLOPS]
const v4sf distSq =
vec_add(
vec_add(
vec_add(
vec_mul( dx, dx ),
vec_mul( dy, dy )
),
vec_mul( dz, dz )
),
softeningSquared
);
// invDistCube =1/distSqr^(3/2) [4 FLOPS (2 mul, 1 sqrt, 1 inv)]
const v4sf invDist = rcp_sqrt_nr_ps( distSq );
const v4sf invDistCube =
vec_mul(
invDist,
vec_mul(
invDist, invDist )
);
// s = m_j * invDistCube [1 FLOP]
const v4sf s = vec_mul( mass1, invDistCube );
// (m_1 * r_01) / (d^2 + e^2)^(3/2) [6 FLOPS]
*fx = vec_add( *fx, vec_mul( dx, s ) );
*fy = vec_add( *fy, vec_mul( dy, s ) );
*fz = vec_add( *fz, vec_mul( dz, s ) );
}
#pragma GCC diagnostic pop
#endif
/* vim: set ts=4 sts=4 sw=4 et: */