Simplify endian (byte order) detection

The following:

    #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
    #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

are apparently semi-standard nowadays.
This commit is contained in:
Lassi Kortela 2019-08-09 21:30:30 +03:00
parent 5817003816
commit 12fb30462b
5 changed files with 17 additions and 63 deletions

View File

@ -60,33 +60,6 @@
#define DLLEXPORT __attribute__((visibility("default"))) #define DLLEXPORT __attribute__((visibility("default")))
#endif #endif
#if defined(LINUX)
#include <features.h>
#include <endian.h>
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#define PDP_ENDIAN __PDP_ENDIAN
#define BYTE_ORDER __BYTE_ORDER
#elif defined(MACOSX) || defined(OPENBSD) || defined(FREEBSD)
#include <machine/endian.h>
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __BIG_ENDIAN BIG_ENDIAN
#define __PDP_ENDIAN PDP_ENDIAN
#define __BYTE_ORDER BYTE_ORDER
#elif defined(WIN32)
#define __LITTLE_ENDIAN 1234
#define __BIG_ENDIAN 4321
#define __PDP_ENDIAN 3412
#define __BYTE_ORDER __LITTLE_ENDIAN
#define __FLOAT_WORD_ORDER __LITTLE_ENDIAN
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#define PDP_ENDIAN __PDP_ENDIAN
#define BYTE_ORDER __BYTE_ORDER
#else
#error "unknown platform"
#endif
#define LLT_ALLOC(n) malloc(n) #define LLT_ALLOC(n) malloc(n)
#define LLT_REALLOC(p, n) realloc((p), (n)) #define LLT_REALLOC(p, n) realloc((p), (n))
#define LLT_FREE(x) free(x) #define LLT_FREE(x) free(x)

View File

@ -924,18 +924,21 @@ no_kw:
return nargs; return nargs;
} }
#if BYTE_ORDER == BIG_ENDIAN #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define GET_INT32(a) \ #define GET_INT32(a) \
((int32_t)((((int32_t)a[0]) << 0) | (((int32_t)a[1]) << 8) | \ ((int32_t)((((int32_t)a[0]) << 0) | (((int32_t)a[1]) << 8) | \
(((int32_t)a[2]) << 16) | (((int32_t)a[3]) << 24))) (((int32_t)a[2]) << 16) | (((int32_t)a[3]) << 24)))
#define GET_INT16(a) \ #define GET_INT16(a) \
((int16_t)((((int16_t)a[0]) << 0) | (((int16_t)a[1]) << 8))) ((int16_t)((((int16_t)a[0]) << 0) | (((int16_t)a[1]) << 8)))
#define PUT_INT32(a, i) (*(int32_t *)(a) = bswap_32((int32_t)(i))) #define PUT_INT32(a, i) (*(int32_t *)(a) = bswap_32((int32_t)(i)))
#else #endif
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define GET_INT32(a) (*(int32_t *)a) #define GET_INT32(a) (*(int32_t *)a)
#define GET_INT16(a) (*(int16_t *)a) #define GET_INT16(a) (*(int16_t *)a)
#define PUT_INT32(a, i) (*(int32_t *)(a) = (int32_t)(i)) #define PUT_INT32(a, i) (*(int32_t *)(a) = (int32_t)(i))
#endif #endif
#define SWAP_INT32(a) (*(int32_t *)(a) = bswap_32(*(int32_t *)(a))) #define SWAP_INT32(a) (*(int32_t *)(a) = bswap_32(*(int32_t *)(a)))
#define SWAP_INT16(a) (*(int16_t *)(a) = bswap_16(*(int16_t *)(a))) #define SWAP_INT16(a) (*(int16_t *)(a) = bswap_16(*(int16_t *)(a)))
@ -2243,7 +2246,7 @@ static value_t fl_function(value_t *args, uint32_t nargs)
for (i = 0; i < sz; i++) for (i = 0; i < sz; i++)
data[i] -= 48; data[i] -= 48;
} else { } else {
#if BYTE_ORDER == BIG_ENDIAN #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
swap = 1; swap = 1;
#endif #endif
} }

