| 1 |
/** |
| 2 |
** @file mruby/endian.h - detect endian-ness |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef MRUBY_ENDIAN_H |
| 8 |
#define MRUBY_ENDIAN_H |
| 9 |
|
| 10 |
#include <limits.h> |
| 11 |
|
| 12 |
MRB_BEGIN_DECL |
| 13 |
|
| 14 |
#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER__) |
| 15 |
# define BYTE_ORDER __BYTE_ORDER__ |
| 16 |
#endif |
| 17 |
#if !defined(BIG_ENDIAN) && defined(__ORDER_BIG_ENDIAN__) |
| 18 |
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__ |
| 19 |
#endif |
| 20 |
#if !defined(LITTLE_ENDIAN) && defined(__ORDER_LITTLE_ENDIAN__) |
| 21 |
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifdef BYTE_ORDER |
| 25 |
# if BYTE_ORDER == BIG_ENDIAN |
| 26 |
# define littleendian 0 |
| 27 |
# elif BYTE_ORDER == LITTLE_ENDIAN |
| 28 |
# define littleendian 1 |
| 29 |
# endif |
| 30 |
#endif |
| 31 |
#ifndef littleendian |
| 32 |
/* can't distinguish endian in compile time */ |
| 33 |
static inline int |
| 34 |
check_little_endian(void) |
| 35 |
{ |
| 36 |
unsigned int n = 1; |
| 37 |
return (*(unsigned char*)&n == 1); |
| 38 |
} |
| 39 |
# define littleendian check_little_endian() |
| 40 |
#endif |
| 41 |
|
| 42 |
MRB_END_DECL |
| 43 |
|
| 44 |
#endif /* MRUBY_ENDIAN_H */ |