Skip to content

Commit

Permalink
#125: fix: Detect platform word size based on SIZE_MAX
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BurningEnlightenment authored and aklomp committed Jan 8, 2024
1 parent 3a5add8 commit 88607fe
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/env.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef BASE64_ENV_H
#define BASE64_ENV_H

#include <stdint.h>

// This header file contains macro definitions that describe certain aspects of
// the compile-time environment. Compatibility and portability macros go here.

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 88607fe

Please sign in to comment.