APM:Libraries
sparse-endian.h
Go to the documentation of this file.
1 /* Copyright (c) 2012 Josh Triplett <josh@joshtriplett.org>
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 #pragma once
22 
23 #include <byteswap.h>
24 #include <endian.h>
25 #include <stdint.h>
26 
27 #ifdef __CHECKER__
28 #define __ap_bitwise __attribute__((bitwise))
29 #define __ap_force __attribute__((force))
30 #else
31 #define __ap_bitwise
32 #define __ap_force
33 #endif
34 
35 typedef uint16_t __ap_bitwise le16_t;
36 typedef uint16_t __ap_bitwise be16_t;
37 typedef uint32_t __ap_bitwise le32_t;
38 typedef uint32_t __ap_bitwise be32_t;
39 typedef uint64_t __ap_bitwise le64_t;
40 typedef uint64_t __ap_bitwise be64_t;
41 
42 #undef htobe16
43 #undef htole16
44 #undef be16toh
45 #undef le16toh
46 #undef htobe32
47 #undef htole32
48 #undef be32toh
49 #undef le32toh
50 #undef htobe64
51 #undef htole64
52 #undef be64toh
53 #undef le64toh
54 
55 #if __BYTE_ORDER == __LITTLE_ENDIAN
56 #define bswap_16_on_le(x) __bswap_16(x)
57 #define bswap_32_on_le(x) __bswap_32(x)
58 #define bswap_64_on_le(x) __bswap_64(x)
59 #define bswap_16_on_be(x) (x)
60 #define bswap_32_on_be(x) (x)
61 #define bswap_64_on_be(x) (x)
62 #elif __BYTE_ORDER == __BIG_ENDIAN
63 #define bswap_16_on_le(x) (x)
64 #define bswap_32_on_le(x) (x)
65 #define bswap_64_on_le(x) (x)
66 #define bswap_16_on_be(x) __bswap_16(x)
67 #define bswap_32_on_be(x) __bswap_32(x)
68 #define bswap_64_on_be(x) __bswap_64(x)
69 #endif
70 
71 static inline le16_t htole16(uint16_t value) { return (le16_t __ap_force) bswap_16_on_be(value); }
72 static inline le32_t htole32(uint32_t value) { return (le32_t __ap_force) bswap_32_on_be(value); }
73 static inline le64_t htole64(uint64_t value) { return (le64_t __ap_force) bswap_64_on_be(value); }
74 
75 static inline be16_t htobe16(uint16_t value) { return (be16_t __ap_force) bswap_16_on_le(value); }
76 static inline be32_t htobe32(uint32_t value) { return (be32_t __ap_force) bswap_32_on_le(value); }
77 static inline be64_t htobe64(uint64_t value) { return (be64_t __ap_force) bswap_64_on_le(value); }
78 
79 static inline uint16_t le16toh(le16_t value) { return bswap_16_on_be((uint16_t __ap_force)value); }
80 static inline uint32_t le32toh(le32_t value) { return bswap_32_on_be((uint32_t __ap_force)value); }
81 static inline uint64_t le64toh(le64_t value) { return bswap_64_on_be((uint64_t __ap_force)value); }
82 
83 static inline uint16_t be16toh(be16_t value) { return bswap_16_on_le((uint16_t __ap_force)value); }
84 static inline uint32_t be32toh(be32_t value) { return bswap_32_on_le((uint32_t __ap_force)value); }
85 static inline uint64_t be64toh(be64_t value) { return bswap_64_on_le((uint64_t __ap_force)value); }
86 
87 #undef __ap_bitwise
88 #undef __ap_force
static uint32_t be32toh(be32_t value)
Definition: sparse-endian.h:84
static le32_t htole32(uint32_t value)
Definition: sparse-endian.h:72
#define bswap_64_on_be(x)
Definition: sparse-endian.h:61
uint64_t __ap_bitwise be64_t
Definition: sparse-endian.h:40
#define bswap_32_on_be(x)
Definition: sparse-endian.h:60
uint32_t __ap_bitwise le32_t
Definition: sparse-endian.h:37
#define bswap_16_on_le(x)
Definition: sparse-endian.h:56
#define bswap_16_on_be(x)
Definition: sparse-endian.h:59
uint64_t __ap_bitwise le64_t
Definition: sparse-endian.h:39
#define __ap_force
Definition: sparse-endian.h:32
static le16_t htole16(uint16_t value)
Definition: sparse-endian.h:71
static be32_t htobe32(uint32_t value)
Definition: sparse-endian.h:76
#define bswap_64_on_le(x)
Definition: sparse-endian.h:58
static uint16_t be16toh(be16_t value)
Definition: sparse-endian.h:83
uint32_t __ap_bitwise be32_t
Definition: sparse-endian.h:38
static be16_t htobe16(uint16_t value)
Definition: sparse-endian.h:75
#define __ap_bitwise
Definition: sparse-endian.h:31
static uint64_t be64toh(be64_t value)
Definition: sparse-endian.h:85
uint16_t __ap_bitwise le16_t
Definition: sparse-endian.h:35
static uint32_t le32toh(le32_t value)
Definition: sparse-endian.h:80
static uint64_t le64toh(le64_t value)
Definition: sparse-endian.h:81
float value
#define bswap_32_on_le(x)
Definition: sparse-endian.h:57
static le64_t htole64(uint64_t value)
Definition: sparse-endian.h:73
static uint16_t le16toh(le16_t value)
Definition: sparse-endian.h:79
static be64_t htobe64(uint64_t value)
Definition: sparse-endian.h:77
uint16_t __ap_bitwise be16_t
Definition: sparse-endian.h:36