Wirepas SDK
util.h
Go to the documentation of this file.
1/* Copyright 2017 Wirepas Ltd. All Rights Reserved.
2 *
3 * See file LICENSE.txt for full license details.
4 *
5 */
6
7#ifndef UTIL_H
8#define UTIL_H
9
46#include <stdint.h>
47#include <stdbool.h>
48
49#define Util_isLtUint8(a, b) ((int8_t)((a) - (b)) < 0)
50#define Util_isGtOrEqUint8(a, b) ((int8_t)((a) - (b)) >= 0)
51#define Util_isLtUint16(a, b) ((int16_t)((a) - (b)) < 0)
52#define Util_isGtOrEqUint16(a, b) ((int16_t)((a) - (b)) >= 0)
53#define Util_isGtOrEqUint32(a, b) !Util_isLtUint32(a, b)
54
55bool Util_isLtUint32(uint32_t a, uint32_t b);
56
57#define Util_isGtUint8(a, b) (!Util_isGtOrEqUint8(b, a))
58#define Util_isGtUint16(a, b) (!Util_isGtOrEqUint16(b, a))
59#define Util_isGtUint32(a, b) (!Util_isGtOrEqUint32(b, a))
60
66bool Util_isSmallest(const uint32_t a, const uint32_t b, const uint32_t c);
67
75bool Util_inBetween(const uint32_t a, const uint32_t b, const uint32_t c);
76
77/* Aligning macros for structs.
78 *
79 * These can be used in structs to align the struct members correctly,
80 * especially when preceeding struct member size varies by some compilation
81 * flag
82 *
83 * Usage:
84 * STRUCT_ALIGN_4(prev_type);
85 * STRUCT_ALIGN_2(prev_type);
86 *
87 * These align the next member in struct to 4 or 2 byte boundary.
88 *
89 * For example:
90 * typedef struct
91 * {
92 * another_struct_t a;
93 *
94 * STRUCT_ALIGN_4(another_struct_t);
95 *
96 * uint32_t b;
97 * } example_struct_t;
98 *
99 * In this case, if length of another_struct_t varies, this ensures that b is
100 * aligned to 32-bit boundary (4 bytes)
101 *
102 * Another example:
103 * typedef struct
104 * {
105 * another_struct_t a;
106 *
107 * STRUCT_ALIGN_2(another_struct_t);
108 *
109 * uint16_t b;
110 * } example_struct_t;
111 *
112 * This case, align macro ensures that b is aligned to 16-bit boundary
113 *
114 * NOTE: Do NOT use in PACKED structs!
115 */
116
117// These are macro replacement redirection forumulas to do the correct operation
118#define SA_MEMBERNAME(line) align ## line
119#define SA_BASE(prev_type, size, line) \
120 uint8_t SA_MEMBERNAME(line) \
121 [sizeof(prev_type)%size ? size-(sizeof(prev_type)%size) : 0]
122
123#define STRUCT_ALIGN_4(prev_type) SA_BASE(prev_type, 4, __LINE__)
124 //uint8_t align ## __LINE__ [sizeof(prev_type)%4 ? 4-(sizeof(prev_type)%4) : 0]
125#define STRUCT_ALIGN_2(prev_type) SA_BASE(prev_type, 2, __LINE__)
126 // uint8_t align ## __LINE__ [sizeof(prev_type)%2 ? 1 : 0]
127
134uint8_t Util_bitCountU8(uint8_t value);
135
136#endif // #ifndef __UTIL_H__
uint8_t Util_bitCountU8(uint8_t value)
Calculate the '1' bits in 8-bit value.
bool Util_isSmallest(const uint32_t a, const uint32_t b, const uint32_t c)
bool Util_inBetween(const uint32_t a, const uint32_t b, const uint32_t c)
bool Util_isLtUint32(uint32_t a, uint32_t b)