1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2016 Intel Corporation.
3 * All rights reserved.
4 */
5
6 /**
7 * \file
8 * Endian conversion functions
9 */
10
11 #ifndef SPDK_ENDIAN_H
12 #define SPDK_ENDIAN_H
13
14 #include "spdk/stdinc.h"
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 static inline uint16_t
from_be16(const void * ptr)21 from_be16(const void *ptr)
22 {
23 const uint8_t *tmp = (const uint8_t *)ptr;
24 return (((uint16_t)tmp[0] << 8) | tmp[1]);
25 }
26
27 static inline void
to_be16(void * out,uint16_t in)28 to_be16(void *out, uint16_t in)
29 {
30 uint8_t *tmp = (uint8_t *)out;
31 tmp[0] = (in >> 8) & 0xFF;
32 tmp[1] = in & 0xFF;
33 }
34
35 static inline uint32_t
from_be32(const void * ptr)36 from_be32(const void *ptr)
37 {
38 const uint8_t *tmp = (const uint8_t *)ptr;
39 return (((uint32_t)tmp[0] << 24) |
40 ((uint32_t)tmp[1] << 16) |
41 ((uint32_t)tmp[2] << 8) |
42 ((uint32_t)tmp[3]));
43 }
44
45 static inline void
to_be32(void * out,uint32_t in)46 to_be32(void *out, uint32_t in)
47 {
48 uint8_t *tmp = (uint8_t *)out;
49 tmp[0] = (in >> 24) & 0xFF;
50 tmp[1] = (in >> 16) & 0xFF;
51 tmp[2] = (in >> 8) & 0xFF;
52 tmp[3] = in & 0xFF;
53 }
54
55 static inline uint64_t
from_be64(const void * ptr)56 from_be64(const void *ptr)
57 {
58 const uint8_t *tmp = (const uint8_t *)ptr;
59 return (((uint64_t)tmp[0] << 56) |
60 ((uint64_t)tmp[1] << 48) |
61 ((uint64_t)tmp[2] << 40) |
62 ((uint64_t)tmp[3] << 32) |
63 ((uint64_t)tmp[4] << 24) |
64 ((uint64_t)tmp[5] << 16) |
65 ((uint64_t)tmp[6] << 8) |
66 ((uint64_t)tmp[7]));
67 }
68
69 static inline void
to_be64(void * out,uint64_t in)70 to_be64(void *out, uint64_t in)
71 {
72 uint8_t *tmp = (uint8_t *)out;
73 tmp[0] = (in >> 56) & 0xFF;
74 tmp[1] = (in >> 48) & 0xFF;
75 tmp[2] = (in >> 40) & 0xFF;
76 tmp[3] = (in >> 32) & 0xFF;
77 tmp[4] = (in >> 24) & 0xFF;
78 tmp[5] = (in >> 16) & 0xFF;
79 tmp[6] = (in >> 8) & 0xFF;
80 tmp[7] = in & 0xFF;
81 }
82
83 static inline uint16_t
from_le16(const void * ptr)84 from_le16(const void *ptr)
85 {
86 const uint8_t *tmp = (const uint8_t *)ptr;
87 return (((uint16_t)tmp[1] << 8) | tmp[0]);
88 }
89
90 static inline void
to_le16(void * out,uint16_t in)91 to_le16(void *out, uint16_t in)
92 {
93 uint8_t *tmp = (uint8_t *)out;
94 tmp[1] = (in >> 8) & 0xFF;
95 tmp[0] = in & 0xFF;
96 }
97
98 static inline uint32_t
from_le32(const void * ptr)99 from_le32(const void *ptr)
100 {
101 const uint8_t *tmp = (const uint8_t *)ptr;
102 return (((uint32_t)tmp[3] << 24) |
103 ((uint32_t)tmp[2] << 16) |
104 ((uint32_t)tmp[1] << 8) |
105 ((uint32_t)tmp[0]));
106 }
107
108 static inline void
to_le32(void * out,uint32_t in)109 to_le32(void *out, uint32_t in)
110 {
111 uint8_t *tmp = (uint8_t *)out;
112 tmp[3] = (in >> 24) & 0xFF;
113 tmp[2] = (in >> 16) & 0xFF;
114 tmp[1] = (in >> 8) & 0xFF;
115 tmp[0] = in & 0xFF;
116 }
117
118 static inline uint64_t
from_le64(const void * ptr)119 from_le64(const void *ptr)
120 {
121 const uint8_t *tmp = (const uint8_t *)ptr;
122 return (((uint64_t)tmp[7] << 56) |
123 ((uint64_t)tmp[6] << 48) |
124 ((uint64_t)tmp[5] << 40) |
125 ((uint64_t)tmp[4] << 32) |
126 ((uint64_t)tmp[3] << 24) |
127 ((uint64_t)tmp[2] << 16) |
128 ((uint64_t)tmp[1] << 8) |
129 ((uint64_t)tmp[0]));
130 }
131
132 static inline void
to_le64(void * out,uint64_t in)133 to_le64(void *out, uint64_t in)
134 {
135 uint8_t *tmp = (uint8_t *)out;
136 tmp[7] = (in >> 56) & 0xFF;
137 tmp[6] = (in >> 48) & 0xFF;
138 tmp[5] = (in >> 40) & 0xFF;
139 tmp[4] = (in >> 32) & 0xFF;
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 #ifdef __cplusplus
147 }
148 #endif
149
150 #endif
151