webmcp

changeset 501:01f53490f262

moonhash library: determine endianess on FreeBSD, OpenBSD, NetBSD, and Linux
author jbe
date Sun Aug 13 14:55:50 2017 +0200 (2017-08-13)
parents 5e6dbaa3e219
children fa902b26589f
files libraries/moonhash/moonhash_sha3.c
line diff
     1.1 --- a/libraries/moonhash/moonhash_sha3.c	Sun Aug 13 14:31:21 2017 +0200
     1.2 +++ b/libraries/moonhash/moonhash_sha3.c	Sun Aug 13 14:55:50 2017 +0200
     1.3 @@ -2,6 +2,20 @@
     1.4  
     1.5  #include <stdint.h>
     1.6  
     1.7 +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
     1.8 +#include <sys/endian.h>
     1.9 +#if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN
    1.10 +#define MOONHASH_LITTLE_ENDIAN
    1.11 +#endif
    1.12 +#elif defined(__linux__)
    1.13 +#include <endian.h>
    1.14 +#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
    1.15 +#define MOONHASH_LITTLE_ENDIAN
    1.16 +#endif
    1.17 +#else
    1.18 +#warning Could not determine endianess, revert to fail safe.
    1.19 +#endif
    1.20 +
    1.21  #define FOR(i,n) for(i=0; i<n; ++i)
    1.22  typedef uint8_t u8;
    1.23  typedef uint64_t u64;
    1.24 @@ -17,9 +31,15 @@
    1.25  
    1.26  static int LFSR86540(u8 *R) { (*R)=((*R)<<1)^(((*R)&0x80)?0x71:0); return ((*R)&2)>>1; }
    1.27  #define ROL(a,o) ((((u64)a)<<o)^(((u64)a)>>(64-o)))
    1.28 +#ifdef MOONHASH_LITTLE_ENDIAN
    1.29 +#define load64(src) (*(uint64_t *)(src))
    1.30 +#define store64(dst, src) do { *(uint64_t *)(dst) = src; } while (0)
    1.31 +#define xor64(dst, src) do { *(uint64_t *)(dst) ^= src; } while (0)
    1.32 +#else
    1.33  static u64 load64(const u8 *x) { ui i; u64 u=0; FOR(i,8) { u<<=8; u|=x[7-i]; } return u; }
    1.34  static void store64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]=u; u>>=8; } }
    1.35  static void xor64(u8 *x, u64 u) { ui i; FOR(i,8) { x[i]^=u; u>>=8; } }
    1.36 +#endif
    1.37  #define rL(x,y) load64((u8*)s+8*(x+5*y))
    1.38  #define wL(x,y,l) store64((u8*)s+8*(x+5*y),l)
    1.39  #define XL(x,y,l) xor64((u8*)s+8*(x+5*y),l)

Impressum / About Us