P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpf_common.h
1/*
2Copyright 2019 Orange
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef ___constant_swab16
18#define ___constant_swab16(x) ((uint16_t)( \
19 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
20 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
21#endif
22
23#ifndef ___constant_swab32
24#define ___constant_swab32(x) ((uint32_t)( \
25 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
26 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
27 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
28 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
29#endif
30
31#ifndef ___constant_swab64
32#define ___constant_swab64(x) ((uint64_t)( \
33 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
34 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
35 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
36 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
37 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
38 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
39 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
40 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
41#endif
42
43#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
44#ifndef __constant_htonll
45#define __constant_htonll(x) (___constant_swab64((x)))
46#endif
47
48#ifndef __constant_ntohll
49#define __constant_ntohll(x) (___constant_swab64((x)))
50#endif
51
52#define __constant_htonl(x) (___constant_swab32((x)))
53#define __constant_ntohl(x) (___constant_swab32(x))
54#define __constant_htons(x) (___constant_swab16((x)))
55#define __constant_ntohs(x) ___constant_swab16((x))
56
57#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
58# warning "I never tested BIG_ENDIAN machine!"
59#define __constant_htonll(x) (x)
60#define __constant_ntohll(X) (x)
61#define __constant_htonl(x) (x)
62#define __constant_ntohl(x) (x)
63#define __constant_htons(x) (x)
64#define __constant_ntohs(x) (x)
65
66#else
67# error "Fix your compiler's __BYTE_ORDER__?!"
68#endif
69
70#undef htonl
71#define htonl(d) __constant_htonl(d)
72#undef htons
73#define htons(d) __constant_htons(d)
74#undef htonll
75#define htonll(d) __constant_htonll(d)
76#undef ntohl
77#define ntohl(d) __constant_ntohl(d)
78#undef ntohs
79#define ntohs(d) __constant_ntohs(d)
80#undef ntohll
81#define ntohll(d) __constant_ntohll(d)
82
83#define load_byte(data, b) (*(((uint8_t*)(data)) + (b)))
84#define load_half(data, b) __constant_ntohs(*(uint16_t *)((uint8_t*)(data) + (b)))
85#define load_word(data, b) __constant_ntohl(*(uint32_t *)((uint8_t*)(data) + (b)))
86#define load_dword(data, b) __constant_ntohll(*(uint64_t *)((uint8_t*)(data) + (b)))