csapp lab1 data-lab记录(四)

11.unsigned floatScale2(unsigned uf)
//float
/* 
 * floatScale2 - Return bit-level equivalent of expression 2*f for
 *   floating point argument f.
 *   Both the argument and result are passed as unsigned int's, but
 *   they are to be interpreted as the bit-level representation of
 *   single-precision floating point values.
 *   When argument is NaN, return argument
 *   Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
 *   Max ops: 30
 *   Rating: 4
 */
unsigned floatScale2(unsigned uf) {
    unsigned s = (uf >> 31) & (0x1);
    unsigned expr = (uf >>23) & (0xFF);
    unsigned frac = (uf & 0x7FFFFF);
    
    if(expr == 0 && frac == 0)
    {
        return uf;
    }
    if(expr == 0xFF)
    {
        return uf;
    }
    if(expr == 0)
    {
        frac <<= 1;
        return (s <<31) | frac;
    }
    //规格化    
    expr++;
    //E = expr - 127
    return (s<<31)| (expr <<23) | (frac);

}
 12.int floatFloat2Int(unsigned uf)
/* 
 * floatFloat2Int - Return bit-level equivalent of expression (int) f
 *   for floating point argument f.
 *   Argument is passed as unsigned int, but
 *   it is to be interpreted as the bit-level representation of a
 *   single-precision floating point value.
 *   Anything out of range (including NaN and infinity) should return
 *   0x80000000u.
 *   Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
 *   Max ops: 30
 *   Rating: 4
 */
int floatFloat2Int(unsigned uf) {
  unsigned sign = uf>>31;
  unsigned exp = (uf & 0x7F800000) >>23;
  unsigned frac = uf & 0x7FFFFF;
 
  int result = 0;
  int E = exp - 127;
 
  frac = frac | 0x00900000;
 
  if(E < 0) return 0;
  if(E > 31) return 0x80000000u;
  
  if(E >23) result = frac<<(E - 23);
  else result = frac >> (23 - E);
 
  if(sign == 1)
    return (~result) + 1;
  else
    return result;
}
13.floatPower2
/* 
 * floatPower2 - Return bit-level equivalent of the expression 2.0^x
 *   (2.0 raised to the power x) for any 32-bit integer x.
 *
 *   The unsigned value that is returned should have the identical bit
 *   representation as the single-precision floating-point number 2.0^x.
 *   If the result is too small to be represented as a denorm, return
 *   0. If too large, return +INF.
 * 
 *   Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while 
 *   Max ops: 30 
 *   Rating: 4
 */
unsigned floatPower2(int x) {
  int exp;
  unsigned ret;
 
  if(x < -149) return 0; //值小返回0
  if(x > 127) return (0xFF << 23);//值大返回无穷
  //非规格化数最接近0 exp = 0 , f = 0000 ... 0000 0001 
  //E = 1-bias = -126    M = f = 2 ^ -23    x == -149
  //exp = 254, E = exp - 127, Emax = 127   x>127  返回正无穷
 
  //有一部分接近零,要用非规表示
  //exp = 1, E = exp - bias(127) = -126  Vmin = -126   -147 ~ -127用非规
 
  //-126 ~ 127 规格化的数
 
 
  if(x < -126) return 0x1 << (x + 149);
  exp = x + 127;
  ret = exp << 23;
  return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值