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