xref: /linux-6.15/include/linux/tnum.h (revision 67420501)
1f1174f77SEdward Cree /* tnum: tracked (or tristate) numbers
2f1174f77SEdward Cree  *
3f1174f77SEdward Cree  * A tnum tracks knowledge about the bits of a value.  Each bit can be either
4f1174f77SEdward Cree  * known (0 or 1), or unknown (x).  Arithmetic operations on tnums will
5f1174f77SEdward Cree  * propagate the unknown bits such that the tnum result represents all the
6f1174f77SEdward Cree  * possible results for possible values of the operands.
7f1174f77SEdward Cree  */
869ecfdaaSMasahiro Yamada 
969ecfdaaSMasahiro Yamada #ifndef _LINUX_TNUM_H
1069ecfdaaSMasahiro Yamada #define _LINUX_TNUM_H
1169ecfdaaSMasahiro Yamada 
12f1174f77SEdward Cree #include <linux/types.h>
13f1174f77SEdward Cree 
14f1174f77SEdward Cree struct tnum {
15f1174f77SEdward Cree 	u64 value;
16f1174f77SEdward Cree 	u64 mask;
17f1174f77SEdward Cree };
18f1174f77SEdward Cree 
19f1174f77SEdward Cree /* Constructors */
20f1174f77SEdward Cree /* Represent a known constant as a tnum. */
21f1174f77SEdward Cree struct tnum tnum_const(u64 value);
22f1174f77SEdward Cree /* A completely unknown value */
23f1174f77SEdward Cree extern const struct tnum tnum_unknown;
24dc84dbbcSShung-Hsi Yu /* An unknown value that is a superset of @min <= value <= @max.
25dc84dbbcSShung-Hsi Yu  *
26dc84dbbcSShung-Hsi Yu  * Could include values outside the range of [@min, @max].
27dc84dbbcSShung-Hsi Yu  * For example tnum_range(0, 2) is represented by {0, 1, 2, *3*},
28dc84dbbcSShung-Hsi Yu  * rather than the intended set of {0, 1, 2}.
29dc84dbbcSShung-Hsi Yu  */
30b03c9f9fSEdward Cree struct tnum tnum_range(u64 min, u64 max);
31f1174f77SEdward Cree 
32f1174f77SEdward Cree /* Arithmetic and logical ops */
33f1174f77SEdward Cree /* Shift a tnum left (by a fixed shift) */
34f1174f77SEdward Cree struct tnum tnum_lshift(struct tnum a, u8 shift);
359cbe1f5aSYonghong Song /* Shift (rsh) a tnum right (by a fixed shift) */
36f1174f77SEdward Cree struct tnum tnum_rshift(struct tnum a, u8 shift);
379cbe1f5aSYonghong Song /* Shift (arsh) a tnum right (by a fixed min_shift) */
380af2ffc9SDaniel Borkmann struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness);
39f1174f77SEdward Cree /* Add two tnums, return @a + @b */
40f1174f77SEdward Cree struct tnum tnum_add(struct tnum a, struct tnum b);
41f1174f77SEdward Cree /* Subtract two tnums, return @a - @b */
42f1174f77SEdward Cree struct tnum tnum_sub(struct tnum a, struct tnum b);
43f1174f77SEdward Cree /* Bitwise-AND, return @a & @b */
44f1174f77SEdward Cree struct tnum tnum_and(struct tnum a, struct tnum b);
45f1174f77SEdward Cree /* Bitwise-OR, return @a | @b */
46f1174f77SEdward Cree struct tnum tnum_or(struct tnum a, struct tnum b);
47f1174f77SEdward Cree /* Bitwise-XOR, return @a ^ @b */
48f1174f77SEdward Cree struct tnum tnum_xor(struct tnum a, struct tnum b);
49f1174f77SEdward Cree /* Multiply two tnums, return @a * @b */
50f1174f77SEdward Cree struct tnum tnum_mul(struct tnum a, struct tnum b);
51f1174f77SEdward Cree 
52f1174f77SEdward Cree /* Return a tnum representing numbers satisfying both @a and @b */
53f1174f77SEdward Cree struct tnum tnum_intersect(struct tnum a, struct tnum b);
54f1174f77SEdward Cree 
55f1174f77SEdward Cree /* Return @a with all but the lowest @size bytes cleared */
56f1174f77SEdward Cree struct tnum tnum_cast(struct tnum a, u8 size);
57f1174f77SEdward Cree 
58f1174f77SEdward Cree /* Returns true if @a is a known constant */
tnum_is_const(struct tnum a)59f1174f77SEdward Cree static inline bool tnum_is_const(struct tnum a)
60f1174f77SEdward Cree {
61f1174f77SEdward Cree 	return !a.mask;
62f1174f77SEdward Cree }
63f1174f77SEdward Cree 
64f1174f77SEdward Cree /* Returns true if @a == tnum_const(@b) */
tnum_equals_const(struct tnum a,u64 b)65f1174f77SEdward Cree static inline bool tnum_equals_const(struct tnum a, u64 b)
66f1174f77SEdward Cree {
67f1174f77SEdward Cree 	return tnum_is_const(a) && a.value == b;
68f1174f77SEdward Cree }
69f1174f77SEdward Cree 
70f1174f77SEdward Cree /* Returns true if @a is completely unknown */
tnum_is_unknown(struct tnum a)71f1174f77SEdward Cree static inline bool tnum_is_unknown(struct tnum a)
72f1174f77SEdward Cree {
73f1174f77SEdward Cree 	return !~a.mask;
74f1174f77SEdward Cree }
75f1174f77SEdward Cree 
76f1174f77SEdward Cree /* Returns true if @a is known to be a multiple of @size.
77f1174f77SEdward Cree  * @size must be a power of two.
78f1174f77SEdward Cree  */
79f1174f77SEdward Cree bool tnum_is_aligned(struct tnum a, u64 size);
80f1174f77SEdward Cree 
81dc84dbbcSShung-Hsi Yu /* Returns true if @b represents a subset of @a.
82dc84dbbcSShung-Hsi Yu  *
83dc84dbbcSShung-Hsi Yu  * Note that using tnum_range() as @a requires extra cautions as tnum_in() may
84dc84dbbcSShung-Hsi Yu  * return true unexpectedly due to tnum limited ability to represent tight
85dc84dbbcSShung-Hsi Yu  * range, e.g.
86dc84dbbcSShung-Hsi Yu  *
87dc84dbbcSShung-Hsi Yu  *   tnum_in(tnum_range(0, 2), tnum_const(3)) == true
88dc84dbbcSShung-Hsi Yu  *
89dc84dbbcSShung-Hsi Yu  * As a rule of thumb, if @a is explicitly coded rather than coming from
90dc84dbbcSShung-Hsi Yu  * reg->var_off, it should be in form of tnum_const(), tnum_range(0, 2**n - 1),
91dc84dbbcSShung-Hsi Yu  * or tnum_range(2**n, 2**(n+1) - 1).
92dc84dbbcSShung-Hsi Yu  */
93f1174f77SEdward Cree bool tnum_in(struct tnum a, struct tnum b);
94f1174f77SEdward Cree 
95f1174f77SEdward Cree /* Formatting functions.  These have snprintf-like semantics: they will write
96f1174f77SEdward Cree  * up to @size bytes (including the terminating NUL byte), and return the number
97f1174f77SEdward Cree  * of bytes (excluding the terminating NUL) which would have been written had
98f1174f77SEdward Cree  * sufficient space been available.  (Thus tnum_sbin always returns 64.)
99f1174f77SEdward Cree  */
100f1174f77SEdward Cree /* Format a tnum as a pair of hex numbers (value; mask) */
101f1174f77SEdward Cree int tnum_strn(char *str, size_t size, struct tnum a);
102f1174f77SEdward Cree /* Format a tnum as tristate binary expansion */
103f1174f77SEdward Cree int tnum_sbin(char *str, size_t size, struct tnum a);
10469ecfdaaSMasahiro Yamada 
1053f50f132SJohn Fastabend /* Returns the 32-bit subreg */
1063f50f132SJohn Fastabend struct tnum tnum_subreg(struct tnum a);
1073f50f132SJohn Fastabend /* Returns the tnum with the lower 32-bit subreg cleared */
1083f50f132SJohn Fastabend struct tnum tnum_clear_subreg(struct tnum a);
109*67420501SAndrii Nakryiko /* Returns the tnum with the lower 32-bit subreg in *reg* set to the lower
110*67420501SAndrii Nakryiko  * 32-bit subreg in *subreg*
111*67420501SAndrii Nakryiko  */
112*67420501SAndrii Nakryiko struct tnum tnum_with_subreg(struct tnum reg, struct tnum subreg);
1133f50f132SJohn Fastabend /* Returns the tnum with the lower 32-bit subreg set to value */
1143f50f132SJohn Fastabend struct tnum tnum_const_subreg(struct tnum a, u32 value);
1153f50f132SJohn Fastabend /* Returns true if 32-bit subreg @a is a known constant*/
tnum_subreg_is_const(struct tnum a)1163f50f132SJohn Fastabend static inline bool tnum_subreg_is_const(struct tnum a)
1173f50f132SJohn Fastabend {
1183f50f132SJohn Fastabend 	return !(tnum_subreg(a)).mask;
1193f50f132SJohn Fastabend }
1203f50f132SJohn Fastabend 
12169ecfdaaSMasahiro Yamada #endif /* _LINUX_TNUM_H */
122