Optimizing linear maps modulo 2 (i.e.: fast xor sequences for - - PDF document

optimizing linear maps modulo 2 i e fast xor sequences
SMART_READER_LITE
LIVE PREVIEW

Optimizing linear maps modulo 2 (i.e.: fast xor sequences for - - PDF document

Optimizing linear maps modulo 2 (i.e.: fast xor sequences for bitsliced software) D. J. Bernstein University of Illinois at Chicago NSF ITR0716498 Example: size-4 poly Karatsuba. Start with size 2: F = F 0 + F 1 x , G = G 0 + G 1 x , H 0 =


slide-1
SLIDE 1

Optimizing linear maps modulo 2 (i.e.: fast xor sequences for bitsliced software)

  • D. J. Bernstein

University of Illinois at Chicago NSF ITR–0716498

slide-2
SLIDE 2

Example: size-4 poly Karatsuba. Start with size 2:

F = F0 + F1 x, G = G0 + G1 x, H0 = F0 G0, H2 = F1 G1, H1 = ( F0+ F1)( G0+ G1)
  • H0
  • H2,
) F G = H0 + H1 x + H2 x2.

Substitute

x = t2 etc.: F = f0 + f1 t + f2 t2 + f3 t3, G = f0 + f1 t + f2 t2 + f3 t3, H0 = ( f0 + f1 t)( g0 + g1 t), H2 = ( f2 + f3 t)( g2 + g3 t), H1 = ( f0 + f2 + ( f1 + f3) t)
  • (
g0 + g2 + ( g1 + g3) t)
  • H0
  • H2
) F G = H0 + H1 t2 + H2 t4.
slide-3
SLIDE 3

Initial linear computation:

f0 + f2 ; f1 + f3 ; g0 + g2 ; g1 + g3;

algebraic complexity 4. Three size-2 mults producing

H0 = p0 + p1 t + p2 t2; H2 = q0 + q1 t + q2 t2; H0 + H1 + H2 = r0 + r1 t + r2 t2.

Final linear reconstruction:

H1 = ( r0
  • p0
  • q0) +

(

r1
  • p1
  • q1)
t +

(

r2
  • p2
  • q2)
t2,

algebraic complexity 6;

F G = H0 + H1 t2 + H2 t4,

algebraic complexity 2.

slide-4
SLIDE 4

Let’s look more closely at the reconstruction:

h0 = p0; h1 = p1; h2 = p2 + ( r0
  • p0
  • q0);
h3 = ( r1
  • p1
  • q1);
h4 = ( r2
  • p2
  • q2) +
q0; h5 = q1; h6 = q2.
slide-5
SLIDE 5

Let’s look more closely at the reconstruction:

h0 = p0; h1 = p1; h2 = p2 + ( r0
  • p0
  • q0);
h3 = ( r1
  • p1
  • q1);
h4 = ( r2
  • p2
  • q2) +
q0; h5 = q1; h6 = q2.

Can observe manually that

p2
  • q0 is repeated.

See, e.g., 2000 Bernstein.

slide-6
SLIDE 6

Some addition-chain algorithms will automatically find this speedup. Consider, e.g., greedy additive CSE algorithm from 1997 Paar:

find input pair i0 ; i1

with most popular

i0
  • i1;
compute i0
  • i1;
simplify using i0
  • i1;
repeat.

This algorithm would have automatically found

p2
  • q0

inside Karatsuba reconstruction.

slide-7
SLIDE 7

Today’s algorithm: “xor largest.” Start with the matrix mod 2 for the desired linear map.

h0: 100000000 h1: 010000000 h2: 101100100 h3: 010010010 h4: 001101001 h5: 000010000 h6: 000001000

Each row has coefficients of

p0 ; p1 ; p2 ; q0 ; q1 ; q2 ; r0 ; r1 ; r2.
slide-8
SLIDE 8

