tweak C1-era CARR
[openc2e.git] / endianlove.h
blobee3853faa8182d9f5b51b2f97021fc789893b0c3
1 /*
2 * endianlove.h
3 * openc2e
5 * Created by Alyssa Milburn on Sat May 22 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #ifndef _ENDIANLOVE_H
21 #define _ENDIANLOVE_H
23 #include <stdlib.h> // load the standard libraries for these defines
25 #if HAVE_STDINT_H
27 #include <stdint.h>
29 typedef uint8_t uint8;
30 typedef uint16_t uint16;
31 typedef uint32_t uint32;
33 #else
35 typedef unsigned char uint8;
36 typedef unsigned short uint16;
37 typedef unsigned int uint32;
39 #endif
41 #ifndef OC2E_BIG_ENDIAN
42 # ifdef __GNU__
43 # include <endian.h>
44 # if __BYTE_ORDER == __LITTLE_ENDIAN
45 # define OC2E_BIG_ENDIAN 0
46 # else
47 # define OC2E_BIG_ENDIAN 1
48 # endif
49 # else
50 # if defined(_MSC_VER) || defined(__i386__)
51 # define OC2E_BIG_ENDIAN 0
52 # else
53 # define OC2E_BIG_ENDIAN 1
54 # endif
55 # endif
56 #endif
58 #if OC2E_BIG_ENDIAN
60 # if HAVE_BYTESWAP_H
62 #include <byteswap.h>
63 static inline uint16 swapEndianShort(uint16 a) {
64 return bswap_16(a);
67 static inline uint32 swapEndianLong(uint32 a) {
68 return bswap_32(a);
71 # else // HAVE_BYTESWAP_H
73 static inline uint16 swapEndianShort(uint16 a) {
74 return ((((uint16)(a) & 0xff00) >> 8) |
75 (((uint16)(a) & 0x00ff) << 8));
78 static inline uint32 swapEndianLong(uint32 a) {
79 return ((((uint32)(a) & 0xff000000) >> 24) |
80 (((uint32)(a) & 0x00ff0000) >> 8) |
81 (((uint32)(a) & 0x0000ff00) << 8) |
82 (((uint32)(a) & 0x000000ff) << 24));
85 # endif
87 #else // OC2E_BIG_ENDIAN
89 static inline uint16 swapEndianShort(uint16 a) {
90 return a;
93 static inline uint32 swapEndianLong(uint32 a) {
94 return a;
97 #endif
99 #endif // _ENDIANLOVE_H
101 /* vim: set noet: */