sbgECom Library  5.1.708-stable
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 
66 SBG_INLINE uint32_t sbgSwap32(uint32_t x)
67 {
68  return ((x << 24) | ((x << 8) & (0xFF0000)) | ((x >> 8) & (0xFF00)) | (x >> 24));
69 }
70 
77 SBG_INLINE uint64_t sbgSwap64(uint64_t x)
78 {
79  uint32_t hi, lo;
80 
81  //
82  // Separate into high and low 32-bit values
83  //
84  lo = (uint32_t)(x&0xFFFFFFFF);
85  x >>= 32;
86  hi = (uint32_t)(x&0xFFFFFFFF);
87 
88  //
89  // Swap each part and rebuild our 64 bit vale
90  //
91  x = sbgSwap32(lo);
92  x <<= 32;
93  x |= sbgSwap32(hi);
94 
95  return x;
96 }
97 
104 SBG_INLINE float sbgSwapFloat(float val)
105 {
106  SbgFloatNint tmpFloat;
107 
108  //
109  // We use a union to do the type punning
110  //
111  tmpFloat.valF = val;
112  tmpFloat.valU = sbgSwap32(tmpFloat.valU);
113 
114  //
115  // Return the swapped float
116  //
117  return tmpFloat.valF;
118 }
119 
126 SBG_INLINE double sbgSwapDouble(double val)
127 {
128  SbgDoubleNint tmpDouble;
129 
130  //
131  // We use a union to do the type punning
132  //
133  tmpDouble.valF = val;
134  tmpDouble.valU = sbgSwap64(tmpDouble.valU);
135 
136  //
137  // Return the swapped double
138  //
139  return tmpDouble.valF;
140 }
141 
142 //----------------------------------------------------------------------//
143 //- Footer (close extern C block) -//
144 //----------------------------------------------------------------------//
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #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:77
SBG_INLINE float sbgSwapFloat(float val)
Definition: sbgSwap.h:104
SBG_INLINE uint32_t sbgSwap32(uint32_t x)
Definition: sbgSwap.h:66
SBG_INLINE double sbgSwapDouble(double val)
Definition: sbgSwap.h:126
SBG_INLINE uint16_t sbgSwap16(uint16_t x)
Definition: sbgSwap.h:55
Definition: sbgTypes.h:149
Definition: sbgTypes.h:139