# LUT5

11 Dec 2012 20:53 - 11 Dec 2012 20:59 #27578 by BigJohnT
LUT5 was created by BigJohnT
I'm still trying to get a grip on the lut5 component so I can improve the manual.

The truth table seems to only show the "and" part
```Bits
4	3	2	1	0	Weight
0	0	0	0	0	0x1
0	0	0	0	1	0x2
0	0	0	1	0	0x4
0	0	0	1	1	0x8
0	0	1	0	0	0x10```

I tried making sense of the hex to binary conversion but that doesn't make sense either.
```binary conversion from hex
0	0	0	0	1	0x1
0	0	0	1	0	0x2
0	0	1	0	0	0x4```

So then I went to look at the code in the comp and that didn't make much sense either.
```FUNCTION(_) {
int shift = 0;
if(in_0) shift += 1;
if(in_1) shift += 2;
if(in_2) shift += 4;
if(in_3) shift += 8;
if(in_4) shift += 16;

out = (function & (1<<shift)) != 0;
}```

The man page hints that the lut5 component is magical and has three types of functions 'and', 'or', and 'xor' but the truth table only shows the 'and' I think as the or function uses 0xfffffffe and that number is not in the truth table. Some experimentation shows that you can combine and and xor in some ways so that is confusing too.

the man page wrote: A 5-input and function is TRUE only when all the inputs are true, so the correct value for function is 0x80000000.

A 5-input or function is TRUE whenever any of the inputs are true, so the correct value for function is 0xfffffffe.

A 2-input xor function is TRUE whenever exactly one of the inputs is true, so the correct value for function is 0x6. Only in-0 and in-1 should be connected to signals, because if any other bit is TRUE then the output will be FALSE.

I know Peter has been trying to explain this to me in another thread but I didn't want to hijack that thread any more. I suspect that bit shifting plays a part in this with the pattern between the truth table and a straight binary conversion of the hex number.

Thanks
John
Last edit: 11 Dec 2012 20:59 by BigJohnT.

Please Log in or Create an account to join the conversation.

11 Dec 2012 21:38 #27579
Replied by cncbasher on topic LUT5
just to make your brain hurt more John ,
en.wikipedia.org/wiki/Bitwise_operation

Please Log in or Create an account to join the conversation.

11 Dec 2012 21:47 - 11 Dec 2012 21:49 #27580 by BigJohnT
Replied by BigJohnT on topic LUT5
I am somewhat familiar with bit masking from PLC programming I've done in the past and I assumed that was how lut5 was working somehow.
```    0011 (decimal 3)
AND 0010 (decimal 2)
= 0010 (decimal 2)```
What eludes me is how to determine what numbers to use for the xor and or functions as they are not in the truth table.

John
Last edit: 11 Dec 2012 21:49 by BigJohnT.

Please Log in or Create an account to join the conversation.

11 Dec 2012 23:17 #27583
Replied by mhaberler on topic LUT5

Please Log in or Create an account to join the conversation.

11 Dec 2012 23:20 #27584 by PCW
Replied by PCW on topic LUT5
Note that LUT5 will generate any of the 4,294,967,296
logical functions of 5 inputs so AND,OR,NAND,NOR,XOR
and every other combinatorial function is possible.

I think its easier to not look at it as a logical function generator
but rather as a lookup table. When looked at this way it becomes
simple to generate any logical function of 5 inputs.

Heres how:

write all the input states down (32 for all 5 inputs 16 for 4 inputs , 8 for 3 inputs. 4 for 2 inputs ) with the weight and the desired output state:

Inputs Weight Output
0 0 0 0 0 1 0
0 0 0 0 1 2 1
0 0 0 1 0 4 1
0 0 0 1 1 8 0

When done, add the weights of all the rows with a '1' output together and voila you have the function value for your desired logic. (6 in this case or 2 input XOR)

This could be done with a spreadsheet/python widget fairly easily

Please Log in or Create an account to join the conversation.

12 Dec 2012 20:59 #27622 by BigJohnT
Replied by BigJohnT on topic LUT5

mhaberler wrote: see here: wiki.linuxcnc.org/cgi-bin/wiki.pl?Lut5

-m

The program is flawed, it only seems to work for all 5 inputs. For example if I put :
```./lut5.py 'i0 &  i1'
# expression = i0 &  i1
...
# setp lut5.N.function 0x88888888```

but the real answer is 0x8.

If I put
```./lut5.py 'i0 &  i1 &  i2 &  i3 &  i4'
# expression = i0 &  i1 &  i2 &  i3 &  i4
...
# setp lut5.N.function 0x80000000```

which is correct.

Darn it!

John

Please Log in or Create an account to join the conversation.

