Example
Dependencies: FXAS21002 FXOS8700Q
simple-mbed-cloud-client/mbed-cloud-client/nanostack-libservice/source/libBits/common_functions.c@0:11cc2b7889af, 2019-11-19 (annotated)
- Committer:
- maygup01
- Date:
- Tue Nov 19 09:49:38 2019 +0000
- Revision:
- 0:11cc2b7889af
Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maygup01 | 0:11cc2b7889af | 1 | /* |
maygup01 | 0:11cc2b7889af | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
maygup01 | 0:11cc2b7889af | 3 | * SPDX-License-Identifier: Apache-2.0 |
maygup01 | 0:11cc2b7889af | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
maygup01 | 0:11cc2b7889af | 5 | * not use this file except in compliance with the License. |
maygup01 | 0:11cc2b7889af | 6 | * You may obtain a copy of the License at |
maygup01 | 0:11cc2b7889af | 7 | * |
maygup01 | 0:11cc2b7889af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
maygup01 | 0:11cc2b7889af | 9 | * |
maygup01 | 0:11cc2b7889af | 10 | * Unless required by applicable law or agreed to in writing, software |
maygup01 | 0:11cc2b7889af | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
maygup01 | 0:11cc2b7889af | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maygup01 | 0:11cc2b7889af | 13 | * See the License for the specific language governing permissions and |
maygup01 | 0:11cc2b7889af | 14 | * limitations under the License. |
maygup01 | 0:11cc2b7889af | 15 | */ |
maygup01 | 0:11cc2b7889af | 16 | |
maygup01 | 0:11cc2b7889af | 17 | /* |
maygup01 | 0:11cc2b7889af | 18 | * Most functions can be inlined, and definitions are in common_functions.h. |
maygup01 | 0:11cc2b7889af | 19 | * Define COMMON_FUNCTIONS_FN before including it to generate external definitions. |
maygup01 | 0:11cc2b7889af | 20 | */ |
maygup01 | 0:11cc2b7889af | 21 | #define COMMON_FUNCTIONS_FN extern |
maygup01 | 0:11cc2b7889af | 22 | |
maygup01 | 0:11cc2b7889af | 23 | #include "common_functions.h" |
maygup01 | 0:11cc2b7889af | 24 | |
maygup01 | 0:11cc2b7889af | 25 | #include <string.h> |
maygup01 | 0:11cc2b7889af | 26 | |
maygup01 | 0:11cc2b7889af | 27 | /* Returns mask for <split_value> (0-8) most-significant bits of a byte */ |
maygup01 | 0:11cc2b7889af | 28 | static inline uint8_t context_split_mask(uint_fast8_t split_value) |
maygup01 | 0:11cc2b7889af | 29 | { |
maygup01 | 0:11cc2b7889af | 30 | return (uint8_t) - (0x100u >> split_value); |
maygup01 | 0:11cc2b7889af | 31 | } |
maygup01 | 0:11cc2b7889af | 32 | |
maygup01 | 0:11cc2b7889af | 33 | bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits) |
maygup01 | 0:11cc2b7889af | 34 | { |
maygup01 | 0:11cc2b7889af | 35 | uint_fast8_t bytes = bits / 8; |
maygup01 | 0:11cc2b7889af | 36 | bits %= 8; |
maygup01 | 0:11cc2b7889af | 37 | |
maygup01 | 0:11cc2b7889af | 38 | if (bytes && memcmp(a, b, bytes)) { |
maygup01 | 0:11cc2b7889af | 39 | return false; |
maygup01 | 0:11cc2b7889af | 40 | } |
maygup01 | 0:11cc2b7889af | 41 | |
maygup01 | 0:11cc2b7889af | 42 | if (bits) { |
maygup01 | 0:11cc2b7889af | 43 | uint_fast8_t split_bit = context_split_mask(bits); |
maygup01 | 0:11cc2b7889af | 44 | if ((a[bytes] & split_bit) != (b[bytes] & split_bit)) { |
maygup01 | 0:11cc2b7889af | 45 | return false; |
maygup01 | 0:11cc2b7889af | 46 | } |
maygup01 | 0:11cc2b7889af | 47 | } |
maygup01 | 0:11cc2b7889af | 48 | |
maygup01 | 0:11cc2b7889af | 49 | return true; |
maygup01 | 0:11cc2b7889af | 50 | } |
maygup01 | 0:11cc2b7889af | 51 | |
maygup01 | 0:11cc2b7889af | 52 | uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits) |
maygup01 | 0:11cc2b7889af | 53 | { |
maygup01 | 0:11cc2b7889af | 54 | uint_fast8_t bytes = bits / 8; |
maygup01 | 0:11cc2b7889af | 55 | bits %= 8; |
maygup01 | 0:11cc2b7889af | 56 | |
maygup01 | 0:11cc2b7889af | 57 | if (bytes) { |
maygup01 | 0:11cc2b7889af | 58 | dst = (uint8_t *) memcpy(dst, src, bytes) + bytes; |
maygup01 | 0:11cc2b7889af | 59 | src += bytes; |
maygup01 | 0:11cc2b7889af | 60 | } |
maygup01 | 0:11cc2b7889af | 61 | |
maygup01 | 0:11cc2b7889af | 62 | if (bits) { |
maygup01 | 0:11cc2b7889af | 63 | uint_fast8_t split_bit = context_split_mask(bits); |
maygup01 | 0:11cc2b7889af | 64 | *dst = (*src & split_bit) | (*dst & ~ split_bit); |
maygup01 | 0:11cc2b7889af | 65 | } |
maygup01 | 0:11cc2b7889af | 66 | |
maygup01 | 0:11cc2b7889af | 67 | return dst; |
maygup01 | 0:11cc2b7889af | 68 | } |
maygup01 | 0:11cc2b7889af | 69 | |
maygup01 | 0:11cc2b7889af | 70 | uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits) |
maygup01 | 0:11cc2b7889af | 71 | { |
maygup01 | 0:11cc2b7889af | 72 | uint_fast8_t bytes = bits / 8; |
maygup01 | 0:11cc2b7889af | 73 | bits %= 8; |
maygup01 | 0:11cc2b7889af | 74 | |
maygup01 | 0:11cc2b7889af | 75 | if (bytes) { |
maygup01 | 0:11cc2b7889af | 76 | dst = (uint8_t *) memcpy(dst, src, bytes) + bytes; |
maygup01 | 0:11cc2b7889af | 77 | src += bytes; |
maygup01 | 0:11cc2b7889af | 78 | } |
maygup01 | 0:11cc2b7889af | 79 | |
maygup01 | 0:11cc2b7889af | 80 | if (bits) { |
maygup01 | 0:11cc2b7889af | 81 | uint_fast8_t split_bit = context_split_mask(bits); |
maygup01 | 0:11cc2b7889af | 82 | *dst = (*src & split_bit); |
maygup01 | 0:11cc2b7889af | 83 | } |
maygup01 | 0:11cc2b7889af | 84 | |
maygup01 | 0:11cc2b7889af | 85 | return dst; |
maygup01 | 0:11cc2b7889af | 86 | } |