예제 #1
0
파일: symbol.hpp 프로젝트: gendalph/FLIF
template <int bits, typename SymbolCoder> int reader(SymbolCoder& coder, int min, int max) {
    assert(min<=max);
    if (min == max) return min;

    bool sign;
    assert(min <= 0 && max >= 0); // should always be the case, because guess should always be in valid range
//    if (max >= 0 && min <= 0) {
      if (coder.read(BIT_ZERO)) return 0;
      if (min < 0) {
        if (max > 0) {
                sign = coder.read(BIT_SIGN);
                if (sign) min = 1; else max = -1;
        } else {sign = false; max=-1;}
      } else {sign = true; min=1;}
//    } else {
        // max < 0 || min > 0
//        if (min<0) sign = false;
//        else sign = true;
//    }

    const int amin = (sign? min : -max);
    const int amax = (sign? max : -min);

    const int emax = maniac::util::ilog2(amax);
    int e = maniac::util::ilog2(amin);

    //for (; e < emax; e++) {
    for (; e < emax; e++) {
        // if exponent >e is impossible, we are done
        // actually that cannot happen
        //if ((1 << (e+1)) > amax) break;
        if (coder.read(BIT_EXP,(e<<1)+sign)) break;
    }

    int have = (1 << e);
    int left = have-1;
    for (int pos = e; pos>0;) {
        //int bit = 1;
        //left ^= (1 << (--pos));
        left >>= 1; pos--;
        int minabs1 = have | (1<<pos);
        int maxabs0 = have | left;
        if (minabs1 > amax) { // 1-bit is impossible
            //bit = 0;
            continue;
        } else if (maxabs0 >= amin) { // 0-bit and 1-bit are both possible
            //bit = coder.read(BIT_MANT,pos);
            if (coder.read(BIT_MANT,pos)) have = minabs1;
        } // else 0-bit is impossible, so bit stays 1
        else have = minabs1;
        //have |= (bit << pos);
    }
    return (sign ? have : -have);
}
예제 #2
0
파일: symbol.hpp 프로젝트: kif/FLIF
template <int bits, typename SymbolCoder> int reader(SymbolCoder& coder, int min, int max)
{
    assert(min<=max);
    if (min == max) return min;

    bool sign;
    if (max >= 0 && min <= 0) {
      if (coder.read(BIT_ZERO)) return 0;
      if (min < 0) {
        if (max > 0) {
                sign = coder.read(BIT_SIGN);
                if (sign) min = 1; else max = -1;
        } else {sign = false; max=-1;}
      } else {sign = true; min=1;}
//      if (min == max) return min; // nodig?
    } else {
        if (min<0) sign = false;
        else sign = true;
    }


//    if (sign && min <= 0) min = 1;
//    if (!sign && max >= 0) max = -1;

    int amin = (sign? min : -max);
    int amax = (sign? max : -min);

    int emax = maniac::util::ilog2(amax);
    int i = maniac::util::ilog2(amin);

    for (; i < emax; i++) {
        // if exponent >i is impossible, we are done
        if ((1 << (i+1)) > amax) break;
        if (coder.read(BIT_EXP,i)) break;
    }
    int e = i;
    int have = (1 << e);
    int left = have-1;
    for (int pos = e; pos>0;) {
        int bit = 1;
        left ^= (1 << (--pos));
        int minabs1 = have | (1<<pos);
        int maxabs0 = have | left;
        if (minabs1 > amax) { // 1-bit is impossible
            bit = 0;
        } else if (maxabs0 >= amin) { // 0-bit and 1-bit are both possible
            bit = coder.read(BIT_MANT,pos);
        }
        have |= (bit << pos);
    }
    return (sign ? have : -have);
}
예제 #3
0
파일: symbol.hpp 프로젝트: gendalph/FLIF
template <typename SymbolCoder> int reader(SymbolCoder& coder, int bits) {
  int pos=0;
  int value=0;
  int b=1;
  while (pos++ < bits) {
    if (coder.read(BIT_MANT, pos)) value += b;
    b *= 2;
  }
  return value;
}