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