[vhdl-200x-ft] Fixed point compare with integer and real

From: David Bishop <dbishop_at_.....>
Date: Wed Mar 02 2005 - 08:06:56 PST
While playing with the fixed point packages I came up with the following problem:

xxx : ufixed (-2 downto -3);
xxx <= "11"; -- 0.375

if 1 = xxx then ....

Originally I had:
function "=" (l : natural, r : ufixed)
   variable l_var : ufixed (r'range);
begin
   l_var := to_ufixed (l, r'high, r'low);
   return l_var = r;

Since "l_var" would saturate to "11" = .375 then the result would
come back as "true" with a "vector truncated" warning.
Note that this also is a problem if we do this:
constant yyy : ufixed (3 downto 2) := "10"; -- 4
Then "5" will be rounded to "4" making
"5 = yyy" true.

So I modified it a little:

l_var := to_ufixed (arg => l,
                     left_index => maximum (r'high, 0),
                     right_index => 0);
In this case you can still saturate, but the compare becomes:
"1.000" = "0.011", which shows up as "false".

For signed fixed point I did this:
l_var := to_sfixed (arg => l,
                     left_index => maximum (r'high, 1),
                     right_index => 0);
To preserve the sign bit.

For a "real" to fixed point compare I also had overflow problems, so I did this:
l_var := to_ufixed (arg => l,
                     left_index => r'high+1,
                     right_index = r'low);
Which fixes overflow, but not underflow, which is not as much of a problem
with "real" as it is with integer.

Addmittedly this is a corner case, but it is worth doing right.

-- 
David W. Bishop dbishop@vhdl.org       All standard disclaimers apply.
Received on Wed Mar 2 08:06:59 2005

This archive was generated by hypermail 2.1.8 : Wed Mar 02 2005 - 08:08:16 PST