Cleanup of the byteorder patch
[umurmur.git] / src / byteorder.h
1 /* Copyright (C) 2014, Felix Morgner <felix.morgner@gmail.com>
2
3    All rights reserved.
4
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8
9    - Redistributions of source code must retain the above copyright notice,
10      this list of conditions and the following disclaimer.
11    - Redistributions in binary form must reproduce the above copyright notice,
12      this list of conditions and the following disclaimer in the documentation
13      and/or other materials provided with the distribution.
14    - Neither the name of the Developers nor the names of its contributors may
15      be used to endorse or promote products derived from this software without
16      specific prior written permission.
17
18    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
22    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef BYTEORDER_H_
32 #define BYTEORDER_H_
33
34 #include <stdint.h>
35
36 #if defined(NETBSD) || defined(FREEBSD) || defined(OPENBSD) || defined(MACOSX)
37 #include <machine/endian.h>
38 #if BYTE_ORDER == BIG_ENDIAN
39 #define BYTE_ORDER_BIG_ENDIAN
40 #endif // BYTE_ORDER == BIG_ENDIAN
41 #elif defined(LINUX)
42 #include <endian.h>
43 #if __BYTE_ORDER == __BIG_ENDIAN
44 #define BYTE_ORDER_BIG_ENDIAN
45 #endif // __BYTE_ORDER == __BIG_ENDIAN
46 #endif // defined(NETBSD) || defined(FREEBSD) || defined(OPENBSD)
47
48 #if defined(__LP64__)
49 #define BLOCKSIZE 2
50 #define SHIFTBITS 63
51 typedef uint64_t subblock;
52 #if defined(BYTE_ORDER_BIG_ENDIAN)
53 #define SWAPPED(x) (x)
54 #elif defined( __x86_64__)
55 #define SWAPPED(x) ({register uint64_t __out, __in = (x); __asm__("bswap %q0" : "=r"(__out) : "0"(__in)); __out;})
56 #else
57 #include <byteswap.h>
58 #define SWAPPED(x) bswap_64(x)
59 #endif // defined(BYTE_ORDER_BIG_ENDIAN)
60 #else
61 #define BLOCKSIZE 4
62 #define SHIFTBITS 31
63 typedef uint32_t subblock;
64 #define SWAPPED(x) htonl(x)
65 #endif // defined(__LP64__)
66
67 #define HIGHBIT (1<<SHIFTBITS);
68
69 #endif // BYTEORDER_H_