Replace largest row by its xor with second-largest row. 100000000 010000000 001100100 010010010 001101001 000010000 000001000 Recursively compute this, and finish with one xor.

slide-9
SLIDE 9

If two largest rows don’t have same first bit, change largest row by clearing first bit. 000000000 010000000 001100100 010010010 001101001 000010000 000001000 Recursively compute this, and finish with one xor (often just a copy).

slide-10
SLIDE 10

Continue in the same way: 100000000 010000000 101100100 010010010 001101001 000010000 000001000 (starting matrix again)

slide-11
SLIDE 11

Continue in the same way: 100000000 010000000 001100100 010010010 001101001 000010000 000001000 plus 1 xor.

slide-12
SLIDE 12

Continue in the same way: 000000000 010000000 001100100 010010010 001101001 000010000 000001000 plus 1 xor, 1 input load.

slide-13
SLIDE 13

Continue in the same way: 000000000 010000000 001100100 000010010 001101001 000010000 000001000 plus 2 xors, 1 input load.

slide-14
SLIDE 14

Continue in the same way: 000000000 000000000 001100100 000010010 001101001 000010000 000001000 plus 2 xors, 2 input loads.

slide-15
SLIDE 15

Continue in the same way: 000000000 000000000 001100100 000010010 000001101 000010000 000001000 plus 3 xors, 2 input loads.

slide-16
SLIDE 16

Continue in the same way: 000000000 000000000 000100100 000010010 000001101 000010000 000001000 plus 4 xors, 3 input loads.

slide-17
SLIDE 17

Continue in the same way: 000000000 000000000 000000100 000010010 000001101 000010000 000001000 plus 5 xors, 4 input loads.

slide-18
SLIDE 18

Continue in the same way: 000000000 000000000 000000100 000000010 000001101 000010000 000001000 plus 6 xors, 4 input loads.

slide-19
SLIDE 19

Continue in the same way: 000000000 000000000 000000100 000000010 000001101 000000000 000001000 plus 6 xors, 5 input loads.

slide-20
SLIDE 20

Continue in the same way: 000000000 000000000 000000100 000000010 000000101 000000000 000001000 plus 7 xors, 5 input loads.

slide-21
SLIDE 21

Continue in the same way: 000000000 000000000 000000100 000000010 000000101 000000000 000000000 plus 7 xors, 6 input loads.

slide-22
SLIDE 22

Continue in the same way: 000000000 000000000 000000100 000000010 000000001 000000000 000000000 plus 8 xors, 6 input loads.

slide-23
SLIDE 23

Continue in the same way: 000000000 000000000 000000000 000000010 000000001 000000000 000000000 plus 8 xors, 7 input loads.

slide-24
SLIDE 24

Continue in the same way: 000000000 000000000 000000000 000000000 000000001 000000000 000000000 plus 8 xors, 8 input loads.

slide-25
SLIDE 25

Continue in the same way: 000000000 000000000 000000000 000000000 000000000 000000000 000000000 plus 8 xors, 9 input loads.

slide-26
SLIDE 26

Continue in the same way: 000000000 000000000 000000000 000000000 000000000 000000000 000000000 plus 8 xors, 9 input loads. “Is this supposed to be an interesting algorithm?”

slide-27
SLIDE 27

Another example: 000100000 000010000 100101100 010010010 001001101 000000010 000000001 Same matrix, but inputs in a different order: first

r’s (used once each),

then

p’s (used twice each),

then

q’s (used twice each).
slide-28
SLIDE 28

Another example: 000100000 000010000 000101100 010010010 001001101 000000010 000000001 plus 1 xor, 1 input load.

slide-29
SLIDE 29

Another example: 000100000 000010000 000101100 000010010 001001101 000000010 000000001 plus 2 xors, 2 input loads.

slide-30
SLIDE 30

Another example: 000100000 000010000 000101100 000010010 000001101 000000010 000000001 plus 3 xors, 3 input loads.

slide-31
SLIDE 31