View File

@ -2,18 +2,6 @@
Hashing Hashing
*/ */
#ifndef WIN32
#include <sys/param.h> /* attempt to define endianness */
#else
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#endif
#ifdef LINUX
#include <endian.h> /* attempt to define endianness */
#endif
#include <math.h> #include <math.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>

View File

@ -2,12 +2,12 @@ union ieee754_float {
float f; float f;
struct { struct {
#if BYTE_ORDER == BIG_ENDIAN #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
unsigned int negative : 1; unsigned int negative : 1;
unsigned int exponent : 8; unsigned int exponent : 8;
unsigned int mantissa : 23; unsigned int mantissa : 23;
#endif #endif
#if BYTE_ORDER == LITTLE_ENDIAN #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
unsigned int mantissa : 23; unsigned int mantissa : 23;
unsigned int exponent : 8; unsigned int exponent : 8;
unsigned int negative : 1; unsigned int negative : 1;
@ -21,13 +21,13 @@ union ieee754_double {
double d; double d;
struct { struct {
#if BYTE_ORDER == BIG_ENDIAN #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
unsigned int negative : 1; unsigned int negative : 1;
unsigned int exponent : 11; unsigned int exponent : 11;
unsigned int mantissa0 : 20; unsigned int mantissa0 : 20;
unsigned int mantissa1 : 32; unsigned int mantissa1 : 32;
#endif #endif
#if BYTE_ORDER == LITTLE_ENDIAN #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
unsigned int mantissa1 : 32; unsigned int mantissa1 : 32;
unsigned int mantissa0 : 20; unsigned int mantissa0 : 20;
unsigned int exponent : 11; unsigned int exponent : 11;
@ -42,14 +42,14 @@ union ieee854_long_double {
long double d; long double d;
struct { struct {
#if BYTE_ORDER == BIG_ENDIAN #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
unsigned int negative : 1; unsigned int negative : 1;
unsigned int exponent : 15; unsigned int exponent : 15;
unsigned int empty : 16; unsigned int empty : 16;
unsigned int mantissa0 : 32; unsigned int mantissa0 : 32;
unsigned int mantissa1 : 32; unsigned int mantissa1 : 32;
#endif #endif
#if BYTE_ORDER == LITTLE_ENDIAN #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
unsigned int mantissa1 : 32; unsigned int mantissa1 : 32;
unsigned int mantissa0 : 32; unsigned int mantissa0 : 32;
unsigned int exponent : 15; unsigned int exponent : 15;

View File

@ -35,24 +35,14 @@ on 1 byte), but shoehorning those bytes into integers efficiently is messy.
*/ */
//#define SELF_TEST 1 //#define SELF_TEST 1
/* #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
* My best guess at if you are big-endian or little-endian. This may
* need adjustment.
*/
#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
__BYTE_ORDER == __LITTLE_ENDIAN) || \
(defined(i386) || defined(__i386__) || defined(__i486__) || \
defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
#define HASH_LITTLE_ENDIAN 1
#define HASH_BIG_ENDIAN 0
#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
__BYTE_ORDER == __BIG_ENDIAN) || \
(defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
#define HASH_LITTLE_ENDIAN 0
#define HASH_BIG_ENDIAN 1 #define HASH_BIG_ENDIAN 1
#else
#define HASH_LITTLE_ENDIAN 0 #define HASH_LITTLE_ENDIAN 0
#endif
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define HASH_BIG_ENDIAN 0 #define HASH_BIG_ENDIAN 0
#define HASH_LITTLE_ENDIAN 1
#endif #endif
#define hashsize(n) ((uint32_t)1 << (n)) #define hashsize(n) ((uint32_t)1 << (n))