tnum.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* 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. struct tnum {
  10. u64 value;
  11. u64 mask;
  12. };
  13. /* Constructors */
  14. /* Represent a known constant as a tnum. */
  15. struct tnum tnum_const(u64 value);
  16. /* A completely unknown value */
  17. extern const struct tnum tnum_unknown;
  18. /* A value that's unknown except that @min <= value <= @max */
  19. struct tnum tnum_range(u64 min, u64 max);
  20. /* Arithmetic and logical ops */
  21. /* Shift a tnum left (by a fixed shift) */
  22. struct tnum tnum_lshift(struct tnum a, u8 shift);
  23. /* Shift (rsh) a tnum right (by a fixed shift) */
  24. struct tnum tnum_rshift(struct tnum a, u8 shift);
  25. /* Shift (arsh) a tnum right (by a fixed min_shift) */
  26. struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness);
  27. /* Add two tnums, return @a + @b */
  28. struct tnum tnum_add(struct tnum a, struct tnum b);
  29. /* Subtract two tnums, return @a - @b */
  30. struct tnum tnum_sub(struct tnum a, struct tnum b);
  31. /* Bitwise-AND, return @a & @b */
  32. struct tnum tnum_and(struct tnum a, struct tnum b);
  33. /* Bitwise-OR, return @a | @b */
  34. struct tnum tnum_or(struct tnum a, struct tnum b);
  35. /* Bitwise-XOR, return @a ^ @b */
  36. struct tnum tnum_xor(struct tnum a, struct tnum b);
  37. /* Multiply two tnums, return @a * @b */
  38. struct tnum tnum_mul(struct tnum a, struct tnum b);
  39. /* Return a tnum representing numbers satisfying both @a and @b */
  40. struct tnum tnum_intersect(struct tnum a, struct tnum b);
  41. /* Return @a with all but the lowest @size bytes cleared */
  42. struct tnum tnum_cast(struct tnum a, u8 size);
  43. /* Returns true if @a is a known constant */
  44. static inline bool tnum_is_const(struct tnum a)
  45. {
  46. return !a.mask;
  47. }
  48. /* Returns true if @a == tnum_const(@b) */
  49. static inline bool tnum_equals_const(struct tnum a, u64 b)
  50. {
  51. return tnum_is_const(a) && a.value == b;
  52. }
  53. /* Returns true if @a is completely unknown */
  54. static inline bool tnum_is_unknown(struct tnum a)
  55. {
  56. return !~a.mask;
  57. }
  58. /* Returns true if @a is known to be a multiple of @size.
  59. * @size must be a power of two.
  60. */
  61. bool tnum_is_aligned(struct tnum a, u64 size);
  62. /* Returns true if @b represents a subset of @a. */
  63. bool tnum_in(struct tnum a, struct tnum b);
  64. /* Formatting functions. These have snprintf-like semantics: they will write
  65. * up to @size bytes (including the terminating NUL byte), and return the number
  66. * of bytes (excluding the terminating NUL) which would have been written had
  67. * sufficient space been available. (Thus tnum_sbin always returns 64.)
  68. */
  69. /* Format a tnum as a pair of hex numbers (value; mask) */
  70. int tnum_strn(char *str, size_t size, struct tnum a);
  71. /* Format a tnum as tristate binary expansion */
  72. int tnum_sbin(char *str, size_t size, struct tnum a);