]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/endian.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / include / spdk / endian.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /**
35 * \file
36 * Endian conversion functions
37 */
38
39 #ifndef SPDK_ENDIAN_H
40 #define SPDK_ENDIAN_H
41
42 #include "spdk/stdinc.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 static inline uint16_t
49 from_be16(const void *ptr)
50 {
51 const uint8_t *tmp = (const uint8_t *)ptr;
52 return (((uint16_t)tmp[0] << 8) | tmp[1]);
53 }
54
55 static inline void
56 to_be16(void *out, uint16_t in)
57 {
58 uint8_t *tmp = (uint8_t *)out;
59 tmp[0] = (in >> 8) & 0xFF;
60 tmp[1] = in & 0xFF;
61 }
62
63 static inline uint32_t
64 from_be32(const void *ptr)
65 {
66 const uint8_t *tmp = (const uint8_t *)ptr;
67 return (((uint32_t)tmp[0] << 24) |
68 ((uint32_t)tmp[1] << 16) |
69 ((uint32_t)tmp[2] << 8) |
70 ((uint32_t)tmp[3]));
71 }
72
73 static inline void
74 to_be32(void *out, uint32_t in)
75 {
76 uint8_t *tmp = (uint8_t *)out;
77 tmp[0] = (in >> 24) & 0xFF;
78 tmp[1] = (in >> 16) & 0xFF;
79 tmp[2] = (in >> 8) & 0xFF;
80 tmp[3] = in & 0xFF;
81 }
82
83 static inline uint64_t
84 from_be64(const void *ptr)
85 {
86 const uint8_t *tmp = (const uint8_t *)ptr;
87 return (((uint64_t)tmp[0] << 56) |
88 ((uint64_t)tmp[1] << 48) |
89 ((uint64_t)tmp[2] << 40) |
90 ((uint64_t)tmp[3] << 32) |
91 ((uint64_t)tmp[4] << 24) |
92 ((uint64_t)tmp[5] << 16) |
93 ((uint64_t)tmp[6] << 8) |
94 ((uint64_t)tmp[7]));
95 }
96
97 static inline void
98 to_be64(void *out, uint64_t in)
99 {
100 uint8_t *tmp = (uint8_t *)out;
101 tmp[0] = (in >> 56) & 0xFF;
102 tmp[1] = (in >> 48) & 0xFF;
103 tmp[2] = (in >> 40) & 0xFF;
104 tmp[3] = (in >> 32) & 0xFF;
105 tmp[4] = (in >> 24) & 0xFF;
106 tmp[5] = (in >> 16) & 0xFF;
107 tmp[6] = (in >> 8) & 0xFF;
108 tmp[7] = in & 0xFF;
109 }
110
111 static inline uint16_t
112 from_le16(const void *ptr)
113 {
114 const uint8_t *tmp = (const uint8_t *)ptr;
115 return (((uint16_t)tmp[1] << 8) | tmp[0]);
116 }
117
118 static inline void
119 to_le16(void *out, uint16_t in)
120 {
121 uint8_t *tmp = (uint8_t *)out;
122 tmp[1] = (in >> 8) & 0xFF;
123 tmp[0] = in & 0xFF;
124 }
125
126 static inline uint32_t
127 from_le32(const void *ptr)
128 {
129 const uint8_t *tmp = (const uint8_t *)ptr;
130 return (((uint32_t)tmp[3] << 24) |
131 ((uint32_t)tmp[2] << 16) |
132 ((uint32_t)tmp[1] << 8) |
133 ((uint32_t)tmp[0]));
134 }
135
136 static inline void
137 to_le32(void *out, uint32_t in)
138 {
139 uint8_t *tmp = (uint8_t *)out;
140 tmp[3] = (in >> 24) & 0xFF;
141 tmp[2] = (in >> 16) & 0xFF;
142 tmp[1] = (in >> 8) & 0xFF;
143 tmp[0] = in & 0xFF;
144 }
145
146 static inline uint64_t
147 from_le64(const void *ptr)
148 {
149 const uint8_t *tmp = (const uint8_t *)ptr;
150 return (((uint64_t)tmp[7] << 56) |
151 ((uint64_t)tmp[6] << 48) |
152 ((uint64_t)tmp[5] << 40) |
153 ((uint64_t)tmp[4] << 32) |
154 ((uint64_t)tmp[3] << 24) |
155 ((uint64_t)tmp[2] << 16) |
156 ((uint64_t)tmp[1] << 8) |
157 ((uint64_t)tmp[0]));
158 }
159
160 static inline void
161 to_le64(void *out, uint64_t in)
162 {
163 uint8_t *tmp = (uint8_t *)out;
164 tmp[7] = (in >> 56) & 0xFF;
165 tmp[6] = (in >> 48) & 0xFF;
166 tmp[5] = (in >> 40) & 0xFF;
167 tmp[4] = (in >> 32) & 0xFF;
168 tmp[3] = (in >> 24) & 0xFF;
169 tmp[2] = (in >> 16) & 0xFF;
170 tmp[1] = (in >> 8) & 0xFF;
171 tmp[0] = in & 0xFF;
172 }
173
174 #ifdef __cplusplus
175 }
176 #endif
177
178 #endif