⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floatcompressornew.cpp

📁 对浮点型数据进行压缩
💻 CPP
📖 第 1 页 / 共 2 页
字号:

int FloatCompressorNew::compress_mantissa(int exponent, int mantissaPred, int mantissaReal, RangeEncoder* re_mantissa[3], RangeModel** rmMantissaBits)
{
  int c, c1, k, k1;

  // the corrector c will be within the interval [ - (2^23 - 1)  ...  + (2^23 - 1) 

  c = mantissaReal - mantissaPred;

  // we fold c into the interval [ - (2^22 - 1)  ...  + (2^22) ]

  if (c < (1-(1<<22)))
  {
    c += (1<<23);
  }
  else if (c > (1<<22))
  {
    c -= (1<<23); 
  }

  // now we find the tighest interval [ - (2^k - 1)  ...  + (2^k) ] that contains c

  k = 0;

  // we adjust c1 for positive c to eliminate special handling in case c is 2^k

  c1 = (c <= 0 ? -c : c-1);

  // this loop could be replaced with more efficient code

  while (c1)
  {
    c1 = c1 >> 1;
    k = k + 1;
  }

  // now the number k will be between 0 and 22

  if (rmMantissaBits[exponent] == 0)
  {
    rmMantissaBits[exponent] = new RangeModel(23,0,1,512,16);
  }

  // compress (in dependence on the exponent) the number of bits k+1 that we need to store to correct the mantissa

  re_mantissa[0]->encode(rmMantissaBits[exponent], k);

  // compress the k+1 bits of the mantissa corrector

  if (k < BITS_HIGH)
  {
    // we translate c into the interval [ 0 ...  + (2^(k+1)-1) ]

    c += ((1<<k) - 1);

    // allocate a new range table if necessary

    if (rmMantissa[k] == 0)
    {
      rmMantissa[k] = new RangeModel((1<<(k+1)),0,1,8192,16);
    }

    // compress c with the range coder

    re_mantissa[1]->encode(rmMantissa[k], c);
  }
  else
  {
    // we need to break up the k+1 bits in two smaller bit patterns

    k1 = k-BITS_HIGH+1;

    // c1 represents the lowest k-BITS_HIGH+1 bits
    // c represents the highest BITS_HIGH bits

    if (c < 0)
    {
      c1 = (-c) & ((1<<k1) - 1);
      c = -((-c) >> k1);
    }
    else
    {
      c1 = c & ((1<<k1) - 1);
      c = c >> k1;
    }

    // we translate c into the interval [ 0 ...  + (2^(BITS_HIGH)-1) ]

    c += ((1<<(BITS_HIGH-1)) - 1);

    // allocate a new range table if necessary

    if (rmMantissa[k] == 0)
    {
      if (rmMantissa[BITS_HIGH-1] == 0)
      {
        rmMantissa[BITS_HIGH-1] = new RangeModel(1<<BITS_HIGH,0,1,8192,16);
      }
      rmMantissa[k] = new RangeModel(1<<k1,0,1,2048,16);
    }

    re_mantissa[1]->encode(rmMantissa[BITS_HIGH-1], c);
    re_mantissa[2]->encode(rmMantissa[k], c1); 
  }

  return mantissaReal;
}

