tnum.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* tnum: tracked (or tristate) numbers
  3. *
  4. * A tnum tracks knowledge about the bits of a value. Each bit can be either
  5. * known (0 or 1), or unknown (x). Arithmetic operations on tnums will
  6. * propagate the unknown bits such that the tnum result represents all the
  7. * possible results for possible values of the operands.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/tnum.h>
  11. #define TNUM(_v, _m) (struct tnum){.value = _v, .mask = _m}
  12. /* A completely unknown value */
  13. const struct tnum tnum_unknown = { .value = 0, .mask = -1 };
  14. struct tnum tnum_const(u64 value)
  15. {
  16. return TNUM(value, 0);
  17. }
  18. struct tnum tnum_range(u64 min, u64 max)
  19. {
  20. u64 chi = min ^ max, delta;
  21. u8 bits = fls64(chi);
  22. /* special case, needed because 1ULL << 64 is undefined */
  23. if (bits > 63)
  24. return tnum_unknown;
  25. /* e.g. if chi = 4, bits = 3, delta = (1<<3) - 1 = 7.
  26. * if chi = 0, bits = 0, delta = (1<<0) - 1 = 0, so we return
  27. * constant min (since min == max).
  28. */
  29. delta = (1ULL << bits) - 1;
  30. return TNUM(min & ~delta, delta);
  31. }
  32. struct tnum tnum_lshift(struct tnum a, u8 shift)
  33. {
  34. return TNUM(a.value << shift, a.mask << shift);
  35. }
  36. struct tnum tnum_rshift(struct tnum a, u8 shift)
  37. {
  38. return TNUM(a.value >> shift, a.mask >> shift);
  39. }
  40. struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness)
  41. {
  42. /* if a.value is negative, arithmetic shifting by minimum shift
  43. * will have larger negative offset compared to more shifting.
  44. * If a.value is nonnegative, arithmetic shifting by minimum shift
  45. * will have larger positive offset compare to more shifting.
  46. */
  47. if (insn_bitness == 32)
  48. return TNUM((u32)(((s32)a.value) >> min_shift),
  49. (u32)(((s32)a.mask) >> min_shift));
  50. else
  51. return TNUM((s64)a.value >> min_shift,
  52. (s64)a.mask >> min_shift);
  53. }
  54. struct tnum tnum_add(struct tnum a, struct tnum b)
  55. {
  56. u64 sm, sv, sigma, chi, mu;
  57. sm = a.mask + b.mask;
  58. sv = a.value + b.value;
  59. sigma = sm + sv;
  60. chi = sigma ^ sv;
  61. mu = chi | a.mask | b.mask;
  62. return TNUM(sv & ~mu, mu);
  63. }
  64. struct tnum tnum_sub(struct tnum a, struct tnum b)
  65. {
  66. u64 dv, alpha, beta, chi, mu;
  67. dv = a.value - b.value;
  68. alpha = dv + a.mask;
  69. beta = dv - b.mask;
  70. chi = alpha ^ beta;
  71. mu = chi | a.mask | b.mask;
  72. return TNUM(dv & ~mu, mu);
  73. }
  74. struct tnum tnum_and(struct tnum a, struct tnum b)
  75. {
  76. u64 alpha, beta, v;
  77. alpha = a.value | a.mask;
  78. beta = b.value | b.mask;
  79. v = a.value & b.value;
  80. return TNUM(v, alpha & beta & ~v);
  81. }
  82. struct tnum tnum_or(struct tnum a, struct tnum b)
  83. {
  84. u64 v, mu;
  85. v = a.value | b.value;
  86. mu = a.mask | b.mask;
  87. return TNUM(v, mu & ~v);
  88. }
  89. struct tnum tnum_xor(struct tnum a, struct tnum b)
  90. {
  91. u64 v, mu;
  92. v = a.value ^ b.value;
  93. mu = a.mask | b.mask;
  94. return TNUM(v & ~mu, mu);
  95. }
  96. /* Generate partial products by multiplying each bit in the multiplier (tnum a)
  97. * with the multiplicand (tnum b), and add the partial products after
  98. * appropriately bit-shifting them. Instead of directly performing tnum addition
  99. * on the generated partial products, equivalenty, decompose each partial
  100. * product into two tnums, consisting of the value-sum (acc_v) and the
  101. * mask-sum (acc_m) and then perform tnum addition on them. The following paper
  102. * explains the algorithm in more detail: https://arxiv.org/abs/2105.05398.
  103. */
  104. struct tnum tnum_mul(struct tnum a, struct tnum b)
  105. {
  106. u64 acc_v = a.value * b.value;
  107. struct tnum acc_m = TNUM(0, 0);
  108. while (a.value || a.mask) {
  109. /* LSB of tnum a is a certain 1 */
  110. if (a.value & 1)
  111. acc_m = tnum_add(acc_m, TNUM(0, b.mask));
  112. /* LSB of tnum a is uncertain */
  113. else if (a.mask & 1)
  114. acc_m = tnum_add(acc_m, TNUM(0, b.value | b.mask));
  115. /* Note: no case for LSB is certain 0 */
  116. a = tnum_rshift(a, 1);
  117. b = tnum_lshift(b, 1);
  118. }
  119. return tnum_add(TNUM(acc_v, 0), acc_m);
  120. }
  121. /* Note that if a and b disagree - i.e. one has a 'known 1' where the other has
  122. * a 'known 0' - this will return a 'known 1' for that bit.
  123. */
  124. struct tnum tnum_intersect(struct tnum a, struct tnum b)
  125. {
  126. u64 v, mu;
  127. v = a.value | b.value;
  128. mu = a.mask & b.mask;
  129. return TNUM(v & ~mu, mu);
  130. }
  131. struct tnum tnum_cast(struct tnum a, u8 size)
  132. {
  133. a.value &= (1ULL << (size * 8)) - 1;
  134. a.mask &= (1ULL << (size * 8)) - 1;
  135. return a;
  136. }
  137. bool tnum_is_aligned(struct tnum a, u64 size)
  138. {
  139. if (!size)
  140. return true;
  141. return !((a.value | a.mask) & (size - 1));
  142. }
  143. bool tnum_in(struct tnum a, struct tnum b)
  144. {
  145. if (b.mask & ~a.mask)
  146. return false;
  147. b.value &= ~a.mask;
  148. return a.value == b.value;
  149. }
  150. int tnum_strn(char *str, size_t size, struct tnum a)
  151. {
  152. return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask);
  153. }
  154. EXPORT_SYMBOL_GPL(tnum_strn);
  155. int tnum_sbin(char *str, size_t size, struct tnum a)
  156. {
  157. size_t n;
  158. for (n = 64; n; n--) {
  159. if (n < size) {
  160. if (a.mask & 1)
  161. str[n - 1] = 'x';
  162. else if (a.value & 1)
  163. str[n - 1] = '1';
  164. else
  165. str[n - 1] = '0';
  166. }
  167. a.mask >>= 1;
  168. a.value >>= 1;
  169. }
  170. str[min(size - 1, (size_t)64)] = 0;
  171. return 64;
  172. }
  173. struct tnum tnum_subreg(struct tnum a)
  174. {
  175. return tnum_cast(a, 4);
  176. }
  177. struct tnum tnum_clear_subreg(struct tnum a)
  178. {
  179. return tnum_lshift(tnum_rshift(a, 32), 32);
  180. }
  181. struct tnum tnum_const_subreg(struct tnum a, u32 value)
  182. {
  183. return tnum_or(tnum_clear_subreg(a), tnum_const(value));
  184. }