summaryrefslogtreecommitdiffstats
path: root/include/platform.h
blob: 837c87d7958d50ac82928cd7b8b0c97178041501 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef VL_ENDIAN_H_INCLUDED
#define VL_ENDIAN_H_INCLUDED

#include "config.h"
#include <stdint.h>
#include <stddef.h>

#ifdef __GNUC__
#define VL_SWAP16_EXPR(_u) __builtin_bswap16(_u)
#define VL_SWAP32_EXPR(_u) __builtin_bswap32(_u)
#define VL_SWAP64_EXPR(_u) __builtin_bswap64(_u)
#else

#define VL_SWAP16_EXPR(_u) (uint16_t)((_u) << 8 | (_u) >> 8)

#define VL_SWAP32_EXPR(_u) (uint32_t) \
  ((((_u) & 0xFF000000u) >> 24) | \
   (((_u) & 0x00FF0000u) >>  8) | \
   (((_u) & 0x0000FF00u) <<  8) | \
   (((_u) & 0x000000FFu) << 24))

#define VL_SWAP64_EXPR(_u) (uint64_t) \
  ((((_u) & UINT64_C(0xFF00000000000000)) >> 56) | \
   (((_u) & UINT64_C(0x00FF000000000000)) >> 40) | \
   (((_u) & UINT64_C(0x0000FF0000000000)) >> 24) | \
   (((_u) & UINT64_C(0x000000FF00000000)) >>  8) | \
   (((_u) & UINT64_C(0x00000000FF000000)) <<  8) | \
   (((_u) & UINT64_C(0x0000000000FF0000)) << 24) | \
   (((_u) & UINT64_C(0x000000000000FF00)) << 40) | \
   (((_u) & UINT64_C(0x00000000000000FF)) << 56))

#endif

static inline uint16_t vl_swap16(uint16_t in)
{
  return VL_SWAP16_EXPR(in);
}

static inline uint32_t vl_swap32(uint32_t in)
{
  return VL_SWAP32_EXPR(in);
}

static inline uint64_t vl_swap64(uint64_t in)
{
  return VL_SWAP64_EXPR(in);
}

/* avoid leaking non-public macro definitions -- encourage the use of inline functions instead */
#undef VL_SWAP16_EXPR
#undef VL_SWAP32_EXPR
#undef VL_SWAP64_EXPR

#if VL_ENDIAN_LITTLE
#define vl_htobe16(_x) vl_swap16(_x)
#define vl_htobe32(_x) vl_swap32(_x)
#define vl_htobe64(_x) vl_swap64(_x)

#define vl_htole16(_x) (uint16_t)(_x)
#define vl_htole32(_x) (uint32_t)(_x)
#define vl_htole64(_x) (uint64_t)(_x)

#define vl_betoh16(_x) vl_swap16(_x)
#define vl_betoh32(_x) vl_swap32(_x)
#define vl_betoh64(_x) vl_swap64(_x)

#define vl_letoh16(_x) (uint16_t)(_x)
#define vl_letoh32(_x) (uint32_t)(_x)
#define vl_letoh64(_x) (uint64_t)(_x)
#else /* VL_ENDIAN_BIG */
#define vl_htobe16(_x) (uint16_t)(_x)
#define vl_htobe32(_x) (uint32_t)(_x)
#define vl_htobe64(_x) (uint64_t)(_x)

#define vl_htole16(_x) vl_swap16(_x)
#define vl_htole32(_x) vl_swap32(_x)
#define vl_htole64(_x) vl_swap64(_x)

#define vl_betoh16(_x) (uint16_t)(_x)
#define vl_betoh32(_x) (uint32_t)(_x)
#define vl_betoh64(_x) (uint64_t)(_x)

#define vl_letoh16(_x) vl_swap16(_x)
#define vl_letoh32(_x) vl_swap32(_x)
#define vl_letoh64(_x) vl_swap64(_x)
#endif

#endif