blob: 1ac5c58d327f3e9219444684426ad430871977cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* xrick/system/basic_funcs.h
*
* Copyright (C) 2008-2014 Pierluigi Vicinanza. All rights reserved.
*
* The use and distribution terms for this software are contained in the file
* named README, which can be found in the root of this distribution. By
* using this software in any fashion, you are agreeing to be bound by the
* terms of this license.
*
* You must not remove this notice, or any other, from this software.
*/
#ifndef _BASIC_FUNCS_H
#define _BASIC_FUNCS_H
#include "xrick/system/basic_types.h"
#include "xrick/system/system.h"
#ifdef __WIN32__
/* Windows is little endian only */
# define __ORDER_LITTLE_ENDIAN__ 1234
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# define USE_DEFAULT_ENDIANNESS_FUNCTIONS
# include <stdlib.h> /* _byteswap_XXX */
#elif defined(ROCKBOX)
/* Rockbox*/
# include "plugin.h"
# define __ORDER_LITTLE_ENDIAN__ 1234
# define __ORDER_BIG_ENDIAN__ 4321
# ifdef ROCKBOX_BIG_ENDIAN
# define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
# else
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# endif
#elif (defined(__FreeBSD__) && __FreeBSD_version >= 470000) || defined(__OpenBSD__) || defined(__NetBSD__)
/* *BSD */
# include <sys/endian.h>
# define __ORDER_BIG_ENDIAN__ BIG_ENDIAN
# define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN
# define __BYTE_ORDER__ BYTE_ORDER
#elif (defined(BSD) && (BSD >= 199103)) || defined(__MacOSX__)
/* more BSD */
# include <machine/endian.h>
# define __ORDER_BIG_ENDIAN__ BIG_ENDIAN
# define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN
# define __BYTE_ORDER__ BYTE_ORDER
#elif defined(__linux__) /*|| defined (__BEOS__)*/
/* Linux, BeOS */
# include <endian.h>
# define betoh16(x) be16toh(x)
# define letoh16(x) le16toh(x)
# define betoh32(x) be32toh(x)
# define letoh32(x) le32toh(x)
#else
/* shall we just '#include <endian.h>'? */
# define USE_DEFAULT_ENDIANNESS_FUNCTIONS
#endif /* __WIN32__ */
/* define default endianness */
#ifndef __ORDER_LITTLE_ENDIAN__
# define __ORDER_LITTLE_ENDIAN__ 1234
#endif
#ifndef __ORDER_BIG_ENDIAN__
# define __ORDER_BIG_ENDIAN__ 4321
#endif
#ifndef __BYTE_ORDER__
# warning "Byte order not defined on your system, assuming little endian!"
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#endif
/* provide default endianness functions */
#ifdef USE_DEFAULT_ENDIANNESS_FUNCTIONS
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
inline uint32_t swap32(uint32_t x)
{
# ifdef _MSC_VER
return _byteswap_ulong(x);
# elif (GCC_VERSION > 40300) || defined(__clang__)
return __builtin_bswap32(x);
# else
return (x >> 24) |
((x >> 8) & 0x0000FF00) |
((x << 8) & 0x00FF0000) |
(x << 24);
# endif /* _MSC_VER */
}
inline uint16_t swap16(uint16_t x)
{
# ifdef _MSC_VER
return _byteswap_ushort(x);
# elif (GCC_VERSION > 40800) || defined(__clang__)
return __builtin_bswap16(x);
# else
return (x << 8)|(x >> 8);
# endif /* _MSC_VER */
}
# if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__)
inline uint16_t htobe16(uint16_t host) { return swap16(host); }
inline uint16_t htole16(uint16_t host) { return host; }
inline uint16_t betoh16(uint16_t big_endian) { return swap16(big_endian); }
inline uint16_t letoh16(uint16_t little_endian) { return little_endian; }
inline uint32_t htobe32(uint32_t host) { return swap32(host); }
inline uint32_t htole32(uint32_t host) { return host; }
inline uint32_t betoh32(uint32_t big_endian) { return swap32(big_endian); }
inline uint32_t letoh32(uint32_t little_endian) { return little_endian; }
# elif (__BYTE_ORDER__==__ORDER_BIG_ENDIAN__)
inline uint16_t htobe16(uint16_t host) { return host; }
inline uint16_t htole16(uint16_t host) { return swap16(host); }
inline uint16_t betoh16(uint16_t big_endian) { return big_endian; }
inline uint16_t letoh16(uint16_t little_endian) { return swap16(little_endian); }
inline uint32_t htobe32(uint32_t host) { return host; }
inline uint32_t htole32(uint32_t host) { return swap32(host); }
inline uint32_t betoh32(uint32_t big_endian) { return big_endian; }
inline uint32_t letoh32(uint32_t little_endian) { return swap32(little_endian); }
# else
# error "Unknown/unsupported byte order!"
# endif
#endif /* USE_DEFAULT_ENDIANNESS_FUNCTIONS */
#endif /* ndef _BASIC_FUNCS_H */
/* eof */
|