Back to home page

OSCL-LXR

 
 

    


0001 /* tnum: tracked (or tristate) numbers
0002  *
0003  * A tnum tracks knowledge about the bits of a value.  Each bit can be either
0004  * known (0 or 1), or unknown (x).  Arithmetic operations on tnums will
0005  * propagate the unknown bits such that the tnum result represents all the
0006  * possible results for possible values of the operands.
0007  */
0008 
0009 #ifndef _LINUX_TNUM_H
0010 #define _LINUX_TNUM_H
0011 
0012 #include <linux/types.h>
0013 
0014 struct tnum {
0015     u64 value;
0016     u64 mask;
0017 };
0018 
0019 /* Constructors */
0020 /* Represent a known constant as a tnum. */
0021 struct tnum tnum_const(u64 value);
0022 /* A completely unknown value */
0023 extern const struct tnum tnum_unknown;
0024 /* A value that's unknown except that @min <= value <= @max */
0025 struct tnum tnum_range(u64 min, u64 max);
0026 
0027 /* Arithmetic and logical ops */
0028 /* Shift a tnum left (by a fixed shift) */
0029 struct tnum tnum_lshift(struct tnum a, u8 shift);
0030 /* Shift (rsh) a tnum right (by a fixed shift) */
0031 struct tnum tnum_rshift(struct tnum a, u8 shift);
0032 /* Shift (arsh) a tnum right (by a fixed min_shift) */
0033 struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness);
0034 /* Add two tnums, return @a + @b */
0035 struct tnum tnum_add(struct tnum a, struct tnum b);
0036 /* Subtract two tnums, return @a - @b */
0037 struct tnum tnum_sub(struct tnum a, struct tnum b);
0038 /* Bitwise-AND, return @a & @b */
0039 struct tnum tnum_and(struct tnum a, struct tnum b);
0040 /* Bitwise-OR, return @a | @b */
0041 struct tnum tnum_or(struct tnum a, struct tnum b);
0042 /* Bitwise-XOR, return @a ^ @b */
0043 struct tnum tnum_xor(struct tnum a, struct tnum b);
0044 /* Multiply two tnums, return @a * @b */
0045 struct tnum tnum_mul(struct tnum a, struct tnum b);
0046 
0047 /* Return a tnum representing numbers satisfying both @a and @b */
0048 struct tnum tnum_intersect(struct tnum a, struct tnum b);
0049 
0050 /* Return @a with all but the lowest @size bytes cleared */
0051 struct tnum tnum_cast(struct tnum a, u8 size);
0052 
0053 /* Returns true if @a is a known constant */
0054 static inline bool tnum_is_const(struct tnum a)
0055 {
0056     return !a.mask;
0057 }
0058 
0059 /* Returns true if @a == tnum_const(@b) */
0060 static inline bool tnum_equals_const(struct tnum a, u64 b)
0061 {
0062     return tnum_is_const(a) && a.value == b;
0063 }
0064 
0065 /* Returns true if @a is completely unknown */
0066 static inline bool tnum_is_unknown(struct tnum a)
0067 {
0068     return !~a.mask;
0069 }
0070 
0071 /* Returns true if @a is known to be a multiple of @size.
0072  * @size must be a power of two.
0073  */
0074 bool tnum_is_aligned(struct tnum a, u64 size);
0075 
0076 /* Returns true if @b represents a subset of @a. */
0077 bool tnum_in(struct tnum a, struct tnum b);
0078 
0079 /* Formatting functions.  These have snprintf-like semantics: they will write
0080  * up to @size bytes (including the terminating NUL byte), and return the number
0081  * of bytes (excluding the terminating NUL) which would have been written had
0082  * sufficient space been available.  (Thus tnum_sbin always returns 64.)
0083  */
0084 /* Format a tnum as a pair of hex numbers (value; mask) */
0085 int tnum_strn(char *str, size_t size, struct tnum a);
0086 /* Format a tnum as tristate binary expansion */
0087 int tnum_sbin(char *str, size_t size, struct tnum a);
0088 
0089 /* Returns the 32-bit subreg */
0090 struct tnum tnum_subreg(struct tnum a);
0091 /* Returns the tnum with the lower 32-bit subreg cleared */
0092 struct tnum tnum_clear_subreg(struct tnum a);
0093 /* Returns the tnum with the lower 32-bit subreg set to value */
0094 struct tnum tnum_const_subreg(struct tnum a, u32 value);
0095 /* Returns true if 32-bit subreg @a is a known constant*/
0096 static inline bool tnum_subreg_is_const(struct tnum a)
0097 {
0098     return !(tnum_subreg(a)).mask;
0099 }
0100 
0101 #endif /* _LINUX_TNUM_H */