转换上浮到只使用位操作一个int(float2int) [英] Converting float to an int (float2int) using only bitwise manipulation

查看:219
本文介绍了转换上浮到只使用位操作一个int(float2int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果有人可以让我与我的工作有问题的正确方向。我想下面的C函数不使用的是什么只有ARM汇编以及位操作要做到:

  INT float2int(浮X){
回报(INT)X;
}

我已经codeD这个(int2float),而不许多问题相反。我只是不能确定在哪里这个新的问题开始。

例如:

  3(INT)= 0x40400000(浮点)
0011 = 0千万10000000000000000000000

其中0为符号位,千万是指数,而10000000000000000000000是尾数/分。

有人能简单地指出我这个问题的正确方向?即使是一个C伪code再presentation将是有益的。我知道我需要提取符号位,提取指数和反向偏压(127),并提取部分,但我只是不知道从哪里开始。

还有,如果浮子不能重新presented为整数的问题(因为它溢出或为NaN)。

任何帮助将是AP preciated!


解决方案

  //假设int可以容纳所有的浮点的precision。
INT float2int(浮X){
  INT注册= f_SignRawBit(X);
  无符号的尾数为f_RawMantissaBits(X); // 0 - 0x7FFFFF
  INT世博= f_RawExpoBits(X); // 0 - 255
  //形成正确的指数和尾数
  如果(世博== EXPO_MAX){
    Handle_NAN_INF();
  }
  否则,如果(世博== EXPO_MIN){
    世博+ = BIAS + 1 - MantissaOffset / * 23 * /;
  }
  其他{
    世博+ = BIAS - MantissaOffset / * 23 * /;
    尾数| = ImpliedBit;
  }
  而(世博会大于0){
    Expo--;
    //添加code检测溢出
    尾数* = 2;
  }
  而(博览会暨小于0){
    世博++;
    //添加code要注意最后移出位
    //添加code注意如果有非零位移出
    尾数/ = 2;
  }  //添加四舍五入code取决于`最后一个移出bit`和'非零位移位out`。如果向0四舍五入可能不被需要的。  //添加code检测过/欠流在以下
  如果(登录){
    返回-Mantissa;
  }
  返回尾数;
}

I am wondering if someone could set me in the right direction with a problem I am working on. I am trying to do what the following C function does using only ARM assembly and bit manipulation:

int float2int(float x) {
return (int) x;
}

I have already coded the reverse of this (int2float) without many issues. Im just unsure of where to start with this new problem.

For example:

3 (int) = 0x40400000 (float) 
0011 = 0 10000000 10000000000000000000000

Where 0 is the Sign Bit, 10000000 is the exponent, and 10000000000000000000000 is the mantissa/fraction.

Can someone simply point me in the right direction with this problem? Even a C pseudocode representation would be helpful. I know I need to extract the sign bit, extract the exponent and reverse the bias (127) and also extract the fraction but I just have no idea where to begin.

There is also the issue of if the float cannot be represented as an integer (because it overflows or is a NaN).

Any help would be appreciated!

解决方案

// Assume int can hold all the precision of a float.
int float2int(float x) {
  int Sign = f_SignRawBit(x);
  unsigned Mantissa = f_RawMantissaBits(x);  // 0 - 0x7FFFFF
  int Expo = f_RawExpoBits(x); // 0 - 255
  // Form correct exponent and mantissa
  if (Expo == EXPO_MAX) {
    Handle_NAN_INF();
  }
  else if (Expo == EXPO_MIN) {
    Expo += BIAS + 1 - MantissaOffset /* 23 */;
  }
  else {
    Expo += BIAS - MantissaOffset /* 23 */;
    Mantissa |= ImpliedBit;
  }
  while (Expo > 0) {
    Expo--;
    // Add code to detect overflow
    Mantissa *= 2;
  }
  while (Expo < 0) {
    Expo++;
    // Add code to note last shifted out bit
    // Add code to note if any non-zero bit shifted out
    Mantissa /= 2;
  }

  // Add rounding code depending on `last shifted out bit` and `non-zero bit shifted out`.  May not be need if rounding toward 0.

  // Add code to detect over/under flow in the following
  if (Sign) {
    return -Mantissa;
  }
  return Mantissa;
}

这篇关于转换上浮到只使用位操作一个int(float2int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