Skip to content

Commit

Permalink
A few bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EimaMei committed Dec 1, 2024
1 parent 0187d3c commit 7437d4b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AR = ar
OUTPUT = build
NAME = sili

GNU_FLAGS = -std=c11 -Wall -Wextra -Wpedantic \
GNU_FLAGS = -std=c11 -O2 -Wall -Wextra -Wpedantic \
-Wconversion -Wno-float-conversion -Wno-sign-conversion \
-Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-prototypes \
-Wvla -Wcast-align -Wcast-align=strict \
Expand Down Expand Up @@ -41,7 +41,7 @@ ifneq (,$(filter $(CC),winegcc x86_64-w64-mingw32-gcc w64gcc w32gcc i686-w64-min
FLAGS = $(GNU_FLAGS)
INCLUDES = $(GNU_INCLUDES)

LIBS = -lkernel32 -lole32 -lopengl32 -lntdll
LIBS = -lkernel32 -lole32 -lopengl32 -lntoskrnl
EXE = $(OUTPUT)/test.exe
LINKER = $(CC)

Expand Down
66 changes: 50 additions & 16 deletions sili.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ extern "C" {

#if SI_SYSTEM_IS_APPLE
#include <sys/socket.h>
#include <sys/sysctl.h>
#endif

#if SI_SYSTEM_IS_UNIX
Expand Down Expand Up @@ -7347,7 +7348,7 @@ u64 si_timeStampStart(void) {
return si_RDTSC();
}

inline
SIDEF
void si_timeStampPrintSince(u64 ts) {
u64 end = si_RDTSC();
u64 diff = (end - ts) / si_cpuClockSpeed() * 1000;
Expand Down Expand Up @@ -7940,37 +7941,53 @@ u32 si_numLenI64Ex(i64 num, u32 base) {
return si_numLenEx(unsignedNum, base) + (u32)isNegative;
}

#define SI_CHECK_ARITHMETIC_IMPL_ALL(func, def, ...) \
SI_CHECK_ARITHMETIC_DEC(i8, func, SIDEF, { const i8 max = INT8_MAX; const i8 min = INT8_MIN; __VA_ARGS__ }) \
#define SI_CHECK_ARITHMETIC_IMPL_ALL_I(func, def, ...) \
SI_CHECK_ARITHMETIC_DEC(i8, func, SIDEF, { const i8 max = INT8_MAX; const i8 min = INT8_MIN; __VA_ARGS__ }) \
SI_CHECK_ARITHMETIC_DEC(i16, func, SIDEF, { const i16 max = INT16_MAX; const i16 min = INT16_MIN; __VA_ARGS__ }) \
SI_CHECK_ARITHMETIC_DEC(i32, func, SIDEF, { const i32 max = INT32_MAX; const i32 min = INT32_MIN; __VA_ARGS__ }) \
SI_CHECK_ARITHMETIC_DEC(i64, func, SIDEF, { const i64 max = INT64_MAX; const i64 min = INT64_MIN; __VA_ARGS__ }) \
SI_CHECK_ARITHMETIC_DEC(isize, func, SIDEF, { const isize max = ISIZE_MAX; const isize min = ISIZE_MIN; __VA_ARGS__ }) \
\
SI_CHECK_ARITHMETIC_DEC(u8, func, SIDEF, { const u8 max = UINT8_MAX; const u8 min = 0; __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(u16, func, SIDEF, { const u16 max = UINT16_MAX; const u16 min = 0; __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(u32, func, SIDEF, { const u32 max = UINT32_MAX; const u32 min = 0; __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(u64, func, SIDEF, { const u64 max = UINT64_MAX; const u64 min = 0; __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(usize, func, SIDEF, { const usize max = USIZE_MAX; const usize min = 0; __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(isize, func, SIDEF, { const isize max = ISIZE_MAX; const isize min = ISIZE_MIN; __VA_ARGS__ })

#define SI_CHECK_ARITHMETIC_IMPL_ALL_U(func, def, ...) \
SI_CHECK_ARITHMETIC_DEC(u8, func, SIDEF, { const u8 max = UINT8_MAX; SI_UNUSED(max); __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(u16, func, SIDEF, { const u16 max = UINT16_MAX; SI_UNUSED(max); __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(u32, func, SIDEF, { const u32 max = UINT32_MAX; SI_UNUSED(max); __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(u64, func, SIDEF, { const u64 max = UINT64_MAX; SI_UNUSED(max); __VA_ARGS__ } ) \
SI_CHECK_ARITHMETIC_DEC(usize, func, SIDEF, { const usize max = USIZE_MAX; SI_UNUSED(max); __VA_ARGS__ } ) \


SI_CHECK_ARITHMETIC_IMPL_ALL(Add, SIDEF,
SI_CHECK_ARITHMETIC_IMPL_ALL_U(Add, SIDEF,
SI_ASSERT_NOT_NULL(res); \
return a > (*res = a + b); \
)
SI_CHECK_ARITHMETIC_IMPL_ALL_I(Add, SIDEF,
SI_ASSERT_NOT_NULL(res); \
*res = a + b ; \
if (a >= 0) { \
return (max - a < b); \
} \
return (b < min - a); \
)
SI_CHECK_ARITHMETIC_IMPL_ALL(Sub, SIDEF,

SI_CHECK_ARITHMETIC_IMPL_ALL_U(Sub, SIDEF,
SI_ASSERT_NOT_NULL(res); \
return a < (*res = a - b); \
)
SI_CHECK_ARITHMETIC_IMPL_ALL_I(Sub, SIDEF,
SI_ASSERT_NOT_NULL(res); \
*res = a - b ; \
if (b < 0) { \
return (max + b < a); \
} \
return (min + b > a); \
)
SI_CHECK_ARITHMETIC_IMPL_ALL(Mul, SIDEF,

SI_CHECK_ARITHMETIC_IMPL_ALL_U(Mul, SIDEF,
SI_ASSERT_NOT_NULL(res); \
*res = a * b ; \
return (b > 0 && a > max / b);
)
SI_CHECK_ARITHMETIC_IMPL_ALL_I(Mul, SIDEF,
SI_ASSERT_NOT_NULL(res); \
*res = a * b ; \
if (a > 0) { \
Expand All @@ -7983,7 +8000,8 @@ SI_CHECK_ARITHMETIC_IMPL_ALL(Mul, SIDEF,
); \
)

#undef SI_CHECK_ARITHMETIC_IMPL_ALL
#undef SI_CHECK_ARITHMETIC_IMPL_ALL_U
#undef SI_CHECK_ARITHMETIC_IMPL_ALL_S
#undef SI_CHECK_ARITHMETIC_DEC

#endif /* SI_IMPLEMENTATION_BIT */
Expand Down Expand Up @@ -8078,7 +8096,7 @@ siString si_envVarGet(siString name, u8* out, usize capacity) {
}

#if SI_COMPILER_MSVC
#pragma comment(lib, "ntdll")
#pragma comment(lib, "ntoskrnl")
#endif


Expand Down Expand Up @@ -8176,8 +8194,10 @@ void si_CPUID(u32 ID, u32 registers[4]) {
)
SI_ASM_INPUT("a"(ID), "c"(0))
);
#else
SI_UNUSED(ID); SI_UNUSED(registers);

#endif /* !SI_NO_INLINE_ASM && SI_ARCH_IS_X86 */
#endif
}


Expand Down Expand Up @@ -8222,6 +8242,20 @@ u32 si_cpuProcessorCount(void) {
}

si_mfree(processors);

#elif SI_SYSTEM_IS_APPLE
i64 count;
usize size = sizeof(i64);
if (sysctlbyname("hw.logicalcpu", &count, &size, nil, 0) == 0 && count > 0) {
procCount = (u32)count;
}
else {
procCount = 0;
}

#else
procCount = 0;

#endif

return procCount;
Expand Down
7 changes: 4 additions & 3 deletions tests/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ int main(void) {
res = si_unixIsX11();
SI_ASSERT(res == true);

si_printf("%i\n", si_unixGetDE());
siUnixDE de = si_unixGetDE();
SI_ASSERT(de == siUnixDE_KDE);

#endif

si_printf("%i\n", si_cpuProcessorCount());
u32 count = si_cpuProcessorCount();
SI_ASSERT(count == 4);

si_printf("%CTest '" __FILE__ "' has been completed!%C\n", si_printColor3bitEx(siPrintColorAnsi_Yellow, true, false));
}
Expand Down

0 comments on commit 7437d4b

Please sign in to comment.