Skip to content

Commit 24b0298

Browse files
nandedamanaKernel Patches Daemon
authored andcommitted
bpf: improve the general precision of tnum_mul
This commit addresses a challenge explained in an open question ("How can we incorporate correlation in unknown bits across partial products?") left by Harishankar et al. in their paper: https://arxiv.org/abs/2105.05398 When LSB(a) is uncertain, we know for sure that it is either 0 or 1, from which we could find two possible partial products and take a union. Experiment shows that applying this technique in long multiplication improves the precision in a significant number of cases (at the cost of losing precision in a relatively lower number of cases). This commit also removes the value-mask decomposition technique employed by Harishankar et al., as its direct incorporation did not result in any improvements for the new algorithm. Signed-off-by: Nandakumar Edamana <[email protected]>
1 parent 61c9cef commit 24b0298

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

include/linux/tnum.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ struct tnum tnum_mul(struct tnum a, struct tnum b);
5454
/* Return a tnum representing numbers satisfying both @a and @b */
5555
struct tnum tnum_intersect(struct tnum a, struct tnum b);
5656

57+
/* Returns a tnum representing numbers satisfying either @a or @b */
58+
struct tnum tnum_union(struct tnum t1, struct tnum t2);
59+
5760
/* Return @a with all but the lowest @size bytes cleared */
5861
struct tnum tnum_cast(struct tnum a, u8 size);
5962

kernel/bpf/tnum.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,39 @@ struct tnum tnum_xor(struct tnum a, struct tnum b)
116116
return TNUM(v & ~mu, mu);
117117
}
118118

119-
/* Generate partial products by multiplying each bit in the multiplier (tnum a)
120-
* with the multiplicand (tnum b), and add the partial products after
121-
* appropriately bit-shifting them. Instead of directly performing tnum addition
122-
* on the generated partial products, equivalenty, decompose each partial
123-
* product into two tnums, consisting of the value-sum (acc_v) and the
124-
* mask-sum (acc_m) and then perform tnum addition on them. The following paper
125-
* explains the algorithm in more detail: https://arxiv.org/abs/2105.05398.
119+
/* Perform long multiplication, iterating through the trits in a.
120+
* Inside `else if (a.mask & 1)`, instead of simply multiplying b with LSB(a)'s
121+
* uncertainty and accumulating directly, we find two possible partial products
122+
* (one for LSB(a) = 0 and another for LSB(a) = 1), and add their union to the
123+
* accumulator. This addresses an issue pointed out in an open question ("How
124+
* can we incorporate correlation in unknown bits across partial products?")
125+
* left by Harishankar et al. (https://arxiv.org/abs/2105.05398), improving
126+
* the general precision significantly.
126127
*/
127128
struct tnum tnum_mul(struct tnum a, struct tnum b)
128129
{
129-
u64 acc_v = a.value * b.value;
130-
struct tnum acc_m = TNUM(0, 0);
130+
struct tnum acc = TNUM(0, 0);
131131

132132
while (a.value || a.mask) {
133133
/* LSB of tnum a is a certain 1 */
134134
if (a.value & 1)
135-
acc_m = tnum_add(acc_m, TNUM(0, b.mask));
135+
acc = tnum_add(acc, b);
136136
/* LSB of tnum a is uncertain */
137-
else if (a.mask & 1)
138-
acc_m = tnum_add(acc_m, TNUM(0, b.value | b.mask));
137+
else if (a.mask & 1) {
138+
/* acc += tnum_union(acc_0, acc_1), where acc_0 and
139+
* acc_1 are partial accumulators for cases
140+
* LSB(a) = certain 0 and LSB(a) = certain 1.
141+
* acc_0 = acc + 0 * b = acc.
142+
* acc_1 = acc + 1 * b = tnum_add(acc, b).
143+
*/
144+
145+
acc = tnum_union(acc, tnum_add(acc, b));
146+
}
139147
/* Note: no case for LSB is certain 0 */
140148
a = tnum_rshift(a, 1);
141149
b = tnum_lshift(b, 1);
142150
}
143-
return tnum_add(TNUM(acc_v, 0), acc_m);
151+
return acc;
144152
}
145153

146154
/* Note that if a and b disagree - i.e. one has a 'known 1' where the other has
@@ -155,6 +163,14 @@ struct tnum tnum_intersect(struct tnum a, struct tnum b)
155163
return TNUM(v & ~mu, mu);
156164
}
157165

166+
struct tnum tnum_union(struct tnum a, struct tnum b)
167+
{
168+
u64 v = a.value & b.value;
169+
u64 mu = (a.value ^ b.value) | a.mask | b.mask;
170+
171+
return TNUM(v & ~mu, mu);
172+
}
173+
158174
struct tnum tnum_cast(struct tnum a, u8 size)
159175
{
160176
a.value &= (1ULL << (size * 8)) - 1;

0 commit comments

Comments
 (0)