tnum.h 3.4 KB

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