Skip to content

Commit 89a62f8

Browse files
committed
Fix Array AddRef, added Normalize data algorithm
1 parent 5f484df commit 89a62f8

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

Include/PipeAlgorithms.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,26 @@ namespace p
535535
}
536536
#pragma endregion Sort
537537

538+
#pragma region Transformations
539+
template<typename T, typename Index>
540+
void Normalize(T* first, Index size)
541+
{
542+
T magnitude = {};
543+
for (Index i = 0; i < size; ++i)
544+
{
545+
const T& value = first[i];
546+
magnitude += value * value;
547+
}
548+
if (magnitude > T(0))
549+
{
550+
const T invMagnitude = InvSqrt(magnitude);
551+
for (Index i = 0; i < size; ++i)
552+
{
553+
first[i] *= invMagnitude;
554+
}
555+
}
556+
}
557+
#pragma endregion Transformations
538558

539559
/** Generates CRC hash of the memory area */
540560
PIPE_API u32 MemCrc32(const void* Data, i32 Length, u32 CRC = 0);

Include/PipeArrays.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -841,17 +841,20 @@ namespace p
841841
new (Super::data + firstIdx) Type(value);
842842
return firstIdx;
843843
}
844-
Type& AddRef()
844+
inline Type& AddRef()
845845
{
846-
return Super::data[Add()];
846+
const i32 idx = Add();
847+
return Super::data[idx];
847848
}
848-
Type& AddRef(Type&& value)
849+
inline Type& AddRef(Type&& value)
849850
{
850-
return Super::data[Add(p::Forward<Type>(value))];
851+
const i32 idx = Add(p::Forward<Type>(value));
852+
return Super::data[idx];
851853
}
852-
Type& AddRef(const Type& value)
854+
inline Type& AddRef(const Type& value)
853855
{
854-
return Super::data[Add(value)];
856+
const i32 idx = Add(value);
857+
return Super::data[idx];
855858
}
856859

857860
template<typename SortPredicate = TLess<Type>>

Include/PipeMath.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ namespace p
259259
{
260260
return 1.f / Sqrt(x);
261261
}
262+
inline PIPE_API double InvSqrt(double x)
263+
{
264+
return 1. / Sqrt(x);
265+
}
266+
inline PIPE_API float InvSqrt(i32 x)
267+
{
268+
return 1.f / Sqrt(x);
269+
}
270+
inline PIPE_API double InvSqrt(i64 x)
271+
{
272+
return 1. / Sqrt(x);
273+
}
262274

263275
template<typename T>
264276
static constexpr T Square(T val)

Include/PipeVectors.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace p
8585
const T lengthSquared = LengthSquared();
8686
if (lengthSquared > 0.f)
8787
{
88-
const float scale = InvSqrt(lengthSquared);
88+
const auto scale = InvSqrt(lengthSquared);
8989
x *= scale;
9090
y *= scale;
9191
}
@@ -365,7 +365,7 @@ namespace p
365365
const T lengthSqrt = LengthSquared();
366366
if (lengthSqrt > 0.f)
367367
{
368-
const float scale = InvSqrt(lengthSqrt);
368+
const auto scale = InvSqrt(lengthSqrt);
369369
x *= scale;
370370
y *= scale;
371371
z *= scale;

0 commit comments

Comments
 (0)