diff --git a/Single_Header/dn_single_header.cpp b/Single_Header/dn_single_header.cpp index 3e7692c..035adee 100644 --- a/Single_Header/dn_single_header.cpp +++ b/Single_Header/dn_single_header.cpp @@ -1,4 +1,4 @@ -// Generated by the DN single header generator 2025-06-30 22:12:04 +// Generated by the DN single header generator 2025-07-14 23:14:47 #define DN_BASE_INC_CPP @@ -8,17 +8,24 @@ // DN: Single header generator commented out this header => #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 #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; } @@ -66,6 +73,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; @@ -206,10 +214,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) @@ -2958,11 +2965,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; } @@ -2970,8 +2977,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]) { @@ -4906,7 +4913,11 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args) os->page_size = system_info.dwPageSize; os->alloc_granularity = system_info.dwAllocationGranularity; #else - os->logical_processor_count = get_nprocs(); + #if defined(DN_PLATFORM_EMSCRIPTEN) + os->logical_processor_count = 1; + #else + os->logical_processor_count = get_nprocs(); + #endif os->page_size = getpagesize(); os->alloc_granularity = os->page_size; #endif @@ -4916,10 +4927,15 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args) DN_OS_EmitLogsWithOSPrintFunctions(os); { + #if defined(DN_PLATFORM_EMSCRIPTEN) os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack); + #else + os->arena = DN_Arena_InitFromOSHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack); + #endif + #if defined(DN_PLATFORM_WIN32) os->platform_context = DN_Arena_New(&os->arena, DN_W32Core, DN_ZeroMem_Yes); - #elif defined(DN_PLATFORM_POSIX) + #elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN) os->platform_context = DN_Arena_New(&os->arena, DN_POSIXCore, DN_ZeroMem_Yes); #endif @@ -6267,7 +6283,7 @@ DN_API DN_Str8 DN_Str8Builder_BuildFromOSHeap(DN_Str8Builder const *builder) return result; } -#if defined(DN_PLATFORM_POSIX) +#if defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN) // DN: Single header generator inlined this file => #include "OS/dn_os_posix.cpp" #define DN_OS_POSIX_CPP @@ -7278,14 +7294,14 @@ static DN_POSIXCore *DN_OS_GetPOSIXCore_() static DN_POSIXSyncPrimitive *DN_OS_U64ToPOSIXSyncPrimitive_(DN_U64 u64) { DN_POSIXSyncPrimitive *result = nullptr; - DN_Memcpy(&result, &u64, sizeof(u64)); + DN_Memcpy(&result, &u64, sizeof(result)); return result; } static DN_U64 DN_POSIX_SyncPrimitiveToU64(DN_POSIXSyncPrimitive *primitive) { DN_U64 result = 0; - static_assert(sizeof(result) == sizeof(primitive), "Pointer size mis-match"); + static_assert(sizeof(result) >= sizeof(primitive), "Pointer size mis-match"); DN_Memcpy(&result, &primitive, sizeof(result)); return result; } @@ -7563,10 +7579,14 @@ DN_API DN_U32 DN_OS_ThreadID() DN_API void DN_Posix_ThreadSetName(DN_Str8 name) { +#if defined(DN_PLATFORM_EMSCRIPTEN) + (void)name; +#else DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr); - DN_Str8 copy = DN_Str8_Copy(tmem.arena, name); - pthread_t thread = pthread_self(); + DN_Str8 copy = DN_Str8_Copy(tmem.arena, name); + pthread_t thread = pthread_self(); pthread_setname_np(thread, (char *)copy.data); +#endif } DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus() @@ -7758,7 +7778,7 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response, fetch_attribs.onerror = DN_OS_HttpRequestEMFetchOnErrorCallback; fetch_attribs.userData = response; - DN_Str8 url = DN_Str8_InitF(scratch_arena, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path)); + DN_Str8 url = DN_Str8_InitF(tmem, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path)); DN_LOG_InfoF("Initiating HTTP '%s' request to '%.*s' with payload '%.*s'", fetch_attribs.requestMethod, DN_STR_FMT(url), diff --git a/Single_Header/dn_single_header.h b/Single_Header/dn_single_header.h index 780621e..492c57b 100644 --- a/Single_Header/dn_single_header.h +++ b/Single_Header/dn_single_header.h @@ -1,4 +1,4 @@ -// Generated by the DN single header generator 2025-06-30 22:12:04 +// Generated by the DN single header generator 2025-07-14 23:14:47 #if !defined(DN_BASE_INC_H) #define DN_BASE_INC_H @@ -465,12 +465,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 #else #include @@ -484,6 +488,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 @@ -501,6 +507,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 { @@ -5607,7 +5619,7 @@ DN_API DN_Str8 DN_W32_WorkingDir (DN_Arena *arena, DN_Str8 suffix); DN_API DN_Str16 DN_W32_WorkingDirW (DN_Arena *arena, DN_Str16 suffix); DN_API bool DN_W32_DirWIterate (DN_Str16 path, DN_W32FolderIteratorW *it); #endif // !defined(DN_OS_WIN32) -#elif defined(DN_PLATFORM_POSIX) +#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN) // DN: Single header generator inlined this file => #include "OS/dn_os_posix.h" #pragma once diff --git a/Source/Base/dn_base.cpp b/Source/Base/dn_base.cpp index 6deb114..0e9a99c 100644 --- a/Source/Base/dn_base.cpp +++ b/Source/Base/dn_base.cpp @@ -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 #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) diff --git a/Source/Base/dn_base.h b/Source/Base/dn_base.h index ac0a72f..d37843b 100644 --- a/Source/Base/dn_base.h +++ b/Source/Base/dn_base.h @@ -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 #else #include @@ -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 { diff --git a/Source/Base/dn_base_mem.cpp b/Source/Base/dn_base_mem.cpp index a0d1214..8393a39 100644 --- a/Source/Base/dn_base_mem.cpp +++ b/Source/Base/dn_base_mem.cpp @@ -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]) { diff --git a/Source/OS/dn_os.cpp b/Source/OS/dn_os.cpp index 41f8ceb..08d5612 100644 --- a/Source/OS/dn_os.cpp +++ b/Source/OS/dn_os.cpp @@ -96,7 +96,11 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args) os->page_size = system_info.dwPageSize; os->alloc_granularity = system_info.dwAllocationGranularity; #else - os->logical_processor_count = get_nprocs(); + #if defined(DN_PLATFORM_EMSCRIPTEN) + os->logical_processor_count = 1; + #else + os->logical_processor_count = get_nprocs(); + #endif os->page_size = getpagesize(); os->alloc_granularity = os->page_size; #endif @@ -106,10 +110,15 @@ DN_API void DN_OS_Init(DN_OSCore *os, DN_OSInitArgs *args) DN_OS_EmitLogsWithOSPrintFunctions(os); { + #if defined(DN_PLATFORM_EMSCRIPTEN) os->arena = DN_Arena_InitFromOSVMem(DN_Megabytes(1), DN_Kilobytes(4), DN_ArenaFlags_NoAllocTrack); + #else + os->arena = DN_Arena_InitFromOSHeap(DN_Megabytes(1), DN_ArenaFlags_NoAllocTrack); + #endif + #if defined(DN_PLATFORM_WIN32) os->platform_context = DN_Arena_New(&os->arena, DN_W32Core, DN_ZeroMem_Yes); - #elif defined(DN_PLATFORM_POSIX) + #elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN) os->platform_context = DN_Arena_New(&os->arena, DN_POSIXCore, DN_ZeroMem_Yes); #endif diff --git a/Source/OS/dn_os_posix.cpp b/Source/OS/dn_os_posix.cpp index cbcc753..08fb98d 100644 --- a/Source/OS/dn_os_posix.cpp +++ b/Source/OS/dn_os_posix.cpp @@ -1007,14 +1007,14 @@ static DN_POSIXCore *DN_OS_GetPOSIXCore_() static DN_POSIXSyncPrimitive *DN_OS_U64ToPOSIXSyncPrimitive_(DN_U64 u64) { DN_POSIXSyncPrimitive *result = nullptr; - DN_Memcpy(&result, &u64, sizeof(u64)); + DN_Memcpy(&result, &u64, sizeof(result)); return result; } static DN_U64 DN_POSIX_SyncPrimitiveToU64(DN_POSIXSyncPrimitive *primitive) { DN_U64 result = 0; - static_assert(sizeof(result) == sizeof(primitive), "Pointer size mis-match"); + static_assert(sizeof(result) >= sizeof(primitive), "Pointer size mis-match"); DN_Memcpy(&result, &primitive, sizeof(result)); return result; } @@ -1292,10 +1292,14 @@ DN_API DN_U32 DN_OS_ThreadID() DN_API void DN_Posix_ThreadSetName(DN_Str8 name) { +#if defined(DN_PLATFORM_EMSCRIPTEN) + (void)name; +#else DN_OSTLSTMem tmem = DN_OS_TLSPushTMem(nullptr); - DN_Str8 copy = DN_Str8_Copy(tmem.arena, name); - pthread_t thread = pthread_self(); + DN_Str8 copy = DN_Str8_Copy(tmem.arena, name); + pthread_t thread = pthread_self(); pthread_setname_np(thread, (char *)copy.data); +#endif } DN_API DN_POSIXProcSelfStatus DN_Posix_ProcSelfStatus() @@ -1487,7 +1491,7 @@ DN_API void DN_OS_HttpRequestAsync(DN_OSHttpResponse *response, fetch_attribs.onerror = DN_OS_HttpRequestEMFetchOnErrorCallback; fetch_attribs.userData = response; - DN_Str8 url = DN_Str8_InitF(scratch_arena, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path)); + DN_Str8 url = DN_Str8_InitF(tmem, "%.*s%.*s", DN_STR_FMT(host), DN_STR_FMT(path)); DN_LOG_InfoF("Initiating HTTP '%s' request to '%.*s' with payload '%.*s'", fetch_attribs.requestMethod, DN_STR_FMT(url), diff --git a/Source/dn_os_inc.cpp b/Source/dn_os_inc.cpp index d3b111e..a34b381 100644 --- a/Source/dn_os_inc.cpp +++ b/Source/dn_os_inc.cpp @@ -7,7 +7,7 @@ #include "OS/dn_os_print.cpp" #include "OS/dn_os_string.cpp" -#if defined(DN_PLATFORM_POSIX) +#if defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN) #include "OS/dn_os_posix.cpp" #elif defined(DN_PLATFORM_WIN32) #include "OS/dn_os_w32.cpp" diff --git a/Source/dn_os_inc.h b/Source/dn_os_inc.h index bfd7e8c..fa57cd8 100644 --- a/Source/dn_os_inc.h +++ b/Source/dn_os_inc.h @@ -4,7 +4,7 @@ #if defined(DN_PLATFORM_WIN32) #include "OS/dn_os_windows.h" #include "OS/dn_os_w32.h" -#elif defined(DN_PLATFORM_POSIX) +#elif defined(DN_PLATFORM_POSIX) || defined(DN_PLATFORM_EMSCRIPTEN) #include "OS/dn_os_posix.h" #else #error Please define a platform e.g. 'DN_PLATFORM_WIN32' to enable the correct implementation for platform APIs