Skip to content

Commit

Permalink
Add simple little-endian checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
tempname11 committed Sep 21, 2023
1 parent ffb341c commit 524e122
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/src/platform/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static_assert(sizeof(size_t) == 8);
static_assert((size_t) NULL == (size_t) 0);
static_assert(CHAR_BIT == 8);

// Additional assumption: little endian (no simple way to check it?).
static_assert(__LITTLE_ENDIAN__); // Clang-specific.

// Aliases for types we will use a lot, short and consistent.
// Like other types, these have uppercase first letter naming.
Expand Down
23 changes: 23 additions & 0 deletions svf_runtime/src/svf_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define SVF_RUNTIME_H

#ifdef __cplusplus
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdbool>
#else
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
Expand All @@ -15,6 +17,27 @@
extern "C" {
#endif

#if defined(__clang__) && defined(__LITTLE_ENDIAN__)
#define SVF_PLATFORM_LITTLE_ENDIAN 1
#endif

#ifndef SVF_PLATFORM_LITTLE_ENDIAN
#error "This library only supports little-endian platforms. Auto-detection failed, so if your platform is indeed little-endian, please #define SVF_PLATFORM_LITTLE_ENDIAN manually."
#endif

#if CHAR_BIT != 8
#error "This library only supports 8-bit bytes. What on earth are you compiling for?!"
#endif

// Use the "negative array size" trick to emulate `static_assert`. The function
// itself is not needed, it can be discarded as long as it compiles.
#define X(expr) (void) sizeof(int[(expr) ? 1 : -1]);
static inline
void SVFRT_compile_time_checks(void) {
X(sizeof(char) == 1);
}
#undef X

#ifndef SVF_COMMON_C_TYPES_INCLUDED
#define SVF_COMMON_C_TYPES_INCLUDED
#pragma pack(push, 1)
Expand Down

0 comments on commit 524e122

Please sign in to comment.