From 88607feedaa57abd382d6ceb4b296629c6626f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20S=2E=20Ga=C3=9Fmann?= Date: Sun, 19 Nov 2023 21:15:49 +0100 Subject: [PATCH] #125: fix: Detect platform word size based on `SIZE_MAX` The native word size on a platform matches the size of `size_t` with a few rare exceptions like the x32 ABI on linux. Therefore we can derive a general cross platform value for `BASE64_WORDSIZE` based on `SIZE_MAX` which the C99 standard guarantees to be available and well defined. This fixes the misdection of x86/arm32 win32 as a 64bit platform. See also https://en.cppreference.com/w/c/types/limits Resolves #123. Resolves #125. --- lib/env.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/env.h b/lib/env.h index d5c2fdb7..d68d488c 100644 --- a/lib/env.h +++ b/lib/env.h @@ -1,6 +1,8 @@ #ifndef BASE64_ENV_H #define BASE64_ENV_H +#include + // This header file contains macro definitions that describe certain aspects of // the compile-time environment. Compatibility and portability macros go here. @@ -46,12 +48,14 @@ #if defined (__x86_64__) // This also works for the x32 ABI, which has a 64-bit word size. # define BASE64_WORDSIZE 64 -#elif defined (_INTEGRAL_MAX_BITS) -# define BASE64_WORDSIZE _INTEGRAL_MAX_BITS #elif defined (__WORDSIZE) # define BASE64_WORDSIZE __WORDSIZE #elif defined (__SIZE_WIDTH__) # define BASE64_WORDSIZE __SIZE_WIDTH__ +#elif SIZE_MAX == UINT32_MAX +# define BASE64_WORDSIZE 32 +#elif SIZE_MAX == UINT64_MAX +# define BASE64_WORDSIZE 64 #else # error BASE64_WORDSIZE_NOT_DEFINED #endif