cpu_endian.c
Go to the documentation of this file.
1 /**
2  * @file cpu_endian.c
3  * @brief Byte order conversion
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 2.4.4
27  **/
28 
29 //Dependencies
30 #include "cpu_endian.h"
31 
32 
33 /**
34  * @brief Reverse the byte order of a 16-bit word
35  * @param[in] value 16-bit value
36  * @return 16-bit value with byte order swapped
37  **/
38 
39 uint16_t swapInt16(uint16_t value)
40 {
41  return SWAPINT16(value);
42 }
43 
44 
45 /**
46  * @brief Reverse the byte order of a 32-bit word
47  * @param[in] value 32-bit value
48  * @return 32-bit value with byte order swapped
49  **/
50 
51 uint32_t swapInt32(uint32_t value)
52 {
53  return SWAPINT32(value);
54 }
55 
56 
57 /**
58  * @brief Reverse the byte order of a 64-bit word
59  * @param[in] value 64-bit value
60  * @return 64-bit value with byte order swapped
61  **/
62 
63 uint64_t swapInt64(uint64_t value)
64 {
65  return SWAPINT64(value);
66 }
67 
68 
69 /**
70  * @brief Reverse bit order in a 4-bit word
71  * @param[in] value 4-bit value
72  * @return 4-bit value with bit order reversed
73  **/
74 
75 uint8_t reverseInt4(uint8_t value)
76 {
77  value = ((value & 0x0C) >> 2) | ((value & 0x03) << 2);
78  value = ((value & 0x0A) >> 1) | ((value & 0x05) << 1);
79 
80  return value;
81 }
82 
83 
84 /**
85  * @brief Reverse bit order in a byte
86  * @param[in] value 8-bit value
87  * @return 8-bit value with bit order reversed
88  **/
89 
90 uint8_t reverseInt8(uint8_t value)
91 {
92  value = ((value & 0xF0) >> 4) | ((value & 0x0F) << 4);
93  value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
94  value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
95 
96  return value;
97 }
98 
99 
100 /**
101  * @brief Reverse bit order in a 16-bit word
102  * @param[in] value 16-bit value
103  * @return 16-bit value with bit order reversed
104  **/
105 
106 uint16_t reverseInt16(uint16_t value)
107 {
108  value = ((value & 0xFF00) >> 8) | ((value & 0x00FF) << 8);
109  value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4);
110  value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2);
111  value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1);
112 
113  return value;
114 }
115 
116 
117 /**
118  * @brief Reverse bit order in a 32-bit word
119  * @param[in] value 32-bit value
120  * @return 32-bit value with bit order reversed
121  **/
122 
123 uint32_t reverseInt32(uint32_t value)
124 {
125  value = ((value & 0xFFFF0000UL) >> 16) | ((value & 0x0000FFFFUL) << 16);
126  value = ((value & 0xFF00FF00UL) >> 8) | ((value & 0x00FF00FFUL) << 8);
127  value = ((value & 0xF0F0F0F0UL) >> 4) | ((value & 0x0F0F0F0FUL) << 4);
128  value = ((value & 0xCCCCCCCCUL) >> 2) | ((value & 0x33333333UL) << 2);
129  value = ((value & 0xAAAAAAAAUL) >> 1) | ((value & 0x55555555UL) << 1);
130 
131  return value;
132 }
133 
134 
135 /**
136  * @brief Reverse bit order in a 64-bit word
137  * @param[in] value 64-bit value
138  * @return 64-bit value with bit order reversed
139  **/
140 
141 uint64_t reverseInt64(uint64_t value)
142 {
143  value = ((value & 0xFFFFFFFF00000000ULL) >> 32) | ((value & 0x00000000FFFFFFFFULL) << 32);
144  value = ((value & 0xFFFF0000FFFF0000ULL) >> 16) | ((value & 0x0000FFFF0000FFFFULL) << 16);
145  value = ((value & 0xFF00FF00FF00FF00ULL) >> 8) | ((value & 0x00FF00FF00FF00FFULL) << 8);
146  value = ((value & 0xF0F0F0F0F0F0F0F0ULL) >> 4) | ((value & 0x0F0F0F0F0F0F0F0FULL) << 4);
147  value = ((value & 0xCCCCCCCCCCCCCCCCULL) >> 2) | ((value & 0x3333333333333333ULL) << 2);
148  value = ((value & 0xAAAAAAAAAAAAAAAAULL) >> 1) | ((value & 0x5555555555555555ULL) << 1);
149 
150  return value;
151 }
uint64_t swapInt64(uint64_t value)
Reverse the byte order of a 64-bit word.
Definition: cpu_endian.c:63
#define SWAPINT32(x)
Definition: cpu_endian.h:338
uint16_t reverseInt16(uint16_t value)
Reverse bit order in a 16-bit word.
Definition: cpu_endian.c:106
uint32_t swapInt32(uint32_t value)
Reverse the byte order of a 32-bit word.
Definition: cpu_endian.c:51
Byte order conversion.
uint16_t swapInt16(uint16_t value)
Reverse the byte order of a 16-bit word.
Definition: cpu_endian.c:39
uint8_t value[]
Definition: tcp.h:369
uint8_t reverseInt8(uint8_t value)
Reverse bit order in a byte.
Definition: cpu_endian.c:90
uint64_t reverseInt64(uint64_t value)
Reverse bit order in a 64-bit word.
Definition: cpu_endian.c:141
uint32_t reverseInt32(uint32_t value)
Reverse bit order in a 32-bit word.
Definition: cpu_endian.c:123
uint8_t reverseInt4(uint8_t value)
Reverse bit order in a 4-bit word.
Definition: cpu_endian.c:75
#define SWAPINT64(x)
Definition: cpu_endian.h:345
#define SWAPINT16(x)
Definition: cpu_endian.h:333