Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] comparing unsigned numbers

Status
Not open for further replies.

nsgil85

Member level 4
Joined
Dec 11, 2012
Messages
73
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,832
Hi everyone

What is the syntex for comparing 2 unsigned number with diffrent length
for example:

signal sig_A :unsigned(3 downto 0) := to_unsigned(1, sig_A 'range) ;
signal sig_B :unsigned(2 downto 0) := to_unsigned(3, sig_B 'range) ;

if sigB > sig_A then
...


will i need to convert both numbers first to integer and then compare?


Thanks
Gil
 

vGoodtimes

Advanced Member level 4
Joined
Feb 16, 2015
Messages
1,089
Helped
307
Reputation
614
Reaction score
303
Trophy points
83
Activity points
8,730
you can compare unsigned values of different length, there are no special requirements. The greater/less operators will treat the comparison as a numeric comparison between unsigned values.

Some developers will also compare std_logic_vectors of equal length using greater/less. The comparison is done lexically, which matches a comparison between unsigned values. But only when the values don't have 'Z' or 'X' or etc... and are of equal length.


Basically, there is no need to change sizes if the types are "unsigned". You may see people resize the values because they had issues (incorrectly) comparing std_logic_vectors in the past.
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,068
Helped
2,078
Reputation
4,173
Reaction score
2,031
Trophy points
1,393
Activity points
39,145
Some developers will also compare std_logic_vectors of equal length using greater/less. The comparison is done lexically, which matches a comparison between unsigned values. But only when the values don't have 'Z' or 'X' or etc... and are of equal length.

Unless you're using the non-standard std_logic_unsigned library (or the standard 2008 library numeric_std_unsigned) then the results from this can be counter intuitive and give odd simulation results. I highly recommend you use the numeric_std library type unsigned.
 

nsgil85

Member level 4
Joined
Dec 11, 2012
Messages
73
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,832
thank you for the advice

Ok, so what can be done to compare variable with signal unsigned type?

BTY
I use numeric_std lib

Gil
 

c_mitra

Advanced Member level 5
Joined
Nov 13, 2012
Messages
3,684
Helped
914
Reputation
1,830
Reaction score
891
Trophy points
1,393
Activity points
28,865
will i need to convert both numbers first to integer and then compare?

This is strongly compiler dependent; if in doubt use assembler instructions (perhaps inline)
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,068
Helped
2,078
Reputation
4,173
Reaction score
2,031
Trophy points
1,393
Activity points
39,145
This is strongly compiler dependent; if in doubt use assembler instructions (perhaps inline)

The OP is talking about HDL - not CPU programming!
HDL has no assembler instructions.

Ok, so what can be done to compare variable with signal unsigned type?

You can just compare two unsigned directly. No need to modify anything about them.
Do you have a specific use case you want help with?
 

nsgil85

Member level 4
Joined
Dec 11, 2012
Messages
73
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,832
No assembler for today :)

TrickyDicky, yes as following:


signal sig_C :unsigned(2 downto 0) := to_unsigned(3, sig_C 'range) ;

process:
variable var_D :unsigned(3 downto 0) := to_unsigned(1, sig_D 'range) ;
begin
if var_D > sig_C then
...

thanks
Gil
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,068
Helped
2,078
Reputation
4,173
Reaction score
2,031
Trophy points
1,393
Activity points
39,145
What exactly do you want help with - it should work as written - always false because 1 is not greater than 3
 

nsgil85

Member level 4
Joined
Dec 11, 2012
Messages
73
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,832
found my mistake, it didn't compile due to range issue,

Thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top