blob: 9d0327c29c860ef9bf6a30bcc56a0d806c2373c3 [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: csilvers@google.com (Craig Silverstein)
3//
4// Provides macros and typedefs based on config.h settings.
5// Provides the following macros:
6// UNALIGNED_LOAD32 (may be an inline function on some architectures)
7// and the following typedefs:
8// uint32
9// uint64
10
11#ifndef CTEMPLATE_MACROS_H_
12#define CTEMPLATE_MACROS_H_
13
14#include <config.h>
15#ifdef HAVE_STDINT_H
16#include <stdint.h> // the normal place uint32_t is defined
17#endif
18#ifdef HAVE_SYS_TYPES_H
19#include <sys/types.h> // the normal place u_int32_t is defined
20#endif
21#ifdef HAVE_INTTYPES_H
22#ifdef HAVE_INTTYPES_H
23# include <inttypes.h>
24#endif // a third place for uint32_t or u_int32_t
25#endif
26
27#if defined(HAVE_U_INT32_T)
28typedef u_int32_t uint32;
29#elif defined(HAVE_UINT32_T)
30typedef uint32_t uint32;
31#elif defined(HAVE___INT32)
32typedef unsigned __int32 uint32;
33#endif
34
35#if defined(HAVE_U_INT64_T)
36typedef u_int64_t uint64;
37#elif defined(HAVE_UINT64_T)
38typedef uint64_t uint64;
39#elif defined(HAVE___INT64)
40typedef unsigned __int64 uint64;
41#endif
42
43
44// This is all to figure out endian-ness and byte-swapping on various systems
45#if defined(HAVE_ENDIAN_H)
46#include <endian.h> // for the __BYTE_ORDER use below
47#elif defined(HAVE_SYS_ENDIAN_H)
48#include <sys/endian.h> // location on FreeBSD
49#elif defined(HAVE_MACHINE_ENDIAN_H)
50#include <machine/endian.h> // location on OS X
51#endif
52#if defined(HAVE_SYS_BYTEORDER_H)
53#include <sys/byteorder.h> // BSWAP_32 on Solaris 10
54#endif
55#ifdef HAVE_SYS_ISA_DEFS_H
56#include <sys/isa_defs.h> // _BIG_ENDIAN/_LITTLE_ENDIAN on Solaris 10
57#endif
58
59// MurmurHash does a lot of 4-byte unaligned integer access. It
60// interprets these integers in little-endian order. This is perfect
61// on x86, for which this is a natural memory access; for other systems
62// we do what we can to make this as efficient as possible.
63#if defined(HAVE_BYTESWAP_H)
64# include <byteswap.h> // GNU (especially linux)
65# define BSWAP32(x) bswap_32(x)
66#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
67# include <libkern/OSByteOrder.h> // OS X
68# define BSWAP32(x) OSSwapInt32(x)
69#elif defined(bswap32) // FreeBSD
70 // FreeBSD defines bswap32 as a macro in sys/endian.h (already #included)
71# define BSWAP32(x) bswap32(x)
72#elif defined(BSWAP_32) // Solaris 10
73 // Solaris defines BSWSAP_32 as a macro in sys/byteorder.h (already #included)
74# define BSWAP32(x) BSWAP_32(x)
75#elif !defined(BSWAP32)
76# define BSWAP32(x) ((((x) & 0x000000ff) << 24) | \
77 (((x) & 0x0000ff00) << 8) | \
78 (((x) & 0x00ff0000) >> 8) | \
79 (((x) & 0xff000000) >> 24));
80#else
81# define CTEMPLATE_BSWAP32_ALREADY_DEFINED
82#endif
83
84#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
85 // We know they allow unaligned memory access and are little-endian
86# define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
87#elif defined(__ppc__) || defined(__ppc64__)
88 // We know they allow unaligned memory access and are big-endian
89# define UNALIGNED_LOAD32(_p) BSWAP32(*reinterpret_cast<const uint32 *>(_p))
90#elif (BYTE_ORDER == 1234) || (_BYTE_ORDER == 1234) || defined(_LITTLE_ENDIAN)
91 // Use memcpy to align the memory properly
92 inline uint32 UNALIGNED_LOAD32(const void *p) {
93 uint32 t;
94 memcpy(&t, p, sizeof(t));
95 return t;
96 }
97#elif (BYTE_ORDER == 4321) || (_BYTE_ORDER == 4321) || defined(_BIG_ENDIAN)
98 inline uint32 UNALIGNED_LOAD32(const void *p) {
99 uint32 t;
100 memcpy(&t, p, sizeof(t));
101 return BSWAP32(t);
102 }
103#else
104 // Means we can't find find endian.h on this machine:
105# error Need to define UNALIGNED_LOAD32 for this architecture
106#endif
107
108#ifndef CTEMPLATE_BSWAP32_ALREADY_DEFINED
109# undef BSWAP32 // don't leak outside this file
110#else
111# undef CTEMPLATE_BSWAP32_ALREADY_DEFINED // just cleaning up
112#endif
113
114#endif // CTEMPLATE_MACROS_H_