amino  1.0-beta2
Lightweight Robot Utility Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
endconv.h
Go to the documentation of this file.
1 /* -*- mode: C; c-basic-offset: 4 -*- */
2 /* ex: set shiftwidth=4 tabstop=4 expandtab: */
3 /*
4  * Copyright (c) 2008-2011, Georgia Tech Research Corporation
5  * All rights reserved.
6  *
7  * Author(s): Neil T. Dantam <ntd@gatech.edu>
8  * Georgia Tech Humanoid Robotics Lab
9  * Under Direction of Prof. Mike Stilman <mstilman@cc.gatech.edu>
10  *
11  *
12  * This file is provided under the following "BSD-style" License:
13  *
14  *
15  * Redistribution and use in source and binary forms, with or
16  * without modification, are permitted provided that the following
17  * conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  *
41  */
42 
43 #ifndef _ENDCONV_H
44 #define _ENDCONV_H
45 
55 /* See the README and the doxygen docs for usage info. */
56 
57 
58 
59 /****************/
61 /****************/
62 
64 static inline int64_t aa_endconv_ld_le_i64( void *src );
66 static inline void aa_endconv_st_le_i64( void *dst, int64_t src);
68 static inline int64_t aa_endconv_ld_be_i64( void *src );
70 static inline void aa_endconv_st_be_i64( void *dst, int64_t src);
72 static inline int64_t aa_endconv_h_to_le_i64( int64_t i_host);
74 static inline int64_t aa_endconv_le_to_h_i64( int64_t i_le);
76 static inline int64_t aa_endconv_h_to_be_i64( int64_t i_host);
78 static inline int64_t aa_endconv_be_to_h_i64( int64_t i_be);
79 
80 
82 static inline uint64_t aa_endconv_ld_le_u64( void *src );
84 static inline void aa_endconv_st_le_u64( void *dst, uint64_t src);
86 static inline uint64_t aa_endconv_ld_be_u64( void *src );
88 static inline void aa_endconv_st_be_u64( void *dst, uint64_t src);
90 static inline uint64_t aa_endconv_h_to_le_u64( uint64_t i_host);
92 static inline uint64_t aa_endconv_le_to_h_u64( uint64_t i_le);
94 static inline uint64_t aa_endconv_h_to_be_u64( uint64_t i_host);
96 static inline uint64_t aa_endconv_be_to_h_u64( uint64_t i_be);
97 
98 
100 static inline int32_t aa_endconv_ld_le_i32( void *src );
102 static inline void aa_endconv_st_le_i32( void *dst, int32_t src);
104 static inline int32_t aa_endconv_ld_be_i32( void *src );
106 static inline void aa_endconv_st_be_i32( void *dst, int32_t src);
108 static inline int32_t aa_endconv_h_to_le_i32( int32_t i_host);
110 static inline int32_t aa_endconv_le_to_h_i32( int32_t i_le);
112 static inline int32_t aa_endconv_h_to_be_i32( int32_t i_host);
114 static inline int32_t aa_endconv_be_to_h_i32( int32_t i_be);
115 
117 static inline uint32_t aa_endconv_ld_le_u32( void *src );
119 static inline void aa_endconv_st_le_u32( void *dst, uint32_t src);
121 static inline uint32_t aa_endconv_ld_be_u32( void *src );
123 static inline void aa_endconv_st_be_u32( void *dst, uint32_t src);
125 static inline uint32_t aa_endconv_h_to_le_u32( uint32_t i_host);
127 static inline uint32_t aa_endconv_le_to_h_u32( uint32_t i_le);
129 static inline uint32_t aa_endconv_h_to_be_u32( uint32_t i_host);
131 static inline uint32_t aa_endconv_be_to_h_u32( uint32_t i_be);
132 
133 
135 static inline int16_t aa_endconv_ld_le_i16( void *src );
137 static inline void aa_endconv_st_le_i16( void *dst, int16_t src);
139 static inline int16_t aa_endconv_ld_be_i16( void *src );
141 static inline void aa_endconv_st_be_i16( void *dst, int16_t src);
143 static inline int16_t aa_endconv_h_to_le_i16( int16_t i_host);
145 static inline int16_t aa_endconv_le_to_h_i16( int16_t i_le);
147 static inline int16_t aa_endconv_h_to_be_i16( int16_t i_host);
149 static inline int16_t aa_endconv_be_to_h_i16( int16_t i_be);
150 
152 static inline uint16_t aa_endconv_ld_le_u16( void *src );
154 static inline void aa_endconv_st_le_u16( void *dst, uint16_t src);
156 static inline uint16_t aa_endconv_ld_be_u16( void *src );
158 static inline void aa_endconv_st_be_u16( void *dst, uint16_t src);
160 static inline uint16_t aa_endconv_h_to_le_u16( uint16_t i_host);
162 static inline uint16_t aa_endconv_le_to_h_u16( uint16_t i_le);
164 static inline uint16_t aa_endconv_h_to_be_u16( uint16_t i_host);
166 static inline uint16_t aa_endconv_be_to_h_u16( uint16_t i_be);
167 
168 
170 static inline double aa_endconv_ld_le_d( void *src );
172 static inline void aa_endconv_st_le_d( void *dst, double src);
174 static inline double aa_endconv_ld_be_d( void *src );
176 static inline void aa_endconv_st_be_d( void *dst, double src);
177 
179 static inline double aa_endconv_ld_le_d( void *src );
181 static inline void aa_endconv_st_le_d( void *dst, double src);
183 static inline double aa_endconv_ld_be_d( void *src );
185 static inline void aa_endconv_st_be_d( void *dst, double src);
186 
188 static inline float aa_endconv_ld_le_s( void *src );
190 static inline void aa_endconv_st_le_s( void *dst, float src);
192 static inline float aa_endconv_ld_be_s( void *src );
194 static inline void aa_endconv_st_be_s( void *dst, float src);
195 
197 static inline float aa_endconv_ld_le_s( void *src );
199 static inline void aa_endconv_st_le_s( void *dst, float src);
201 static inline float aa_endconv_ld_be_s( void *src );
203 static inline void aa_endconv_st_be_s( void *dst, float src);
204 
205 
206 
207 /*************/
208 /* HELPERS */
209 /*************/
210 
212 static inline void aa_endconv_reverse( void * dst , void *src, size_t len ) {
213  size_t i;
214  for( i = 0; i < len; i ++ ) {
215  size_t j = len - i - 1;
216  uint8_t s = ((uint8_t*)src)[j];
217  //printf("reversing: src[%d] -> dst[%d]: %x\n", j, i, s );
218  ((uint8_t*)dst)[i] = s;
219  }
220 }
221 
222 
224 static inline void aa_endconv_st_le( void *dst, void *src, size_t len ) {
225 #if __BYTE_ORDER == __LITTLE_ENDIAN
226  memcpy( dst, src, len );
227 #elif __BYTE_ORDER == __BIG_ENDIAN
228  aa_endconv_reverse( dst, src, len );
229 #else
230 #error "Unknown Endian Convention"
231 #endif
232 }
233 
235 static inline void aa_endconv_st_be( void *dst, void *src, size_t len ) {
236 #if __BYTE_ORDER == __LITTLE_ENDIAN
237  aa_endconv_reverse( dst, src, len );
238 #elif __BYTE_ORDER == __BIG_ENDIAN
239  memcpy( dst, src, len );
240 #else
241 #error "Unknown Endian Convention"
242 #endif
243 }
244 
245 /*****************/
246 /* DEFINITIONS */
247 /*****************/
248 
249 
250 // typing is work, so let's make some macros to do our function defs
251 
252 // this should probably be unrolled by a smart optimizer
254 #define AA_ENDCONV_MAKE_REVERSE( type, suffix ) \
255  static inline type aa_endconv_reverse_##suffix( type x ) { \
256  type y = 0; \
257  type v; \
258  type j; \
259  size_t i; \
260  for( i = 0 ; i < 8*sizeof(type); i+=8 ) { \
261  j = (type) (8*sizeof(type) - i - 8); \
262  v = (type) (((x >> i) & 0xff) << j); \
263  y |= v; \
264  } \
265  return y; \
266  };
267 
268 
269 // definer macros for aa_endconv_h_to_{l,b}e functions
270 #if __BYTE_ORDER == __LITTLE_ENDIAN
271 
273 #define AA_ENDCONV_MAKE_H_TO_LE( type, suffix ) \
274  static inline type aa_endconv_h_to_le_##suffix( type v ) { return v; }
275 #define AA_ENDCONV_MAKE_H_TO_BE( type, suffix ) \
277  static inline type aa_endconv_h_to_be_##suffix( type v ) { \
278  return aa_endconv_reverse_##suffix( v ); \
279  };
280 
282 #define AA_ENDCONV_MAKE_LE_TO_H( type, suffix ) \
283  static inline type aa_endconv_le_to_h_##suffix( type v ) { return v; }
284 #define AA_ENDCONV_MAKE_BE_TO_H( type, suffix ) \
286  static inline type aa_endconv_be_to_h_##suffix( type v ) { \
287  return aa_endconv_reverse_##suffix( v ); \
288  };
289 
290 #elif __BYTE_ORDER == __BIG_ENDIAN
291 
292 #define AA_ENDCONV_MAKE_H_TO_LE( type, suffix ) \
293  static inline type aa_endconv_h_to_le_##suffix( type v ) { \
294  return aa_endconv_reverse_##suffix( v ); \
295  };
296 #define AA_ENDCONV_MAKE_H_TO_BE( type, suffix ) \
297  static inline type aa_endconv_h_to_le_##suffix( type v ) { return v; }
298 
299 #define AA_ENDCONV_MAKE_LE_TO_H( type, suffix ) \
300  static inline type aa_endconv_le_to_h_##suffix( type v ) { \
301  return aa_endconv_reverse_##suffix( v ); \
302  };
303 #define AA_ENDCONV_MAKE_BE_TO_H( type, suffix ) \
304  static inline type aa_endconv_be_to_h_##suffix( type v ) { return v; }
305 
306 #else
307 #error "Unknown Endian Convention"
308 #endif
309 
311 #define AA_ENDCONV_MAKE_LDST_DEF(type,suffix) \
312  static inline void aa_endconv_st_le_##suffix( void *dst, type src ) { \
313  aa_endconv_st_le( dst, &src, sizeof(type) ); } \
314  static inline void aa_endconv_st_be_##suffix( void *dst, type src ) { \
315  aa_endconv_st_be( dst, &src, sizeof(type) ); } \
316  static inline type aa_endconv_ld_le_##suffix( void *src ) { \
317  type r; \
318  aa_endconv_st_le( &r, src, sizeof(type) ); \
319  return r;} \
320  static inline type aa_endconv_ld_be_##suffix( void *src ) { \
321  type r; \
322  aa_endconv_st_be( &r, src, sizeof(type) ); \
323  return r; } \
324 #define AA_ENDCONV_MAKE_CONV_DEF(type,suffix) \
326  AA_ENDCONV_MAKE_REVERSE( type, suffix ) \
327  AA_ENDCONV_MAKE_H_TO_LE( type, suffix ) \
328  AA_ENDCONV_MAKE_H_TO_BE( type, suffix ) \
329  AA_ENDCONV_MAKE_LE_TO_H( type, suffix ) \
330  AA_ENDCONV_MAKE_BE_TO_H( type, suffix ) \
331 
332 
334 AA_ENDCONV_MAKE_LDST_DEF( int16_t, i16 );
336 AA_ENDCONV_MAKE_CONV_DEF( int16_t, i16 );
338 AA_ENDCONV_MAKE_LDST_DEF( uint16_t, u16 );
340 AA_ENDCONV_MAKE_CONV_DEF( uint16_t, u16 );
341 
343 AA_ENDCONV_MAKE_LDST_DEF( int32_t, i32 );
345 AA_ENDCONV_MAKE_CONV_DEF( int32_t, i32 );
346 
348 AA_ENDCONV_MAKE_LDST_DEF( uint32_t, u32 );
350 AA_ENDCONV_MAKE_CONV_DEF( uint32_t, u32 );
351 
353 AA_ENDCONV_MAKE_LDST_DEF( int64_t, i64 );
355 AA_ENDCONV_MAKE_CONV_DEF( int64_t, i64 );
356 
358 AA_ENDCONV_MAKE_LDST_DEF( uint64_t, u64 );
360 AA_ENDCONV_MAKE_CONV_DEF( uint64_t, u64 );
361 
363 AA_ENDCONV_MAKE_LDST_DEF( double, d );
365 AA_ENDCONV_MAKE_LDST_DEF( float, s );
366 
367 
368 #endif
static int16_t aa_endconv_le_to_h_i16(int16_t i_le)
convert little-endian representation to native
static void aa_endconv_st_be_i32(void *dst, int32_t src)
convert native src to big-endian dst
static uint64_t aa_endconv_ld_le_u64(void *src)
Memory Load: convert little endian src to native type.
static int32_t aa_endconv_ld_le_i32(void *src)
Memory Load: convert little endian src to native type.
static int32_t aa_endconv_be_to_h_i32(int32_t i_be)
convert big-endian representation to native
static uint64_t aa_endconv_le_to_h_u64(uint64_t i_le)
convert little-endian representation to native
static void aa_endconv_st_be_u64(void *dst, uint64_t src)
convert native src to big-endian dst
static float aa_endconv_ld_be_s(void *src)
Memory Load: convert big-endian src to native.
static void aa_endconv_st_le_u32(void *dst, uint32_t src)
convert native to src to little-endian dst
static int16_t aa_endconv_h_to_be_i16(int16_t i_host)
convert native representation to big-endian
static uint32_t aa_endconv_h_to_le_u32(uint32_t i_host)
convert native representation to little-endian
#define AA_ENDCONV_MAKE_LDST_DEF(type, suffix)
macro make to load/store function defs
Definition: endconv.h:311
#define AA_ENDCONV_MAKE_CONV_DEF(type, suffix)
macro to make conversion function defs
Definition: endconv.h:325
static int64_t aa_endconv_ld_le_i64(void *src)
PROTOTYPES.
static int16_t aa_endconv_be_to_h_i16(int16_t i_be)
convert big-endian representation to native
static float aa_endconv_ld_le_s(void *src)
Memory Load: convert little endian src to native type.
static void aa_endconv_st_le(void *dst, void *src, size_t len)
st src to dst with dst being little endian
Definition: endconv.h:224
static int16_t aa_endconv_ld_be_i16(void *src)
Memory Load: convert big-endian src to native.
static void aa_endconv_st_be_s(void *dst, float src)
convert native src to big-endian dst
static uint64_t aa_endconv_ld_be_u64(void *src)
Memory Load: convert big-endian src to native.
static uint32_t aa_endconv_ld_be_u32(void *src)
Memory Load: convert big-endian src to native.
static void aa_endconv_st_be_u16(void *dst, uint16_t src)
convert native src to big-endian dst
static double aa_endconv_ld_le_d(void *src)
Memory Load: convert little endian src to native type.
static uint64_t aa_endconv_h_to_be_u64(uint64_t i_host)
convert native representation to big-endian
static double aa_endconv_ld_be_d(void *src)
Memory Load: convert big-endian src to native.
static uint16_t aa_endconv_le_to_h_u16(uint16_t i_le)
convert little-endian representation to native
static int32_t aa_endconv_le_to_h_i32(int32_t i_le)
convert little-endian representation to native
static int64_t aa_endconv_ld_be_i64(void *src)
Memory Load: convert big-endian src to native.
static int64_t aa_endconv_le_to_h_i64(int64_t i_le)
convert little-endian representation to native
static void aa_endconv_st_be_d(void *dst, double src)
convert native src to big-endian dst
static uint16_t aa_endconv_ld_be_u16(void *src)
Memory Load: convert big-endian src to native.
static int16_t aa_endconv_h_to_le_i16(int16_t i_host)
convert native representation to little-endian
static uint32_t aa_endconv_le_to_h_u32(uint32_t i_le)
convert little-endian representation to native
static int64_t aa_endconv_be_to_h_i64(int64_t i_be)
convert big-endian representation to native
static int64_t aa_endconv_h_to_le_i64(int64_t i_host)
convert native representation to little-endian
static int32_t aa_endconv_h_to_be_i32(int32_t i_host)
convert native representation to big-endian
static int32_t aa_endconv_h_to_le_i32(int32_t i_host)
convert native representation to little-endian
static void aa_endconv_st_be_i16(void *dst, int16_t src)
convert native src to big-endian dst
static void aa_endconv_st_le_i64(void *dst, int64_t src)
convert native to src to little-endian dst
static void aa_endconv_st_le_i16(void *dst, int16_t src)
convert native to src to little-endian dst
static void aa_endconv_st_be_u32(void *dst, uint32_t src)
convert native src to big-endian dst
static uint16_t aa_endconv_h_to_be_u16(uint16_t i_host)
convert native representation to big-endian
static int16_t aa_endconv_ld_le_i16(void *src)
Memory Load: convert little endian src to native type.
static void aa_endconv_st_be(void *dst, void *src, size_t len)
st src to dst with dst being big endian
Definition: endconv.h:235
static void aa_endconv_st_le_s(void *dst, float src)
convert native to src to little-endian dst
static uint16_t aa_endconv_ld_le_u16(void *src)
Memory Load: convert little endian src to native type.
static void aa_endconv_st_le_u64(void *dst, uint64_t src)
convert native to src to little-endian dst
static uint32_t aa_endconv_ld_le_u32(void *src)
Memory Load: convert little endian src to native type.
static int32_t aa_endconv_ld_be_i32(void *src)
Memory Load: convert big-endian src to native.
static void aa_endconv_st_le_i32(void *dst, int32_t src)
convert native to src to little-endian dst
static int64_t aa_endconv_h_to_be_i64(int64_t i_host)
convert native representation to big-endian
static uint16_t aa_endconv_h_to_le_u16(uint16_t i_host)
convert native representation to little-endian
static void aa_endconv_st_be_i64(void *dst, int64_t src)
convert native src to big-endian dst
static void aa_endconv_st_le_u16(void *dst, uint16_t src)
convert native to src to little-endian dst
static uint16_t aa_endconv_be_to_h_u16(uint16_t i_be)
convert big-endian representation to native
static uint32_t aa_endconv_be_to_h_u32(uint32_t i_be)
convert big-endian representation to native
static void aa_endconv_reverse(void *dst, void *src, size_t len)
copy src to dst in reverse byte order
Definition: endconv.h:212
static uint32_t aa_endconv_h_to_be_u32(uint32_t i_host)
convert native representation to big-endian
static uint64_t aa_endconv_h_to_le_u64(uint64_t i_host)
convert native representation to little-endian
static uint64_t aa_endconv_be_to_h_u64(uint64_t i_be)
convert big-endian representation to native
static void aa_endconv_st_le_d(void *dst, double src)
convert native to src to little-endian dst