Newton Dynamics  4.00
dGeneralVector.h
1 /* Copyright (c) <2003-2019> <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 __D_GENERAL_VECTOR_H__
23 #define __D_GENERAL_VECTOR_H__
24 
25 #include "dCoreStdafx.h"
26 #include "dTypes.h"
27 //#include "dMemory.h"
28 
29 template <class T>
30 T dSQRH(const T num, const T den)
31 {
32  T r(num / den);
33  return T(sqrt(T(1.0f) + r * r));
34 }
35 
36 template <class T>
37 T dPythag(const T a, const T b)
38 {
39  T absa(dAbs(a));
40  T absb(dAbs(b));
41  return (absa > absb) ? (absa * dSQRH(absb, absa)) : ((absb == T(0.0f) ? T(0.0f) : (absb * dSQRH(absa, absb))));
42 }
43 
44 template <class T>
45 T dSign(const T a, const T b)
46 {
47  return (b >= T(0.0f)) ? (a >= T(0.0f) ? a : -a) : (a >= T(0.0f) ? -a : a);
48 }
49 
50 // return dot product
51 template<class T>
52 T dDotProduct(dInt32 size, const T* const A, const T* const B)
53 {
54  T val(0.0f);
55  for (dInt32 i = 0; i < size; i++)
56  {
57  val = val + A[i] * B[i];
58  }
59  return val;
60 }
61 
62 template<class T>
63 void dAdd(dInt32 size, T* const X, const T* const A, const T* const B)
64 {
65  for (dInt32 i = 0; i < size; i++)
66  {
67  X[i] = A[i] + B[i];
68  }
69 }
70 
71 template<class T>
72 void dSub(dInt32 size, T* const X, const T* const A, const T* const B)
73 {
74  for (dInt32 i = 0; i < size; i++)
75  {
76  X[i] = A[i] - B[i];
77  }
78 }
79 
80 template<class T>
81 void dMulAdd(dInt32 size, T* const X, const T* const A, const T* const B, T C)
82 {
83  for (dInt32 i = 0; i < size; i++)
84  {
85  X[i] = A[i] + B[i] * C;
86  }
87 }
88 
89 #endif
90 
91