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