acc_alg_basic_utils.h
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2016-2024
2 // All rights reserved
3
4 #ifndef ACC_ALG_BASIC_UTILS_H_
5 #define ACC_ALG_BASIC_UTILS_H_
6
7 #include <stdbool.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #define ACC_ALG_SPEED_OF_LIGHT 299792458.0f
12
13 #ifndef M_PI
14 #define M_PI 3.14159265358979323846
15 #endif
16
17 /*
18 * The mathematical constant pi.
19 */
20 #define ACC_ALG_BASIC_MATH_PI M_PI
21
22 /**
23 * @brief Calculate CRC32 checksum on byte array
24 *
25 * @param[in] input byte array
26 * @param[in] len Length of byte array
27 *
28 * @return CRC32 checksum
29 */
30 uint32_t acc_alg_basic_util_crc32(const uint8_t *input, size_t len);
31
32 /**
33 * @brief Calculate length of 32-bit array to contain size number of bits
34 *
35 * @param number_of_bits Number of bits to contain in bit array
36 * @return Length of 32-bit array
37 */
38 static inline size_t acc_alg_basic_utils_calculate_length_of_bitarray_uint32(size_t number_of_bits)
39 {
40 return (number_of_bits + (32U - 1U)) / 32U;
41 }
42
43 /**
44 * @brief Set bit in bit array
45 *
46 * @param[in, out] bitarray Array to set bit in
47 * @param[in] bit_index Index of bit to set
48 */
49 static inline void acc_alg_basic_utils_set_bit_bitarray_uint32(uint32_t *bitarray, size_t bit_index)
50 {
51 bitarray[bit_index / 32U] |= (uint32_t)1U << (bit_index & 0x1FU);
52 }
53
54 /**
55 * @brief Clear bit in bit array
56 *
57 * @param[in, out] bitarray Array to clear bit in
58 * @param[in] bit_index Index of bit to clear
59 */
60 static inline void acc_alg_basic_utils_clear_bit_bitarray_uint32(uint32_t *bitarray, size_t bit_index)
61 {
62 bitarray[bit_index / 32U] &= ~((uint32_t)1U << (bit_index & 0x1FU));
63 }
64
65 /**
66 * @brief Check if bit is set in bit array
67 *
68 * @param[in] bitarray Array to check bit in
69 * @param[in] bit_index Index of bit to check
70 * @return True if bit is set
71 */
72 static inline bool acc_alg_basic_utils_is_bit_set_bitarray_uint32(const uint32_t *bitarray, size_t bit_index)
73 {
74 return (bitarray[bit_index / 32U] & ((uint32_t)1U << (bit_index & 0x1FU))) != 0U;
75 }
76
77 #endif
acc_alg_basic_util_crc32
uint32_t acc_alg_basic_util_crc32(const uint8_t *input, size_t len)
Calculate CRC32 checksum on byte array.
acc_alg_basic_utils_calculate_length_of_bitarray_uint32
static size_t acc_alg_basic_utils_calculate_length_of_bitarray_uint32(size_t number_of_bits)
Calculate length of 32-bit array to contain size number of bits.
Definition: acc_alg_basic_utils.h:38
acc_alg_basic_utils_is_bit_set_bitarray_uint32
static bool acc_alg_basic_utils_is_bit_set_bitarray_uint32(const uint32_t *bitarray, size_t bit_index)
Check if bit is set in bit array.
Definition: acc_alg_basic_utils.h:72
acc_alg_basic_utils_clear_bit_bitarray_uint32
static void acc_alg_basic_utils_clear_bit_bitarray_uint32(uint32_t *bitarray, size_t bit_index)
Clear bit in bit array.
Definition: acc_alg_basic_utils.h:60
acc_alg_basic_utils_set_bit_bitarray_uint32
static void acc_alg_basic_utils_set_bit_bitarray_uint32(uint32_t *bitarray, size_t bit_index)
Set bit in bit array.
Definition: acc_alg_basic_utils.h:49