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