-
Notifications
You must be signed in to change notification settings - Fork 0
/
twtw-byteorder.h
142 lines (113 loc) · 3.23 KB
/
twtw-byteorder.h
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
/*
* twtw-byteorder.h
* TwentyTwenty
*
* Created by Pauli Ojala on 4.12.2008.
* Copyright 2008 Pauli Olavi Ojala. All rights reserved.
*
*/
/*
This file is part of TwentyTwenty.
TwentyTwenty is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TwentyTwenty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TwentyTwenty. If not, see <http://www.gnu.org/licenses/>.
*/
// basic byte order flippers à la Ogg nomenclature.
// these are static functions -> no multiple inclusion of this header
#ifndef _TWTW_BYTEORDER_H_
#define _TWTW_BYTEORDER_H_
#include <stdint.h>
#ifndef FUNCATTR_PURE
// these are GCC-specific
#define FUNCATTR_PURE __attribute__ ((pure))
#define FUNCATTR_ALWAYS_INLINE __attribute__ ((always_inline))
#endif
#if defined(__BIG_ENDIAN__) && !defined(WORDS_BIGENDIAN)
#define WORDS_BIGENDIAN
#endif
static inline uint16_t _le_16 (uint16_t s) FUNCATTR_PURE FUNCATTR_ALWAYS_INLINE;
static inline uint16_t _le_16 (uint16_t s)
{
uint16_t ret=s;
#ifdef WORDS_BIGENDIAN
ret = (s>>8) & 0x00ffU;
ret += (s<<8) & 0xff00U;
#endif
return ret;
}
static inline int16_t _le_16_s (int16_t s) FUNCATTR_PURE FUNCATTR_ALWAYS_INLINE;
static inline int16_t _le_16_s (int16_t s)
{
int16_t ret=s;
#ifdef WORDS_BIGENDIAN
uint16_t u = _le_16( *((uint16_t *)&s) );
ret = *((int16_t *)&u);
#endif
return ret;
}
static inline uint16_t _be_16 (uint16_t s) FUNCATTR_PURE FUNCATTR_ALWAYS_INLINE;
static inline uint16_t _be_16 (uint16_t s)
{
uint16_t ret=s;
#ifndef WORDS_BIGENDIAN
ret = (s>>8) & 0x00ffU;
ret += (s<<8) & 0xff00U;
#endif
return ret;
}
#define _from_le_16 _le_16
#define _from_be_16 _be_16
static inline uint32_t _le_32 (uint32_t i) FUNCATTR_PURE FUNCATTR_ALWAYS_INLINE;
static inline uint32_t _le_32 (uint32_t i)
{
uint32_t ret=i;
#ifdef WORDS_BIGENDIAN
ret = (i>>24);
ret += (i>>8) & 0x0000ff00;
ret += (i<<8) & 0x00ff0000;
ret += (i<<24);
#endif
return ret;
}
static inline int32_t _le_32_s (int32_t s) FUNCATTR_PURE FUNCATTR_ALWAYS_INLINE;
static inline int32_t _le_32_s (int32_t s)
{
int32_t ret=s;
#ifdef WORDS_BIGENDIAN
uint32_t u = _le_32( *((uint32_t *)&s) );
ret = *((int32_t *)&u);
#endif
return ret;
}
static inline int64_t _le_64 (int64_t l) FUNCATTR_PURE FUNCATTR_ALWAYS_INLINE;
static inline int64_t _le_64 (int64_t l)
{
int64_t ret=l;
unsigned char *ucptr = (unsigned char *)&ret;
#ifdef WORDS_BIGENDIAN
unsigned char temp;
temp = ucptr [0] ;
ucptr [0] = ucptr [7] ;
ucptr [7] = temp ;
temp = ucptr [1] ;
ucptr [1] = ucptr [6] ;
ucptr [6] = temp ;
temp = ucptr [2] ;
ucptr [2] = ucptr [5] ;
ucptr [5] = temp ;
temp = ucptr [3] ;
ucptr [3] = ucptr [4] ;
ucptr [4] = temp ;
#endif
return (*(int64_t *)ucptr);
}
#define _from_le_32 _le_32
#define _from_le_64 _le_64
#endif