Update platform for emscripten builds

This commit is contained in:
2025-07-14 23:15:14 +10:00
parent 941f61ec34
commit f6f2148888
9 changed files with 100 additions and 36 deletions
+11 -4
View File
@@ -3,17 +3,24 @@
#include "../dn_clangd.h"
// NOTE: [$INTR] Intrinsics ////////////////////////////////////////////////////////////////////////
DN_CPUFeatureDecl g_dn_cpu_feature_decl[DN_CPUFeature_Count];
#if !defined(DN_PLATFORM_ARM64) && !defined(DN_PLATFORM_EMSCRIPTEN)
#define DN_SUPPORTS_CPU_ID
#endif
#if defined(DN_SUPPORTS_CPU_ID)
#if defined(DN_COMPILER_GCC) || defined(DN_COMPILER_CLANG)
#include <cpuid.h>
#endif
DN_CPUFeatureDecl g_dn_cpu_feature_decl[DN_CPUFeature_Count];
#endif // defined(DN_SUPPORTS_CPU_ID)
DN_API DN_CPUIDResult DN_CPU_ID(DN_CPUIDArgs args)
{
DN_CPUIDResult result = {};
#if defined(DN_SUPPORTS_CPU_ID)
__cpuidex(result.values, args.eax, args.ecx);
#endif
return result;
}
@@ -61,6 +68,7 @@ DN_API void DN_CPU_SetFeature(DN_CPUReport *report, DN_CPUFeature feature)
DN_API DN_CPUReport DN_CPU_Report()
{
DN_CPUReport result = {};
#if defined(DN_SUPPORTS_CPU_ID)
DN_CPUIDResult fn_0000_[500] = {};
DN_CPUIDResult fn_8000_[500] = {};
int const EXTENDED_FUNC_BASE_EAX = 0x8000'0000;
@@ -201,10 +209,9 @@ DN_API DN_CPUReport DN_CPU_Report()
if (available)
DN_CPU_SetFeature(&result, DN_CAST(DN_CPUFeature) ext_index);
}
#endif // DN_SUPPORTS_CPU_ID
return result;
}
#endif // !defined(DN_PLATFORM_ARM64) && !defined(DN_PLATFORM_EMSCRIPTEN)
// NOTE: DN_TicketMutex ////////////////////////////////////////////////////////////////////////////
DN_API void DN_TicketMutex_Begin(DN_TicketMutex *mutex)
+12
View File
@@ -255,12 +255,16 @@ struct DN_CallSite
#define DN_Atomic_SubU64(target, value) DN_Atomic_AddU64(target, (DN_U64) - value)
#define DN_CountLeadingZerosU64(value) __lzcnt64(value)
#define DN_CountLeadingZerosU32(value) __lzcnt(value)
#define DN_CPU_TSC() __rdtsc()
#define DN_CompilerReadBarrierAndCPUReadFence _ReadBarrier(); _mm_lfence()
#define DN_CompilerWriteBarrierAndCPUWriteFence _WriteBarrier(); _mm_sfence()
#elif defined(DN_COMPILER_GCC) || defined(DN_COMPILER_CLANG)
#if defined(__ANDROID__)
#elif defined(DN_PLATFORM_EMSCRIPTEN)
#if !defined(__wasm_simd128__)
#error DN_Base requires -msse2 to be passed to Emscripten
#endif
#include <emmintrin.h>
#else
#include <x86intrin.h>
@@ -274,6 +278,8 @@ struct DN_CallSite
#define DN_Atomic_SubU64(target, value) __atomic_fetch_sub(target, value, __ATOMIC_ACQ_REL)
#define DN_CountLeadingZerosU64(value) __builtin_clzll(value)
#define DN_CountLeadingZerosU32(value) __builtin_clzl(value)
#if defined(DN_COMPILER_GCC)
#define DN_CPU_TSC() __rdtsc()
#else
@@ -291,6 +297,12 @@ struct DN_CallSite
#error "Compiler not supported"
#endif
#if defined(__aarch64__) || defined(_M_X64) || defined(__x86_64__) || defined(__x86_64)
#define DN_CountLeadingZerosUSize(value) DN_CountLeadingZerosU64(value)
#else
#define DN_CountLeadingZerosUSize(value) DN_CountLeadingZerosU32(value)
#endif
#if !defined(DN_PLATFORM_ARM64)
struct DN_CPURegisters
{
+4 -4
View File
@@ -421,11 +421,11 @@ DN_API void *DN_Pool_Alloc(DN_Pool *pool, DN_USize size)
DN_USize slot_index = 0;
if (required_size > 32) {
// NOTE: Round up if not PoT as the low bits are set.
DN_USize dist_to_next_msb = DN_CountLeadingZerosU64(required_size) + 1;
DN_USize dist_to_next_msb = DN_CountLeadingZerosUSize(required_size) + 1;
dist_to_next_msb -= DN_CAST(DN_USize)(!DN_IsPowerOfTwo(required_size));
DN_USize const register_size = sizeof(DN_USize) * 8;
DN_Assert(register_size >= dist_to_next_msb + size_to_slot_offset);
DN_AssertF(register_size >= (dist_to_next_msb - size_to_slot_offset), "lhs=%zu, rhs=%zu");
slot_index = register_size - dist_to_next_msb - size_to_slot_offset;
}
@@ -433,8 +433,8 @@ DN_API void *DN_Pool_Alloc(DN_Pool *pool, DN_USize size)
return result;
DN_USize slot_size_in_bytes = 1ULL << (slot_index + size_to_slot_offset);
DN_Assert(required_size <= (slot_size_in_bytes << 0));
DN_Assert(required_size >= (slot_size_in_bytes >> 1));
DN_AssertF(required_size <= (slot_size_in_bytes << 0), "slot_index=%zu, lhs=%zu, rhs=%zu", slot_index, required_size, (slot_size_in_bytes << 0));
DN_AssertF(required_size >= (slot_size_in_bytes >> 1), "slot_index=%zu, lhs=%zu, rhs=%zu", slot_index, required_size, (slot_size_in_bytes >> 1));
DN_PoolSlot *slot = nullptr;
if (pool->slots[slot_index]) {