Newton Dynamics  4.00
cuVector.h
1 /* Copyright (c) <2003-2021> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_CU_VECTOR_H__
23 #define __ND_CU_VECTOR_H__
24 
25 #include <cuda.h>
26 #include <vector_types.h>
27 #include <cuda_runtime.h>
28 #include <ndNewtonStdafx.h>
29 #include "cuIntrinsics.h"
30 
31 class cuVector: public float4
32 {
33  public:
34  inline __device__ __host__ cuVector()
35  {
36  }
37 
38  inline __device__ __host__ cuVector(float val)
39  {
40  x = val;
41  y = val;
42  z = val;
43  w = val;
44  }
45 
46  inline __device__ __host__ cuVector(float val_x, float val_y, float val_z, float val_w)
47  {
48  x = val_x;
49  y = val_y;
50  z = val_z;
51  w = val_w;
52  }
53 
54  inline __device__ __host__ cuVector(const cuVector& src)
55  {
56  x = src.x;
57  y = src.y;
58  z = src.z;
59  w = src.w;
60  }
61 
62  inline cuVector(const ndVector& src)
63  {
64  x = src.m_x;
65  y = src.m_y;
66  z = src.m_z;
67  w = src.m_w;
68  }
69 
70  inline ndVector ToNdVector(const cuVector& src) const
71  {
72  return ndVector(src.x, src.y, src.z, src.w);
73  }
74 
75  inline float __device__ __host__ GetElement(int i) const
76  {
77  return (&this->x)[i];
78  }
79 
80  inline void __device__ __host__ SetElement(int i, float val)
81  {
82  (&this->x)[i] = val;
83  }
84 
85  inline cuVector __device__ __host__ operator+ (const cuVector& A) const
86  {
87  return cuVector(x + A.x, y + A.y, z + A.z, w + A.w);
88  }
89 
90  inline cuVector __device__ __host__ operator- (const cuVector& A) const
91  {
92  return cuVector(x - A.x, y - A.y, z - A.z, w - A.w);
93  }
94 
95  inline cuVector __device__ __host__ operator* (const cuVector& A) const
96  {
97  return cuVector(x * A.x, y * A.y, z * A.z, w * A.w);
98  }
99 
100  inline cuVector __device__ __host__ operator> (const cuVector& A) const
101  {
102  return cuVector(
103  cuSelect(x > A.x, 1.0f, 0.0f),
104  cuSelect(y > A.y, 1.0f, 0.0f),
105  cuSelect(z > A.z, 1.0f, 0.0f),
106  cuSelect(w > A.w, 1.0f, 0.0f));
107  }
108 
109  inline cuVector __device__ __host__ operator< (const cuVector& A) const
110  {
111  return cuVector(
112  cuSelect(x < A.x, 1.0f, 0.0f),
113  cuSelect(y < A.y, 1.0f, 0.0f),
114  cuSelect(z < A.z, 1.0f, 0.0f),
115  cuSelect(w < A.w, 1.0f, 0.0f));
116  }
117 
118  inline cuVector __device__ __host__ Abs() const
119  {
120  return cuVector(cuAbs(x), cuAbs(y), cuAbs(z), cuAbs(w));
121  }
122 
123  inline cuVector __device__ __host__ Select(const cuVector& test, const cuVector& A) const
124  {
125  return cuVector(*this * test + A * (cuVector(1.0f) - test));
126  }
127 
128  inline cuVector __device__ __host__ Min (const cuVector& A) const
129  {
130  return cuVector(cuMin(x, A.x), cuMin(y, A.y), cuMin(z, A.z), cuMin(w, A.w));
131  }
132 
133  inline cuVector __device__ __host__ Max(const cuVector& A) const
134  {
135  return cuVector(cuMax(x, A.x), cuMax(y, A.y), cuMax(z, A.z), cuMax(w, A.w));
136  }
137 
138  inline cuVector __device__ __host__ Floor() const
139  {
140  return cuVector(cuFloor(x), cuFloor(y), cuFloor(z), cuFloor(w));
141  }
142 
143  inline cuVector __device__ __host__ Scale(float s) const
144  {
145  return cuVector(x * s, y * s, z * s, w * s);
146  }
147 
148  inline float __device__ __host__ AddHorizontal() const
149  {
150  return x + y + z + w;
151  }
152 
153  inline float __device__ __host__ DotProduct(const cuVector& B) const
154  {
155  return (*this * B).AddHorizontal();
156  }
157 
158  inline cuVector __device__ __host__ CrossProduct(const cuVector& B) const
159  {
160  return cuVector(y * B.z - z * B.y,
161  z * B.x - x * B.z,
162  x * B.y - y * B.x,
163  w);
164  }
165 
166  inline cuVector __device__ __host__ Normalize() const
167  {
168  float den = 1.0f / sqrt(DotProduct(*this));
169  return cuVector(x * den, y * den, z * den, w * den);
170  }
171 };
172 
173 #endif
cuVector
Definition: cuVector.h:32
ndVector
Definition: ndVectorArmNeon.h:41