Newton Dynamics  4.00
ndGeneralVector.h
1 /* Copyright (c) <2003-2022> <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_GENERAL_VECTOR_H__
23 #define __ND_GENERAL_VECTOR_H__
24 
25 #include "ndCoreStdafx.h"
26 #include "ndTypes.h"
27 
28 template <class T>
29 T ndSQRH(const T num, const T den)
30 {
31  T r(num / den);
32  return T(sqrt(T(1.0f) + r * r));
33 }
34 
35 template <class T>
36 T ndPythag(const T a, const T b)
37 {
38  T absa(ndAbs(a));
39  T absb(ndAbs(b));
40  return (absa > absb) ? (absa * ndSQRH(absb, absa)) : ((absb == T(0.0f) ? T(0.0f) : (absb * ndSQRH(absa, absb))));
41 }
42 
43 template <class T>
44 T ndSign(const T a, const T b)
45 {
46  return (b >= T(0.0f)) ? (a >= T(0.0f) ? a : -a) : (a >= T(0.0f) ? -a : a);
47 }
48 
49 // return dot product
50 template<class T>
51 T ndDotProduct(ndInt32 size, const T* const A, const T* const B)
52 {
53  T val(0.0f);
54  for (ndInt32 i = 0; i < size; ++i)
55  {
56  val = val + A[i] * B[i];
57  ndAssert(ndCheckFloat(val));
58  }
59  return val;
60 }
61 
62 template<class T>
63 void ndScaleSet(ndInt32 size, T* const X, const T* const A, T scale)
64 {
65  for (ndInt32 i = 0; i < size; ++i)
66  {
67  X[i] = A[i] * scale;
68  ndAssert(ndCheckFloat(X[i]));
69  }
70 }
71 
72 template<class T>
73 void ndAdd(ndInt32 size, T* const X, const T* const A, const T* const B)
74 {
75  for (ndInt32 i = 0; i < size; ++i)
76  {
77  X[i] = A[i] + B[i];
78  ndAssert(ndCheckFloat(X[i]));
79  }
80 }
81 
82 template<class T>
83 void ndSub(ndInt32 size, T* const X, const T* const A, const T* const B)
84 {
85  for (ndInt32 i = 0; i < size; ++i)
86  {
87  X[i] = A[i] - B[i];
88  ndAssert(ndCheckFloat(X[i]));
89  }
90 }
91 
92 template<class T>
93 void ndMul(ndInt32 size, T* const X, const T* const A, const T* const B)
94 {
95  for (ndInt32 i = 0; i < size; ++i)
96  {
97  X[i] = A[i] * B[i];
98  ndAssert(ndCheckFloat(X[i]));
99  }
100 }
101 
102 template<class T>
103 void ndScaleAdd(ndInt32 size, T* const X, const T* const A, const T* const B, T C)
104 {
105  for (ndInt32 i = 0; i < size; ++i)
106  {
107  X[i] = A[i] + B[i] * C;
108  ndAssert(ndCheckFloat(X[i]));
109  }
110 }
111 
112 template<class T>
113 void ndMulAdd(ndInt32 size, T* const X, const T* const A, const T* const B, const T* const C)
114 {
115  for (ndInt32 i = 0; i < size; ++i)
116  {
117  X[i] = A[i] + B[i] * C[i];
118  ndAssert(ndCheckFloat(X[i]));
119  }
120 }
121 
122 template<class T>
123 void ndMulSub(ndInt32 size, T* const X, const T* const A, const T* const B, const T* const C)
124 {
125  for (ndInt32 i = 0; i < size; ++i)
126  {
127  X[i] = A[i] - B[i] * C[i];
128  ndAssert(ndCheckFloat(X[i]));
129  }
130 }
131 
132 #endif
133 
134