Another example: 000100000 000010000 000001100 000010010 000001101 000000010 000000001 plus 4 xors, 3 input loads.

slide-32
SLIDE 32

Another example: 000000000 000010000 000001100 000010010 000001101 000000010 000000001 plus 4 xors, 4 input loads.

slide-33
SLIDE 33

Another example: 000000000 000010000 000001100 000000010 000001101 000000010 000000001 plus 5 xors, 4 input loads.

slide-34
SLIDE 34

Another example: 000000000 000000000 000001100 000000010 000001101 000000010 000000001 plus 5 xors, 5 input loads.

slide-35
SLIDE 35

Another example: 000000000 000000000 000001100 000000010 000000001 000000010 000000001 plus 6 xors, 5 input loads.

slide-36
SLIDE 36

Another example: 000000000 000000000 000000100 000000010 000000001 000000010 000000001 plus 7 xors, 6 input loads.

slide-37
SLIDE 37

Another example: 000000000 000000000 000000000 000000010 000000001 000000010 000000001 plus 7 xors, 7 input loads.

slide-38
SLIDE 38

Another example: 000000000 000000000 000000000 000000000 000000001 000000010 000000001 plus 7 xors, 7 input loads.

slide-39
SLIDE 39

Another example: 000000000 000000000 000000000 000000000 000000001 000000000 000000001 plus 7 xors, 8 input loads.

slide-40
SLIDE 40

Another example: 000000000 000000000 000000000 000000000 000000000 000000000 000000001 plus 7 xors, 8 input loads.

slide-41
SLIDE 41

Another example: 000000000 000000000 000000000 000000000 000000000 000000000 000000000 plus 7 xors, 9 input loads. Algorithm found the speedup.

slide-42
SLIDE 42

Another example: 000000000 000000000 000000000 000000000 000000000 000000000 000000000 plus 7 xors, 9 input loads. Algorithm found the speedup. Also has other useful features.

slide-43
SLIDE 43

Memory friendliness: Algorithm writes only to the output registers. No temporary storage.

n inputs, n outputs:

total 2

n registers

with 0 loads, 0 stores. Or

n + 1 registers

with

n loads, 0 stores:

each input is read only once. Or

n registers

with

n loads, 0 stores,

if platform has load-xor insn.

slide-44
SLIDE 44

Two-operand friendliness: Platform with

a a
  • b

but without

a b
  • uses only
n extra copies.

Naive column sweep also uses

n + 1 registers, n loads,

but usually many more xors. Input partitioning (e.g., 1956 Lupanov) uses somewhat more xors, copies; somewhat more registers. Greedy additive CSE uses somewhat fewer xors but many more copies, registers.

slide-45
SLIDE 45

For

m inputs and n outputs,

average

n
  • m matrix:

The xor-largest algorithm uses

  • mn= lg
n two-operand xors; n copies; m loads; n + 1 regs.
slide-46
SLIDE 46

For

m inputs and n outputs,

average

n
  • m matrix:

The xor-largest algorithm uses

  • mn= lg
n two-operand xors; n copies; m loads; n + 1 regs.

Pippenger’s algorithm uses

  • mn= lg
mn three-operand xors

but seems to need many regs. Pippenger proved that his algebraic complexity was near optimal for most matrices (at least without mod 2), but didn’t consider regs, two-operand complexity, etc.

slide-47
SLIDE 47

Case study of benefits produced by xor-largest: 131-bit conversion from poly basis to normal basis. “Random” 131

131 matrix.

On Cell (

1 xor per cycle,

128

  • registers) bitsliced

code took

9600 cycles.

Output of xor-largest: code with only 3380 xors fitting into 132 registers. Schwabe tuned asm for Cell:

4000 cycles.
slide-48
SLIDE 48

Inspiration: 1989 Bos–Coster. 000100000 = 32 000010000 = 16 100101100 = 300 010010010 = 146 001001101 = 77 000000010 = 2 000000001 = 1 Goal: Compute 32

