acc_alg_basic_utils.h
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2016-2023
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 /**
24 * @brief Calculate CRC32 checksum on byte array
25 *
26 * @param[in] input byte array
27 * @param[in] len Length of byte array
28 *
29 * @return CRC32 checksum
30 */
31 uint32_t acc_alg_basic_util_crc32(const uint8_t *input, size_t len);
32
33
34 /**
35 * @brief Calculate length of 32-bit array to contain size number of bits
36 *
37 * @param number_of_bits Number of bits to contain in bit array
38 * @return Length of 32-bit array
39 */
40 static inline size_t acc_alg_basic_utils_calculate_length_of_bitarray_uint32(size_t number_of_bits)
41 {
42 return (number_of_bits + (32U - 1U)) / 32U;
43 }
44
45
46 /**
47 * @brief Set bit in bit array
48 *
49 * @param[in, out] bitarray Array to set bit in
50 * @param[in] bit_index Index of bit to set
51 */
52 static inline void acc_alg_basic_utils_set_bit_bitarray_uint32(uint32_t *bitarray, size_t bit_index)
53 {
54 bitarray[bit_index / 32U] |= (uint32_t)1U << (bit_index & 0x1FU);
55 }
56
57
58 /**
59 * @brief Clear bit in bit array
60 *
61 * @param[in, out] bitarray Array to clear bit in
62 * @param[in] bit_index Index of bit to clear
63 */
64 static inline void acc_alg_basic_utils_clear_bit_bitarray_uint32(uint32_t *bitarray, size_t bit_index)
65 {
66 bitarray[bit_index / 32U] &= ~((uint32_t)1U << (bit_index & 0x1FU));
67 }
68
69
70 /**
71 * @brief Check if bit is set in bit array
72 *
73 * @param[in] bitarray Array to check bit in
74 * @param[in] bit_index Index of bit to check
75 * @return True if bit is set
76 */
77 static inline bool acc_alg_basic_utils_is_bit_set_bitarray_uint32(const uint32_t *bitarray, size_t bit_index)
78 {
79 return (bitarray[bit_index / 32U] & ((uint32_t)1U << (bit_index & 0x1FU))) != 0U;
80 }
81
82
83 #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:40
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:77
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:64
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:52