16位浮点转32位浮点

在DX中有着半精度浮点转单精度浮点的详细函数,我们先来了解一下数据的存储结构。

浮点结构

   type Bits1  is mod 2**1;
   type Bits4  is mod 2**4;
   type Bits5  is mod 2**5;
   type Bits7  is mod 2**7;
   type Bits8  is mod 2**8;
   type Bits10 is mod 2**10;
   type Bits22 is mod 2**22;
   type Bits23 is mod 2**23;


   type WORD is mod 2**16;
   type UINT is mod 2**32; 

    type FLOAT32(i:Integer:=0) is record
      case i is
         when 0=>
            Fraction:Bits23;
            Exponent:Bits8;
            Sign:Bits1;
         when 1=>
            Fr:Bits23;
            Exp:Bits7;
            Es:Bits1;
            S:Bits1;
         when 2=>
            f:Float;
         when others=>
            U:UINT;
      end case;
   end record with Unchecked_Union,Pack,size=>32;

   type HALF(i:Integer:=0) is record
      case i is
         when 0=>
            Mantissa:Bits10;
            Exponent:Bits5;
            Sign:Bits1;
         when 1=>
            Fr:Bits10;
            Exp:Bits4;
            Es:Bits1;
            S:Bits1;
         when others=>
            w:word;
      end case;
   end record with Unchecked_Union,Pack,size=>16;

    function Shift_Left(u:UINT;b:Natural) return uint with Import,Convention=>Intrinsic;
   function Lsh(u:uint;b:Natural) return uint is (Shift_Left(u,b));

   function Shift_Right(u:UINT;b:Natural) return uint with Import,Convention=>Intrinsic;
   function Rsh(u:uint;b:Natural) return uint is (Shift_Left(u,b));

   function ConvertHalfToFloat(h:HALF) return FLOAT32 is
      f32:FLOAT32;
      Man:uint:=uint(h.Mantissa);
      exp:uint:=uint(h.Exp);--*补码+偏置*
      esc:uint;--*原码*
   begin

      f32.Sign:=h.Sign;
      f32.Es:=h.Es;

      if exp=0 then
         if man/=0 then
            exp:=1;
            for i in 1..10 loop
               exp:=exp-1;
               man:=Lsh(man,1);
               exit when (man and 2#1_0000_0000_00#)/=0;
            end loop;
            man:=man and 2#1111_1111_11#;
         else
            f32.Fraction:=0;
            f32.Exponent:=0;
            return f32;
         end if;
      end if;


      esc:=(if h.es=1 then not (Exp-15-1) else not(Exp-15));

      f32.exp:=(if h.es=1 then Bits7((not esc)+1+127) else Bits7((not esc)+127));

      f32.Fraction:=Bits23(Lsh(Man,13));

      return f32;

   end;

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值