libosmocore  0.9.6.250-0d49f
Osmocom core library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
byteswap.h
1 #pragma once
2 #include <stdint.h>
3 #include <osmocom/core/endian.h>
4 
8 static inline uint32_t osmo_swab32(uint32_t in)
9 {
10  uint32_t out;
11 
12  out = (in & 0xff) << 24;
13  out |= (in & 0xff00) << 8;
14  out |= (in & 0xff0000) >> 8;
15  out |= (in & 0xff000000) >> 24;
16 
17  return out;
18 }
19 
23 static inline uint16_t osmo_swab16(uint16_t in)
24 {
25  uint16_t out;
26 
27  out = (in & 0xff) << 8;
28  out |= (in & 0xff00) >> 8;
29 
30  return out;
31 }
32 
33 #ifdef OSMO_IS_LITTLE_ENDIAN
34 #define osmo_ntohl(x) osmo_swab32(x)
35 #define osmo_ntohs(x) osmo_swab16(x)
36 #define osmo_htonl(x) osmo_swab32(x)
37 #define osmo_htons(x) osmo_swab16(x)
38 #else
39 #define osmo_ntohl(x) (x)
40 #define osmo_ntohs(x) (x)
41 #define osmo_htonl(x) (x)
42 #define osmo_htons(x) (x)
43 #endif