]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/endian.h
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 * Endian conversion functions
42 #include "spdk/stdinc.h"
48 static inline uint16_t
49 from_be16(const void *ptr
)
51 const uint8_t *tmp
= (const uint8_t *)ptr
;
52 return (((uint16_t)tmp
[0] << 8) | tmp
[1]);
56 to_be16(void *out
, uint16_t in
)
58 uint8_t *tmp
= (uint8_t *)out
;
59 tmp
[0] = (in
>> 8) & 0xFF;
63 static inline uint32_t
64 from_be32(const void *ptr
)
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) |
74 to_be32(void *out
, uint32_t in
)
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;
83 static inline uint64_t
84 from_be64(const void *ptr
)
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) |
98 to_be64(void *out
, uint64_t in
)
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;
111 static inline uint16_t
112 from_le16(const void *ptr
)
114 const uint8_t *tmp
= (const uint8_t *)ptr
;
115 return (((uint16_t)tmp
[1] << 8) | tmp
[0]);
119 to_le16(void *out
, uint16_t in
)
121 uint8_t *tmp
= (uint8_t *)out
;
122 tmp
[1] = (in
>> 8) & 0xFF;
126 static inline uint32_t
127 from_le32(const void *ptr
)
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) |
137 to_le32(void *out
, uint32_t in
)
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;
146 static inline uint64_t
147 from_le64(const void *ptr
)
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) |
161 to_le64(void *out
, uint64_t in
)
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;