Committer:
leothedragon
Date:
Sun Apr 18 15:20:23 2021 +0000
Revision:
0:25fa8795676b
DS

Who changed what in which revision?

UserRevisionLine numberNew 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
leothedragon 0:25fa8795676b 17 /*
leothedragon 0:25fa8795676b 18 * Most functions can be inlined, and definitions are in common_functions.h.
leothedragon 0:25fa8795676b 19 * Define COMMON_FUNCTIONS_FN before including it to generate external definitions.
leothedragon 0:25fa8795676b 20 */
leothedragon 0:25fa8795676b 21 #define COMMON_FUNCTIONS_FN extern
leothedragon 0:25fa8795676b 22
leothedragon 0:25fa8795676b 23 #include "common_functions.h"
leothedragon 0:25fa8795676b 24
leothedragon 0:25fa8795676b 25 #include <string.h>
leothedragon 0:25fa8795676b 26
leothedragon 0:25fa8795676b 27 /* Returns mask for <split_value> (0-8) most-significant bits of a byte */
leothedragon 0:25fa8795676b 28 static inline uint8_t context_split_mask(uint_fast8_t split_value)
leothedragon 0:25fa8795676b 29 {
leothedragon 0:25fa8795676b 30 return (uint8_t) - (0x100u >> split_value);
leothedragon 0:25fa8795676b 31 }
leothedragon 0:25fa8795676b 32
leothedragon 0:25fa8795676b 33 bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits)
leothedragon 0:25fa8795676b 34 {
leothedragon 0:25fa8795676b 35 uint_fast8_t bytes = bits / 8;
leothedragon 0:25fa8795676b 36 bits %= 8;
leothedragon 0:25fa8795676b 37
leothedragon 0:25fa8795676b 38 if (bytes && memcmp(a, b, bytes)) {
leothedragon 0:25fa8795676b 39 return false;
leothedragon 0:25fa8795676b 40 }
leothedragon 0:25fa8795676b 41
leothedragon 0:25fa8795676b 42 if (bits) {
leothedragon 0:25fa8795676b 43 uint_fast8_t split_bit = context_split_mask(bits);
leothedragon 0:25fa8795676b 44 if ((a[bytes] & split_bit) != (b[bytes] & split_bit)) {
leothedragon 0:25fa8795676b 45 return false;
leothedragon 0:25fa8795676b 46 }
leothedragon 0:25fa8795676b 47 }
leothedragon 0:25fa8795676b 48
leothedragon 0:25fa8795676b 49 return true;
leothedragon 0:25fa8795676b 50 }
leothedragon 0:25fa8795676b 51
leothedragon 0:25fa8795676b 52 uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits)
leothedragon 0:25fa8795676b 53 {
leothedragon 0:25fa8795676b 54 uint_fast8_t bytes = bits / 8;
leothedragon 0:25fa8795676b 55 bits %= 8;
leothedragon 0:25fa8795676b 56
leothedragon 0:25fa8795676b 57 if (bytes) {
leothedragon 0:25fa8795676b 58 dst = (uint8_t *) memcpy(dst, src, bytes) + bytes;
leothedragon 0:25fa8795676b 59 src += bytes;
leothedragon 0:25fa8795676b 60 }
leothedragon 0:25fa8795676b 61
leothedragon 0:25fa8795676b 62 if (bits) {
leothedragon 0:25fa8795676b 63 uint_fast8_t split_bit = context_split_mask(bits);
leothedragon 0:25fa8795676b 64 *dst = (*src & split_bit) | (*dst & ~ split_bit);
leothedragon 0:25fa8795676b 65 }
leothedragon 0:25fa8795676b 66
leothedragon 0:25fa8795676b 67 return dst;
leothedragon 0:25fa8795676b 68 }
leothedragon 0:25fa8795676b 69
leothedragon 0:25fa8795676b 70 uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits)
leothedragon 0:25fa8795676b 71 {
leothedragon 0:25fa8795676b 72 uint_fast8_t bytes = bits / 8;
leothedragon 0:25fa8795676b 73 bits %= 8;
leothedragon 0:25fa8795676b 74
leothedragon 0:25fa8795676b 75 if (bytes) {
leothedragon 0:25fa8795676b 76 dst = (uint8_t *) memcpy(dst, src, bytes) + bytes;
leothedragon 0:25fa8795676b 77 src += bytes;
leothedragon 0:25fa8795676b 78 }
leothedragon 0:25fa8795676b 79
leothedragon 0:25fa8795676b 80 if (bits) {
leothedragon 0:25fa8795676b 81 uint_fast8_t split_bit = context_split_mask(bits);
leothedragon 0:25fa8795676b 82 *dst = (*src & split_bit);
leothedragon 0:25fa8795676b 83 }
leothedragon 0:25fa8795676b 84
leothedragon 0:25fa8795676b 85 return dst;
leothedragon 0:25fa8795676b 86 }