P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpf_common.h
1/*
2 * Copyright 2019 Orange
3 * SPDX-FileCopyrightText: 2019 Orange
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ___constant_swab16
9#define ___constant_swab16(x) ((uint16_t)( \
10 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
11 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
12#endif
13
14#ifndef ___constant_swab32
15#define ___constant_swab32(x) ((uint32_t)( \
16 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
17 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
18 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
19 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
20#endif
21
22#ifndef ___constant_swab64
23#define ___constant_swab64(x) ((uint64_t)( \
24 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
25 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
26 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
27 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
28 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
29 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
30 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
31 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
32#endif
33
34#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
35#ifndef __constant_htonll
36#define __constant_htonll(x) (___constant_swab64((x)))
37#endif
38
39#ifndef __constant_ntohll
40#define __constant_ntohll(x) (___constant_swab64((x)))
41#endif
42
43#define __constant_htonl(x) (___constant_swab32((x)))
44#define __constant_ntohl(x) (___constant_swab32(x))
45#define __constant_htons(x) (___constant_swab16((x)))
46#define __constant_ntohs(x) ___constant_swab16((x))
47
48#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
49# warning "I never tested BIG_ENDIAN machine!"
50#define __constant_htonll(x) (x)
51#define __constant_ntohll(X) (x)
52#define __constant_htonl(x) (x)
53#define __constant_ntohl(x) (x)
54#define __constant_htons(x) (x)
55#define __constant_ntohs(x) (x)
56
57#else
58# error "Fix your compiler's __BYTE_ORDER__?!"
59#endif
60
61#undef htonl
62#define htonl(d) __constant_htonl(d)
63#undef htons
64#define htons(d) __constant_htons(d)
65#undef htonll
66#define htonll(d) __constant_htonll(d)
67#undef ntohl
68#define ntohl(d) __constant_ntohl(d)
69#undef ntohs
70#define ntohs(d) __constant_ntohs(d)
71#undef ntohll
72#define ntohll(d) __constant_ntohll(d)
73
74#define load_byte(data, b) (*(((uint8_t*)(data)) + (b)))
75#define load_half(data, b) __constant_ntohs(*(uint16_t *)((uint8_t*)(data) + (b)))
76#define load_word(data, b) __constant_ntohl(*(uint32_t *)((uint8_t*)(data) + (b)))
77#define load_dword(data, b) __constant_ntohll(*(uint64_t *)((uint8_t*)(data) + (b)))