blob: 0d2d3da461397017753231edf208438d6f18b482 [file] [log] [blame]
Edward Creef1174f72017-08-07 15:26:19 +01001/* tnum: tracked (or tristate) numbers
2 *
3 * A tnum tracks knowledge about the bits of a value. Each bit can be either
4 * known (0 or 1), or unknown (x). Arithmetic operations on tnums will
5 * propagate the unknown bits such that the tnum result represents all the
6 * possible results for possible values of the operands.
7 */
8#include <linux/types.h>
9
10struct tnum {
11 u64 value;
12 u64 mask;
13};
14
15/* Constructors */
16/* Represent a known constant as a tnum. */
17struct tnum tnum_const(u64 value);
18/* A completely unknown value */
19extern const struct tnum tnum_unknown;
Edward Creeb03c9f92017-08-07 15:26:36 +010020/* A value that's unknown except that @min <= value <= @max */
21struct tnum tnum_range(u64 min, u64 max);
Edward Creef1174f72017-08-07 15:26:19 +010022
23/* Arithmetic and logical ops */
24/* Shift a tnum left (by a fixed shift) */
25struct tnum tnum_lshift(struct tnum a, u8 shift);
26/* Shift a tnum right (by a fixed shift) */
27struct tnum tnum_rshift(struct tnum a, u8 shift);
28/* Add two tnums, return @a + @b */
29struct tnum tnum_add(struct tnum a, struct tnum b);
30/* Subtract two tnums, return @a - @b */
31struct tnum tnum_sub(struct tnum a, struct tnum b);
32/* Bitwise-AND, return @a & @b */
33struct tnum tnum_and(struct tnum a, struct tnum b);
34/* Bitwise-OR, return @a | @b */
35struct tnum tnum_or(struct tnum a, struct tnum b);
36/* Bitwise-XOR, return @a ^ @b */
37struct tnum tnum_xor(struct tnum a, struct tnum b);
38/* Multiply two tnums, return @a * @b */
39struct tnum tnum_mul(struct tnum a, struct tnum b);
40
41/* Return a tnum representing numbers satisfying both @a and @b */
42struct tnum tnum_intersect(struct tnum a, struct tnum b);
43
44/* Return @a with all but the lowest @size bytes cleared */
45struct tnum tnum_cast(struct tnum a, u8 size);
46
47/* Returns true if @a is a known constant */
48static inline bool tnum_is_const(struct tnum a)
49{
50 return !a.mask;
51}
52
53/* Returns true if @a == tnum_const(@b) */
54static inline bool tnum_equals_const(struct tnum a, u64 b)
55{
56 return tnum_is_const(a) && a.value == b;
57}
58
59/* Returns true if @a is completely unknown */
60static inline bool tnum_is_unknown(struct tnum a)
61{
62 return !~a.mask;
63}
64
65/* Returns true if @a is known to be a multiple of @size.
66 * @size must be a power of two.
67 */
68bool tnum_is_aligned(struct tnum a, u64 size);
69
70/* Returns true if @b represents a subset of @a. */
71bool tnum_in(struct tnum a, struct tnum b);
72
73/* Formatting functions. These have snprintf-like semantics: they will write
74 * up to @size bytes (including the terminating NUL byte), and return the number
75 * of bytes (excluding the terminating NUL) which would have been written had
76 * sufficient space been available. (Thus tnum_sbin always returns 64.)
77 */
78/* Format a tnum as a pair of hex numbers (value; mask) */
79int tnum_strn(char *str, size_t size, struct tnum a);
80/* Format a tnum as tristate binary expansion */
81int tnum_sbin(char *str, size_t size, struct tnum a);