Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
byte_order.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * byte order (endianness) support routines.
25  */
26 
27 #include "precompiled.h"
28 #include "lib/byte_order.h"
29 
30 #include "lib/bits.h"
31 
32 #ifndef swap16
33 u16 swap16(const u16 x)
34 {
35  return (u16)(((x & 0xff) << 8) | (x >> 8));
36 }
37 #endif
38 
39 #ifndef swap32
40 u32 swap32(const u32 x)
41 {
42  return (x << 24) |
43  (x >> 24) |
44  ((x << 8) & 0x00ff0000) |
45  ((x >> 8) & 0x0000ff00);
46 }
47 #endif
48 
49 #ifndef swap64
50 u64 swap64(const u64 x)
51 {
52  const u32 lo = (u32)(x & 0xFFFFFFFF);
53  const u32 hi = (u32)(x >> 32);
54  u64 ret = swap32(lo);
55  ret <<= 32;
56  // careful: must shift var of type u64, not u32
57  ret |= swap32(hi);
58  return ret;
59 }
60 #endif
61 
62 
63 //-----------------------------------------------------------------------------
64 
65 
66 u16 read_le16(const void* p)
67 {
68  u16 n;
69  memcpy(&n, p, sizeof(n));
70  return to_le16(n);
71 }
72 
73 u32 read_le32(const void* p)
74 {
75  u32 n;
76  memcpy(&n, p, sizeof(n));
77  return to_le32(n);
78 }
79 
80 u64 read_le64(const void* p)
81 {
82  u64 n;
83  memcpy(&n, p, sizeof(n));
84  return to_le64(n);
85 }
86 
87 
88 u16 read_be16(const void* p)
89 {
90  u16 n;
91  memcpy(&n, p, sizeof(n));
92  return to_be16(n);
93 }
94 
95 u32 read_be32(const void* p)
96 {
97  u32 n;
98  memcpy(&n, p, sizeof(n));
99  return to_be32(n);
100 }
101 
102 u64 read_be64(const void* p)
103 {
104  u64 n;
105  memcpy(&n, p, sizeof(n));
106  return to_be64(n);
107 }
108 
109 
110 void write_le16(void* p, u16 x)
111 {
112  u16 n = to_le16(x);
113  memcpy(p, &n, sizeof(n));
114 }
115 
116 void write_le32(void* p, u32 x)
117 {
118  u32 n = to_le32(x);
119  memcpy(p, &n, sizeof(n));
120 }
121 
122 void write_le64(void* p, u64 x)
123 {
124  u64 n = to_le64(x);
125  memcpy(p, &n, sizeof(n));
126 }
127 
128 
129 void write_be16(void* p, u16 x)
130 {
131  u16 n = to_be16(x);
132  memcpy(p, &n, sizeof(n));
133 }
134 
135 void write_be32(void* p, u32 x)
136 {
137  u32 n = to_be32(x);
138  memcpy(p, &n, sizeof(n));
139 }
140 
141 void write_be64(void* p, u64 x)
142 {
143  u64 n = to_be64(x);
144  memcpy(p, &n, sizeof(n));
145 }
146 
147 
148 u64 movzx_le64(const u8* p, size_t size_bytes)
149 {
150  u64 number = 0;
151  for(size_t i = 0; i < std::min(size_bytes, (size_t)8u); i++)
152  number |= ((u64)p[i]) << (i*8);
153 
154  return number;
155 }
156 
157 u64 movzx_be64(const u8* p, size_t size_bytes)
158 {
159  u64 number = 0;
160  for(size_t i = 0; i < std::min(size_bytes, (size_t)8u); i++)
161  {
162  number <<= 8;
163  number |= p[i];
164  }
165 
166  return number;
167 }
168 
169 
170 static inline i64 SignExtend(u64 bits, size_t size_bytes)
171 {
172  // no point in sign-extending if >= 8 bytes were requested
173  if(size_bytes < 8)
174  {
175  const u64 sign_bit = Bit<u64>((size_bytes*8)-1);
176 
177  // number would be negative in the smaller type,
178  // so sign-extend, i.e. set all more significant bits.
179  if(bits & sign_bit)
180  {
181  const u64 valid_bit_mask = (sign_bit+sign_bit)-1;
182  bits |= ~valid_bit_mask;
183  }
184  }
185 
186  const i64 number = static_cast<i64>(bits);
187  return number;
188 }
189 
190 i64 movsx_le64(const u8* p, size_t size_bytes)
191 {
192  const u64 number = movzx_le64(p, size_bytes);
193  return SignExtend(number, size_bytes);
194 }
195 
196 i64 movsx_be64(const u8* p, size_t size_bytes)
197 {
198  const u64 number = movzx_be64(p, size_bytes);
199  return SignExtend(number, size_bytes);
200 }
#define u8
Definition: types.h:39
u32 read_le32(const void *p)
Definition: byte_order.cpp:73
#define to_le16(x)
Definition: byte_order.h:77
void write_be32(void *p, u32 x)
Definition: byte_order.cpp:135
u16 swap16(const u16 x)
Definition: byte_order.cpp:33
void write_le64(void *p, u64 x)
see write_le16
Definition: byte_order.cpp:122
u64 swap64(const u64 x)
Definition: byte_order.cpp:50
u64 read_le64(const void *p)
see read_le16
Definition: byte_order.cpp:80
void write_be64(void *p, u64 x)
see write_be16
Definition: byte_order.cpp:141
i64 movsx_le64(const u8 *p, size_t size_bytes)
sign-extend &lt;size&gt; (truncated to 8) bytes of little-endian data to i64, starting at address &lt;p&gt; (need...
Definition: byte_order.cpp:190
u64 movzx_be64(const u8 *p, size_t size_bytes)
Definition: byte_order.cpp:157
u32 read_be32(const void *p)
Definition: byte_order.cpp:95
u32 swap32(const u32 x)
Definition: byte_order.cpp:40
#define to_le32(x)
Definition: byte_order.h:78
i64 movsx_be64(const u8 *p, size_t size_bytes)
Definition: byte_order.cpp:196
T bits(T num, size_t lo_idx, size_t hi_idx)
extract the value of bits hi_idx:lo_idx within num
Definition: bits.h:97
#define to_be16(x)
Definition: byte_order.h:81
#define u16
Definition: types.h:40
#define u64
Definition: types.h:42
#define i64
Definition: types.h:37
#define u32
Definition: types.h:41
u16 read_le16(const void *p)
read a little-endian number from memory into native byte order.
Definition: byte_order.cpp:66
#define to_be32(x)
Definition: byte_order.h:82
void write_be16(void *p, u16 x)
see write_le16
Definition: byte_order.cpp:129
#define to_be64(x)
Definition: byte_order.h:83
u16 read_be16(const void *p)
see read_le16
Definition: byte_order.cpp:88
u64 read_be64(const void *p)
see read_be16
Definition: byte_order.cpp:102
void write_le16(void *p, u16 x)
see read_be16
Definition: byte_order.cpp:110
#define to_le64(x)
Definition: byte_order.h:79
static i64 SignExtend(u64 bits, size_t size_bytes)
Definition: byte_order.cpp:170
u64 movzx_le64(const u8 *p, size_t size_bytes)
see write_be16
Definition: byte_order.cpp:148
void write_le32(void *p, u32 x)
Definition: byte_order.cpp:116