减少比较

上一篇 / 下一篇  2007-12-11 16:19:05 / 个人分类:示例代码

一直对于比较不太友好,现在要从9个数据中找最大值,自然我们可以使用比较实现
-- 块浮点的指数值判决
        function Jexp (Pexp,ai,aq,bi,bq,ci,cq,di,dq:std_logic_vector) return std_logic_vector is
        variable Cexp :std_logic_vector(1 downto 0); -- 0~~3
        variable Ti,Tq,temp :std_logic_vector(2 downto 0);

    begin
        Ti := (ai or bi) or (ci or di);
        Tq := (aq or bq) or (cq or dq);
        temp := Ti or Tq;
        if(temp(2)='1') then -- 第一列有 1,溢出三位
            Cexp := "11";
        elsif(temp(1)='1') then   
            Cexp := "10";
        elsif(temp(0)='1') then   
            Cexp := "01";
        else Cexp := "00";
        end if;
        if Pexp >= Cexp then -- 非 3,即不是最大溢出
            return Pexp;
        else
            return Cexp;
        end if;
    end function Jexp;
为了把比较减少,可以使用查表法
        function Jexp (Pexp,ai,aq,bi,bq,ci,cq,di,dq:std_logic_vector) return std_logic_vector is
        variable Cexp :std_logic_vector(1 downto 0); -- 0~~3
        variable Ti,Tq,temp :std_logic_vector(2 downto 0);
       
        type rom_type is array(0 to 7) of Std_Logic_Vector(1 downto 0);
        constant table : rom_type := ("00","01","10","10","11","11","11","11");

    begin
        Ti := (ai or bi) or (ci or di);
        Tq := (aq or bq) or (cq or dq);
        temp := Ti or Tq;
        Cexp := table(conv_integer(temp));
        if Pexp >= Cexp then
            return Pexp;
        else
            return Cexp;
        end if;
    end function Jexp;

相关阅读:

TAG: 比较 查表 优化

zhongyunde的个人空间 引用 删除 zhongyunde   /   2007-12-11 17:22:35
function Jexp (Pexp,ai,aq,bi,bq,ci,cq,di,dq:std_logic_vector) return std_logic_vector is
                variable Cexp :std_logic_vector(1 downto 0); -- 0~~3
                variable Ti,Tq,temp :std_logic_vector(2 downto 0);
               
                type rom_type is array(0 to 7) of Std_Logic_Vector(1 downto 0);
                constant table : rom_type := ("00","01","10","10","11","11","11","11");
                variable result :std_logic_vector(1 downto 0);
        begin
                Ti := (ai or bi) or (ci or di);
                Tq := (aq or bq) or (cq or dq);
                temp := Ti or Tq;
               
                Cexp := table(conv_integer(temp));
                result(1) := Cexp(1) or Pexp(1);
                result(0) := (Cexp(1) and Cexp(0)) or (Pexp(1) and Pexp(0)) or
                         (not Pexp(1) and Cexp(0)) or (not Cexp(1) and Pexp(0));
                return result;
               
        end function Jexp;
进一步优化,把最会的 if 语句也去掉
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-12-12  
 123456
78910111213
14151617181920
21222324252627
28293031   

数据统计

  • 访问量: 843
  • 日志数: 7
  • 文件数: 1
  • 建立时间: 2007-03-16
  • 更新时间: 2007-12-16

RSS订阅

Open Toolbar