Newton Dynamics  4.00
ndCudaVector.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_CUDA_VECTOR_H__
23 #define __ND_CUDA_VECTOR_H__
24 
25 #include <cuda.h>
26 #include <vector_types.h>
27 #include <cuda_runtime.h>
28 #include "ndCudaIntrinsics.h"
29 
30 class ndCudaVector: public float4
31 {
32  public:
33  inline __device__ __host__ ndCudaVector()
34  {
35  }
36 
37  inline __device__ __host__ ndCudaVector(float val)
38  {
39  x = val;
40  y = val;
41  z = val;
42  w = val;
43  }
44 
45  inline __device__ __host__ ndCudaVector(float val_x, float val_y, float val_z, float val_w)
46  {
47  x = val_x;
48  y = val_y;
49  z = val_z;
50  w = val_w;
51  }
52 
53  inline __device__ __host__ ndCudaVector(const ndCudaVector& src)
54  {
55  x = src.x;
56  y = src.y;
57  z = src.z;
58  w = src.w;
59  }
60 
61  //inline ndCudaVector(const ndVector& src)
62  //{
63  // x = src.m_x;
64  // y = src.m_y;
65  // z = src.m_z;
66  // w = src.m_w;
67  //}
68  //
69  //inline ndVector ToNdVector(const ndCudaVector& src) const
70  //{
71  // return ndVector(src.x, src.y, src.z, src.w);
72  //}
73 
74  inline float __device__ __host__ GetElement(int i) const
75  {
76  return (&this->x)[i];
77  }
78 
79  inline void __device__ __host__ SetElement(int i, float val)
80  {
81  (&this->x)[i] = val;
82  }
83 
84  inline ndCudaVector __device__ __host__ operator+ (const ndCudaVector& A) const
85  {
86  return ndCudaVector(x + A.x, y + A.y, z + A.z, w + A.w);
87  }
88 
89  inline ndCudaVector __device__ __host__ operator- (const ndCudaVector& A) const
90  {
91  return ndCudaVector(x - A.x, y - A.y, z - A.z, w - A.w);
92  }
93 
94  inline ndCudaVector __device__ __host__ operator* (const ndCudaVector& A) const
95  {
96  return ndCudaVector(x * A.x, y * A.y, z * A.z, w * A.w);
97  }
98 
99  inline ndCudaVector __device__ __host__ operator> (const ndCudaVector& A) const
100  {
101  return ndCudaVector(
102  cuSelect(x > A.x, 1.0f, 0.0f),
103  cuSelect(y > A.y, 1.0f, 0.0f),
104  cuSelect(z > A.z, 1.0f, 0.0f),
105  cuSelect(w > A.w, 1.0f, 0.0f));
106  }
107 
108  inline ndCudaVector __device__ __host__ operator< (const ndCudaVector& A) const
109  {
110  return ndCudaVector(
111  cuSelect(x < A.x, 1.0f, 0.0f),
112  cuSelect(y < A.y, 1.0f, 0.0f),
113  cuSelect(z < A.z, 1.0f, 0.0f),
114  cuSelect(w < A.w, 1.0f, 0.0f));
115  }
116 
117  inline ndCudaVector __device__ __host__ Abs() const
118  {
119  return ndCudaVector(cuAbs(x), cuAbs(y), cuAbs(z), cuAbs(w));
120  }
121 
122  inline ndCudaVector __device__ __host__ Select(const ndCudaVector& test, const ndCudaVector& A) const
123  {
124  return ndCudaVector(*this * test + A * (ndCudaVector(1.0f) - test));
125  }
126 
127  inline ndCudaVector __device__ __host__ Min (const ndCudaVector& A) const
128  {
129  return ndCudaVector(cuMin(x, A.x), cuMin(y, A.y), cuMin(z, A.z), cuMin(w, A.w));
130  }
131 
132  inline ndCudaVector __device__ __host__ Max(const ndCudaVector& A) const
133  {
134  return ndCudaVector(cuMax(x, A.x), cuMax(y, A.y), cuMax(z, A.z), cuMax(w, A.w));
135  }
136 
137  inline ndCudaVector __device__ __host__ Floor() const
138  {
139  return ndCudaVector(cuFloor(x), cuFloor(y), cuFloor(z), cuFloor(w));
140  }
141 
142  inline ndCudaVector __device__ __host__ Scale(float s) const
143  {
144  return ndCudaVector(x * s, y * s, z * s, w * s);
145  }
146 
147  inline float __device__ __host__ AddHorizontal() const
148  {
149  return x + y + z + w;
150  }
151 
152  inline float __device__ __host__ DotProduct(const ndCudaVector& B) const
153  {
154  return (*this * B).AddHorizontal();
155  }
156 
157  inline ndCudaVector __device__ __host__ CrossProduct(const ndCudaVector& B) const
158  {
159  return ndCudaVector(y * B.z - z * B.y,
160  z * B.x - x * B.z,
161  x * B.y - y * B.x,
162  w);
163  }
164 
165  inline ndCudaVector __device__ __host__ Normalize() const
166  {
167  float den = 1.0f / sqrtf(DotProduct(*this));
168  return ndCudaVector(x * den, y * den, z * den, w * den);
169  }
170 };
171 
172 #endif
ndCudaVector
Definition: ndCudaVector.h:31