PAQ8/orig/paq8o10t.cpp
// This combines all the context models with a Mixer.
This combines all the context models with a Mixer.
This combines all the context models with a Mixer .
int contextModel2() {
  static ContextMap cm(MEM*32, 9);
  static RunContextMap rcm7(MEM), rcm9(MEM), rcm10(MEM);
  static Mixer m(800, 3088, 7, 128);
  static U32 cxt[16];  
  static Filetype filetype=DEFAULT;
  static int size=0;  
  if (bpos==0) {
    --size;
    if (size==-1) filetype=(Filetype)buf(1);
    if (size==-5) {
      size=buf(4)<<24|buf(3)<<16|buf(2)<<8|buf(1);
      if (filetype<=14)  typesize[filetype]+=size;
      if (filetype==EXE) size+=8;
    }
  }
  m.update();
  m.add(256);
  int ismatch=ilog(matchModel(m));  
  switch (filetype)
	{
	case JPEG: {
			int isjpeg=jpegModel(m);  
			if (isjpeg) {
				m.set(1, 8);
				m.set(isjpeg-1, 257);
				m.set(buf(1), 256);
				return m.p();
			}
			break;
		}
	case BMPFILE24:
	case TIFFFILE:{ 
			int isbmp=bmpModel(m); 
			if (isbmp>0) {
				static int col=0;
				if (++col>=24) col=0;
				m.set(2, 8);
				m.set(col, 24);
				m.set(buf(isbmp)+buf(3)>>4, 32);
				m.set(c0, 256);
				return m.p();
			}
			break;
		}
	case BMPFILE8:{ 
			if (bmpModel8(m)>0) return m.p(); 
			break;
		}
	case PGMFILE:{
			if (pgmModel(m)>0) return m.p(); 
			break;
		}
	case BMPFILE1:{
			bmpModel1(m);
			break;
		} 
	case RGBFILE:{ 
			if (rgbModel8(m)>0) return m.p(); Image width (bytes) if RGB8 detected, or 0 
			break;
		}
	default: {
			break;
		}
  }
  Normal model
  if (bpos==0) {
     int i=0;
    for ( i=15; i>0; --i)  update order 0-11 context hashes
      cxt[i]=cxt[i-1]*257+(c4&255)+1;
    for ( i=0; i<7; ++i)
      cm.set(cxt[i]);
    rcm7.set(cxt[7]);
    cm.set(cxt[8]);
    rcm9.set(cxt[10]);
    rcm10.set(cxt[12]);
    cm.set(cxt[14]);
  }
  int order=cm.mix(m);
  rcm7.mix(m);
  rcm9.mix(m);
  rcm10.mix(m);
  if (level>=4) {
	switch (filetype)
	{
	case TXTUTF8:
	case TEXT: { 
			sparseModel(m,ismatch,order);
			nestModel(m);
			wordModel(m);
			indirectModel(m);
			dmcModel(m);
			 break;
		}
	case EXE: {
			sparseModel(m,ismatch,order);
			indirectModel(m);
			dmcModel(m);
			exeModel(m);
			break;
		} 
	case BMPFILE1: break;
	default: { 
			sparseModel(m,ismatch,order);
			distanceModel(m);
			picModel(m);
			recordModel(m);  
			indirectModel(m);
			dmcModel(m);
			break;
		} 
    }
	}
  order = order-2;
  if(order<0) order=0;
  U32 c1=buf(1), c2=buf(2), c3=buf(3), c;
  m.set(c1+8, 264);
  m.set(c0, 256);
  m.set(order+8*(c4>>5&7)+64*(c1==c2)+128*(filetype==EXE), 256);
  m.set(c2, 256);
  m.set(c3, 256);
  m.set(ismatch, 256);
  if(bpos)
  {	
    c=c0<<(8-bpos); if(bpos==1)c+=c3/2;
    c=(min(bpos,5))*256+c1/32+8*(c2/32)+(c&192);
  }
  else c=c3/128+(c4>>31)*2+4*(c2/64)+(c1&240);
  m.set(c, 1536);
  int pr=m.p();
  return pr;
}
int contextModel2 ( ) { static ContextMap cm ( MEM * 32 , 9 ) ; static RunContextMap rcm7 ( MEM ) , rcm9 ( MEM ) , rcm10 ( MEM ) ; static Mixer m ( 800 , 3088 , 7 , 128 ) ; static U32 cxt [ 16 ] ; static Filetype filetype = DEFAULT ; static int size = 0 ; if ( bpos == 0 ) { -- size ; if ( size == -1 ) filetype = ( Filetype ) buf ( 1 ) ; if ( size == -5 ) { size = buf ( 4 ) << 24 | buf ( 3 ) << 16 | buf ( 2 ) << 8 | buf ( 1 ) ; if ( filetype <= 14 ) typesize [ filetype ] += size ; if ( filetype == EXE ) size += 8 ; } } m . update ( ) ; m . add ( 256 ) ; int ismatch = ilog ( matchModel ( m ) ) ; switch ( filetype ) { case JPEG : { int isjpeg = jpegModel ( m ) ; if ( isjpeg ) { m . set ( 1 , 8 ) ; m . set ( isjpeg - 1 , 257 ) ; m . set ( buf ( 1 ) , 256 ) ; return m . p ( ) ; } break ; } case BMPFILE24 : case TIFFFILE : { int isbmp = bmpModel ( m ) ; if ( isbmp > 0 ) { static int col = 0 ; if ( ++ col >= 24 ) col = 0 ; m . set ( 2 , 8 ) ; m . set ( col , 24 ) ; m . set ( buf ( isbmp ) + buf ( 3 ) >> 4 , 32 ) ; m . set ( c0 , 256 ) ; return m . p ( ) ; } break ; } case BMPFILE8 : { if ( bmpModel8 ( m ) > 0 ) return m . p ( ) ; break ; } case PGMFILE : { if ( pgmModel ( m ) > 0 ) return m . p ( ) ; break ; } case BMPFILE1 : { bmpModel1 ( m ) ; break ; } case RGBFILE : { if ( rgbModel8 ( m ) > 0 ) return m . p ( ) ; break ; } default : { break ; } } if ( bpos == 0 ) { int i = 0 ; for ( i = 15 ; i > 0 ; -- i ) cxt [ i ] = cxt [ i - 1 ] * 257 + ( c4 & 255 ) + 1 ; for ( i = 0 ; i < 7 ; ++ i ) cm . set ( cxt [ i ] ) ; rcm7 . set ( cxt [ 7 ] ) ; cm . set ( cxt [ 8 ] ) ; rcm9 . set ( cxt [ 10 ] ) ; rcm10 . set ( cxt [ 12 ] ) ; cm . set ( cxt [ 14 ] ) ; } int order = cm . mix ( m ) ; rcm7 . mix ( m ) ; rcm9 . mix ( m ) ; rcm10 . mix ( m ) ; if ( level >= 4 ) { switch ( filetype ) { case TXTUTF8 : case TEXT : { sparseModel ( m , ismatch , order ) ; nestModel ( m ) ; wordModel ( m ) ; indirectModel ( m ) ; dmcModel ( m ) ; break ; } case EXE : { sparseModel ( m , ismatch , order ) ; indirectModel ( m ) ; dmcModel ( m ) ; exeModel ( m ) ; break ; } case BMPFILE1 : break ; default : { sparseModel ( m , ismatch , order ) ; distanceModel ( m ) ; picModel ( m ) ; recordModel ( m ) ; indirectModel ( m ) ; dmcModel ( m ) ; break ; } } } order = order - 2 ; if ( order < 0 ) order = 0 ; U32 c1 = buf ( 1 ) , c2 = buf ( 2 ) , c3 = buf ( 3 ) , c ; m . set ( c1 + 8 , 264 ) ; m . set ( c0 , 256 ) ; m . set ( order + 8 * ( c4 >> 5 & 7 ) + 64 * ( c1 == c2 ) + 128 * ( filetype == EXE ) , 256 ) ; m . set ( c2 , 256 ) ; m . set ( c3 , 256 ) ; m . set ( ismatch , 256 ) ; if ( bpos ) { c = c0 << ( 8 - bpos ) ; if ( bpos == 1 ) c += c3 / 2 ; c = ( min ( bpos , 5 ) ) * 256 + c1 / 32 + 8 * ( c2 / 32 ) + ( c & 192 ) ; } else c = c3 / 128 + ( c4 >> 31 ) * 2 + 4 * ( c2 / 64 ) + ( c1 & 240 ) ; m . set ( c , 1536 ) ; int pr = m . p ( ) ; return pr ; }
This combines all the context models with a Mixer.
This combines all the context models with a Mixer .
// order 0-11 contexts // bytes remaining in block // Parse filetype and size /*printf("\n(%s%d)", typenames[filetype], size),*/ // Test for special file types // Length of longest matching context //case DEFAULT:	break; // 1-257 if JPEG is detected, else 0 // Image width (bytes) if BMP or TIFF detected, or 0 // Image width (bytes) if BMP8 detected, or 0  // Image width (bytes) if PGM (P5,PGM_MAXVAL = 255) detected, or 0 //  // Image width (bytes) if RGB8 detected, or 0  // Normal model // update order 0-11 context hashes //	distanceModel(m); //recordModel(m);   //	distanceModel(m); //	recordModel(m);   //wordModel(m); //wordModel(m);