774b9c2814ddd6feb03ddd730592ff3f3c7e8e80
[alexxy/gromacs.git] / src / gmxlib / cuda_tools / vectype_ops.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2012, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38
39 #ifndef VECTYPE_OPS_CUH
40 #define VECTYPE_OPS_CUH
41
42 /**** float3 ****/
43 inline __host__ __device__ float3 make_float3(float s)
44 {
45     return make_float3(s, s, s);
46 }
47 inline __host__ __device__ float3 make_float3(float4 a)
48 {
49     return make_float3(a.x, a.y, a.z);
50 }
51 inline __host__ __device__ float3 operator-(float3 &a)
52 {
53     return make_float3(-a.x, -a.y, -a.z);
54 }
55 inline __host__ __device__ float3 operator+(float3 a, float3 b)
56 {
57     return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
58 }
59 inline __host__ __device__ float3 operator-(float3 a, float3 b)
60 {
61     return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
62 }
63 inline __host__ __device__ float3 operator*(float3 a, float k)
64 {
65     return make_float3(k * a.x, k * a.y, k * a.z);
66 }
67 inline __host__ __device__ float3 operator*(float k, float3 a)
68 {
69     return make_float3(k * a.x, k * a.y, k * a.z);
70 }
71 inline __host__ __device__ void operator+=(float3 &a, float3 b)
72 {
73     a.x += b.x; a.y += b.y; a.z += b.z;
74 }
75 inline __host__ __device__ void operator+=(float3 &a, float4 b)
76 {
77     a.x += b.x; a.y += b.y; a.z += b.z;
78 }
79 inline __host__ __device__ void operator-=(float3 &a, float3 b)
80 {
81     a.x -= b.x; a.y -= b.y; a.z -= b.z;
82 }
83 inline __host__ __device__ float norm(float3 a)
84 {
85     return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
86 }
87 inline __host__ __device__ float norm2(float3 a)
88 {
89     return (a.x * a.x + a.y * a.y + a.z * a.z);
90 }
91 inline __host__ __device__ float dist3(float3 a, float3 b)
92 {
93     return norm(b - a);
94 }
95 inline __host__ __device__ float3 operator*(float3 a, float3 b)
96 {
97     return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
98 }
99 inline __host__ __device__ void operator*=(float3 &a, float3 b)
100 {
101     a.x *= b.x; a.y *= b.y; a.z *= b.z;
102 }
103 inline __device__ void atomicAdd(float3 *addr, float3 val)
104 {
105     atomicAdd(&addr->x, val.x);
106     atomicAdd(&addr->y, val.y);
107     atomicAdd(&addr->z, val.z);
108 }
109 /****************************************************************/
110
111 /**** float4 ****/
112 inline __host__ __device__ float4 make_float4(float s)
113 {
114     return make_float4(s, s, s, s);
115 }
116 inline __host__ __device__ float4 make_float4(float3 a)
117 {
118     return make_float4(a.x, a.y, a.z, 0.0f);
119 }
120 inline __host__ __device__ float4 operator+(float4 a, float4 b)
121 {
122     return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
123 }
124 inline __host__ __device__ float4 operator+(float4 a, float3 b)
125 {
126     return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w);
127 }
128 inline __host__ __device__ float4 operator-(float4 a, float4 b)
129 {
130     return make_float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
131 }
132 inline __host__ __device__ float4 operator*(float4 a, float k)
133 {
134     return make_float4(k * a.x, k * a.y, k * a.z, k * a.w);
135 }
136 inline __host__ __device__ void operator+=(float4 &a, float4 b)
137 {
138     a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
139 }
140 inline __host__ __device__ void operator+=(float4 &a, float3 b)
141 {
142     a.x += b.x; a.y += b.y; a.z += b.z;
143 }
144 inline __host__ __device__ void operator-=(float4 &a, float3 b)
145 {
146     a.x -= b.x; a.y -= b.y; a.z -= b.z;
147 }
148
149 inline __host__ __device__ float norm(float4 a)
150 {
151     return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
152 }
153
154 inline __host__ __device__ float dist3(float4 a, float4 b)
155 {
156     return norm(b - a);
157 }
158
159 #endif /* VECTYPE_OPS_CUH */