sbgECom Library  4.0.1987-stable
C library to interface SBG Systems IMU/AHRS/INS
sbgSwap.h
Go to the documentation of this file.
1 
33 #ifndef SBG_SWAP_H
34 #define SBG_SWAP_H
35 
36 //----------------------------------------------------------------------//
37 //- Header (open extern C block) -//
38 //----------------------------------------------------------------------//
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #include <sbgCommon.h>
44 
45 //----------------------------------------------------------------------//
46 //- Internal swap functions -//
47 //----------------------------------------------------------------------//
48 
55 SBG_INLINE uint16_t sbgSwap16(uint16_t x)
56 {
57  return ((x<<8)|(x>>8));
58 }
59 
65 SBG_INLINE uint32_t sbgSwap32(uint32_t x)
66 {
67  return ((x << 24) | ((x << 8) & (0xFF0000)) | ((x >> 8) & (0xFF00)) | (x >> 24));
68 }
69 
76 SBG_INLINE uint64_t sbgSwap64(uint64_t x)
77 {
78  uint32_t hi, lo;
79 
80  //
81  // Separate into high and low 32-bit values
82  //
83  lo = (uint32_t)(x&0xFFFFFFFF);
84  x >>= 32;
85  hi = (uint32_t)(x&0xFFFFFFFF);
86 
87  //
88  // Swap each part and rebuild our 64 bit vale
89  //
90  x = sbgSwap32(lo);
91  x <<= 32;
92  x |= sbgSwap32(hi);
93 
94  return x;
95 }
96 
103 SBG_INLINE float sbgSwapFloat(float val)
104 {
105  SbgFloatNint tmpFloat;
106 
107  //
108  // We use a union to do the type punning
109  //
110  tmpFloat.valF = val;
111  tmpFloat.valU = sbgSwap32(tmpFloat.valU);
112 
113  //
114  // Return the swapped float
115  //
116  return tmpFloat.valF;
117 }
118 
125 SBG_INLINE double sbgSwapDouble(double val)
126 {
127  SbgDoubleNint tmpDouble;
128 
129  //
130  // We use a union to do the type punning
131  //
132  tmpDouble.valF = val;
133  tmpDouble.valU = sbgSwap64(tmpDouble.valU);
134 
135  //
136  // Return the swapped double
137  //
138  return tmpDouble.valF;
139 }
140 
141 //----------------------------------------------------------------------//
142 //- Footer (close extern C block) -//
143 //----------------------------------------------------------------------//
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif /* SBG_SWAP_H */
Main header for the SBG Systems common C library.
#define SBG_INLINE
Definition: sbgDefines.h:227
SBG_INLINE uint64_t sbgSwap64(uint64_t x)
Definition: sbgSwap.h:76
SBG_INLINE float sbgSwapFloat(float val)
Definition: sbgSwap.h:103
SBG_INLINE uint32_t sbgSwap32(uint32_t x)
Definition: sbgSwap.h:65
SBG_INLINE double sbgSwapDouble(double val)
Definition: sbgSwap.h:125
SBG_INLINE uint16_t sbgSwap16(uint16_t x)
Definition: sbgSwap.h:55
Definition: sbgTypes.h:149
Definition: sbgTypes.h:139