tnum.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/kernel.h>
  9. #include <linux/tnum.h>
  10. #define TNUM(_v, _m) (struct tnum){.value = _v, .mask = _m}
  11. /* A completely unknown value */
  12. const struct tnum tnum_unknown = { .value = 0, .mask = -1 };
  13. struct tnum tnum_const(u64 value)
  14. {
  15. return TNUM(value, 0);
  16. }
  17. struct tnum tnum_range(u64 min, u64 max)
  18. {
  19. u64 chi = min ^ max, delta;
  20. u8 bits = fls64(chi);
  21. /* special case, needed because 1ULL << 64 is undefined */
  22. if (bits > 63)
  23. return tnum_unknown;
  24. /* e.g. if chi = 4, bits = 3, delta = (1<<3) - 1 = 7.
  25. * if chi = 0, bits = 0, delta = (1<<0) - 1 = 0, so we return
  26. * constant min (since min == max).
  27. */
  28. delta = (1ULL << bits) - 1;
  29. return TNUM(min & ~delta, delta);
  30. }
  31. struct tnum tnum_lshift(struct tnum a, u8 shift)
  32. {
  33. return TNUM(a.value << shift, a.mask << shift);
  34. }
  35. struct tnum tnum_rshift(struct tnum a, u8 shift)
  36. {
  37. return TNUM(a.value >> shift, a.mask >> shift);
  38. }
  39. struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness)
  40. {
  41. /* if a.value is negative, arithmetic shifting by minimum shift
  42. * will have larger negative offset compared to more shifting.
  43. * If a.value is nonnegative, arithmetic shifting by minimum shift
  44. * will have larger positive offset compare to more shifting.
  45. */
  46. if (insn_bitness == 32)
  47. return TNUM((u32)(((s32)a.value) >> min_shift),
  48. (u32)(((s32)a.mask) >> min_shift));
  49. else
  50. return TNUM((s64)a.value >> min_shift,
  51. (s64)a.mask >> min_shift);
  52. }
  53. struct tnum tnum_add(struct tnum a, struct tnum b)
  54. {
  55. u64 sm, sv, sigma, chi, mu;
  56. sm = a.mask + b.mask;
  57. sv = a.value + b.value;
  58. sigma = sm + sv;
  59. chi = sigma ^ sv;
  60. mu = chi | a.mask | b.mask;
  61. return TNUM(sv & ~mu, mu);
  62. }
  63. struct tnum tnum_sub(struct tnum a, struct tnum b)
  64. {
  65. u64 dv, alpha, beta, chi, mu;
  66. dv = a.value - b.value;
  67. alpha = dv + a.mask;
  68. beta = dv - b.mask;
  69. chi = alpha ^ beta;
  70. mu = chi | a.mask | b.mask;
  71. return TNUM(dv & ~mu, mu);
  72. }
  73. struct tnum tnum_and(struct tnum a, struct tnum b)
  74. {
  75. u64 alpha, beta, v;
  76. alpha = a.value | a.mask;
  77. beta = b.value | b.mask;
  78. v = a.value & b.value;
  79. return TNUM(v, alpha & beta & ~v);
  80. }
  81. struct tnum tnum_or(struct tnum a, struct tnum b)
  82. {
  83. u64 v, mu;
  84. v = a.value | b.value;
  85. mu = a.mask | b.mask;
  86. return TNUM(v, mu & ~v);
  87. }
  88. struct tnum tnum_xor(struct tnum a, struct tnum b)
  89. {
  90. u64 v, mu;
  91. v = a.value ^ b.value;
  92. mu = a.mask | b.mask;
  93. return TNUM(v & ~mu, mu);
  94. }
  95. /* half-multiply add: acc += (unknown * mask * value).
  96. * An intermediate step in the multiply algorithm.
  97. */
  98. static struct tnum hma(struct tnum acc, u64 value, u64 mask)
  99. {
  100. while (mask) {
  101. if (mask & 1)
  102. acc = tnum_add(acc, TNUM(0, value));
  103. mask >>= 1;
  104. value <<= 1;
  105. }
  106. return acc;
  107. }
  108. struct tnum tnum_mul(struct tnum a, struct tnum b)
  109. {
  110. struct tnum acc;
  111. u64 pi;
  112. pi = a.value * b.value;
  113. acc = hma(TNUM(pi, 0), a.mask, b.mask | b.value);
  114. return hma(acc, b.mask, a.value);
  115. }
  116. /* Note that if a and b disagree - i.e. one has a 'known 1' where the other has
  117. * a 'known 0' - this will return a 'known 1' for that bit.
  118. */
  119. struct tnum tnum_intersect(struct tnum a, struct tnum b)
  120. {
  121. u64 v, mu;
  122. v = a.value | b.value;
  123. mu = a.mask & b.mask;
  124. return TNUM(v & ~mu, mu);
  125. }
  126. struct tnum tnum_cast(struct tnum a, u8 size)
  127. {
  128. a.value &= (1ULL << (size * 8)) - 1;
  129. a.mask &= (1ULL << (size * 8)) - 1;
  130. return a;
  131. }
  132. bool tnum_is_aligned(struct tnum a, u64 size)
  133. {
  134. if (!size)
  135. return true;
  136. return !((a.value | a.mask) & (size - 1));
  137. }
  138. bool tnum_in(struct tnum a, struct tnum b)
  139. {
  140. if (b.mask & ~a.mask)
  141. return false;
  142. b.value &= ~a.mask;
  143. return a.value == b.value;
  144. }
  145. int tnum_strn(char *str, size_t size, struct tnum a)
  146. {
  147. return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask);
  148. }
  149. EXPORT_SYMBOL_GPL(tnum_strn);
  150. int tnum_sbin(char *str, size_t size, struct tnum a)
  151. {
  152. size_t n;
  153. for (n = 64; n; n--) {
  154. if (n < size) {
  155. if (a.mask & 1)
  156. str[n - 1] = 'x';
  157. else if (a.value & 1)
  158. str[n - 1] = '1';
  159. else
  160. str[n - 1] = '0';
  161. }
  162. a.mask >>= 1;
  163. a.value >>= 1;
  164. }
  165. str[min(size - 1, (size_t)64)] = 0;
  166. return 64;
  167. }