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