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

From: David Bishop <dbishop_at_.....>
Date: Mon Apr 25 2005 - 13:39:55 PDT
> A16  FP         David         Issue:  Compare w/ integers & reals if only have  
>                               negative index results are incorrect. 
>                               Document and post to reflector. 

This has been a sticky issue for some time in the fixed point package.
I went around this one with several users, and this is what we came up
with:

ufixed to natural =
     return (l = to_ufixed (arg            => r,
                            left_index     => maximum (l'high+1, 0),
                            right_index    => 0,        -- always 0 for integer
                            overflow_style => true,     -- always saturate
                            round_style    => false));  -- rounding not needed

thus:
variable a : ufixed (3 downto -3) := from_string("1111.111"); -- 15.875
if a = 16 returns false.
variable b : ufixed (-1 downto -3) := "000"; -- 0.5
if b = 0 returns true, anything else would return false.

For sfixed, I do:
    return (l = to_sfixed (arg            => r,
                            left_index     => maximum (l'high+1, 1),
                            right_index    => 0,
                            overflow_style => true,     -- always saturate
                            round_style    => false));  -- no rounding needed
I need one more bit fore the sign.


ufixed to real =
    return (l = to_ufixed (arg            => r,
                        left_index     => l'high + 1,
                        right_index    => l'low,
                        overflow_style => true,  -- always saturate REAL
                        round_style    => fixed_round, -- defaults to true
                        guard_bits     => fixed_guard_bits)); -- defaults to 3

This fixes the above case, but you can also do:
variable a : ufixed (3 downto -5) := from_string("0000.01011"); -- 1/3 rounded
if a = (1.0/3.0) returns true.

For "sfixed = REAL" I do the same as I do for ufixed = real.

Remember the resize rules for the compare operations:

     constant left_index       : INTEGER := maximum(l'high, r'high);
     constant right_index      : INTEGER := mins(l'low, r'low);
     variable lresize, rresize : ufixed (left_index downto right_index);
begin
     lresize := resize (l, left_index, right_index);
     rresize := resize (r, left_index, right_index);



-- 
David W. Bishop dbishop@vhdl.org       All standard disclaimers apply.
Received on Mon Apr 25 13:40:03 2005

This archive was generated by hypermail 2.1.8 : Mon Apr 25 2005 - 13:40:55 PDT