blob: 4274b8fbfe5014f8e1e090c478b02e39adc2e379 [file] [log] [blame]
Harald Welte3318c652017-05-15 12:07:51 +02001#pragma once
2#include <stdint.h>
3#include <osmocom/core/endian.h>
4
Neels Hofmeyr87e45502017-06-20 00:17:59 +02005/*! byte-swap a 32bit word
Harald Welte3318c652017-05-15 12:07:51 +02006 * \param[in] in to be swapped 32bit word
7 * \returns byte-swapped 32bit word */
8static 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020020/*! byte-swap a 16bit word
Harald Welte3318c652017-05-15 12:07:51 +020021 * \param[in] in to be swapped 16bit word
22 * \returns byte-swapped 16bit word */
23static 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