EHS/src/Math.cpp
karutoh 1a4a1ecd9c
Some checks failed
Build & Release / Linux-x86_64-Build (push) Successful in 40s
Build & Release / Linux-AARCH64-Build (push) Has been cancelled
First commit.
2024-01-31 22:28:19 -08:00

91 lines
1.8 KiB
C++

#include "ehs/Math.h"
#include "ehs/system/Architecture.h"
namespace ehs
{
bool Math::AbsCmp(const float a, const float b)
{
return Abs(a - b) <= fltEpsilon;
}
bool Math::AbsCmp(const double a, const double b)
{
return Abs(a - b) <= fltEpsilon;
}
bool Math::RelCmp(const float a, const float b)
{
return Abs(a - b) <= fltEpsilon * Max(Abs(a), Abs(b));
}
bool Math::RelCmp(const double a, const double b)
{
return Abs(a - b) <= fltEpsilon * Max(Abs(a), Abs(b));
}
bool Math::ComCmp(const float a, const float b)
{
return Abs(a - b) <= fltEpsilon * Max(1.0f, Max(Abs(a), Abs(b)));
}
bool Math::ComCmp(const double a, const double b)
{
return Abs(a - b) <= dblEpsilon * Max(1.0, Max(Abs(a), Abs(b)));
}
double Math::Sqrt(const double from)
{
#if defined(EHS_ARCH_X64)
if (CPU::HasAVX())
return Sqrt_AVX(from);
else if (CPU::HasSSE())
return Sqrt_SSE2(from);
double temp = 0.0;
double result = from / 2.0;
while (result != temp)
{
temp = result;
result = (from / temp + temp) / 2.0;
}
return result;
#elif defined(EHS_ARCH_ARM64)
return Sqrt_VFP4(from);
#endif
}
float Math::Sqrt(const float from)
{
#if defined(EHS_ARCH_X64)
if (CPU::HasAVX())
return Sqrt_AVX(from);
else if (CPU::HasSSE())
return Sqrt_SSE(from);
float temp = 0.0f;
float result = from / 2.0f;
while (result != temp)
{
temp = result;
result = (from / temp + temp) / 2.0f;
}
return result;
#elif defined(EHS_ARCH_ARM64)
return Sqrt_VFP4(from);
#endif
}
float Math::Mod(const float from, const float divisor)
{
return from - Trunc(from / divisor) * divisor;
}
double Math::Mod(const double from, const double divisor)
{
return from - Trunc(from / divisor) * divisor;
}
}