int32_t ramcoder_putbits(HUFF *hp,struct ramcoder *coder,int32_t flushflag) { int32_t numbits = 0; while ( 1 ) { switch ( ramcoder_state(coder) ) { case 1: coder->underflowBits++, ramcoder_normalize(coder); break; case 0: hputbit(hp,(coder->upper & RAMMASK_BIT(0)) != 0), numbits++; while ( coder->underflowBits > 0 ) { hputbit(hp,(coder->upper & RAMMASK_BIT(0)) == 0), numbits++; coder->underflowBits--; } break; default: if ( flushflag != 0 ) { hputbit(hp,(coder->lower & RAMMASK_BIT(1)) != 0), numbits++; for (coder->underflowBits++; coder->underflowBits>0; coder->underflowBits--) hputbit(hp,(coder->lower & RAMMASK_BIT(1)) == 0), numbits++; } return(numbits); break; } ramcoder_shiftbits(coder); } }
int32_t serdes777_rwbits(int32_t rwflag,void *ptr,int32_t len,HUFF *hp) { int32_t i,bit; if ( rwflag == 0 ) { for (i=0; i<len; i++) { if ( (bit= hgetbit(hp)) < 0 ) return(-1); if ( bit != 0 ) SETBIT(ptr,i); else CLEARBIT(ptr,i); } //printf("rbits.%d (%02x)\n",len,*(uint8_t *)ptr); } else { //printf("wbits.%d (%02x)\n",len,*(uint8_t *)ptr); for (i=0; i<len; i++) if ( hputbit(hp,GETBIT(ptr,i) != 0) < 0 ) return(-100*i-100); } //printf("rwbits len.%d (%02x)\n",len,*(uint8_t *)dest); //printf("(%d) ",*(uint8_t *)ptr); return(len); }
int32_t serdes777_rwsizebits(int32_t rwflag,void *xptr,uint64_t xval,int32_t sizebits,HUFF *hp) { int32_t numbits = 0; if ( 1 && sizebits == 3 ) { if ( rwflag != 0 ) { if ( xval == 0 ) { hputbit(hp,0); return(1); } else { hputbit(hp,1); if ( xval < 3 ) { hputbit(hp,(xval&1) != 0); hputbit(hp,(xval&2) != 0); //hputbit(hp,(xval&4) != 0); return(3); } else { hputbit(hp,1), hputbit(hp,1);//, hputbit(hp,1); if ( serdes777_rwbits(1,xptr,8,hp) != 8 ) return(-30); return(11); } } } else { if ( hgetbit(hp) == 0 ) { *(uint8_t *)xptr = 0; return(1); } else { numbits = hgetbit(hp) + (hgetbit(hp) << 1);// + (hgetbit(hp) << 2); if ( numbits < 3 ) { *(uint8_t *)xptr = numbits; return(3); } else { if ( serdes777_rwbits(0,xptr,8,hp) != 8 ) return(-4); return(11); } } } } else { if ( rwflag != 0 ) { numbits = hcalc_bitsize(xval) - 1; //printf("rwsizebits.%d numbits.%d %02x %llu\n",sizebits,numbits,*(uint8_t *)xptr,(long long)xval); if ( numbits >= (1 << sizebits) ) { printf("numbits overflow.%d doesnt fit into sizebits.%d\n",numbits,sizebits); return(-10); } } if ( serdes777_rwbits(rwflag,&numbits,sizebits,hp) != sizebits ) return(-20); if ( serdes777_rwbits(rwflag,xptr,numbits+1,hp) != numbits+1 ) return(-30); //printf("return.(%02x) numbits.%d + 1 + sizebits.%d\n",*(uint8_t *)xptr,numbits,sizebits); //printf("(%02x) ",*(uint8_t *)xptr); return(numbits + 1 + sizebits); } }