P
Patrick Dubois
Guest
Hello,
I just realized something quite strange with the ADD operator of
numeric_std. Say I try to add 2 numbers:
signal A,B unsigned8 downto 0);
signal C unsigned(9 downto 0);
C <= A + B;
This does not work because apprently, the ADD operator returns a bit
width that is the largest of A or B, missing one bit from the
"normally expected" behavior. The simulator returns a "incompatible
range" error. Now, I tried this at first:
C <= resize(A+B, C'length);
It simulates but doesn't give the right answer because A+B is only 9
bits (according to the ADD operator) so resizing it to 10 bits after
the fact just adds one 0. The correct way of doing this is therefore:
C <= (resize(A, C'length) + resize(B, C'length));
Doesn't this seem a little crazy? Why doesn't ADD return one more bit
in the first place? This is so tricky that even Ray Andraka fell into
the trap:
http://tinyurl.com/3akf3o
Patrick
I just realized something quite strange with the ADD operator of
numeric_std. Say I try to add 2 numbers:
signal A,B unsigned8 downto 0);
signal C unsigned(9 downto 0);
C <= A + B;
This does not work because apprently, the ADD operator returns a bit
width that is the largest of A or B, missing one bit from the
"normally expected" behavior. The simulator returns a "incompatible
range" error. Now, I tried this at first:
C <= resize(A+B, C'length);
It simulates but doesn't give the right answer because A+B is only 9
bits (according to the ADD operator) so resizing it to 10 bits after
the fact just adds one 0. The correct way of doing this is therefore:
C <= (resize(A, C'length) + resize(B, C'length));
Doesn't this seem a little crazy? Why doesn't ADD return one more bit
in the first place? This is so tricky that even Ray Andraka fell into
the trap:
http://tinyurl.com/3akf3o
Patrick