int FloatCompressorNew::decompress_mantissa(int exponent, int mantissaPred, RangeDecoder** rd_mantissa, RangeModel** rmMantissaBits)
{
  int k,k1,c,c1;

  if (rmMantissaBits[exponent] == 0)
  {
    // the number k will be between 0 and 22
    rmMantissaBits[exponent] = new RangeModel(23,0,0,512,16);
  }

  // now we decompress (in dependence on the exponent) the number of bits k+1 that the mantissa corrector has

  k = rd_mantissa[0]->decode(rmMantissaBits[exponent]);

  if (k < BITS_HIGH)
  {
    // allocate a new range table if necessary

    if (rmMantissa[k] == 0)
    {
      rmMantissa[k] = new RangeModel((1<<(k+1)),0,0,8192,16);
    }

    // decode a corrector within the interval [ 0 ... (2^(k+1)-1) ] 

    c = rd_mantissa[1]->decode(rmMantissa[k]);

    // translate c back into the interval [ 2^(k)-1 ...  2^(k) ]

    c -= ((1<<k) - 1); 
  }
  else
  {
    // we need to break it up in two bit patterns

    k1 = k-BITS_HIGH+1;

    // allocate a new range table if necessary

    if (rmMantissa[k] == 0)
    {
//      rmMantissa[k] = new RangeModel(1<<BITS_HIGH,0,0,8192,16); 
      if (rmMantissa[BITS_HIGH-1] == 0)
      {
        rmMantissa[BITS_HIGH-1] = new RangeModel(1<<BITS_HIGH,0,0,8192,16);
      }
      rmMantissa[k] = new RangeModel(1<<k1,0,0,2048,16);
    }

    // c1 represents the lowest k-BITS_HIGH+1 bits
    // c represents the highest BITS_HIGH bits (inclusive of the corrector's sign)

    c = rd_mantissa[1]->decode(rmMantissa[BITS_HIGH-1]);
    c1 = rd_mantissa[2]->decode(rmMantissa[k]);

    // we translate c back into the interval [ 2^(BITS_HIGH-1)-1 ...  2^(BITS_HIGH-1) ]

    c -= ((1<<(BITS_HIGH-1)) - 1);

    // add the lower bits stored in c1 back into the number

    if (c < 0)
    {
      c = -( ((-c) << k1) | c1);
    }
    else
    {
      c = (c << k1) | c1;
    }
  }

  int mantissaReal = c + mantissaPred;

  // we fold the mantissa into the interval [ 0  ...  + (2^23) - 1 ]

  if (mantissaReal < 0)
  {
    return mantissaReal + (1<<23);
  }
  else if (mantissaReal >= (1<<23))
  {
    return mantissaReal - (1<<23);
  }
  else
  {
    return mantissaReal;
  }
}

F32 FloatCompressorNew::CompressNone(F32 fReal)
{
  I32 signexponentPred = (((U32&)last_float) & 0xFF800000) >> 23;
  I32 mantissaPred = (((U32&)last_float) & 0x007FFFFF);

  I32 signexponentReal = (((U32&)fReal) & 0xFF800000) >> 23;
  I32 mantissaReal = (((U32&)fReal) & 0x007FFFFF);

  // compress sign and exponent
  compress_signexponent(signexponentPred,signexponentReal,ae_signexponent_none,rmSignExponentNone);

  // compress mantissa
  if (signexponentPred == signexponentReal) // sign and exponent predicted correctly
  {
    mantissaReal = compress_mantissa(signexponentReal&0xFF,mantissaPred,mantissaReal,ae_mantissa_none,rmMantissaBitsNone);
  }
  else 
  {
    mantissaReal = compress_mantissa(signexponentReal&0xFF,0x0,mantissaReal,ae_mantissa_none,rmMantissaBitsNone);
  }

  last_float = fReal;

  return fReal;
}

F32 FloatCompressorNew::DecompressNone()
{
  F32 fReal;

  I32 signexponentReal;
  I32 mantissaReal;

  I32 signexponentPred = (((U32&)last_float) & 0xFF800000) >> 23;
  I32 mantissaPred = (((U32&)last_float) & 0x007FFFFF);

  // decompress sign and exponent
  signexponentReal = decompress_signexponent(signexponentPred,ad_signexponent_none,rmSignExponentNone);

  // decompress mantissa
  if (signexponentPred == signexponentReal) // sign and exponent predicted correctly
  {
    mantissaReal = decompress_mantissa(signexponentReal&0xFF,mantissaPred,ad_mantissa_none,rmMantissaBitsNone);
  }
  else
  {
    mantissaReal = decompress_mantissa(signexponentReal&0xFF,0x0,ad_mantissa_none,rmMantissaBitsNone);
  }

  // put together decompressed float value
  ((U32&)fReal) = (signexponentReal << 23) | mantissaReal;

  last_float = fReal;

  return fReal;
}