12 Dec 2012 22:11 #27626 by andypugh
Replied by andypugh on topic LUT5

BigJohnT wrote: The program is flawed, it only seems to work for all 5 inputs.

-n ?

Please Log in or Create an account to join the conversation.

12 Dec 2012 22:34 #27628
Replied by mhaberler on topic LUT5

BigJohnT wrote:

mhaberler wrote: see here: wiki.linuxcnc.org/cgi-bin/wiki.pl?Lut5

-m

The program is flawed, it only seems to work for all 5 inputs. For example if I put :
```./lut5.py 'i0 &  i1'
# expression = i0 &  i1
...
# setp lut5.N.function 0x88888888```

but the real answer is 0x8.

No it isnt, go try in HAL
your expression implies 'ignore i2,i3,i4' and the right thing happens with 0x88888888 - the output reacts only to i0 and i1
with 0x8000000 all of i0-i4 must be true which is not reflected in your expression

-m

If I put
```./lut5.py 'i0 &  i1 &  i2 &  i3 &  i4'
# expression = i0 &  i1 &  i2 &  i3 &  i4
...
# setp lut5.N.function 0x80000000```

which is correct.

Darn it!

John

Please Log in or Create an account to join the conversation.

12 Dec 2012 22:35 #27629 by BigJohnT
Replied by BigJohnT on topic LUT5
opps, it seems I'm flawed...

Please Log in or Create an account to join the conversation.

12 Dec 2012 22:41 - 12 Dec 2012 22:43 #27630 by BigJohnT
Replied by BigJohnT on topic LUT5

PCW wrote: Note that LUT5 will generate any of the 4,294,967,296
logical functions of 5 inputs so AND,OR,NAND,NOR,XOR
and every other combinatorial function is possible.

I think its easier to not look at it as a logical function generator
but rather as a lookup table. When looked at this way it becomes
simple to generate any logical function of 5 inputs.

Heres how:

write all the input states down (32 for all 5 inputs 16 for 4 inputs , 8 for 3 inputs. 4 for 2 inputs ) with the weight and the desired output state:

Inputs Weight Output
0 0 0 0 0 1 0
0 0 0 0 1 2 1
0 0 0 1 0 4 1
0 0 0 1 1 8 0

When done, add the weights of all the rows with a '1' output together and voilĂ  you have the function value for your desired logic. (6 in this case or 2 input XOR)

This could be done with a spreadsheet/python widget fairly easily

Now I finally understand.... for a 5 input or function it is all of them but one! I finally can make this work in my head... now to try and put this in layman's terms for the docs.
```./lut5.py 'i0 |  i1 |  i2 |  i3 |  i4'
# expression = i0 |  i1 |  i2 |  i3 |  i4
#in: i4 i3 i2 i1 i0 out weight
# 0:  0  0  0  0  0  0
# 1:  0  0  0  0  1  1   0x2
# 2:  0  0  0  1  0  1   0x4
# 3:  0  0  0  1  1  1   0x8
# 4:  0  0  1  0  0  1   0x10
# 5:  0  0  1  0  1  1   0x20
# 6:  0  0  1  1  0  1   0x40
# 7:  0  0  1  1  1  1   0x80
# 8:  0  1  0  0  0  1   0x100
# 9:  0  1  0  0  1  1   0x200
#10:  0  1  0  1  0  1   0x400
#11:  0  1  0  1  1  1   0x800
#12:  0  1  1  0  0  1   0x1000
#13:  0  1  1  0  1  1   0x2000
#14:  0  1  1  1  0  1   0x4000
#15:  0  1  1  1  1  1   0x8000
#16:  1  0  0  0  0  1   0x10000
#17:  1  0  0  0  1  1   0x20000
#18:  1  0  0  1  0  1   0x40000
#19:  1  0  0  1  1  1   0x80000
#20:  1  0  1  0  0  1   0x100000
#21:  1  0  1  0  1  1   0x200000
#22:  1  0  1  1  0  1   0x400000
#23:  1  0  1  1  1  1   0x800000
#24:  1  1  0  0  0  1   0x1000000
#25:  1  1  0  0  1  1   0x2000000
#26:  1  1  0  1  0  1   0x4000000
#27:  1  1  0  1  1  1   0x8000000
#28:  1  1  1  0  0  1   0x10000000
#29:  1  1  1  0  1  1   0x20000000
#30:  1  1  1  1  0  1   0x40000000
#31:  1  1  1  1  1  1   0x80000000
# setp lut5.N.function 0xfffffffe```

Thanks guys for taking the time to beat this in between my ears.

John
Last edit: 12 Dec 2012 22:43 by BigJohnT.

Please Log in or Create an account to join the conversation.

Time to create page: 0.117 seconds