Fixed font atlas, feature completed garbage collection, and fix math.

This commit is contained in:
2024-06-29 22:20:53 -07:00
parent f030ac62ae
commit 0b298c6130
12 changed files with 283 additions and 180 deletions

View File

@@ -1,6 +1,5 @@
#pragma once
#include "EHS.h"
#include "Vector.h"
#include "BaseObj.h"
#include "ehs/system/Mutex.h"
@@ -10,10 +9,10 @@ namespace ehs
{
typedef bool (*GcLogic)(BaseObj*);
class GarbageCollector
class GC
{
private:
static Vector<GcLogic>* logic;
static Array<GcLogic> logic;
static Vector<BaseObj*> garbage;
static UInt_64 max;
static Thread thread;
@@ -27,6 +26,10 @@ namespace ehs
static void Stop();
static bool HasLogic(GcLogic logicCb);
static bool AddLogic(GcLogic logicCb);
/// Adds an object to the garbage pile to be deleted.
/// @param[in] obj The object to be deleted.
static void Add(BaseObj* obj);
@@ -59,5 +62,8 @@ namespace ehs
static void Dump();
static bool IsRunning();
private:
static bool ShouldDelete(BaseObj *obj);
};
}

View File

@@ -116,20 +116,51 @@ namespace ehs
return sum;
}
template <typename T = float>
static T Ln_Taylor(T x)
{
T result = 0;
T term = (x - 1) / (x + 1);
T term_squared = term * term;
T denominator = 1;
T current_term = term;
for (int n = 0; n < 100; ++n)
{
result += current_term / denominator;
current_term *= term_squared;
denominator += 2;
}
return 2 * result;
}
template <typename T = float>
static T Ln(T x)
{
if (x <= 0)
return -1;
T result = 0;
if (x == 1)
return 0;
x = (x - 1) / (x + 1);
SSize exp = 0;
while (x > 2)
{
x /= 2;
exp++;
}
for (int i = 1; i <= 19; i += 2)
result += (1 / i) * Exp<T>(x * i);
while (x < 0.5)
{
x *= 2;
exp--;
}
return 2 * result;
T result = Ln_Taylor<T>(x);
result += exp * Ln_Taylor<T>(2);
return result;
}
/// A method for use of exponents.
@@ -142,26 +173,35 @@ namespace ehs
static T Pow(const T base, const I exponent)
{
if (base == 0)
return 0;
return (exponent == 0) ? 1 : 0;
if (exponent == 0)
return 1;
UInt_32 exponentIsInteger = exponent == (UInt_32)exponent;
UInt_32 baseIsNegative = base < 0;
SSize intExp = (SSize)exponent;
bool isInteger = exponent == intExp;
bool isNeg = base < 0;
if (baseIsNegative && exponentIsInteger)
if (isNeg && isInteger)
{
T result = Exp<T>(exponent * Ln<T>(-base));
if (Mod((float)exponent, 2.0f) != 0)
if ((SSize)exponent % 2)
result = -result;
return result;
}
else if (!baseIsNegative)
return Exp<T>(exponent * Ln<T>(base));
return 0;
if (isNeg && !isInteger)
{
T magnitude = Exp<T>(exponent * Ln<T>(-base));
T angle = exponent * Pi<T>();
T realPart = magnitude * Cos<T>(angle);
T imagPart = magnitude * Sin<T>(angle);
return realPart;
}
return Exp<T>(exponent * Ln<T>(base));
}
template <typename T = float>

View File

@@ -1795,23 +1795,23 @@ namespace ehs
return result;
}
/// A 32-bit FNV-1a hash algorithm.
/// @param [in] str The string to hash.
/// @returns The resulting hash. Zero if string does not contain any characters.
static UInt_32 Hash_32(const Str<T, N>& str)
{
/// A 32-bit FNV-1a hash algorithm.
/// @param [in] str The string to hash.
/// @returns The resulting hash. Zero if string does not contain any characters.
static UInt_32 Hash_32(const Str<T, N>& str)
{
if (!str.Size())
return 0;
const Byte* const bytes = str.ToBytes();
const Byte* const bytes = str.ToBytes();
UInt_32 hash = 2166136261ul;
UInt_32 hash = 2166136261ul;
for (N i = 0; i < str.Size(true); ++i)
hash = (hash ^ bytes[i]) * 16777619;
for (N i = 0; i < str.Size(true); ++i)
hash = (hash ^ bytes[i]) * 16777619;
return hash;
}
return hash;
}
/// A 32-bit FNV-1a hash algorithm.
/// @returns The resulting hash. Zero if string does not contain any characters.
@@ -1830,23 +1830,23 @@ namespace ehs
return hash;
}
/// A 64-bit FNV-1a hash algorithm.
/// @param [in] str The string to hash.
/// A 64-bit FNV-1a hash algorithm.
/// @param [in] str The string to hash.
/// @returns The resulting hash. Zero if string does not contain any characters.
static UInt_64 Hash_64(const Str<T, N>& str)
{
static UInt_64 Hash_64(const Str<T, N>& str)
{
if (!str.Size())
return 0;
const Byte* const bytes = str.ToBytes();
const Byte* const bytes = str.ToBytes();
UInt_64 hash = 14695981039346656037ull;
UInt_64 hash = 14695981039346656037ull;
for (N i = 0; i < str.Size(true); ++i)
hash = (hash ^ bytes[i]) * 1099511628211;
for (N i = 0; i < str.Size(true); ++i)
hash = (hash ^ bytes[i]) * 1099511628211;
return hash;
}
return hash;
}
/// A 64-bit FNV-1a hash algorithm.
/// @returns The resulting hash. Zero if string does not contain any characters.

View File

@@ -62,7 +62,9 @@ namespace ehs
#if defined(EHS_64_BIT)
typedef UInt_64 Size;
typedef SInt_64 SSize;
#elif defined(EHS_32_BIT)
typedef UInt_32 Size;
typedef SInt_32 SSize;
#endif
}