F32 FloatCompressorNew::CompressLast(F32 fPred, F32 fReal)
{
  I32 signexponentPred = (((U32&)fPred) & 0xFF800000) >> 23;
  I32 mantissaPred = (((U32&)fPred) & 0x007FFFFF);

  I32 signexponentReal = (((U32&)fReal) & 0xFF800000) >> 23;
  I32 mantissaReal = (((U32&)fReal) & 0x007FFFFF);

  // compress sign and exponent
  compress_signexponent(signexponentPred,signexponentReal,ae_signexponent_last,rmSignExponentLast);

  // compress mantissa
  if (signexponentPred == signexponentReal) // sign and exponent predicted correctly
  {
    mantissaReal = compress_mantissa(signexponentReal&0xFF,mantissaPred,mantissaReal,ae_mantissa_last,rmMantissaBitsLast);
  }
  else 
  {
    mantissaReal = compress_mantissa(signexponentReal&0xFF,0x0,mantissaReal,ae_mantissa_last,rmMantissaBitsLast);
  }

  last_float = fReal;

  return fReal;
}

F32 FloatCompressorNew::DecompressLast(F32 fPred)
{
  F32 fReal;

  I32 signexponentReal;
  I32 mantissaReal;

  I32 signexponentPred = (((U32&)fPred) & 0xFF800000) >> 23;
  I32 mantissaPred = (((U32&)fPred) & 0x007FFFFF);

  // decompress sign and exponent
  signexponentReal = decompress_signexponent(signexponentPred,ad_signexponent_last,rmSignExponentLast);

  // decompress mantissa
  if (signexponentPred == signexponentReal) // sign and exponent predicted correctly
  {
    mantissaReal = decompress_mantissa(signexponentReal&0xFF,mantissaPred,ad_mantissa_last,rmMantissaBitsLast);
  }
  else
  {
    mantissaReal = decompress_mantissa(signexponentReal&0xFF,0x0,ad_mantissa_last,rmMantissaBitsLast);
  }

  // put together decompressed float value
  ((U32&)fReal) = (signexponentReal << 23) | mantissaReal;

  last_float = fReal;

  return fReal;
}

F32 FloatCompressorNew::CompressAcross(F32 fPred, F32 fReal)
{
	//提取预测值和真实值的signexponet及mantissa
  I32 signexponentPred = (((U32&)fPred) & 0xFF800000) >> 23;
  I32 mantissaPred = (((U32&)fPred) & 0x007FFFFF);

  I32 signexponentReal = (((U32&)fReal) & 0xFF800000) >> 23;
  I32 mantissaReal = (((U32&)fReal) & 0x007FFFFF);

  // compress sign and exponent
  compress_signexponent(signexponentPred,signexponentReal,ae_signexponent_across,rmSignExponentAcross);

  // compress mantissa
  if (signexponentPred == signexponentReal) // sign and exponent predicted correctly
  {
    mantissaReal = compress_mantissa(signexponentReal&0xFF,mantissaPred,mantissaReal,ae_mantissa_across,rmMantissaBitsAcross);
  }
  else
  {
    mantissaReal = compress_mantissa(signexponentReal&0xFF,0x0,mantissaReal,ae_mantissa_across,rmMantissaBitsAcross);
  }

  last_float = fReal;

  return fReal;
}

F32 FloatCompressorNew::DecompressAcross(F32 fPred)
{
  F32 fReal;

  I32 signexponentReal;
  I32 mantissaReal;

  I32 signexponentPred = (((U32&)fPred) & 0xFF800000) >> 23;
  I32 mantissaPred = (((U32&)fPred) & 0x007FFFFF);

  // decompress sign and exponent
  signexponentReal = decompress_signexponent(signexponentPred,ad_signexponent_across,rmSignExponentAcross);

  // decompress mantissa
  if (signexponentPred == signexponentReal) // sign and exponent predicted correctly
  {
    mantissaReal = decompress_mantissa(signexponentReal&0xFF,mantissaPred,ad_mantissa_across,rmMantissaBitsAcross);
  }
  else
  {
    mantissaReal = decompress_mantissa(signexponentReal&0xFF,0x0,ad_mantissa_across,rmMantissaBitsAcross);
  }

  // put together decompressed float value
  ((U32&)fReal) = (signexponentReal << 23) | mantissaReal;

  last_float = fReal;

  return fReal;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -