// Create a Integer from the string n. void Integer::create(const char* n) { if (validate(n)) { int size = strlen(n); int i = 0, j = size - 1; if (n[0] == '-') { set_sign(MINUS); } else { set_sign(PLUS); } while (j >= 0 && n[j] != '-') { digits_[i++] = n[j--] - '0'; } set_size(i); adjust(); } else { // Zero by default set_sign(PLUS); set_size(1); digits_[0] = 0; } }
int PXtoLong(unsigned long long number, unsigned long long *ret, int type) { unsigned long long retval = 0; char *s = (char *)&number; char *d = (char *)&retval; switch (type) { case PX_Field_Type_Logical: copy_from_be(d,s,1); if (s[0] & 0x80) { fix_sign(d, 1); } else if (retval == 0) { return VALUE_IS_NULL; } else { set_sign(d, 1); retval = (char )retval; } break; case PX_Field_Type_ShortInt: copy_from_be(d,s,2); if (s[0] & 0x80) { fix_sign(d, 2); } else if (retval == 0) { return VALUE_IS_NULL; } else { set_sign(d, 2); retval = (short)retval; } break; case PX_Field_Type_Incremental: case PX_Field_Type_LongInt: copy_from_be(d,s,4); if (s[0] & 0x80) { fix_sign(d, 4); } else if (retval == 0) { return VALUE_IS_NULL; } else { set_sign(d, 4); retval = (long )retval; } break; default: fprintf(stderr,"%s.%d: Can't convert type (%02x)!\n", __FILE__, __LINE__, type); return VALUE_ERROR; } *ret = retval; return VALUE_OK; }
void mp_int<A,T>::sub_digit(digit_type b) { if (is_negative()) { const digit_type carry = ops_type::add_single_digit(digits_, digits_, size_, b); if (carry) push(carry); return; } if (size_ == 1) { if (digits_[0] < b) // example: 2 - 6 = -4 { digits_[0] = b - digits_[0]; set_sign(-1); } else // example: 8 - 7 = 1 or 5 - 5 = 0 digits_[0] -= b; } else { ops_type::subtract_single_digit(digits_, digits_, size_, b); if (!digits_[size_-1]) --size_; } }
/************************************************* * Construct a BigInt from a "raw" BigInt * *************************************************/ BigInt::BigInt(const BigInt& b) { const u32bit b_words = b.sig_words(); if(b_words) { reg.create(round_up(b_words, 8)); reg.copy(b.data(), b_words); set_sign(b.sign()); } else { reg.create(2); set_sign(Positive); } }
// ---------- Constructors ---------- Integer::Integer() { digits_ = new char[MAX_DIGITS]; set_size(1); digits_[0] = 0; set_sign(PLUS); }
/* * Modulo Operator */ word BigInt::operator%=(word mod) { if(mod == 0) throw BigInt::DivideByZero(); if(is_power_of_2(mod)) { word result = (word_at(0) & (mod - 1)); clear(); grow_to(2); m_reg[0] = result; return result; } word remainder = 0; for(size_t j = sig_words(); j > 0; --j) remainder = bigint_modop(remainder, word_at(j-1), mod); clear(); grow_to(2); if(remainder && sign() == BigInt::Negative) m_reg[0] = mod - remainder; else m_reg[0] = remainder; set_sign(BigInt::Positive); return word_at(0); }
/// Sets *this to the next representable number closer to plus infinity (greater /// = true) or minus infinity (greater = false). void ieee_floatt::next_representable(bool greater) { if(is_NaN()) return; bool old_sign=get_sign(); if(is_zero()) { unpack(1); set_sign(!greater); return; } if(is_infinity()) { if(get_sign()==greater) { make_fltmax(); set_sign(old_sign); } return; } bool dir; if(greater) dir=!get_sign(); else dir=get_sign(); set_sign(false); mp_integer old=pack(); if(dir) ++old; else --old; unpack(old); // sign change impossible (zero case caught earler) set_sign(old_sign); }
/************************************************* * Construct a BigInt from a string * *************************************************/ BigInt::BigInt(const std::string& str) { Base base = Decimal; u32bit markers = 0; bool negative = false; if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; } if(str.length() > markers + 2 && str[markers ] == '0' && str[markers + 1] == 'x') { markers += 2; base = Hexadecimal; } else if(str.length() > markers + 1 && str[markers] == '0') { markers += 1; base = Octal; } *this = decode((const byte*)str.data() + markers, str.length() - markers, base); if(negative) set_sign(Negative); else set_sign(Positive); }
Integer& Integer::operator=(const Integer& n) { set_size(n.size()); set_sign(n.sign()); for (unsigned int i = 0; i < size(); i++) { digits_[i] = n[i]; } return *this; }
// Create a Integer from n. void Integer::create(long long n) { int size = 0; if (n < 0) { n *= -1; set_sign(MINUS); } else { set_sign(PLUS); if (n == 0) { size = 1; } } while (n) { digits_[size++] = n % BASE; n /= BASE; } set_size(size); }
int drv_start(int sub_dev, int UNUSED(DmaMode)) { u32_t enable_bit, result = 0; /* Write default values to device in case user failed to configure. If user did configure properly, everything is written twice. please raise your hand if you object against to this strategy...*/ result |= set_sample_rate(aud_conf[sub_dev].sample_rate, sub_dev); result |= set_stereo(aud_conf[sub_dev].stereo, sub_dev); result |= set_bits(aud_conf[sub_dev].nr_of_bits, sub_dev); result |= set_sign(aud_conf[sub_dev].sign, sub_dev); /* set the interrupt count */ result |= set_int_cnt(sub_dev); if (result) { return EIO; } /* if device currently paused, resume */ drv_resume(sub_dev); switch(sub_dev) { case ADC1_CHAN: enable_bit = ADC1_EN;break; case DAC1_CHAN: enable_bit = DAC1_EN;break; case DAC2_CHAN: enable_bit = DAC2_EN;break; default: return EINVAL; } /* enable interrupts from 'sub device' */ drv_reenable_int(sub_dev); /* this means play!!! */ pci_outw(reg(CHIP_SEL_CTRL), pci_inw(reg(CHIP_SEL_CTRL)) | enable_bit); aud_conf[sub_dev].busy = 1; return OK; }
/* all IO-ctl's sent to the upper driver are passed to this function */ int drv_io_ctl(int request, void * val, int * len, int sub_dev) { int status; switch(request) { case DSPIORATE: status = set_sample_rate(*((u32_t *) val), sub_dev); break; case DSPIOSTEREO: status = set_stereo(*((u32_t *) val), sub_dev); break; case DSPIOBITS: status = set_bits(*((u32_t *) val), sub_dev); break; case DSPIOSIZE: status = set_frag_size(*((u32_t *) val), sub_dev); break; case DSPIOSIGN: status = set_sign(*((u32_t *) val), sub_dev); break; case DSPIOMAX: status = get_max_frag_size(val, len, sub_dev); break; case DSPIORESET: status = reset(sub_dev); break; case DSPIOFREEBUF: status = free_buf(val, len, sub_dev); break; case DSPIOSAMPLESINBUF: status = get_samples_in_buf(val, len, sub_dev); break; case DSPIOPAUSE: status = drv_pause(sub_dev); break; case DSPIORESUME: status = drv_resume(sub_dev); break; case MIXIOGETVOLUME: status = get_set_volume(val, len, sub_dev, 0); break; case MIXIOSETVOLUME: status = get_set_volume(val, len, sub_dev, 1); break; default: status = EINVAL; break; } return status; }
static void float_valuesnowvx (WavpackStream *wps, int32_t *values, int32_t num_values) { while (num_values--) { int shift_count = 0, exp = wps->float_max_exp; f32 outval = 0; if (*values) { *values <<= wps->float_shift; if (*values < 0) { *values = -*values; set_sign (outval, 1); } if (*values >= 0x1000000) { while (*values & 0xf000000) { *values >>= 1; ++exp; } } else if (exp) { while (!(*values & 0x800000) && --exp) { shift_count++; *values <<= 1; } if (shift_count && (wps->float_flags & FLOAT_SHIFT_ONES)) *values |= ((1 << shift_count) - 1); } set_mantissa (outval, *values); set_exponent (outval, exp); } * (f32 *) values++ = outval; }
/*-------------------------------------------------------------------* * Function ACELP_10i40_35bits() * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Algebraic codebook; 35 bits: 10 pulses in a frame of 40 samples. * *-------------------------------------------------------------------* * The code length is 40, containing 10 nonzero pulses: i0...i9. * * All pulses can have two (2) possible amplitudes: +1 or -1. * * Each pulse can have eight (8) possible positions: * * * * i0,i5 : 0, 5, 10, 15, 20, 25, 30, 35. * * i1,i6 : 1, 6, 11, 16, 21, 26, 31, 36. * * i2,i7 : 2, 7, 12, 17, 22, 27, 32, 37. * * i3,i8 : 3, 8, 13, 18, 23, 28, 33, 38. * * i4,i9 : 4, 9, 14, 19, 24, 29, 34, 39. * *-------------------------------------------------------------------*/ void ACELP_10i40_35bits( Word16 x[], /* (i) Q0 : target vector */ Word16 cn[], /* (i) Q0 : residual after long term prediction */ Word16 H[], /* (i) Q12: impulse response of weighted synthesis filter */ Word16 code[], /* (o) Q12: algebraic (fixed) codebook excitation */ Word16 y[], /* (o) Q11: filtered fixed codebook excitation */ Word16 indx[] /* (o) : index 5 words: 7,7,7,7,7 = 35 bits */ ) { Word16 i, j, k, ix, iy, pos, track; Word16 psk, ps, alpk, alp, itrk[3]; Word32 s, corr[NB_TRACK], L_tmp; Word16 *p0, *p1, *h, *h_inv; /* these vectors are not static */ Word16 dn[L_SUBFR], sign[L_SUBFR], vec[L_SUBFR]; Word16 ip[10], codvec[10], pos_max[NB_TRACK]; Word16 cor_x[NB_POS], cor_y[NB_POS]; Word16 h_buf[4*L_SUBFR]; Word16 rrixix[NB_TRACK][NB_POS], rrixiy[NB_TRACK][MSIZE]; h = h_buf; h_inv = h_buf + (2*L_SUBFR); for (i=0; i<L_SUBFR; i++) { *h++ = 0; *h_inv++ = 0; } /* Compute correlation between target x[] and H[] */ cor_h_x_e(H, x, dn); /* find the sign of each pulse position */ set_sign(32767, cn, dn, sign, vec, pos_max, corr); /* Compute correlations of h[] needed for the codebook search. */ cor_h_e(H, sign, vec, h, h_inv, rrixix, rrixiy); /*-------------------------------------------------------------------* * Search starting position for pulse i0 and i1. * * In the deep first search, we start 4 times with different * * position for i0 and i1. At all, we have 5 possible positions to * * start (position 0 to 5). The following loop remove 1 position * * to keep 4 positions for deep first search step. * *-------------------------------------------------------------------*/ s = L_add(corr[4], corr[0]); for (k=0; k<NB_TRACK-1; k++) corr[k] = L_add(corr[k], corr[k+1]); corr[4] = s; for (k=0; k<3; k++) { s = corr[0]; track = 0; for (i=1; i<NB_TRACK; i++) { L_tmp = L_sub(corr[i], s); if (L_tmp > 0) { s = corr[i]; track = i; } } corr[track] = -1; itrk[k] = track; } /*-------------------------------------------------------------------* * Deep first search: 4 iterations of 256 tests = 1024 tests. * * * * Stages of deep first search: * * stage 1 : fix i0 and i1 --> 2 positions is fixed previously. * * stage 2 : fix i2 and i3 --> try 8x8 = 64 positions. * * stage 3 : fix i4 and i5 --> try 8x8 = 64 positions. * * stage 4 : fix i6 and i7 --> try 8x8 = 64 positions. * * stage 5 : fix i8 and i9 --> try 8x8 = 64 positions. * *-------------------------------------------------------------------*/ psk = -1; alpk = 1; for (pos=0; pos<3; pos++) { k = itrk[pos]; /* starting position index */ /* stage 1: fix pulse i0 and i1 according to max of correlation */ ix = pos_max[ipos[k]]; iy = pos_max[ipos[k+1]]; ps = add(dn[ix], dn[iy]); i = mult(ix, Q15_1_5); j = mult(iy, Q15_1_5); alp = add(rrixix[ipos[k]][i], rrixix[ipos[k+1]][j]); i = add(shl(i,3), j); alp = add(alp, rrixiy[ipos[k]][i]); ip[0] = ix; ip[1] = iy; for (i=0; i<L_SUBFR; i++) vec[i] = 0; /* stage 2..5: fix pulse i2,i3,i4,i5,i6,i7,i8 and i9 */ for (j=2; j<10; j+=2) { /*--------------------------------------------------* * Store all impulse response of all fixed pulses * * in vector vec[] for the "cor_h_vec()" function. * *--------------------------------------------------*/ if (sign[ix] < 0) p0 = h_inv - ix; else p0 = h - ix; if (sign[iy] < 0) p1 = h_inv - iy; else p1 = h - iy; for (i=0; i<L_SUBFR; i++) { vec[i] = add(vec[i], add(*p0, *p1)); p0++; p1++; } /*--------------------------------------------------* * Calculate correlation of all possible positions * * of the next 2 pulses with previous fixed pulses. * * Each pulse can have 8 possible positions * *--------------------------------------------------*/ cor_h_vec(h, vec, ipos[k+j], sign, rrixix, cor_x); cor_h_vec(h, vec, ipos[k+j+1], sign, rrixix, cor_y); /*--------------------------------------------------* * Fix 2 pulses, try 8x8 = 64 positions. * *--------------------------------------------------*/ search_ixiy(ipos[k+j], ipos[k+j+1], &ps, &alp, &ix, &iy, dn, cor_x, cor_y, rrixiy); ip[j] = ix; ip[j+1] = iy; } /* memorise new codevector if it's better than the last one. */ ps = mult(ps,ps); s = L_msu(L_mult(alpk,ps),psk,alp); if (s > 0) { psk = ps; alpk = alp; for (i=0; i<10; i++) codvec[i] = ip[i]; } } /* end of for (pos=0; pos<3; pos++) */ /*-------------------------------------------------------------------* * index of 10 pulses = 35 bits on 5 words * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * indx[0] = 7 bits --> 3(pos#6) + 1(sign#1) + 3(pos#1) * * indx[1] = 7 bits --> 3(pos#7) + 1(sign#2) + 3(pos#2) * * indx[2] = 7 bits --> 3(pos#8) + 1(sign#3) + 3(pos#3) * * indx[3] = 7 bits --> 3(pos#9) + 1(sign#4) + 3(pos#4) * * indx[4] = 7 bits --> 3(pos#10)+ 1(sign#5) + 3(pos#5) * *-------------------------------------------------------------------*/ build_code(codvec, sign, 10, H, code, y, indx); for (i=0; i<NB_TRACK; i++) indx[i] = indx[i] & (Word16)127; return; }
Word16 code_2i40_9bits( Word16 subNr, /* i : subframe number */ Word16 x[], /* i : target vector */ Word16 h[], /* i : impulse response of weighted synthesis */ /* filter h[-L_subfr..-1] must be set to 0. */ Word16 T0, /* i : Pitch lag */ Word16 pitch_sharp, /* i : Last quantized pitch gain */ Word16 code[], /* o : Innovative codebook */ Word16 y[], /* o : filtered fixed codebook excitation */ Word16 * sign, /* o : Signs of 2 pulses */ Flag * pOverflow /* o : Flag set when overflow occurs */ ) { Word16 codvec[NB_PULSE]; Word16 dn[L_CODE]; Word16 dn2[L_CODE]; Word16 dn_sign[L_CODE]; Word16 rr[L_CODE][L_CODE]; register Word16 i; Word16 index; Word16 sharp; Word16 temp; Word32 L_temp; L_temp = ((Word32) pitch_sharp) << 1; /* Check for overflow condition */ if (L_temp != (Word32)((Word16) L_temp)) { *(pOverflow) = 1; sharp = (pitch_sharp > 0) ? MAX_16 : MIN_16; } else { sharp = (Word16) L_temp; } if (T0 < L_CODE) { for (i = T0; i < L_CODE; i++) { temp = mult( *(h + i - T0), sharp, pOverflow); *(h + i) = add( *(h + i), temp, pOverflow); } } cor_h_x( h, x, dn, 1, pOverflow); /* dn2[] not used in this codebook search */ set_sign( dn, dn_sign, dn2, 8); cor_h( h, dn_sign, rr, pOverflow); search_2i40( subNr, dn, rr, codvec, pOverflow); index = build_code( subNr, codvec, dn_sign, code, h, y, sign, pOverflow); /*-----------------------------------------------------------------* * Compute innovation vector gain. * * Include fixed-gain pitch contribution into code[]. * *-----------------------------------------------------------------*/ if (T0 < L_CODE) { for (i = T0; i < L_CODE; i++) { temp = mult( *(code + i - T0), sharp, pOverflow); *(code + i) = add( *(code + i), temp, pOverflow); } } return(index); }
/************************************************* * Construct a BigInt from an encoded BigInt * *************************************************/ BigInt::BigInt(const byte input[], u32bit length, Base base) { set_sign(Positive); *this = decode(input, length, base); }
ieee_floatt &ieee_floatt::operator+=(const ieee_floatt &other) { ieee_floatt _other=other; assert(_other.spec==spec); if(other.NaN_flag) make_NaN(); if(NaN_flag) return *this; if(infinity_flag && other.infinity_flag) { if(sign_flag==other.sign_flag) return *this; make_NaN(); return *this; } else if(infinity_flag) return *this; else if(other.infinity_flag) { infinity_flag=true; sign_flag=other.sign_flag; return *this; } // 0 + 0 needs special treatment for the signs if(is_zero() && other.is_zero()) { if(get_sign()==other.get_sign()) return *this; else { if(rounding_mode==ROUND_TO_MINUS_INF) { set_sign(true); return *this; } else { set_sign(false); return *this; } } } // get smaller exponent if(_other.exponent<exponent) { fraction*=power(2, exponent-_other.exponent); exponent=_other.exponent; } else if(exponent<_other.exponent) { _other.fraction*=power(2, _other.exponent-exponent); _other.exponent=exponent; } assert(exponent==_other.exponent); if(sign_flag) fraction.negate(); if(_other.sign_flag) _other.fraction.negate(); fraction+=_other.fraction; // if the result is zero, // there is some set of rules to get the sign if(fraction==0) { if(rounding_mode==ROUND_TO_MINUS_INF) sign_flag=true; else sign_flag=false; } else // fraction!=0 { sign_flag=(fraction<0); if(sign_flag) fraction.negate(); } align(); return *this; }
/* ------------------------------------------------------------------------------ FUNCTION NAME: code_2i40_11bits ------------------------------------------------------------------------------ INPUT AND OUTPUT DEFINITIONS Inputs: x, target vector, array of type Word16 h, impulse response of weighted synthesis filter, array of type Word16 T0, Pitch lag, variable of type Word16 pitch_sharp, Last quantized pitch gain, variable of type Word16 Outputs: code[], Innovative codebook, array of type Word16 y[], filtered fixed codebook excitation, array of type Word16 sign, Signs of 2 pulses, pointer of type Word16 * pOverflow Flag set when overflow occurs, pointer of type Flag * Returns: index Global Variables Used: None Local Variables Needed: None ------------------------------------------------------------------------------ FUNCTION DESCRIPTION Searches a 11 bit algebraic codebook containing 2 pulses in a frame of 40 samples. The code length is 40, containing 2 nonzero pulses: i0...i1. All pulses can have two possible amplitudes: +1 or -1. Pulse i0 can have 2x8=16 possible positions, pulse i1 can have 4x8=32 positions. i0 : 1, 6, 11, 16, 21, 26, 31, 36. 3, 8, 13, 18, 23, 28, 33, 38. i1 : 0, 5, 10, 15, 20, 25, 30, 35. 1, 6, 11, 16, 21, 26, 31, 36. 2, 7, 12, 17, 22, 27, 32, 37. 4, 9, 14, 19, 24, 29, 34, 39. ------------------------------------------------------------------------------ REQUIREMENTS None ------------------------------------------------------------------------------ REFERENCES c2_11pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 ------------------------------------------------------------------------------ PSEUDO-CODE ------------------------------------------------------------------------------ CAUTION [optional] [State any special notes, constraints or cautions for users of this function] ------------------------------------------------------------------------------ */ Word16 code_2i40_11bits( Word16 x[], /* i : target vector */ Word16 h[], /* i : impulse response of weighted synthesis filter */ /* h[-L_subfr..-1] must be set to zero. */ Word16 T0, /* i : Pitch lag */ Word16 pitch_sharp, /* i : Last quantized pitch gain */ Word16 code[], /* o : Innovative codebook */ Word16 y[], /* o : filtered fixed codebook excitation */ Word16 * sign, /* o : Signs of 2 pulses */ Flag * pOverflow /* o : Flag set when overflow occurs */ ) { Word16 codvec[NB_PULSE]; Word16 dn[L_CODE]; Word16 dn2[L_CODE]; Word16 dn_sign[L_CODE]; Word16 rr[L_CODE][L_CODE]; Word16 i; Word16 index; Word16 sharp; Word16 tempWord; sharp = pitch_sharp << 1; if (T0 < L_CODE) { for (i = T0; i < L_CODE; i++) { tempWord = mult( h[i - T0], sharp, pOverflow); h[i] = add_16( h[i], tempWord, pOverflow); } } cor_h_x( h, x, dn, 1, pOverflow); set_sign( dn, dn_sign, dn2, 8); /* dn2[] not used in this codebook search */ cor_h( h, dn_sign, rr, pOverflow); search_2i40( dn, rr, codvec, pOverflow); /* function result */ index = build_code( codvec, dn_sign, code, h, y, sign, pOverflow); /* * Compute innovation vector gain. * Include fixed-gain pitch contribution into code[]. */ if (T0 < L_CODE) { for (i = T0; i < L_CODE; i++) { tempWord = mult( code[i - T0], sharp, pOverflow); code[i] = add_16( code[i], tempWord, pOverflow); } } return index; }
ieee_floatt &ieee_floatt::operator += (const ieee_floatt &other) { ieee_floatt _other=other; assert(_other.spec==spec); if(other.NaN) make_NaN(); if(NaN) return *this; if(infinity && other.infinity) { if(sign==other.sign) return *this; make_NaN(); return *this; } else if(infinity) return *this; else if(other.infinity) { infinity=true; sign=other.sign; return *this; } if(is_zero() && other.is_zero()) { if(get_sign() == other.get_sign()) return *this; else { if(rounding_mode == ROUND_TO_MINUS_INF) { set_sign(true); return *this; } else { set_sign(false); return *this; } } } // get smaller exponent if(_other.exponent<exponent) { fraction*=power(2, exponent-_other.exponent); exponent=_other.exponent; } else if(exponent<_other.exponent) { _other.fraction*=power(2, _other.exponent-exponent); _other.exponent=exponent; } assert(exponent==_other.exponent); if(sign) fraction.negate(); if(_other.sign) _other.fraction.negate(); fraction+=_other.fraction; // on zero, retain original sign if(fraction!=0) { sign=(fraction<0); if(sign) fraction.negate(); } align(); return *this; }
void float_values (WavpackStream *wps, int32_t *values, int32_t num_values) { uint32_t crc = wps->crc_x; if (!bs_is_open (&wps->wvxbits)) { float_valuesnowvx (wps, values, num_values); return; } while (num_values--) { int shift_count = 0, exp = wps->float_max_exp; f32 outval = 0; uint32_t temp; if (*values == 0) { if (wps->float_flags & FLOAT_ZEROS_SENT) { if (getbit (&wps->wvxbits)) { getbits (&temp, 23, &wps->wvxbits); set_mantissa (outval, temp); if (exp >= 25) { getbits (&temp, 8, &wps->wvxbits); set_exponent (outval, temp); } set_sign (outval, getbit (&wps->wvxbits)); } else if (wps->float_flags & FLOAT_NEG_ZEROS) set_sign (outval, getbit (&wps->wvxbits)); } } else { *values <<= wps->float_shift; if (*values < 0) { *values = -*values; set_sign (outval, 1); } if (*values == 0x1000000) { if (getbit (&wps->wvxbits)) { getbits (&temp, 23, &wps->wvxbits); set_mantissa (outval, temp); } set_exponent (outval, 255); } else { if (exp) while (!(*values & 0x800000) && --exp) { shift_count++; *values <<= 1; } if (shift_count) { if ((wps->float_flags & FLOAT_SHIFT_ONES) || ((wps->float_flags & FLOAT_SHIFT_SAME) && getbit (&wps->wvxbits))) *values |= ((1 << shift_count) - 1); else if (wps->float_flags & FLOAT_SHIFT_SENT) { getbits (&temp, shift_count, &wps->wvxbits); *values |= temp & ((1 << shift_count) - 1); } } set_mantissa (outval, *values); set_exponent (outval, exp); } } crc = crc * 27 + get_mantissa (outval) * 9 + get_exponent (outval) * 3 + get_sign (outval); * (f32 *) values++ = outval; } wps->crc_x = crc; }
/************************************************* * Reverse the value of the sign flag * *************************************************/ void BigInt::flip_sign() { set_sign(reverse_sign()); }
void ieee_floatt::next_representable(bool greater) { if(is_NaN()) return; bool old_sign = get_sign(); if(is_zero()) { unpack(1); set_sign(!greater); return; } if(is_infinity()) { if(get_sign() == greater) { make_fltmax(); set_sign(old_sign); } return; } bool dir; if(greater) { if(get_sign()) dir = false; else dir = true; } else { if(get_sign()) dir = true; else dir = false; } set_sign(false); mp_integer old = pack(); if(dir) ++old; else --old; unpack(old); //sign change impossible (zero case caught earler) set_sign(old_sign); //mp_integer new_exp = exponent; //mp_integer new_frac = fraction + dir; //std::cout << exponent << ":" << fraction << std::endl; //std::cout << new_exp << ":" << new_frac << std::endl; //if(get_sign()) // new_frac.negate(); //new_exp -= spec.f; //build(new_frac, new_exp); }
/*-------------------------------------------------------------------* * Function ACELP_12i40_44bits() * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Algebraic codebook; 44 bits: 12 pulses in a frame of 40 samples. * *-------------------------------------------------------------------* * The code length is 40, containing 12 nonzero pulses: i0...i11. * * 12 pulses can have two (2) possible amplitudes: +1 or -1. * * 10 pulses can have eight (8) possible positions: * * i2,i7 : 0, 5, 10, 15, 20, 25, 30, 35. --> t0 * * i3,i8 : 1, 6, 11, 16, 21, 26, 31, 36. --> t1 * * i4,i9 : 2, 7, 12, 17, 22, 27, 32, 37. --> t2 * * i5,i10 : 3, 8, 13, 18, 23, 28, 33, 38. --> t3 * * i6,i11 : 4, 9, 14, 19, 24, 29, 34, 39. --> t4 * * * * The 2 other pulses can be on the following track: * * t0-t1,t1-t2,t2-t3,t3-t4,t4-t0. * *-------------------------------------------------------------------*/ void ACELP_12i40_44bits( Word16 x[], /* (i) Q0 : target vector */ Word16 cn[], /* (i) Q0 : residual after long term prediction */ Word16 H[], /* (i) Q12: impulse response of weighted synthesis filter */ Word16 code[], /* (o) Q12: algebraic (fixed) codebook excitation */ Word16 y[], /* (o) Q11: filtered fixed codebook excitation */ Word16 indx[] /* (o) : index 5 words: 13,10,7,7,7 = 44 bits */ ) { Word16 i, j, k, ix, iy, itrk[3], track, pos, index, idx[NB_TRACK]; Word16 psk, ps, alpk, alp; Word32 s, corr[NB_TRACK]; Word16 *p0, *p1, *h, *h_inv; Word16 dn[L_SUBFR], sign[L_SUBFR], vec[L_SUBFR]; Word16 ip[12], codvec[12], pos_max[NB_TRACK]; Word16 cor_x[NB_POS], cor_y[NB_POS]; Word16 h_buf[4*L_SUBFR]; Word16 rrixix[NB_TRACK][NB_POS], rrixiy[NB_TRACK][MSIZE]; Word32 L_tmp; h = h_buf; h_inv = h_buf + (2*L_SUBFR); for (i=0; i<L_SUBFR; i++) { *h++ = 0; *h_inv++ = 0; } /* Compute correlation between target x[] and H[] */ cor_h_x_e(H, x, dn); /* find the sign of each pulse position */ set_sign(32767, cn, dn, sign, vec, pos_max, corr); /* Compute correlations of h[] needed for the codebook search. */ cor_h_e(H, sign, vec, h, h_inv, rrixix, rrixiy); /*-------------------------------------------------------------------* * Search position for pulse i0 and i1. * *-------------------------------------------------------------------*/ s = L_add(corr[4], corr[0]); for (k=0; k<NB_TRACK-1; k++) corr[k] = L_add(corr[k], corr[k+1]); corr[4] = s; for (k=0; k<3; k++) { s = corr[0]; track = 0; for (i=1; i<NB_TRACK; i++) { L_tmp = L_sub(corr[i], s); if (L_tmp > 0) { s = corr[i]; track = i; } } corr[track] = -1; itrk[k] = track; } /*-------------------------------------------------------------------* * Deep first search: 3 iterations of 320 tests = 960 tests. * * * * Stages of deep first search: * * stage 1 : fix i0 and i1 --> 2 positions is fixed previously. * * stage 2 : fix i2 and i3 --> try 8x8 = 64 positions. * * stage 3 : fix i4 and i5 --> try 8x8 = 64 positions. * * stage 4 : fix i6 and i7 --> try 8x8 = 64 positions. * * stage 5 : fix i8 and i9 --> try 8x8 = 64 positions. * * stage 6 : fix i10 and i11 --> try 8x8 = 64 positions. * *-------------------------------------------------------------------*/ /* stage 0: fix pulse i0 and i1 according to max of correlation */ psk = -1; alpk = 1; for (pos=0; pos<3; pos++) { k = itrk[pos]; /* starting position index */ /* stage 1: fix pulse i0 and i1 according to max of correlation */ ix = pos_max[ipos[k]]; iy = pos_max[ipos[k+1]]; ps = add(dn[ix], dn[iy]); i = mult(ix, Q15_1_5); j = mult(iy, Q15_1_5); alp = add(rrixix[ipos[k]][i], rrixix[ipos[k+1]][j]); i = add(shl(i,3), j); alp = add(alp, rrixiy[ipos[k]][i]); ip[0] = ix; ip[1] = iy; for (i=0; i<L_SUBFR; i++) vec[i] = 0; /* stage 2..5: fix pulse i2,i3,i4,i5,i6,i7,i8 and i9 */ for (j=2; j<12; j+=2) { /*--------------------------------------------------* * Store all impulse response of all fixed pulses * * in vector vec[] for the "cor_h_vec()" function. * *--------------------------------------------------*/ if (sign[ix] < 0) p0 = h_inv - ix; else p0 = h - ix; if (sign[iy] < 0) p1 = h_inv - iy; else p1 = h - iy; for (i=0; i<L_SUBFR; i++) { vec[i] = add(vec[i], add(*p0, *p1)); p0++; p1++; } /*--------------------------------------------------* * Calculate correlation of all possible positions * * of the next 2 pulses with previous fixed pulses. * * Each pulse can have 8 possible positions * *--------------------------------------------------*/ cor_h_vec(h, vec, ipos[k+j], sign, rrixix, cor_x); cor_h_vec(h, vec, ipos[k+j+1], sign, rrixix, cor_y); /*--------------------------------------------------* * Fix 2 pulses, try 8x8 = 64 positions. * *--------------------------------------------------*/ search_ixiy(ipos[k+j], ipos[k+j+1], &ps, &alp, &ix, &iy, dn, cor_x, cor_y, rrixiy); ip[j] = ix; ip[j+1] = iy; } /* memorise new codevector if it's better than the last one. */ ps = mult(ps,ps); s = L_msu(L_mult(alpk,ps),psk,alp); if (s > 0) { psk = ps; alpk = alp; for (i=0; i<12; i++) codvec[i] = ip[i]; } } /* end of for (pos=0; pos<3; pos++) */ /*-------------------------------------------------------------------* * index of 12 pulses = 44 bits on 5 words * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * indx[0] =13 bits --> 3(track) + * * 3(pos#11) + 3(pos#6) + 1(sign#1) + 3(pos#1) * * indx[1] =10 bits --> 3(pos#12) + 3(pos#7) + 1(sign#2) + 3(pos#2) * * indx[2] = 7 bits --> 3(pos#8) + 1(sign#3) + 3(pos#3) * * indx[3] = 7 bits --> 3(pos#9) + 1(sign#4) + 3(pos#4) * * indx[4] = 7 bits --> 3(pos#10)+ 1(sign#5) + 3(pos#5) * *-------------------------------------------------------------------*/ build_code(codvec+2, sign, 10, H, code, y, idx); for (k=0; k<2; k++) { pos = codvec[k]; index = mult(pos, Q15_1_5); /* index = pos/5 */ track = sub(pos, extract_l(L_shr(L_mult(index, 5), 1))); if (sign[pos] > 0) { code[pos] = add(code[pos], 4096); /* 1.0 in Q12 */ for (i=pos, j=0; i<L_SUBFR; i++, j++) y[i] = add(y[i], H[j]); } else { code[pos] = sub(code[pos], 4096); /* 1.0 in Q12 */ for (i=pos, j=0; i<L_SUBFR; i++, j++) y[i] = sub(y[i], H[j]); index = add(index, 8); } ix = shr(idx[track], (Word16)4) & (Word16)15; iy = idx[track] & (Word16)15; index = pack3(ix, iy, index); if (k == 0) index = add(shl(track, 10), index); indx[k] = index; } for (k=2; k<NB_TRACK; k++) { track = add(track, 1); if (track >= NB_TRACK) track = 0; indx[k] = (idx[track] & (Word16)127); } return; }