leo hendrickson
/
S
simple-mbed-cloud-client/mbed-cloud-client/nanostack-libservice/mbed-client-libservice/common_functions.h@0:25fa8795676b, 2021-04-18 (annotated)
- Committer:
- leothedragon
- Date:
- Sun Apr 18 15:20:23 2021 +0000
- Revision:
- 0:25fa8795676b
DS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:25fa8795676b | 1 | /* |
leothedragon | 0:25fa8795676b | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
leothedragon | 0:25fa8795676b | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:25fa8795676b | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:25fa8795676b | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:25fa8795676b | 6 | * You may obtain a copy of the License at |
leothedragon | 0:25fa8795676b | 7 | * |
leothedragon | 0:25fa8795676b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:25fa8795676b | 9 | * |
leothedragon | 0:25fa8795676b | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:25fa8795676b | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:25fa8795676b | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:25fa8795676b | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:25fa8795676b | 14 | * limitations under the License. |
leothedragon | 0:25fa8795676b | 15 | */ |
leothedragon | 0:25fa8795676b | 16 | #ifndef COMMON_FUNCTIONS_H_ |
leothedragon | 0:25fa8795676b | 17 | #define COMMON_FUNCTIONS_H_ |
leothedragon | 0:25fa8795676b | 18 | |
leothedragon | 0:25fa8795676b | 19 | #include "ns_types.h" |
leothedragon | 0:25fa8795676b | 20 | |
leothedragon | 0:25fa8795676b | 21 | #ifdef __cplusplus |
leothedragon | 0:25fa8795676b | 22 | extern "C" { |
leothedragon | 0:25fa8795676b | 23 | #endif |
leothedragon | 0:25fa8795676b | 24 | |
leothedragon | 0:25fa8795676b | 25 | /* |
leothedragon | 0:25fa8795676b | 26 | * Common write 64-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 27 | * |
leothedragon | 0:25fa8795676b | 28 | * Write 64 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 29 | * |
leothedragon | 0:25fa8795676b | 30 | * \param value 64-bit variable |
leothedragon | 0:25fa8795676b | 31 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 32 | * |
leothedragon | 0:25fa8795676b | 33 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 34 | */ |
leothedragon | 0:25fa8795676b | 35 | NS_INLINE uint8_t *common_write_64_bit(uint64_t value, uint8_t ptr[__static 8]); |
leothedragon | 0:25fa8795676b | 36 | |
leothedragon | 0:25fa8795676b | 37 | /* |
leothedragon | 0:25fa8795676b | 38 | * Common read 64-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 39 | * |
leothedragon | 0:25fa8795676b | 40 | * Read 64 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 41 | * |
leothedragon | 0:25fa8795676b | 42 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 43 | * |
leothedragon | 0:25fa8795676b | 44 | * \return 64-bit variable |
leothedragon | 0:25fa8795676b | 45 | */ |
leothedragon | 0:25fa8795676b | 46 | NS_INLINE uint64_t common_read_64_bit(const uint8_t data_buf[__static 8]); |
leothedragon | 0:25fa8795676b | 47 | |
leothedragon | 0:25fa8795676b | 48 | /* |
leothedragon | 0:25fa8795676b | 49 | * Common write 32-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 50 | * |
leothedragon | 0:25fa8795676b | 51 | * Write 32 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 52 | * |
leothedragon | 0:25fa8795676b | 53 | * \param value 32-bit variable |
leothedragon | 0:25fa8795676b | 54 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 55 | * |
leothedragon | 0:25fa8795676b | 56 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 57 | */ |
leothedragon | 0:25fa8795676b | 58 | NS_INLINE uint8_t *common_write_32_bit(uint32_t value, uint8_t ptr[__static 4]); |
leothedragon | 0:25fa8795676b | 59 | |
leothedragon | 0:25fa8795676b | 60 | /* |
leothedragon | 0:25fa8795676b | 61 | * Common read 32-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 62 | * |
leothedragon | 0:25fa8795676b | 63 | * Read 32 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 64 | * |
leothedragon | 0:25fa8795676b | 65 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 66 | * |
leothedragon | 0:25fa8795676b | 67 | * \return 32-bit variable |
leothedragon | 0:25fa8795676b | 68 | */ |
leothedragon | 0:25fa8795676b | 69 | NS_INLINE uint32_t common_read_32_bit(const uint8_t data_buf[__static 4]); |
leothedragon | 0:25fa8795676b | 70 | |
leothedragon | 0:25fa8795676b | 71 | /* |
leothedragon | 0:25fa8795676b | 72 | * Common write 32-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 73 | * |
leothedragon | 0:25fa8795676b | 74 | * Write 32 bits in little-endian byte order. |
leothedragon | 0:25fa8795676b | 75 | * |
leothedragon | 0:25fa8795676b | 76 | * \param value 32-bit variable |
leothedragon | 0:25fa8795676b | 77 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 78 | * |
leothedragon | 0:25fa8795676b | 79 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 80 | */ |
leothedragon | 0:25fa8795676b | 81 | NS_INLINE uint8_t *common_write_32_bit_inverse(uint32_t value, uint8_t ptr[__static 4]); |
leothedragon | 0:25fa8795676b | 82 | |
leothedragon | 0:25fa8795676b | 83 | /* |
leothedragon | 0:25fa8795676b | 84 | * Common read 32-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 85 | * |
leothedragon | 0:25fa8795676b | 86 | * Read 32 bits in little-endian byte order. |
leothedragon | 0:25fa8795676b | 87 | * |
leothedragon | 0:25fa8795676b | 88 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 89 | * |
leothedragon | 0:25fa8795676b | 90 | * \return 32-bit variable |
leothedragon | 0:25fa8795676b | 91 | */ |
leothedragon | 0:25fa8795676b | 92 | NS_INLINE uint32_t common_read_32_bit_inverse(const uint8_t data_buf[__static 4]); |
leothedragon | 0:25fa8795676b | 93 | |
leothedragon | 0:25fa8795676b | 94 | /* |
leothedragon | 0:25fa8795676b | 95 | * Common write 24-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 96 | * |
leothedragon | 0:25fa8795676b | 97 | * Write 24 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 98 | * |
leothedragon | 0:25fa8795676b | 99 | * \param value 24-bit variable |
leothedragon | 0:25fa8795676b | 100 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 101 | * |
leothedragon | 0:25fa8795676b | 102 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 103 | */ |
leothedragon | 0:25fa8795676b | 104 | NS_INLINE uint8_t *common_write_24_bit(uint_fast24_t value, uint8_t ptr[__static 3]); |
leothedragon | 0:25fa8795676b | 105 | |
leothedragon | 0:25fa8795676b | 106 | /* |
leothedragon | 0:25fa8795676b | 107 | * Common read 24-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 108 | * |
leothedragon | 0:25fa8795676b | 109 | * Read 24 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 110 | * |
leothedragon | 0:25fa8795676b | 111 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 112 | * |
leothedragon | 0:25fa8795676b | 113 | * \return 24-bit variable |
leothedragon | 0:25fa8795676b | 114 | */ |
leothedragon | 0:25fa8795676b | 115 | NS_INLINE uint_fast24_t common_read_24_bit(const uint8_t data_buf[__static 3]); |
leothedragon | 0:25fa8795676b | 116 | |
leothedragon | 0:25fa8795676b | 117 | /* |
leothedragon | 0:25fa8795676b | 118 | * Common write 24-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 119 | * |
leothedragon | 0:25fa8795676b | 120 | * Write 24 bits in little-endian byte order. |
leothedragon | 0:25fa8795676b | 121 | * |
leothedragon | 0:25fa8795676b | 122 | * \param value 24-bit variable |
leothedragon | 0:25fa8795676b | 123 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 124 | * |
leothedragon | 0:25fa8795676b | 125 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 126 | */ |
leothedragon | 0:25fa8795676b | 127 | NS_INLINE uint8_t *common_write_24_bit_inverse(uint_fast24_t value, uint8_t ptr[__static 3]); |
leothedragon | 0:25fa8795676b | 128 | |
leothedragon | 0:25fa8795676b | 129 | /* |
leothedragon | 0:25fa8795676b | 130 | * Common read 24-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 131 | * |
leothedragon | 0:25fa8795676b | 132 | * Read 24 bits in little-endian byte order. |
leothedragon | 0:25fa8795676b | 133 | * |
leothedragon | 0:25fa8795676b | 134 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 135 | * |
leothedragon | 0:25fa8795676b | 136 | * \return 24-bit variable |
leothedragon | 0:25fa8795676b | 137 | */ |
leothedragon | 0:25fa8795676b | 138 | NS_INLINE uint_fast24_t common_read_24_bit_inverse(const uint8_t data_buf[__static 3]); |
leothedragon | 0:25fa8795676b | 139 | |
leothedragon | 0:25fa8795676b | 140 | /* |
leothedragon | 0:25fa8795676b | 141 | * Common write 16-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 142 | * |
leothedragon | 0:25fa8795676b | 143 | * Write 16 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 144 | * |
leothedragon | 0:25fa8795676b | 145 | * \param value 16-bit variable |
leothedragon | 0:25fa8795676b | 146 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 147 | * |
leothedragon | 0:25fa8795676b | 148 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 149 | */ |
leothedragon | 0:25fa8795676b | 150 | NS_INLINE uint8_t *common_write_16_bit(uint16_t value, uint8_t ptr[__static 2]); |
leothedragon | 0:25fa8795676b | 151 | |
leothedragon | 0:25fa8795676b | 152 | /* |
leothedragon | 0:25fa8795676b | 153 | * Common read 16-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 154 | * |
leothedragon | 0:25fa8795676b | 155 | * Read 16 bits in big-endian (network) byte order. |
leothedragon | 0:25fa8795676b | 156 | * |
leothedragon | 0:25fa8795676b | 157 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 158 | * |
leothedragon | 0:25fa8795676b | 159 | * \return 16-bit variable |
leothedragon | 0:25fa8795676b | 160 | */ |
leothedragon | 0:25fa8795676b | 161 | NS_INLINE uint16_t common_read_16_bit(const uint8_t data_buf[__static 2]); |
leothedragon | 0:25fa8795676b | 162 | |
leothedragon | 0:25fa8795676b | 163 | /* |
leothedragon | 0:25fa8795676b | 164 | * Common write 16-bit variable to 8-bit pointer. |
leothedragon | 0:25fa8795676b | 165 | * |
leothedragon | 0:25fa8795676b | 166 | * Write 16 bits in little-endian byte order. |
leothedragon | 0:25fa8795676b | 167 | * |
leothedragon | 0:25fa8795676b | 168 | * \param value 16-bit variable |
leothedragon | 0:25fa8795676b | 169 | * \param ptr pointer where data to be written |
leothedragon | 0:25fa8795676b | 170 | * |
leothedragon | 0:25fa8795676b | 171 | * \return updated pointer |
leothedragon | 0:25fa8795676b | 172 | */ |
leothedragon | 0:25fa8795676b | 173 | NS_INLINE uint8_t *common_write_16_bit_inverse(uint16_t value, uint8_t ptr[__static 2]); |
leothedragon | 0:25fa8795676b | 174 | |
leothedragon | 0:25fa8795676b | 175 | /* |
leothedragon | 0:25fa8795676b | 176 | * Common read 16-bit variable from 8-bit pointer. |
leothedragon | 0:25fa8795676b | 177 | * |
leothedragon | 0:25fa8795676b | 178 | * Read 16 bits in little-endian byte order. |
leothedragon | 0:25fa8795676b | 179 | * |
leothedragon | 0:25fa8795676b | 180 | * \param data_buf pointer where data to be read |
leothedragon | 0:25fa8795676b | 181 | * |
leothedragon | 0:25fa8795676b | 182 | * \return 16-bit variable |
leothedragon | 0:25fa8795676b | 183 | */ |
leothedragon | 0:25fa8795676b | 184 | NS_INLINE uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 2]); |
leothedragon | 0:25fa8795676b | 185 | |
leothedragon | 0:25fa8795676b | 186 | /* |
leothedragon | 0:25fa8795676b | 187 | * Count bits in a byte |
leothedragon | 0:25fa8795676b | 188 | * |
leothedragon | 0:25fa8795676b | 189 | * \param value byte to inspect |
leothedragon | 0:25fa8795676b | 190 | * |
leothedragon | 0:25fa8795676b | 191 | * \return number of 1-bits in byte |
leothedragon | 0:25fa8795676b | 192 | */ |
leothedragon | 0:25fa8795676b | 193 | NS_INLINE uint_fast8_t common_count_bits(uint8_t value); |
leothedragon | 0:25fa8795676b | 194 | |
leothedragon | 0:25fa8795676b | 195 | /* |
leothedragon | 0:25fa8795676b | 196 | * Count leading zeros in a byte |
leothedragon | 0:25fa8795676b | 197 | * |
leothedragon | 0:25fa8795676b | 198 | * \deprecated Use common_count_leading_zeros_8 |
leothedragon | 0:25fa8795676b | 199 | * |
leothedragon | 0:25fa8795676b | 200 | * \param value byte to inspect |
leothedragon | 0:25fa8795676b | 201 | * |
leothedragon | 0:25fa8795676b | 202 | * \return number of leading zeros in byte (0-8) |
leothedragon | 0:25fa8795676b | 203 | */ |
leothedragon | 0:25fa8795676b | 204 | NS_INLINE uint_fast8_t common_count_leading_zeros(uint8_t value); |
leothedragon | 0:25fa8795676b | 205 | |
leothedragon | 0:25fa8795676b | 206 | /* |
leothedragon | 0:25fa8795676b | 207 | * Count leading zeros in a byte |
leothedragon | 0:25fa8795676b | 208 | * |
leothedragon | 0:25fa8795676b | 209 | * \param value byte to inspect |
leothedragon | 0:25fa8795676b | 210 | * |
leothedragon | 0:25fa8795676b | 211 | * \return number of leading zeros in byte (0-8) |
leothedragon | 0:25fa8795676b | 212 | */ |
leothedragon | 0:25fa8795676b | 213 | NS_INLINE uint_fast8_t common_count_leading_zeros_8(uint8_t value); |
leothedragon | 0:25fa8795676b | 214 | |
leothedragon | 0:25fa8795676b | 215 | /* |
leothedragon | 0:25fa8795676b | 216 | * Count leading zeros in a 16-bit value |
leothedragon | 0:25fa8795676b | 217 | * |
leothedragon | 0:25fa8795676b | 218 | * \param value value to inspect |
leothedragon | 0:25fa8795676b | 219 | * |
leothedragon | 0:25fa8795676b | 220 | * \return number of leading zeros in byte (0-16) |
leothedragon | 0:25fa8795676b | 221 | */ |
leothedragon | 0:25fa8795676b | 222 | NS_INLINE uint_fast8_t common_count_leading_zeros_16(uint16_t value); |
leothedragon | 0:25fa8795676b | 223 | |
leothedragon | 0:25fa8795676b | 224 | /* |
leothedragon | 0:25fa8795676b | 225 | * Count leading zeros in a 32-bit value |
leothedragon | 0:25fa8795676b | 226 | * |
leothedragon | 0:25fa8795676b | 227 | * \param value value to inspect |
leothedragon | 0:25fa8795676b | 228 | * |
leothedragon | 0:25fa8795676b | 229 | * \return number of leading zeros in byte (0-32) |
leothedragon | 0:25fa8795676b | 230 | */ |
leothedragon | 0:25fa8795676b | 231 | NS_INLINE uint_fast8_t common_count_leading_zeros_32(uint32_t value); |
leothedragon | 0:25fa8795676b | 232 | |
leothedragon | 0:25fa8795676b | 233 | /* |
leothedragon | 0:25fa8795676b | 234 | * Compare 8-bit serial numbers |
leothedragon | 0:25fa8795676b | 235 | * |
leothedragon | 0:25fa8795676b | 236 | * Compare two 8-bit serial numbers, according to RFC 1982 Serial Number |
leothedragon | 0:25fa8795676b | 237 | * Arithmetic. |
leothedragon | 0:25fa8795676b | 238 | * |
leothedragon | 0:25fa8795676b | 239 | * \param s1 first serial number |
leothedragon | 0:25fa8795676b | 240 | * \param s2 second serial number |
leothedragon | 0:25fa8795676b | 241 | * |
leothedragon | 0:25fa8795676b | 242 | * \return true if s1 > s2 |
leothedragon | 0:25fa8795676b | 243 | * \return false if s1 <= s2, or the comparison is undefined |
leothedragon | 0:25fa8795676b | 244 | */ |
leothedragon | 0:25fa8795676b | 245 | NS_INLINE bool common_serial_number_greater_8(uint8_t s1, uint8_t s2); |
leothedragon | 0:25fa8795676b | 246 | |
leothedragon | 0:25fa8795676b | 247 | /* |
leothedragon | 0:25fa8795676b | 248 | * Compare 16-bit serial numbers |
leothedragon | 0:25fa8795676b | 249 | * |
leothedragon | 0:25fa8795676b | 250 | * Compare two 16-bit serial numbers, according to RFC 1982 Serial Number |
leothedragon | 0:25fa8795676b | 251 | * Arithmetic. |
leothedragon | 0:25fa8795676b | 252 | * |
leothedragon | 0:25fa8795676b | 253 | * \param s1 first serial number |
leothedragon | 0:25fa8795676b | 254 | * \param s2 second serial number |
leothedragon | 0:25fa8795676b | 255 | * |
leothedragon | 0:25fa8795676b | 256 | * \return true if s1 > s2 |
leothedragon | 0:25fa8795676b | 257 | * \return false if s1 <= s2, or the comparison is undefined |
leothedragon | 0:25fa8795676b | 258 | */ |
leothedragon | 0:25fa8795676b | 259 | NS_INLINE bool common_serial_number_greater_16(uint16_t s1, uint16_t s2); |
leothedragon | 0:25fa8795676b | 260 | |
leothedragon | 0:25fa8795676b | 261 | /* |
leothedragon | 0:25fa8795676b | 262 | * Compare 32-bit serial numbers |
leothedragon | 0:25fa8795676b | 263 | * |
leothedragon | 0:25fa8795676b | 264 | * Compare two 32-bit serial numbers, according to RFC 1982 Serial Number |
leothedragon | 0:25fa8795676b | 265 | * Arithmetic. |
leothedragon | 0:25fa8795676b | 266 | * |
leothedragon | 0:25fa8795676b | 267 | * \param s1 first serial number |
leothedragon | 0:25fa8795676b | 268 | * \param s2 second serial number |
leothedragon | 0:25fa8795676b | 269 | * |
leothedragon | 0:25fa8795676b | 270 | * \return true if s1 > s2 |
leothedragon | 0:25fa8795676b | 271 | * \return false if s1 <= s2, or the comparison is undefined |
leothedragon | 0:25fa8795676b | 272 | */ |
leothedragon | 0:25fa8795676b | 273 | NS_INLINE bool common_serial_number_greater_32(uint32_t s1, uint32_t s2); |
leothedragon | 0:25fa8795676b | 274 | |
leothedragon | 0:25fa8795676b | 275 | /* |
leothedragon | 0:25fa8795676b | 276 | * Test a bit in an bit array. |
leothedragon | 0:25fa8795676b | 277 | * |
leothedragon | 0:25fa8795676b | 278 | * Check whether a particular bit is set in a bit string. The bit array |
leothedragon | 0:25fa8795676b | 279 | * is in big-endian (network) bit order. |
leothedragon | 0:25fa8795676b | 280 | * |
leothedragon | 0:25fa8795676b | 281 | * \param bitset pointer to bit array |
leothedragon | 0:25fa8795676b | 282 | * \param bit index of bit - 0 is the most significant bit of the first byte |
leothedragon | 0:25fa8795676b | 283 | * |
leothedragon | 0:25fa8795676b | 284 | * \return true if the bit is set |
leothedragon | 0:25fa8795676b | 285 | */ |
leothedragon | 0:25fa8795676b | 286 | NS_INLINE bool bit_test(const uint8_t *bitset, uint_fast8_t bit); |
leothedragon | 0:25fa8795676b | 287 | |
leothedragon | 0:25fa8795676b | 288 | /* |
leothedragon | 0:25fa8795676b | 289 | * Set a bit in an bit array. |
leothedragon | 0:25fa8795676b | 290 | * |
leothedragon | 0:25fa8795676b | 291 | * Set a bit in a bit array. The array is in big-endian (network) bit order. |
leothedragon | 0:25fa8795676b | 292 | * |
leothedragon | 0:25fa8795676b | 293 | * \param bitset pointer to bit array |
leothedragon | 0:25fa8795676b | 294 | * \param bit index of bit - 0 is the most significant bit of the first byte |
leothedragon | 0:25fa8795676b | 295 | */ |
leothedragon | 0:25fa8795676b | 296 | NS_INLINE void bit_set(uint8_t *bitset, uint_fast8_t bit); |
leothedragon | 0:25fa8795676b | 297 | |
leothedragon | 0:25fa8795676b | 298 | /* |
leothedragon | 0:25fa8795676b | 299 | * Clear a bit in an bit array. |
leothedragon | 0:25fa8795676b | 300 | * |
leothedragon | 0:25fa8795676b | 301 | * Clear a bit in a bit array. The bit array is in big-endian (network) bit order. |
leothedragon | 0:25fa8795676b | 302 | * |
leothedragon | 0:25fa8795676b | 303 | * \param bitset pointer to bit array |
leothedragon | 0:25fa8795676b | 304 | * \param bit index of bit - 0 is the most significant bit of the first byte |
leothedragon | 0:25fa8795676b | 305 | */ |
leothedragon | 0:25fa8795676b | 306 | NS_INLINE void bit_clear(uint8_t *bitset, uint_fast8_t bit); |
leothedragon | 0:25fa8795676b | 307 | |
leothedragon | 0:25fa8795676b | 308 | /* |
leothedragon | 0:25fa8795676b | 309 | * Compare two bitstrings. |
leothedragon | 0:25fa8795676b | 310 | * |
leothedragon | 0:25fa8795676b | 311 | * Compare two bitstrings of specified length. The bit strings are in |
leothedragon | 0:25fa8795676b | 312 | * big-endian (network) bit order. |
leothedragon | 0:25fa8795676b | 313 | * |
leothedragon | 0:25fa8795676b | 314 | * \param a pointer to first string |
leothedragon | 0:25fa8795676b | 315 | * \param b pointer to second string |
leothedragon | 0:25fa8795676b | 316 | * \param bits number of bits to compare |
leothedragon | 0:25fa8795676b | 317 | * |
leothedragon | 0:25fa8795676b | 318 | * \return true if the strings compare equal |
leothedragon | 0:25fa8795676b | 319 | */ |
leothedragon | 0:25fa8795676b | 320 | bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits); |
leothedragon | 0:25fa8795676b | 321 | |
leothedragon | 0:25fa8795676b | 322 | /* |
leothedragon | 0:25fa8795676b | 323 | * Copy a bitstring |
leothedragon | 0:25fa8795676b | 324 | * |
leothedragon | 0:25fa8795676b | 325 | * Copy a bitstring of specified length. The bit string is in big-endian |
leothedragon | 0:25fa8795676b | 326 | * (network) bit order. Bits beyond the bitlength at the destination are not |
leothedragon | 0:25fa8795676b | 327 | * modified. |
leothedragon | 0:25fa8795676b | 328 | * |
leothedragon | 0:25fa8795676b | 329 | * For example, copying 4 bits sets the first 4 bits of dst[0] from src[0], |
leothedragon | 0:25fa8795676b | 330 | * the lower 4 bits of dst[0] are unmodified. |
leothedragon | 0:25fa8795676b | 331 | * |
leothedragon | 0:25fa8795676b | 332 | * \param dst destination pointer |
leothedragon | 0:25fa8795676b | 333 | * \param src source pointer |
leothedragon | 0:25fa8795676b | 334 | * \param bits number of bits to copy |
leothedragon | 0:25fa8795676b | 335 | * |
leothedragon | 0:25fa8795676b | 336 | * \return the value of dst |
leothedragon | 0:25fa8795676b | 337 | */ |
leothedragon | 0:25fa8795676b | 338 | uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits); |
leothedragon | 0:25fa8795676b | 339 | |
leothedragon | 0:25fa8795676b | 340 | /* |
leothedragon | 0:25fa8795676b | 341 | * Copy a bitstring and pad last byte with zeros |
leothedragon | 0:25fa8795676b | 342 | * |
leothedragon | 0:25fa8795676b | 343 | * Copy a bitstring of specified length. The bit string is in big-endian |
leothedragon | 0:25fa8795676b | 344 | * (network) bit order. Bits beyond the bitlength in the last destination byte are |
leothedragon | 0:25fa8795676b | 345 | * zeroed. |
leothedragon | 0:25fa8795676b | 346 | * |
leothedragon | 0:25fa8795676b | 347 | * For example, copying 4 bits sets the first 4 bits of dst[0] from src[0], and |
leothedragon | 0:25fa8795676b | 348 | * the lower 4 bits of dst[0] are set to 0. |
leothedragon | 0:25fa8795676b | 349 | * |
leothedragon | 0:25fa8795676b | 350 | * \param dst destination pointer |
leothedragon | 0:25fa8795676b | 351 | * \param src source pointer |
leothedragon | 0:25fa8795676b | 352 | * \param bits number of bits to copy |
leothedragon | 0:25fa8795676b | 353 | * |
leothedragon | 0:25fa8795676b | 354 | * \return the value of dst |
leothedragon | 0:25fa8795676b | 355 | */ |
leothedragon | 0:25fa8795676b | 356 | uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits); |
leothedragon | 0:25fa8795676b | 357 | |
leothedragon | 0:25fa8795676b | 358 | /* Provide definitions, either for inlining, or for common_functions.c */ |
leothedragon | 0:25fa8795676b | 359 | #if defined NS_ALLOW_INLINING || defined COMMON_FUNCTIONS_FN |
leothedragon | 0:25fa8795676b | 360 | #ifndef COMMON_FUNCTIONS_FN |
leothedragon | 0:25fa8795676b | 361 | #define COMMON_FUNCTIONS_FN NS_INLINE |
leothedragon | 0:25fa8795676b | 362 | #endif |
leothedragon | 0:25fa8795676b | 363 | |
leothedragon | 0:25fa8795676b | 364 | COMMON_FUNCTIONS_FN uint8_t *common_write_64_bit(uint64_t value, uint8_t ptr[__static 8]) |
leothedragon | 0:25fa8795676b | 365 | { |
leothedragon | 0:25fa8795676b | 366 | *ptr++ = value >> 56; |
leothedragon | 0:25fa8795676b | 367 | *ptr++ = value >> 48; |
leothedragon | 0:25fa8795676b | 368 | *ptr++ = value >> 40; |
leothedragon | 0:25fa8795676b | 369 | *ptr++ = value >> 32; |
leothedragon | 0:25fa8795676b | 370 | *ptr++ = value >> 24; |
leothedragon | 0:25fa8795676b | 371 | *ptr++ = value >> 16; |
leothedragon | 0:25fa8795676b | 372 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 373 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 374 | return ptr; |
leothedragon | 0:25fa8795676b | 375 | } |
leothedragon | 0:25fa8795676b | 376 | |
leothedragon | 0:25fa8795676b | 377 | COMMON_FUNCTIONS_FN uint64_t common_read_64_bit(const uint8_t data_buf[__static 8]) |
leothedragon | 0:25fa8795676b | 378 | { |
leothedragon | 0:25fa8795676b | 379 | uint64_t temp_64; |
leothedragon | 0:25fa8795676b | 380 | temp_64 = (uint64_t)(*data_buf++) << 56; |
leothedragon | 0:25fa8795676b | 381 | temp_64 += (uint64_t)(*data_buf++) << 48; |
leothedragon | 0:25fa8795676b | 382 | temp_64 += (uint64_t)(*data_buf++) << 40; |
leothedragon | 0:25fa8795676b | 383 | temp_64 += (uint64_t)(*data_buf++) << 32; |
leothedragon | 0:25fa8795676b | 384 | temp_64 += (uint64_t)(*data_buf++) << 24; |
leothedragon | 0:25fa8795676b | 385 | temp_64 += (uint64_t)(*data_buf++) << 16; |
leothedragon | 0:25fa8795676b | 386 | temp_64 += (uint64_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 387 | temp_64 += *data_buf++; |
leothedragon | 0:25fa8795676b | 388 | return temp_64; |
leothedragon | 0:25fa8795676b | 389 | } |
leothedragon | 0:25fa8795676b | 390 | |
leothedragon | 0:25fa8795676b | 391 | COMMON_FUNCTIONS_FN uint8_t *common_write_32_bit(uint32_t value, uint8_t ptr[__static 4]) |
leothedragon | 0:25fa8795676b | 392 | { |
leothedragon | 0:25fa8795676b | 393 | *ptr++ = value >> 24; |
leothedragon | 0:25fa8795676b | 394 | *ptr++ = value >> 16; |
leothedragon | 0:25fa8795676b | 395 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 396 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 397 | return ptr; |
leothedragon | 0:25fa8795676b | 398 | } |
leothedragon | 0:25fa8795676b | 399 | |
leothedragon | 0:25fa8795676b | 400 | COMMON_FUNCTIONS_FN uint32_t common_read_32_bit(const uint8_t data_buf[__static 4]) |
leothedragon | 0:25fa8795676b | 401 | { |
leothedragon | 0:25fa8795676b | 402 | uint32_t temp_32; |
leothedragon | 0:25fa8795676b | 403 | temp_32 = (uint32_t)(*data_buf++) << 24; |
leothedragon | 0:25fa8795676b | 404 | temp_32 += (uint32_t)(*data_buf++) << 16; |
leothedragon | 0:25fa8795676b | 405 | temp_32 += (uint32_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 406 | temp_32 += *data_buf++; |
leothedragon | 0:25fa8795676b | 407 | return temp_32; |
leothedragon | 0:25fa8795676b | 408 | } |
leothedragon | 0:25fa8795676b | 409 | |
leothedragon | 0:25fa8795676b | 410 | COMMON_FUNCTIONS_FN uint8_t *common_write_32_bit_inverse(uint32_t value, uint8_t ptr[__static 4]) |
leothedragon | 0:25fa8795676b | 411 | { |
leothedragon | 0:25fa8795676b | 412 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 413 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 414 | *ptr++ = value >> 16; |
leothedragon | 0:25fa8795676b | 415 | *ptr++ = value >> 24; |
leothedragon | 0:25fa8795676b | 416 | return ptr; |
leothedragon | 0:25fa8795676b | 417 | } |
leothedragon | 0:25fa8795676b | 418 | |
leothedragon | 0:25fa8795676b | 419 | COMMON_FUNCTIONS_FN uint32_t common_read_32_bit_inverse(const uint8_t data_buf[__static 4]) |
leothedragon | 0:25fa8795676b | 420 | { |
leothedragon | 0:25fa8795676b | 421 | uint32_t temp_32; |
leothedragon | 0:25fa8795676b | 422 | temp_32 = *data_buf++; |
leothedragon | 0:25fa8795676b | 423 | temp_32 += (uint32_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 424 | temp_32 += (uint32_t)(*data_buf++) << 16; |
leothedragon | 0:25fa8795676b | 425 | temp_32 += (uint32_t)(*data_buf++) << 24; |
leothedragon | 0:25fa8795676b | 426 | return temp_32; |
leothedragon | 0:25fa8795676b | 427 | } |
leothedragon | 0:25fa8795676b | 428 | |
leothedragon | 0:25fa8795676b | 429 | COMMON_FUNCTIONS_FN uint8_t *common_write_24_bit(uint_fast24_t value, uint8_t ptr[__static 3]) |
leothedragon | 0:25fa8795676b | 430 | { |
leothedragon | 0:25fa8795676b | 431 | *ptr++ = value >> 16; |
leothedragon | 0:25fa8795676b | 432 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 433 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 434 | return ptr; |
leothedragon | 0:25fa8795676b | 435 | } |
leothedragon | 0:25fa8795676b | 436 | |
leothedragon | 0:25fa8795676b | 437 | COMMON_FUNCTIONS_FN uint_fast24_t common_read_24_bit(const uint8_t data_buf[__static 3]) |
leothedragon | 0:25fa8795676b | 438 | { |
leothedragon | 0:25fa8795676b | 439 | uint_fast24_t temp_24; |
leothedragon | 0:25fa8795676b | 440 | temp_24 = (uint_fast24_t)(*data_buf++) << 16; |
leothedragon | 0:25fa8795676b | 441 | temp_24 += (uint_fast24_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 442 | temp_24 += *data_buf++; |
leothedragon | 0:25fa8795676b | 443 | return temp_24; |
leothedragon | 0:25fa8795676b | 444 | } |
leothedragon | 0:25fa8795676b | 445 | |
leothedragon | 0:25fa8795676b | 446 | COMMON_FUNCTIONS_FN uint8_t *common_write_24_bit_inverse(uint_fast24_t value, uint8_t ptr[__static 3]) |
leothedragon | 0:25fa8795676b | 447 | { |
leothedragon | 0:25fa8795676b | 448 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 449 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 450 | *ptr++ = value >> 16; |
leothedragon | 0:25fa8795676b | 451 | return ptr; |
leothedragon | 0:25fa8795676b | 452 | } |
leothedragon | 0:25fa8795676b | 453 | |
leothedragon | 0:25fa8795676b | 454 | COMMON_FUNCTIONS_FN uint_fast24_t common_read_24_bit_inverse(const uint8_t data_buf[__static 3]) |
leothedragon | 0:25fa8795676b | 455 | { |
leothedragon | 0:25fa8795676b | 456 | uint_fast24_t temp_24; |
leothedragon | 0:25fa8795676b | 457 | temp_24 = *data_buf++; |
leothedragon | 0:25fa8795676b | 458 | temp_24 += (uint_fast24_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 459 | temp_24 += (uint_fast24_t)(*data_buf++) << 16; |
leothedragon | 0:25fa8795676b | 460 | return temp_24; |
leothedragon | 0:25fa8795676b | 461 | } |
leothedragon | 0:25fa8795676b | 462 | |
leothedragon | 0:25fa8795676b | 463 | COMMON_FUNCTIONS_FN uint8_t *common_write_16_bit(uint16_t value, uint8_t ptr[__static 2]) |
leothedragon | 0:25fa8795676b | 464 | { |
leothedragon | 0:25fa8795676b | 465 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 466 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 467 | return ptr; |
leothedragon | 0:25fa8795676b | 468 | } |
leothedragon | 0:25fa8795676b | 469 | |
leothedragon | 0:25fa8795676b | 470 | COMMON_FUNCTIONS_FN uint16_t common_read_16_bit(const uint8_t data_buf[__static 2]) |
leothedragon | 0:25fa8795676b | 471 | { |
leothedragon | 0:25fa8795676b | 472 | uint16_t temp_16; |
leothedragon | 0:25fa8795676b | 473 | temp_16 = (uint16_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 474 | temp_16 += *data_buf++; |
leothedragon | 0:25fa8795676b | 475 | return temp_16; |
leothedragon | 0:25fa8795676b | 476 | } |
leothedragon | 0:25fa8795676b | 477 | |
leothedragon | 0:25fa8795676b | 478 | COMMON_FUNCTIONS_FN uint8_t *common_write_16_bit_inverse(uint16_t value, uint8_t ptr[__static 2]) |
leothedragon | 0:25fa8795676b | 479 | { |
leothedragon | 0:25fa8795676b | 480 | *ptr++ = value; |
leothedragon | 0:25fa8795676b | 481 | *ptr++ = value >> 8; |
leothedragon | 0:25fa8795676b | 482 | return ptr; |
leothedragon | 0:25fa8795676b | 483 | } |
leothedragon | 0:25fa8795676b | 484 | |
leothedragon | 0:25fa8795676b | 485 | COMMON_FUNCTIONS_FN uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 2]) |
leothedragon | 0:25fa8795676b | 486 | { |
leothedragon | 0:25fa8795676b | 487 | uint16_t temp_16; |
leothedragon | 0:25fa8795676b | 488 | temp_16 = *data_buf++; |
leothedragon | 0:25fa8795676b | 489 | temp_16 += (uint16_t)(*data_buf++) << 8; |
leothedragon | 0:25fa8795676b | 490 | return temp_16; |
leothedragon | 0:25fa8795676b | 491 | } |
leothedragon | 0:25fa8795676b | 492 | |
leothedragon | 0:25fa8795676b | 493 | COMMON_FUNCTIONS_FN uint_fast8_t common_count_bits(uint8_t value) |
leothedragon | 0:25fa8795676b | 494 | { |
leothedragon | 0:25fa8795676b | 495 | /* First step sets each bit pair to be count of bits (00,01,10) */ |
leothedragon | 0:25fa8795676b | 496 | /* [00-00 = 00, 01-00 = 01, 10-01 = 01, 11-01 = 10] */ |
leothedragon | 0:25fa8795676b | 497 | uint_fast8_t count = value - ((value >> 1) & 0x55); |
leothedragon | 0:25fa8795676b | 498 | /* Add bit pairs to make each nibble contain count of bits (0-4) */ |
leothedragon | 0:25fa8795676b | 499 | count = (count & 0x33) + ((count >> 2) & 0x33); |
leothedragon | 0:25fa8795676b | 500 | /* Final result is sum of nibbles (0-8) */ |
leothedragon | 0:25fa8795676b | 501 | count = (count >> 4) + (count & 0x0F); |
leothedragon | 0:25fa8795676b | 502 | return count; |
leothedragon | 0:25fa8795676b | 503 | } |
leothedragon | 0:25fa8795676b | 504 | |
leothedragon | 0:25fa8795676b | 505 | COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros(uint8_t value) |
leothedragon | 0:25fa8795676b | 506 | { |
leothedragon | 0:25fa8795676b | 507 | return common_count_leading_zeros_8(value); |
leothedragon | 0:25fa8795676b | 508 | } |
leothedragon | 0:25fa8795676b | 509 | |
leothedragon | 0:25fa8795676b | 510 | COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_8(uint8_t value) |
leothedragon | 0:25fa8795676b | 511 | { |
leothedragon | 0:25fa8795676b | 512 | #ifdef __CC_ARM |
leothedragon | 0:25fa8795676b | 513 | return value ? __clz((unsigned int) value << 24) : 8; |
leothedragon | 0:25fa8795676b | 514 | #elif defined __GNUC__ |
leothedragon | 0:25fa8795676b | 515 | return value ? __builtin_clz((unsigned int) value << 24) : 8; |
leothedragon | 0:25fa8795676b | 516 | #else |
leothedragon | 0:25fa8795676b | 517 | uint_fast8_t cnt = 0; |
leothedragon | 0:25fa8795676b | 518 | if (value == 0) { |
leothedragon | 0:25fa8795676b | 519 | return 8; |
leothedragon | 0:25fa8795676b | 520 | } |
leothedragon | 0:25fa8795676b | 521 | if ((value & 0xF0) == 0) { |
leothedragon | 0:25fa8795676b | 522 | value <<= 4; |
leothedragon | 0:25fa8795676b | 523 | cnt += 4; |
leothedragon | 0:25fa8795676b | 524 | } |
leothedragon | 0:25fa8795676b | 525 | if ((value & 0xC0) == 0) { |
leothedragon | 0:25fa8795676b | 526 | value <<= 2; |
leothedragon | 0:25fa8795676b | 527 | cnt += 2; |
leothedragon | 0:25fa8795676b | 528 | } |
leothedragon | 0:25fa8795676b | 529 | if ((value & 0x80) == 0) { |
leothedragon | 0:25fa8795676b | 530 | cnt += 1; |
leothedragon | 0:25fa8795676b | 531 | } |
leothedragon | 0:25fa8795676b | 532 | |
leothedragon | 0:25fa8795676b | 533 | return cnt; |
leothedragon | 0:25fa8795676b | 534 | #endif |
leothedragon | 0:25fa8795676b | 535 | } |
leothedragon | 0:25fa8795676b | 536 | |
leothedragon | 0:25fa8795676b | 537 | COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_16(uint16_t value) |
leothedragon | 0:25fa8795676b | 538 | { |
leothedragon | 0:25fa8795676b | 539 | #ifdef __CC_ARM |
leothedragon | 0:25fa8795676b | 540 | return value ? __clz((unsigned int) value << 16) : 16; |
leothedragon | 0:25fa8795676b | 541 | #elif defined __GNUC__ |
leothedragon | 0:25fa8795676b | 542 | return value ? __builtin_clz((unsigned int) value << 16) : 16; |
leothedragon | 0:25fa8795676b | 543 | #else |
leothedragon | 0:25fa8795676b | 544 | uint_fast8_t cnt = 0; |
leothedragon | 0:25fa8795676b | 545 | if (value == 0) { |
leothedragon | 0:25fa8795676b | 546 | return 16; |
leothedragon | 0:25fa8795676b | 547 | } |
leothedragon | 0:25fa8795676b | 548 | if ((value & 0xFF00) == 0) { |
leothedragon | 0:25fa8795676b | 549 | value <<= 8; |
leothedragon | 0:25fa8795676b | 550 | cnt += 8; |
leothedragon | 0:25fa8795676b | 551 | } |
leothedragon | 0:25fa8795676b | 552 | if ((value & 0xF000) == 0) { |
leothedragon | 0:25fa8795676b | 553 | value <<= 4; |
leothedragon | 0:25fa8795676b | 554 | cnt += 4; |
leothedragon | 0:25fa8795676b | 555 | } |
leothedragon | 0:25fa8795676b | 556 | if ((value & 0xC000) == 0) { |
leothedragon | 0:25fa8795676b | 557 | value <<= 2; |
leothedragon | 0:25fa8795676b | 558 | cnt += 2; |
leothedragon | 0:25fa8795676b | 559 | } |
leothedragon | 0:25fa8795676b | 560 | if ((value & 0x8000) == 0) { |
leothedragon | 0:25fa8795676b | 561 | cnt += 1; |
leothedragon | 0:25fa8795676b | 562 | } |
leothedragon | 0:25fa8795676b | 563 | |
leothedragon | 0:25fa8795676b | 564 | return cnt; |
leothedragon | 0:25fa8795676b | 565 | #endif |
leothedragon | 0:25fa8795676b | 566 | } |
leothedragon | 0:25fa8795676b | 567 | |
leothedragon | 0:25fa8795676b | 568 | COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_32(uint32_t value) |
leothedragon | 0:25fa8795676b | 569 | { |
leothedragon | 0:25fa8795676b | 570 | #ifdef __CC_ARM |
leothedragon | 0:25fa8795676b | 571 | return __clz(value); |
leothedragon | 0:25fa8795676b | 572 | #elif defined __GNUC__ |
leothedragon | 0:25fa8795676b | 573 | return value ? __builtin_clz(value) : 32; |
leothedragon | 0:25fa8795676b | 574 | #else |
leothedragon | 0:25fa8795676b | 575 | uint_fast8_t cnt = 0; |
leothedragon | 0:25fa8795676b | 576 | if (value == 0) { |
leothedragon | 0:25fa8795676b | 577 | return 32; |
leothedragon | 0:25fa8795676b | 578 | } |
leothedragon | 0:25fa8795676b | 579 | if ((value & 0xFFFF0000) == 0) { |
leothedragon | 0:25fa8795676b | 580 | value <<= 16; |
leothedragon | 0:25fa8795676b | 581 | cnt += 16; |
leothedragon | 0:25fa8795676b | 582 | } |
leothedragon | 0:25fa8795676b | 583 | if ((value & 0xFF000000) == 0) { |
leothedragon | 0:25fa8795676b | 584 | value <<= 8; |
leothedragon | 0:25fa8795676b | 585 | cnt += 8; |
leothedragon | 0:25fa8795676b | 586 | } |
leothedragon | 0:25fa8795676b | 587 | if ((value & 0xF0000000) == 0) { |
leothedragon | 0:25fa8795676b | 588 | value <<= 4; |
leothedragon | 0:25fa8795676b | 589 | cnt += 4; |
leothedragon | 0:25fa8795676b | 590 | } |
leothedragon | 0:25fa8795676b | 591 | if ((value & 0xC0000000) == 0) { |
leothedragon | 0:25fa8795676b | 592 | value <<= 2; |
leothedragon | 0:25fa8795676b | 593 | cnt += 2; |
leothedragon | 0:25fa8795676b | 594 | } |
leothedragon | 0:25fa8795676b | 595 | if ((value & 0x80000000) == 0) { |
leothedragon | 0:25fa8795676b | 596 | cnt += 1; |
leothedragon | 0:25fa8795676b | 597 | } |
leothedragon | 0:25fa8795676b | 598 | |
leothedragon | 0:25fa8795676b | 599 | return cnt; |
leothedragon | 0:25fa8795676b | 600 | #endif |
leothedragon | 0:25fa8795676b | 601 | } |
leothedragon | 0:25fa8795676b | 602 | |
leothedragon | 0:25fa8795676b | 603 | COMMON_FUNCTIONS_FN bool common_serial_number_greater_8(uint8_t s1, uint8_t s2) |
leothedragon | 0:25fa8795676b | 604 | { |
leothedragon | 0:25fa8795676b | 605 | return (s1 > s2 && s1 - s2 < UINT8_C(0x80)) || (s1 < s2 && s2 - s1 > UINT8_C(0x80)); |
leothedragon | 0:25fa8795676b | 606 | } |
leothedragon | 0:25fa8795676b | 607 | |
leothedragon | 0:25fa8795676b | 608 | COMMON_FUNCTIONS_FN bool common_serial_number_greater_16(uint16_t s1, uint16_t s2) |
leothedragon | 0:25fa8795676b | 609 | { |
leothedragon | 0:25fa8795676b | 610 | return (s1 > s2 && s1 - s2 < UINT16_C(0x8000)) || (s1 < s2 && s2 - s1 > UINT16_C(0x8000)); |
leothedragon | 0:25fa8795676b | 611 | } |
leothedragon | 0:25fa8795676b | 612 | |
leothedragon | 0:25fa8795676b | 613 | COMMON_FUNCTIONS_FN bool common_serial_number_greater_32(uint32_t s1, uint32_t s2) |
leothedragon | 0:25fa8795676b | 614 | { |
leothedragon | 0:25fa8795676b | 615 | return (s1 > s2 && s1 - s2 < UINT32_C(0x80000000)) || (s1 < s2 && s2 - s1 > UINT32_C(0x80000000)); |
leothedragon | 0:25fa8795676b | 616 | } |
leothedragon | 0:25fa8795676b | 617 | |
leothedragon | 0:25fa8795676b | 618 | COMMON_FUNCTIONS_FN bool bit_test(const uint8_t *bitset, uint_fast8_t bit) |
leothedragon | 0:25fa8795676b | 619 | { |
leothedragon | 0:25fa8795676b | 620 | return bitset[bit >> 3] & (0x80 >> (bit & 7)); |
leothedragon | 0:25fa8795676b | 621 | } |
leothedragon | 0:25fa8795676b | 622 | |
leothedragon | 0:25fa8795676b | 623 | COMMON_FUNCTIONS_FN void bit_set(uint8_t *bitset, uint_fast8_t bit) |
leothedragon | 0:25fa8795676b | 624 | { |
leothedragon | 0:25fa8795676b | 625 | bitset[bit >> 3] |= (0x80 >> (bit & 7)); |
leothedragon | 0:25fa8795676b | 626 | } |
leothedragon | 0:25fa8795676b | 627 | |
leothedragon | 0:25fa8795676b | 628 | COMMON_FUNCTIONS_FN void bit_clear(uint8_t *bitset, uint_fast8_t bit) |
leothedragon | 0:25fa8795676b | 629 | { |
leothedragon | 0:25fa8795676b | 630 | bitset[bit >> 3] &= ~(0x80 >> (bit & 7)); |
leothedragon | 0:25fa8795676b | 631 | } |
leothedragon | 0:25fa8795676b | 632 | |
leothedragon | 0:25fa8795676b | 633 | #endif /* defined NS_ALLOW_INLINING || defined COMMON_FUNCTIONS_FN */ |
leothedragon | 0:25fa8795676b | 634 | |
leothedragon | 0:25fa8795676b | 635 | #ifdef __cplusplus |
leothedragon | 0:25fa8795676b | 636 | } |
leothedragon | 0:25fa8795676b | 637 | #endif |
leothedragon | 0:25fa8795676b | 638 | #endif /*__COMMON_FUNCTIONS_H_*/ |