x, 16x,

300x, 146x, 77x, 2x, 1x.

slide-49
SLIDE 49

Reduce largest row: 000100000 = 32 000010000 = 16 010011010 = 154 010010010 = 146 001001101 = 77 000000010 = 2 000000001 = 1 Integer subtraction

  • f 146 from 300.
slide-50
SLIDE 50

Reduce largest row: 000100000 = 32 000010000 = 16 000001000 = 8 010010010 = 146 001001101 = 77 000000010 = 2 000000001 = 1 plus 2 additions.

slide-51
SLIDE 51

Reduce largest row: 000100000 = 32 000010000 = 16 000001000 = 8 001000101 = 69 001001101 = 77 000000010 = 2 000000001 = 1 plus 3 additions.

slide-52
SLIDE 52

Reduce largest row: 000100000 = 32 000010000 = 16 000001000 = 8 001000101 = 69 000001000 = 8 000000010 = 2 000000001 = 1 plus 4 additions.

slide-53
SLIDE 53

Reduce largest row: 000100000 = 32 000010000 = 16 000001000 = 8 000100101 = 37 000001000 = 8 000000010 = 2 000000001 = 1 plus 5 additions.

slide-54
SLIDE 54

Reduce largest row: 000100000 = 32 000010000 = 16 000001000 = 8 000000101 = 5 000001000 = 8 000000010 = 2 000000001 = 1 plus 6 additions.

slide-55
SLIDE 55

Reduce largest row: 000010000 = 16 000010000 = 16 000001000 = 8 000000101 = 5 000001000 = 8 000000010 = 2 000000001 = 1 plus 7 additions.

slide-56
SLIDE 56

Reduce largest row: 000000000 = 0 000010000 = 16 000001000 = 8 000000101 = 5 000001000 = 8 000000010 = 2 000000001 = 1 plus 7 additions.

slide-57
SLIDE 57

Reduce largest row: 000000000 = 0 000001000 = 8 000001000 = 8 000000101 = 5 000001000 = 8 000000010 = 2 000000001 = 1 plus 8 additions.

slide-58
SLIDE 58

Reduce largest row: 000000000 = 0 000000000 = 0 000001000 = 8 000000101 = 5 000001000 = 8 000000010 = 2 000000001 = 1 plus 8 additions.

slide-59
SLIDE 59

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000101 = 5 000001000 = 8 000000010 = 2 000000001 = 1 plus 8 additions.

slide-60
SLIDE 60

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000101 = 5 000000011 = 3 000000010 = 2 000000001 = 1 plus 9 additions.

slide-61
SLIDE 61

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000010 = 2 000000011 = 3 000000010 = 2 000000001 = 1 plus 10 additions.

slide-62
SLIDE 62

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000010 = 2 000000001 = 1 000000010 = 2 000000001 = 1 plus 11 additions.

slide-63
SLIDE 63

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000001 = 1 000000010 = 2 000000001 = 1 plus 11 additions.

slide-64
SLIDE 64

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000001 = 1 000000001 = 1 000000001 = 1 plus 12 additions.

slide-65
SLIDE 65

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000001 = 1 000000001 = 1 plus 12 additions.

slide-66
SLIDE 66

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000001 = 1 plus 12 additions.

slide-67
SLIDE 67

Reduce largest row: 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 000000000 = 0 plus 12 additions. Final addition chain: 1, 2, 3, 5, 8, 16, 32, 37, 69, 77, 146, 154, 300. Short, no temporary storage, low two-operand complexity, etc.

slide-68
SLIDE 68

Can imagine many other mod-2 adaptations

  • f the Bos–Coster idea.

In reducing largest row: Why use largest of the remaining rows? Why not minimize xor? Out of first-bit-set rows: Why do largest row first? Why not start in middle,

  • r build Hamming tree?

Can reduce xors without compromising regs etc. I’m continuing to experiment.