diff options
-rw-r--r-- | include/compression/coder.h | 40 | ||||
-rw-r--r-- | src/compression/bwlzh.c | 820 | ||||
-rw-r--r-- | src/compression/bwt.c | 320 | ||||
-rw-r--r-- | src/compression/coder.c | 338 | ||||
-rw-r--r-- | src/compression/dict.c | 10 | ||||
-rw-r--r-- | src/compression/huffman.c | 430 | ||||
-rw-r--r-- | src/compression/huffmem.c | 130 | ||||
-rw-r--r-- | src/compression/lz77.c | 386 | ||||
-rw-r--r-- | src/compression/merge_sort.c | 96 | ||||
-rw-r--r-- | src/compression/mtf.c | 128 | ||||
-rw-r--r-- | src/compression/rle.c | 72 | ||||
-rw-r--r-- | src/compression/tng_compress.c | 1244 | ||||
-rw-r--r-- | src/compression/vals16.c | 58 | ||||
-rw-r--r-- | src/compression/widemuldiv.c | 70 | ||||
-rw-r--r-- | src/compression/xtc2.c | 1380 | ||||
-rw-r--r-- | src/compression/xtc3.c | 1812 | ||||
-rw-r--r-- | src/lib/tng_io.c | 94 |
17 files changed, 3720 insertions, 3708 deletions
diff --git a/include/compression/coder.h b/include/compression/coder.h index 5cef38a..570bc6d 100644 --- a/include/compression/coder.h +++ b/include/compression/coder.h @@ -13,6 +13,14 @@ #ifndef CODER_H #define CODER_H +#ifndef DECLSPECDLLEXPORT +#ifdef USE_WINDOWS +#define DECLSPECDLLEXPORT __declspec(dllexport) +#else /* USE_WINDOWS */ +#define DECLSPECDLLEXPORT +#endif /* USE_WINDOWS */ +#endif /* DECLSPECDLLEXPORT */ + struct coder { unsigned int pack_temporary; @@ -21,22 +29,22 @@ struct coder int stat_numval; }; -struct coder *Ptngc_coder_init(void); -void Ptngc_coder_deinit(struct coder *coder); -unsigned char *Ptngc_pack_array(struct coder *coder,int *input, int *length, int coding, int coding_parameter, int natoms, int speed); -int Ptngc_unpack_array(struct coder *coder,unsigned char *packed,int *output, int length, int coding, int coding_parameter, int natoms); -unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length); -int Ptngc_unpack_array_xtc2(struct coder *coder,unsigned char *packed,int *output, int length); -unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int speed); -int Ptngc_unpack_array_xtc3(unsigned char *packed,int *output, int length, int natoms); - -void Ptngc_out8bits(struct coder *coder, unsigned char **output); -void Ptngc_pack_flush(struct coder *coder,unsigned char **output); -void Ptngc_write_pattern(struct coder *coder,unsigned int pattern, int nbits, unsigned char **output); - -void Ptngc_writebits(struct coder *coder,unsigned int value,int nbits, unsigned char **output_ptr); -void Ptngc_write32bits(struct coder *coder,unsigned int value,int nbits, unsigned char **output_ptr); -void Ptngc_writemanybits(struct coder *coder,unsigned char *value,int nbits, unsigned char **output_ptr); +struct coder DECLSPECDLLEXPORT *Ptngc_coder_init(void); +void DECLSPECDLLEXPORT Ptngc_coder_deinit(struct coder *coder); +unsigned char DECLSPECDLLEXPORT *Ptngc_pack_array(struct coder *coder,int *input, int *length, int coding, int coding_parameter, int natoms, int speed); +int DECLSPECDLLEXPORT Ptngc_unpack_array(struct coder *coder,unsigned char *packed,int *output, int length, int coding, int coding_parameter, int natoms); +unsigned char DECLSPECDLLEXPORT *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length); +int DECLSPECDLLEXPORT Ptngc_unpack_array_xtc2(struct coder *coder,unsigned char *packed,int *output, int length); +unsigned char DECLSPECDLLEXPORT *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int speed); +int DECLSPECDLLEXPORT Ptngc_unpack_array_xtc3(unsigned char *packed,int *output, int length, int natoms); + +void DECLSPECDLLEXPORT Ptngc_out8bits(struct coder *coder, unsigned char **output); +void DECLSPECDLLEXPORT Ptngc_pack_flush(struct coder *coder,unsigned char **output); +void DECLSPECDLLEXPORT Ptngc_write_pattern(struct coder *coder,unsigned int pattern, int nbits, unsigned char **output); + +void DECLSPECDLLEXPORT Ptngc_writebits(struct coder *coder,unsigned int value,int nbits, unsigned char **output_ptr); +void DECLSPECDLLEXPORT Ptngc_write32bits(struct coder *coder,unsigned int value,int nbits, unsigned char **output_ptr); +void DECLSPECDLLEXPORT Ptngc_writemanybits(struct coder *coder,unsigned char *value,int nbits, unsigned char **output_ptr); #endif diff --git a/src/compression/bwlzh.c b/src/compression/bwlzh.c index 783a253..c9ab553 100644 --- a/src/compression/bwlzh.c +++ b/src/compression/bwlzh.c @@ -66,15 +66,15 @@ static void printvals(char *name, unsigned int *vals, int nvals) fprintf(stderr,"\n"); } #endif - + } #endif static void bwlzh_compress_gen(unsigned int *vals, int nvals, - unsigned char *output, int *output_len, - int enable_lz77, - int verbose) + unsigned char *output, int *output_len, + int enable_lz77, + int verbose) { unsigned int *vals16; int nvals16; @@ -119,7 +119,7 @@ static void bwlzh_compress_gen(unsigned int *vals, int nvals, lens=tmpmem+max_vals_per_block*15; #ifdef PARTIAL_MTF3 mtf3=warnmalloc(max_vals_per_block*3*3*sizeof *mtf3); /* 3 due to expansion of 32 bit to 16 bit, 3 due to up to 3 bytes - per 16 value. */ + per 16 value. */ #endif if (verbose) { @@ -131,20 +131,20 @@ static void bwlzh_compress_gen(unsigned int *vals, int nvals, output[outdata++]=(((unsigned int)nvals)>>8)&0xFFU; output[outdata++]=(((unsigned int)nvals)>>16)&0xFFU; output[outdata++]=(((unsigned int)nvals)>>24)&0xFFU; - + valsleft=nvals; valstart=0; while (valsleft) { int reducealgo=1; /* Reduce algo is LZ77. */ if (!enable_lz77) - reducealgo=0; + reducealgo=0; thisvals=valsleft; if (thisvals>max_vals_per_block) - thisvals=max_vals_per_block; + thisvals=max_vals_per_block; valsleft-=thisvals; if (verbose) - fprintf(stderr,"Creating vals16 block from %d values.\n",thisvals); + fprintf(stderr,"Creating vals16 block from %d values.\n",thisvals); #ifdef SHOWIT printvals("vals",vals+valstart,thisvals); @@ -160,15 +160,15 @@ static void bwlzh_compress_gen(unsigned int *vals, int nvals, #ifdef SHOWIT printvals("vals16",vals16,nvals16); #endif - + if (verbose) - { - fprintf(stderr,"Resulting vals16 values: %d\n",nvals16); - } + { + fprintf(stderr,"Resulting vals16 values: %d\n",nvals16); + } if (verbose) - { - fprintf(stderr,"BWT\n"); - } + { + fprintf(stderr,"BWT\n"); + } Ptngc_comp_to_bwt(vals16,nvals16,bwt,&bwt_index); #ifdef SHOWIT @@ -181,7 +181,7 @@ static void bwlzh_compress_gen(unsigned int *vals, int nvals, output[outdata++]=(((unsigned int)thisvals)>>8)&0xFFU; output[outdata++]=(((unsigned int)thisvals)>>16)&0xFFU; output[outdata++]=(((unsigned int)thisvals)>>24)&0xFFU; - + /* Store the number of nvals16 values in this block. */ output[outdata++]=((unsigned int)nvals16)&0xFFU; output[outdata++]=(((unsigned int)nvals16)>>8)&0xFFU; @@ -193,261 +193,261 @@ static void bwlzh_compress_gen(unsigned int *vals, int nvals, output[outdata++]=(((unsigned int)bwt_index)>>8)&0xFFU; output[outdata++]=(((unsigned int)bwt_index)>>16)&0xFFU; output[outdata++]=(((unsigned int)bwt_index)>>24)&0xFFU; - + if (verbose) - fprintf(stderr,"MTF\n"); + fprintf(stderr,"MTF\n"); #ifdef PARTIAL_MTF3 Ptngc_comp_conv_to_mtf_partial3(bwt,nvals16, - mtf3); + mtf3); for (imtfinner=0; imtfinner<3; imtfinner++) - { - int i; - if (verbose) - fprintf(stderr,"Doing partial MTF: %d\n",imtfinner); - for (i=0; i<nvals16; i++) - mtf[i]=(unsigned int)mtf3[imtfinner*nvals16+i]; + { + int i; + if (verbose) + fprintf(stderr,"Doing partial MTF: %d\n",imtfinner); + for (i=0; i<nvals16; i++) + mtf[i]=(unsigned int)mtf3[imtfinner*nvals16+i]; #else #ifdef PARTIAL_MTF - Ptngc_comp_conv_to_mtf_partial(bwt,nvals16,mtf); + Ptngc_comp_conv_to_mtf_partial(bwt,nvals16,mtf); #else int ndict; - Ptngc_comp_canonical_dict(dict,&ndict); - Ptngc_comp_conv_to_mtf(bwt,nvals16, - dict,ndict,mtf); + Ptngc_comp_canonical_dict(dict,&ndict); + Ptngc_comp_conv_to_mtf(bwt,nvals16, + dict,ndict,mtf); #endif #ifdef SHOWIT - printvals("mtf",mtf,nvals16); -#endif -#endif - - - if (reducealgo==1) - { - if (verbose) - fprintf(stderr,"LZ77\n"); - reducealgo=1; - Ptngc_comp_to_lz77(mtf,nvals16,rle,&nrle,lens,&nlens,offsets,&noffsets); - - if (verbose) - { - fprintf(stderr,"Resulting LZ77 values: %d\n",nrle); - fprintf(stderr,"Resulting LZ77 lens: %d\n",nlens); - fprintf(stderr,"Resulting LZ77 offsets: %d\n",noffsets); - } + printvals("mtf",mtf,nvals16); +#endif +#endif + + + if (reducealgo==1) + { + if (verbose) + fprintf(stderr,"LZ77\n"); + reducealgo=1; + Ptngc_comp_to_lz77(mtf,nvals16,rle,&nrle,lens,&nlens,offsets,&noffsets); + + if (verbose) + { + fprintf(stderr,"Resulting LZ77 values: %d\n",nrle); + fprintf(stderr,"Resulting LZ77 lens: %d\n",nlens); + fprintf(stderr,"Resulting LZ77 offsets: %d\n",noffsets); + } #ifdef SHOWIT - printvals("lz77 table",rle,nrle); - printvals("lz77 lengths",lens,nlens); - printvals("lz77 offsets",offsets,noffsets); + printvals("lz77 table",rle,nrle); + printvals("lz77 lengths",lens,nlens); + printvals("lz77 offsets",offsets,noffsets); #endif #if 0 - if (noffsets) - { - unsigned int thist[0x20004]; - unsigned int coarse[17]={0,}; - int jj; - Ptngc_comp_make_dict_hist(lens,nlens,dict,&ndict,thist); - for (jj=0; jj<ndict; jj++) - fprintf(stderr,"%d %u %u L\n",jj,dict[jj],thist[jj]); - - Ptngc_comp_make_dict_hist(offsets,noffsets,dict,&ndict,thist); - for (jj=0; jj<ndict; jj++) - { - unsigned int v=dict[jj]; - int numbits=0; - while (v) - { - numbits++; - v>>=1; - } - coarse[numbits-1]+=thist[jj]; - } + if (noffsets) + { + unsigned int thist[0x20004]; + unsigned int coarse[17]={0,}; + int jj; + Ptngc_comp_make_dict_hist(lens,nlens,dict,&ndict,thist); + for (jj=0; jj<ndict; jj++) + fprintf(stderr,"%d %u %u L\n",jj,dict[jj],thist[jj]); + + Ptngc_comp_make_dict_hist(offsets,noffsets,dict,&ndict,thist); + for (jj=0; jj<ndict; jj++) + { + unsigned int v=dict[jj]; + int numbits=0; + while (v) + { + numbits++; + v>>=1; + } + coarse[numbits-1]+=thist[jj]; + } #if 1 - for (jj=0; jj<ndict; jj++) - fprintf(stderr,"%d %u %u O\n",jj,dict[jj],thist[jj]); + for (jj=0; jj<ndict; jj++) + fprintf(stderr,"%d %u %u O\n",jj,dict[jj],thist[jj]); #else - for (jj=0; jj<17; jj++) - fprintf(stderr,"%d %u\n",jj+1,coarse[jj]); + for (jj=0; jj<17; jj++) + fprintf(stderr,"%d %u\n",jj+1,coarse[jj]); #endif - } - exit(0); + } + exit(0); #endif - if (nlens<2) - reducealgo=0; + if (nlens<2) + reducealgo=0; #ifdef SHOWTEST - reducealgo=1; -#endif - } - if (reducealgo==0) - { - if (verbose) - fprintf(stderr,"RLE\n"); - /* Do RLE. For any repetetitive characters. */ - Ptngc_comp_conv_to_rle(mtf,nvals16,rle,&nrle,1); - + reducealgo=1; +#endif + } + if (reducealgo==0) + { + if (verbose) + fprintf(stderr,"RLE\n"); + /* Do RLE. For any repetetitive characters. */ + Ptngc_comp_conv_to_rle(mtf,nvals16,rle,&nrle,1); + #ifdef SHOWIT - printvals("rle",rle,nrle); + printvals("rle",rle,nrle); #endif - if (verbose) - fprintf(stderr,"Resulting RLE values: %d\n",nrle); - } + if (verbose) + fprintf(stderr,"Resulting RLE values: %d\n",nrle); + } + + /* reducealgo: RLE == 0, LZ77 == 1 */ + output[outdata++]=reducealgo; - /* reducealgo: RLE == 0, LZ77 == 1 */ - output[outdata++]=reducealgo; + if (verbose) + fprintf(stderr,"Huffman\n"); - if (verbose) - fprintf(stderr,"Huffman\n"); - - huffalgo=-1; - Ptngc_comp_huff_compress_verbose(rle,nrle,bwlzhhuff,&bwlzhhufflen,&huffdatalen,nhufflen,&huffalgo,1); + huffalgo=-1; + Ptngc_comp_huff_compress_verbose(rle,nrle,bwlzhhuff,&bwlzhhufflen,&huffdatalen,nhufflen,&huffalgo,1); #ifdef SHOWTEST - { - int i; - fprintf(stderr,"Huffman\n"); - for (i=0; i<bwlzhhufflen; i++) - fprintf(stderr,"%02x",(unsigned int)bwlzhhuff[i]); - fprintf(stderr,"\n"); - exit(0); - } -#endif - if (verbose) - { - int i; - fprintf(stderr,"Huffman data length is %d B.\n",huffdatalen); - for (i=0; i<N_HUFFMAN_ALGO; i++) - fprintf(stderr,"Huffman dictionary for algorithm %s is %d B.\n",Ptngc_comp_get_huff_algo_name(i),nhufflen[i]-huffdatalen); - fprintf(stderr,"Resulting algorithm: %s. Size=%d B\n",Ptngc_comp_get_huff_algo_name(huffalgo),bwlzhhufflen); - } - - /* Store the number of huffman values in this block. */ - output[outdata++]=((unsigned int)nrle)&0xFFU; - output[outdata++]=(((unsigned int)nrle)>>8)&0xFFU; - output[outdata++]=(((unsigned int)nrle)>>16)&0xFFU; - output[outdata++]=(((unsigned int)nrle)>>24)&0xFFU; - - /* Store the size of the huffman block. */ - output[outdata++]=((unsigned int)bwlzhhufflen)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>8)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>16)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>24)&0xFFU; - - /* Store the huffman block. */ - memcpy(output+outdata,bwlzhhuff,bwlzhhufflen); - outdata+=bwlzhhufflen; - - if (reducealgo==1) - { - /* Store the number of values in this block. */ - output[outdata++]=((unsigned int)noffsets)&0xFFU; - output[outdata++]=(((unsigned int)noffsets)>>8)&0xFFU; - output[outdata++]=(((unsigned int)noffsets)>>16)&0xFFU; - output[outdata++]=(((unsigned int)noffsets)>>24)&0xFFU; - - if (noffsets>0) - { - if (verbose) - fprintf(stderr,"Huffman for offsets\n"); - - huffalgo=-1; - Ptngc_comp_huff_compress_verbose(offsets,noffsets,bwlzhhuff,&bwlzhhufflen,&huffdatalen,nhufflen,&huffalgo,1); - if (verbose) - { - int i; - fprintf(stderr,"Huffman data length is %d B.\n",huffdatalen); - for (i=0; i<N_HUFFMAN_ALGO; i++) - fprintf(stderr,"Huffman dictionary for algorithm %s is %d B.\n",Ptngc_comp_get_huff_algo_name(i),nhufflen[i]-huffdatalen); - fprintf(stderr,"Resulting algorithm: %s. Size=%d B\n",Ptngc_comp_get_huff_algo_name(huffalgo),bwlzhhufflen); - } - - /* If huffman was bad for these offsets, just store the offsets as pairs of bytes. */ - if (bwlzhhufflen<noffsets*2) - { - output[outdata++]=0; - - /* Store the size of the huffman block. */ - output[outdata++]=((unsigned int)bwlzhhufflen)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>8)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>16)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>24)&0xFFU; - - /* Store the huffman block. */ - memcpy(output+outdata,bwlzhhuff,bwlzhhufflen); - outdata+=bwlzhhufflen; - } - else - { - int i; - output[outdata++]=1; - for (i=0; i<noffsets; i++) - { - output[outdata++]=((unsigned int)offsets[i])&0xFFU; - output[outdata++]=(((unsigned int)offsets[i])>>8)&0xFFU; - } - if (verbose) - fprintf(stderr,"Store raw offsets: %d B\n",noffsets*2); - } - } + { + int i; + fprintf(stderr,"Huffman\n"); + for (i=0; i<bwlzhhufflen; i++) + fprintf(stderr,"%02x",(unsigned int)bwlzhhuff[i]); + fprintf(stderr,"\n"); + exit(0); + } +#endif + if (verbose) + { + int i; + fprintf(stderr,"Huffman data length is %d B.\n",huffdatalen); + for (i=0; i<N_HUFFMAN_ALGO; i++) + fprintf(stderr,"Huffman dictionary for algorithm %s is %d B.\n",Ptngc_comp_get_huff_algo_name(i),nhufflen[i]-huffdatalen); + fprintf(stderr,"Resulting algorithm: %s. Size=%d B\n",Ptngc_comp_get_huff_algo_name(huffalgo),bwlzhhufflen); + } + + /* Store the number of huffman values in this block. */ + output[outdata++]=((unsigned int)nrle)&0xFFU; + output[outdata++]=(((unsigned int)nrle)>>8)&0xFFU; + output[outdata++]=(((unsigned int)nrle)>>16)&0xFFU; + output[outdata++]=(((unsigned int)nrle)>>24)&0xFFU; + + /* Store the size of the huffman block. */ + output[outdata++]=((unsigned int)bwlzhhufflen)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>8)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>16)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>24)&0xFFU; + + /* Store the huffman block. */ + memcpy(output+outdata,bwlzhhuff,bwlzhhufflen); + outdata+=bwlzhhufflen; + + if (reducealgo==1) + { + /* Store the number of values in this block. */ + output[outdata++]=((unsigned int)noffsets)&0xFFU; + output[outdata++]=(((unsigned int)noffsets)>>8)&0xFFU; + output[outdata++]=(((unsigned int)noffsets)>>16)&0xFFU; + output[outdata++]=(((unsigned int)noffsets)>>24)&0xFFU; + + if (noffsets>0) + { + if (verbose) + fprintf(stderr,"Huffman for offsets\n"); + + huffalgo=-1; + Ptngc_comp_huff_compress_verbose(offsets,noffsets,bwlzhhuff,&bwlzhhufflen,&huffdatalen,nhufflen,&huffalgo,1); + if (verbose) + { + int i; + fprintf(stderr,"Huffman data length is %d B.\n",huffdatalen); + for (i=0; i<N_HUFFMAN_ALGO; i++) + fprintf(stderr,"Huffman dictionary for algorithm %s is %d B.\n",Ptngc_comp_get_huff_algo_name(i),nhufflen[i]-huffdatalen); + fprintf(stderr,"Resulting algorithm: %s. Size=%d B\n",Ptngc_comp_get_huff_algo_name(huffalgo),bwlzhhufflen); + } + + /* If huffman was bad for these offsets, just store the offsets as pairs of bytes. */ + if (bwlzhhufflen<noffsets*2) + { + output[outdata++]=0; + + /* Store the size of the huffman block. */ + output[outdata++]=((unsigned int)bwlzhhufflen)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>8)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>16)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>24)&0xFFU; + + /* Store the huffman block. */ + memcpy(output+outdata,bwlzhhuff,bwlzhhufflen); + outdata+=bwlzhhufflen; + } + else + { + int i; + output[outdata++]=1; + for (i=0; i<noffsets; i++) + { + output[outdata++]=((unsigned int)offsets[i])&0xFFU; + output[outdata++]=(((unsigned int)offsets[i])>>8)&0xFFU; + } + if (verbose) + fprintf(stderr,"Store raw offsets: %d B\n",noffsets*2); + } + } #if 0 - { - int i,ndict; - FILE *f=fopen("len.dict","w"); - Ptngc_comp_make_dict_hist(lens,nlens,dict,&ndict,hist); - for (i=0; i<ndict; i++) - fprintf(f,"%d %d %d\n",i,dict[i],hist[i]); - fclose(f); - f=fopen("off.dict","w"); - Ptngc_comp_make_dict_hist(offsets,noffsets,dict,&ndict,hist); - for (i=0; i<ndict; i++) - fprintf(f,"%d %d %d\n",i,dict[i],hist[i]); - fclose(f); - f=fopen("len.time","w"); - for (i=0; i<ndict; i++) - fprintf(f,"%d\n",lens[i]); - fclose(f); - f=fopen("off.time","w"); - for (i=0; i<ndict; i++) - fprintf(f,"%d\n",offsets[i]); - fclose(f); - } -#endif - - if (verbose) - fprintf(stderr,"Huffman for lengths\n"); - - huffalgo=-1; - Ptngc_comp_huff_compress_verbose(lens,nlens,bwlzhhuff,&bwlzhhufflen,&huffdatalen,nhufflen,&huffalgo,1); - if (verbose) - { - int i; - fprintf(stderr,"Huffman data length is %d B.\n",huffdatalen); - for (i=0; i<N_HUFFMAN_ALGO; i++) - fprintf(stderr,"Huffman dictionary for algorithm %s is %d B.\n",Ptngc_comp_get_huff_algo_name(i),nhufflen[i]-huffdatalen); - fprintf(stderr,"Resulting algorithm: %s. Size=%d B\n",Ptngc_comp_get_huff_algo_name(huffalgo),bwlzhhufflen); - } - - /* Store the number of values in this block. */ - output[outdata++]=((unsigned int)nlens)&0xFFU; - output[outdata++]=(((unsigned int)nlens)>>8)&0xFFU; - output[outdata++]=(((unsigned int)nlens)>>16)&0xFFU; - output[outdata++]=(((unsigned int)nlens)>>24)&0xFFU; - - /* Store the size of the huffman block. */ - output[outdata++]=((unsigned int)bwlzhhufflen)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>8)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>16)&0xFFU; - output[outdata++]=(((unsigned int)bwlzhhufflen)>>24)&0xFFU; - - /* Store the huffman block. */ - memcpy(output+outdata,bwlzhhuff,bwlzhhufflen); - outdata+=bwlzhhufflen; - } + { + int i,ndict; + FILE *f=fopen("len.dict","w"); + Ptngc_comp_make_dict_hist(lens,nlens,dict,&ndict,hist); + for (i=0; i<ndict; i++) + fprintf(f,"%d %d %d\n",i,dict[i],hist[i]); + fclose(f); + f=fopen("off.dict","w"); + Ptngc_comp_make_dict_hist(offsets,noffsets,dict,&ndict,hist); + for (i=0; i<ndict; i++) + fprintf(f,"%d %d %d\n",i,dict[i],hist[i]); + fclose(f); + f=fopen("len.time","w"); + for (i=0; i<ndict; i++) + fprintf(f,"%d\n",lens[i]); + fclose(f); + f=fopen("off.time","w"); + for (i=0; i<ndict; i++) + fprintf(f,"%d\n",offsets[i]); + fclose(f); + } +#endif + + if (verbose) + fprintf(stderr,"Huffman for lengths\n"); + + huffalgo=-1; + Ptngc_comp_huff_compress_verbose(lens,nlens,bwlzhhuff,&bwlzhhufflen,&huffdatalen,nhufflen,&huffalgo,1); + if (verbose) + { + int i; + fprintf(stderr,"Huffman data length is %d B.\n",huffdatalen); + for (i=0; i<N_HUFFMAN_ALGO; i++) + fprintf(stderr,"Huffman dictionary for algorithm %s is %d B.\n",Ptngc_comp_get_huff_algo_name(i),nhufflen[i]-huffdatalen); + fprintf(stderr,"Resulting algorithm: %s. Size=%d B\n",Ptngc_comp_get_huff_algo_name(huffalgo),bwlzhhufflen); + } + + /* Store the number of values in this block. */ + output[outdata++]=((unsigned int)nlens)&0xFFU; + output[outdata++]=(((unsigned int)nlens)>>8)&0xFFU; + output[outdata++]=(((unsigned int)nlens)>>16)&0xFFU; + output[outdata++]=(((unsigned int)nlens)>>24)&0xFFU; + + /* Store the size of the huffman block. */ + output[outdata++]=((unsigned int)bwlzhhufflen)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>8)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>16)&0xFFU; + output[outdata++]=(((unsigned int)bwlzhhufflen)>>24)&0xFFU; + + /* Store the huffman block. */ + memcpy(output+outdata,bwlzhhuff,bwlzhhufflen); + outdata+=bwlzhhufflen; + } #ifdef PARTIAL_MTF3 - } + } #endif } @@ -463,39 +463,39 @@ static void bwlzh_compress_gen(unsigned int *vals, int nvals, void DECLSPECDLLEXPORT bwlzh_compress(unsigned int *vals, int nvals, - unsigned char *output, int *output_len) + unsigned char *output, int *output_len) { bwlzh_compress_gen(vals,nvals,output,output_len,1,0); } void DECLSPECDLLEXPORT bwlzh_compress_verbose(unsigned int *vals, int nvals, - unsigned char *output, int *output_len) + unsigned char *output, int *output_len) { bwlzh_compress_gen(vals,nvals,output,output_len,1,1); } void DECLSPECDLLEXPORT bwlzh_compress_no_lz77(unsigned int *vals, int nvals, - unsigned char *output, int *output_len) + unsigned char *output, int *output_len) { bwlzh_compress_gen(vals,nvals,output,output_len,0,0); } void DECLSPECDLLEXPORT bwlzh_compress_no_lz77_verbose(unsigned int *vals, int nvals, - unsigned char *output, int *output_len) + unsigned char *output, int *output_len) { bwlzh_compress_gen(vals,nvals,output,output_len,0,1); } static void bwlzh_decompress_gen(unsigned char *input, int nvals, - unsigned int *vals, - int verbose) + unsigned int *vals, + int verbose) { unsigned int *vals16; int nvals16; int bwt_index; - unsigned int *bwt=NULL; + unsigned int *bwt=NULL; unsigned int *mtf=NULL; #ifdef PARTIAL_MTF3 unsigned char *mtf3=NULL; @@ -532,7 +532,7 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, lens=tmpmem+max_vals_per_block*15; #ifdef PARTIAL_MTF3 mtf3=warnmalloc(max_vals_per_block*3*3*sizeof *mtf3); /* 3 due to expansion of 32 bit to 16 bit, 3 due to up to 3 bytes - per 16 value. */ + per 16 value. */ #endif if (verbose) @@ -542,9 +542,9 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, /* Read the number of real values in the whole block. */ nvalsfile=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); inpdata+=4; if (nvalsfile!=nvals) @@ -552,7 +552,7 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, fprintf(stderr,"BWLZH: The number of values found in the file is different from the number of values expected.\n"); exit(EXIT_FAILURE); } - + valsleft=nvals; valstart=0; while (valsleft) @@ -561,182 +561,182 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, int reducealgo; /* Read the number of real values in this block. */ thisvals=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); inpdata+=4; - + valsleft-=thisvals; /* Read the number of nvals16 values in this block. */ nvals16=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); inpdata+=4; /* Read the BWT index. */ bwt_index=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); inpdata+=4; if (thisvals>max_vals_per_block) - { - /* More memory must be allocated for decompression. */ - max_vals_per_block=thisvals; - if (verbose) - fprintf(stderr,"Allocating more memory: %d B\n",(int)(max_vals_per_block*15*sizeof *tmpmem)); - tmpmem=warnrealloc(tmpmem,max_vals_per_block*18*sizeof *tmpmem); - vals16=tmpmem; - bwt=tmpmem+max_vals_per_block*3; - mtf=tmpmem+max_vals_per_block*6; - rle=tmpmem+max_vals_per_block*9; - offsets=tmpmem+max_vals_per_block*12; - lens=tmpmem+max_vals_per_block*15; + { + /* More memory must be allocated for decompression. */ + max_vals_per_block=thisvals; + if (verbose) + fprintf(stderr,"Allocating more memory: %d B\n",(int)(max_vals_per_block*15*sizeof *tmpmem)); + tmpmem=warnrealloc(tmpmem,max_vals_per_block*18*sizeof *tmpmem); + vals16=tmpmem; + bwt=tmpmem+max_vals_per_block*3; + mtf=tmpmem+max_vals_per_block*6; + rle=tmpmem+max_vals_per_block*9; + offsets=tmpmem+max_vals_per_block*12; + lens=tmpmem+max_vals_per_block*15; #ifdef PARTIAL_MTF3 - mtf3=warnrealloc(mtf3,max_vals_per_block*3*3*sizeof *mtf3); /* 3 due to expansion of 32 bit to 16 bit, 3 due to up to 3 bytes - per 16 value. */ -#endif - } + mtf3=warnrealloc(mtf3,max_vals_per_block*3*3*sizeof *mtf3); /* 3 due to expansion of 32 bit to 16 bit, 3 due to up to 3 bytes + per 16 value. */ +#endif + } #ifdef PARTIAL_MTF3 for (imtfinner=0; imtfinner<3; imtfinner++) - { - int i; - if (verbose) - fprintf(stderr,"Doing partial MTF: %d\n",imtfinner); -#endif - - reducealgo=(int)input[inpdata]; - inpdata++; - - /* Read the number of huffman values in this block. */ - nrle=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); - inpdata+=4; - - /* Read the size of the huffman block. */ - bwlzhhufflen=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); - inpdata+=4; - - if (verbose) - fprintf(stderr,"Decompressing huffman block of length %d.\n",bwlzhhufflen); - /* Decompress the huffman block. */ - Ptngc_comp_huff_decompress(input+inpdata,bwlzhhufflen,rle); - inpdata+=bwlzhhufflen; - - if (reducealgo==1) /* LZ77 */ - { - int offstore; - /* Read the number of huffman values in this block. */ - noffsets=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); - inpdata+=4; - - if (noffsets>0) - { - /* How are the offsets stored? */ - offstore=(int)input[inpdata++]; - if (offstore==0) - { - /* Read the size of the huffman block. */ - bwlzhhufflen=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); - inpdata+=4; - - if (verbose) - fprintf(stderr,"Decompressing offset huffman block.\n"); - - /* Decompress the huffman block. */ - Ptngc_comp_huff_decompress(input+inpdata,bwlzhhufflen,offsets); - inpdata+=bwlzhhufflen; - } - else - { - int i; - if (verbose) - fprintf(stderr,"Reading offset block.\n"); - for (i=0; i<noffsets; i++) - { - offsets[i]=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8)); - inpdata+=2; - } - } - } + { + int i; + if (verbose) + fprintf(stderr,"Doing partial MTF: %d\n",imtfinner); +#endif + + reducealgo=(int)input[inpdata]; + inpdata++; + + /* Read the number of huffman values in this block. */ + nrle=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); + inpdata+=4; + + /* Read the size of the huffman block. */ + bwlzhhufflen=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); + inpdata+=4; + + if (verbose) + fprintf(stderr,"Decompressing huffman block of length %d.\n",bwlzhhufflen); + /* Decompress the huffman block. */ + Ptngc_comp_huff_decompress(input+inpdata,bwlzhhufflen,rle); + inpdata+=bwlzhhufflen; + + if (reducealgo==1) /* LZ77 */ + { + int offstore; + /* Read the number of huffman values in this block. */ + noffsets=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); + inpdata+=4; + + if (noffsets>0) + { + /* How are the offsets stored? */ + offstore=(int)input[inpdata++]; + if (offstore==0) + { + /* Read the size of the huffman block. */ + bwlzhhufflen=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); + inpdata+=4; + + if (verbose) + fprintf(stderr,"Decompressing offset huffman block.\n"); + + /* Decompress the huffman block. */ + Ptngc_comp_huff_decompress(input+inpdata,bwlzhhufflen,offsets); + inpdata+=bwlzhhufflen; + } + else + { + int i; + if (verbose) + fprintf(stderr,"Reading offset block.\n"); + for (i=0; i<noffsets; i++) + { + offsets[i]=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8)); + inpdata+=2; + } + } + } #if 0 - { - int i; - for (i=0; i<nrle; i++) - fprintf(stderr,"RLE %d: %d\n",i,rle[i]); - for (i=0; i<noffsets; i++) - fprintf(stderr,"OFFSET %d: %d\n",i,offsets[i]); - } -#endif - - - /* Read the number of huffman values in this block. */ - nlens=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); - inpdata+=4; - - /* Read the size of the huffman block. */ - bwlzhhufflen=(int)(((unsigned int)input[inpdata]) | - (((unsigned int)input[inpdata+1])<<8) | - (((unsigned int)input[inpdata+2])<<16) | - (((unsigned int)input[inpdata+3])<<24)); - inpdata+=4; - - if (verbose) - fprintf(stderr,"Decompressing length huffman block.\n"); - - /* Decompress the huffman block. */ - Ptngc_comp_huff_decompress(input+inpdata,bwlzhhufflen,lens); - inpdata+=bwlzhhufflen; - - if (verbose) - fprintf(stderr,"Decompressing LZ77.\n"); - - Ptngc_comp_from_lz77(rle,nrle,lens,nlens,offsets,noffsets,mtf,nvals16); - } - else if (reducealgo==0) /* RLE */ - { + { + int i; + for (i=0; i<nrle; i++) + fprintf(stderr,"RLE %d: %d\n",i,rle[i]); + for (i=0; i<noffsets; i++) + fprintf(stderr,"OFFSET %d: %d\n",i,offsets[i]); + } +#endif + + + /* Read the number of huffman values in this block. */ + nlens=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); + inpdata+=4; + + /* Read the size of the huffman block. */ + bwlzhhufflen=(int)(((unsigned int)input[inpdata]) | + (((unsigned int)input[inpdata+1])<<8) | + (((unsigned int)input[inpdata+2])<<16) | + (((unsigned int)input[inpdata+3])<<24)); + inpdata+=4; + + if (verbose) + fprintf(stderr,"Decompressing length huffman block.\n"); + + /* Decompress the huffman block. */ + Ptngc_comp_huff_decompress(input+inpdata,bwlzhhufflen,lens); + inpdata+=bwlzhhufflen; + + if (verbose) + fprintf(stderr,"Decompressing LZ77.\n"); + + Ptngc_comp_from_lz77(rle,nrle,lens,nlens,offsets,noffsets,mtf,nvals16); + } + else if (reducealgo==0) /* RLE */ + { #ifdef SHOWIT - printvals("rle",rle,nrle); + printvals("rle",rle,nrle); #endif - - if (verbose) - fprintf(stderr,"Decompressing rle block.\n"); - Ptngc_comp_conv_from_rle(rle,mtf,nvals16); - } + + if (verbose) + fprintf(stderr,"Decompressing rle block.\n"); + Ptngc_comp_conv_from_rle(rle,mtf,nvals16); + } #ifdef PARTIAL_MTF3 - for (i=0; i<nvals16; i++) - mtf3[imtfinner*nvals16+i]=(unsigned char)mtf[i]; - } + for (i=0; i<nvals16; i++) + mtf3[imtfinner*nvals16+i]=(unsigned char)mtf[i]; + } #else #ifdef SHOWIT printvals("mtf",mtf,nvals16); #endif - + #endif if (verbose) - fprintf(stderr,"Inverse MTF.\n"); + fprintf(stderr,"Inverse MTF.\n"); #ifdef PARTIAL_MTF3 Ptngc_comp_conv_from_mtf_partial3(mtf3,nvals16,bwt); #else @@ -744,7 +744,7 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, Ptngc_comp_conv_from_mtf_partial(mtf,nvals16,bwt); #else int ndict; - Ptngc_comp_canonical_dict(dict,&ndict); + Ptngc_comp_canonical_dict(dict,&ndict); Ptngc_comp_conv_from_mtf(mtf,nvals16,dict,ndict,bwt); #endif #endif @@ -753,9 +753,9 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, printvals("bwt",bwt,nvals16); fprintf(stderr,"BWT INDEX is %d\n",bwt_index); #endif - + if (verbose) - fprintf(stderr,"Inverse BWT.\n"); + fprintf(stderr,"Inverse BWT.\n"); Ptngc_comp_from_bwt(bwt,nvals16,bwt_index,vals16); #ifdef SHOWIT @@ -763,7 +763,7 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, #endif if (verbose) - fprintf(stderr,"Decompressing vals16 block.\n"); + fprintf(stderr,"Decompressing vals16 block.\n"); Ptngc_comp_conv_from_vals16(vals16,nvals16,vals+valstart,&valsnew); #ifdef SHOWIT @@ -771,10 +771,10 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, #endif if (valsnew!=thisvals) - { - fprintf(stderr,"BWLZH: Block contained different number of values than expected.\n"); - exit(EXIT_FAILURE); - } + { + fprintf(stderr,"BWLZH: Block contained different number of values than expected.\n"); + exit(EXIT_FAILURE); + } valstart+=thisvals; } free(hist); @@ -788,13 +788,13 @@ static void bwlzh_decompress_gen(unsigned char *input, int nvals, void DECLSPECDLLEXPORT bwlzh_decompress(unsigned char *input, int nvals, - unsigned int *vals) + unsigned int *vals) { bwlzh_decompress_gen(input,nvals,vals,0); } void DECLSPECDLLEXPORT bwlzh_decompress_verbose(unsigned char *input, int nvals, - unsigned int *vals) + unsigned int *vals) { bwlzh_decompress_gen(input,nvals,vals,1); } diff --git a/src/compression/bwt.c b/src/compression/bwt.c index 6edd1c2..182cd07 100644 --- a/src/compression/bwt.c +++ b/src/compression/bwt.c @@ -30,57 +30,57 @@ static int compare_index(int i1,int i2,int nvals,unsigned int *vals,unsigned int for (i=0; i<nvals; i++) { /* If we have repeating patterns, we might be able to start the - comparison later in the string. */ + comparison later in the string. */ /* Do we have a repeating pattern? If so are - the repeating patterns the same length? */ + the repeating patterns the same length? */ int repeat1=(int)(nrepeat[i1]>>8); int k1=(int)(nrepeat[i1]&0xFFU); int repeat2=(int)(nrepeat[i2]>>8); int k2=(int)(nrepeat[i2]&0xFFU); if ((repeat1>1) && (repeat2>1) && (k1==k2)) - { - int maxskip=0; - /* Yes. Compare the repeating patterns. */ - for (j=0; j<k1; j++) - { - unsigned int v1=vals[(i1+j)%nvals]; - unsigned int v2=vals[(i2+j)%nvals]; - if (v1<v2) - return -1; - else if (v1>v2) - return 1; - } - /* The repeating patterns are equal. Skip as far as we can - before continuing. */ - maxskip=repeat1; - if (repeat2<repeat1) - maxskip=repeat2; - i1=(i1+maxskip)%nvals; - i2=(i2+maxskip)%nvals; - i+=maxskip-1; - } + { + int maxskip=0; + /* Yes. Compare the repeating patterns. */ + for (j=0; j<k1; j++) + { + unsigned int v1=vals[(i1+j)%nvals]; + unsigned int v2=vals[(i2+j)%nvals]; + if (v1<v2) + return -1; + else if (v1>v2) + return 1; + } + /* The repeating patterns are equal. Skip as far as we can + before continuing. */ + maxskip=repeat1; + if (repeat2<repeat1) + maxskip=repeat2; + i1=(i1+maxskip)%nvals; + i2=(i2+maxskip)%nvals; + i+=maxskip-1; + } else - { - if (vals[i1]<vals[i2]) - return -1; - else if (vals[i1]>vals[i2]) - return 1; - i1++; - if (i1>=nvals) - i1=0; - i2++; - if (i2>=nvals) - i2=0; - } + { + if (vals[i1]<vals[i2]) + return -1; + else if (vals[i1]>vals[i2]) + return 1; + i1++; + if (i1>=nvals) + i1=0; + i2++; + if (i2>=nvals) + i2=0; + } } return 0; } void Ptngc_bwt_merge_sort_inner(int *indices, int nvals,unsigned int *vals, - int start, int end, - unsigned int *nrepeat, - int *workarray) + int start, int end, + unsigned int *nrepeat, + int *workarray) { int middle; if ((end-start)>1) @@ -90,60 +90,60 @@ void Ptngc_bwt_merge_sort_inner(int *indices, int nvals,unsigned int *vals, printf("For start %d end %d obtained new middle: %d\n",start,end,middle); #endif Ptngc_bwt_merge_sort_inner(indices,nvals,vals, - start,middle, - nrepeat, - workarray); + start,middle, + nrepeat, + workarray); Ptngc_bwt_merge_sort_inner(indices,nvals,vals, - middle,end, - nrepeat, - workarray); + middle,end, + nrepeat, + workarray); #if 0 printf("For start %d end %d Before merge: Comparing element %d with %d\n",start,end,middle-1,middle); #endif if (compare_index(indices[middle-1],indices[middle],nvals,vals,nrepeat)>0) - { - /* Merge to work array. */ - int i, n=end-start; - int ileft=start; - int iright=middle; - for (i=0; i<n; i++) - { - if (ileft==middle) - { - workarray[i]=indices[iright]; - iright++; - } - else if (iright==end) - { - workarray[i]=indices[ileft]; - ileft++; - } - else - { + { + /* Merge to work array. */ + int i, n=end-start; + int ileft=start; + int iright=middle; + for (i=0; i<n; i++) + { + if (ileft==middle) + { + workarray[i]=indices[iright]; + iright++; + } + else if (iright==end) + { + workarray[i]=indices[ileft]; + ileft++; + } + else + { #if 0 - printf("For start %d end %d In merge: Comparing element %d with %d\n",start,end,ileft,iright); + printf("For start %d end %d In merge: Comparing element %d with %d\n",start,end,ileft,iright); #endif - if (compare_index(indices[ileft],indices[iright],nvals,vals,nrepeat)>0) - { - workarray[i]=indices[iright]; - iright++; - } - else - { - workarray[i]=indices[ileft]; - ileft++; - } - } - } - /* Copy result back. */ - memcpy(indices+start,workarray,(end-start)*sizeof(int)); - } + if (compare_index(indices[ileft],indices[iright],nvals,vals,nrepeat)>0) + { + workarray[i]=indices[iright]; + iright++; + } + else + { + workarray[i]=indices[ileft]; + ileft++; + } + } + } + /* Copy result back. */ + memcpy(indices+start,workarray,(end-start)*sizeof(int)); + } } } /* Burrows-Wheeler transform. */ void Ptngc_comp_to_bwt(unsigned int *vals, int nvals, - unsigned int *output, int *index) + unsigned int *output, int *index) { int i; int *indices=warnmalloc(2*nvals*sizeof *indices); @@ -172,94 +172,94 @@ void Ptngc_comp_to_bwt(unsigned int *vals, int nvals, for (i=0; i<nvals; i++) { /* If we have not already found a repeating string we must find - it. */ + it. */ if (!nrepeat[i]) - { - int maxrepeat=nvals*2; - int j,k,m; - int good_j=-1, good_k=0; - int kmax=16; - /* Track repeating patterns. - k=1 corresponds to AAAAA... - k=2 corresponds to ABABAB... - k=3 corresponds to ABCABCABCABC... - k=4 corresponds to ABCDABCDABCD... - etc. */ - for (k=kmax; k>=1; k--) - { - try_next_k: - if (k>=1) - { + { + int maxrepeat=nvals*2; + int j,k,m; + int good_j=-1, good_k=0; + int kmax=16; + /* Track repeating patterns. + k=1 corresponds to AAAAA... + k=2 corresponds to ABABAB... + k=3 corresponds to ABCABCABCABC... + k=4 corresponds to ABCDABCDABCD... + etc. */ + for (k=kmax; k>=1; k--) + { + try_next_k: + if (k>=1) + { #ifdef SHOWIT - printf("Trying k=%d at i=%d\n",k,i); + printf("Trying k=%d at i=%d\n",k,i); #endif - for (j=k; j<maxrepeat; j+=k) - { - int is_equal=1; + for (j=k; j<maxrepeat; j+=k) + { + int is_equal=1; #ifdef SHOWIT - printf("Trying j=%d at i=%d for k %d\n",j,i,k); + printf("Trying j=%d at i=%d for k %d\n",j,i,k); #endif - for (m=0; m<k; m++) - if (vals[(i+m)%nvals]!=vals[(i+j+m)%nvals]) - { - is_equal=0; - break; - } - if (is_equal) - { - int new_j=j+k; - if (new_j>maxrepeat) - new_j=j; - if ((new_j>good_j) || ((new_j==good_j) && (k<good_k))) - { - good_j=new_j; /* We have found that - the strings repeat - for this length... */ - good_k=k; /* ...and with this - length of the - repeating - pattern. */ + for (m=0; m<k; m++) + if (vals[(i+m)%nvals]!=vals[(i+j+m)%nvals]) + { + is_equal=0; + break; + } + if (is_equal) + { + int new_j=j+k; + if (new_j>maxrepeat) + new_j=j; + if ((new_j>good_j) || ((new_j==good_j) && (k<good_k))) + { + good_j=new_j; /* We have found that + the strings repeat + for this length... */ + good_k=k; /* ...and with this + length of the + repeating + pattern. */ #ifdef SHOWIT - printf("Best j and k is now %d and %d\n",good_j,good_k); + printf("Best j and k is now %d and %d\n",good_j,good_k); #endif - } - } - else - { - /* We know that it is no point in trying - with more than m */ - if (j==0) - { - k=m; + } + } + else + { + /* We know that it is no point in trying + with more than m */ + if (j==0) + { + k=m; #ifdef SHOWIT - printf("Setting new k to m: %d\n",k); + printf("Setting new k to m: %d\n",k); #endif - } - else - k--; + } + else + k--; #ifdef SHOWIT - printf("Trying next k\n"); + printf("Trying next k\n"); #endif - goto try_next_k; - } - } - } - } - /* From good_j and good_k we know the repeat for a large - number of strings. The very last repeat length should not - be assigned, since it can be much longer if a new test is - done. */ - for (m=0; (m+good_k<good_j) && (i+m<nvals); m+=good_k) - { - int repeat=good_j-m; - if (repeat>nvals) - repeat=nvals; - nrepeat[i+m]=((unsigned int) (good_k)) | (((unsigned int) (repeat))<<8); - } - /* If no repetition was found for this value signal that here. */ - if (!nrepeat[i]) - nrepeat[i+m]=257U; /* This is 1<<8 | 1 */ - } + goto try_next_k; + } + } + } + } + /* From good_j and good_k we know the repeat for a large + number of strings. The very last repeat length should not + be assigned, since it can be much longer if a new test is + done. */ + for (m=0; (m+good_k<good_j) && (i+m<nvals); m+=good_k) + { + int repeat=good_j-m; + if (repeat>nvals) + repeat=nvals; + nrepeat[i+m]=((unsigned int) (good_k)) | (((unsigned int) (repeat))<<8); + } + /* If no repetition was found for this value signal that here. */ + if (!nrepeat[i]) + nrepeat[i+m]=257U; /* This is 1<<8 | 1 */ + } } #ifdef SHOWIT for (i=0; i<nvals; i++) @@ -284,7 +284,7 @@ void Ptngc_comp_to_bwt(unsigned int *vals, int nvals, { int j; for (j=0; j<nvals; j++) - printf("%c",vals[(indices[i]+j)%nvals]); + printf("%c",vals[(indices[i]+j)%nvals]); printf("\n"); } #endif @@ -298,7 +298,7 @@ void Ptngc_comp_to_bwt(unsigned int *vals, int nvals, { int lastchar=indices[i]-1; if (lastchar<0) - lastchar=nvals-1; + lastchar=nvals-1; output[i]=vals[lastchar]; } free(nrepeat); @@ -307,7 +307,7 @@ void Ptngc_comp_to_bwt(unsigned int *vals, int nvals, /* Burrows-Wheeler inverse transform. */ void Ptngc_comp_from_bwt(unsigned int *input, int nvals, int index, - unsigned int *vals) + unsigned int *vals) { /* Straightforward from the Burrows-Wheeler paper (page 13). */ int i; diff --git a/src/compression/coder.c b/src/compression/coder.c index c12042c..9e3c880 100644 --- a/src/compression/coder.c +++ b/src/compression/coder.c @@ -32,19 +32,19 @@ #define TNG_SNPRINTF snprintf #endif -struct coder *Ptngc_coder_init(void) +struct coder DECLSPECDLLEXPORT *Ptngc_coder_init(void) { struct coder *coder_inst=warnmalloc(sizeof *coder_inst); coder_inst->pack_temporary_bits=0; return coder_inst; } -void Ptngc_coder_deinit(struct coder *coder_inst) +void DECLSPECDLLEXPORT Ptngc_coder_deinit(struct coder *coder_inst) { free(coder_inst); } -TNG_INLINE void Ptngc_out8bits(struct coder *coder_inst, unsigned char **output) +TNG_INLINE void DECLSPECDLLEXPORT Ptngc_out8bits(struct coder *coder_inst, unsigned char **output) { int pack_temporary_bits=coder_inst->pack_temporary_bits; unsigned int pack_temporary=coder_inst->pack_temporary; @@ -61,7 +61,7 @@ TNG_INLINE void Ptngc_out8bits(struct coder *coder_inst, unsigned char **output) coder_inst->pack_temporary=pack_temporary; } -void Ptngc_write_pattern(struct coder *coder_inst, unsigned int pattern, +void DECLSPECDLLEXPORT Ptngc_write_pattern(struct coder *coder_inst, unsigned int pattern, int nbits, unsigned char **output) { unsigned int mask1,mask2; @@ -71,17 +71,17 @@ void Ptngc_write_pattern(struct coder *coder_inst, unsigned int pattern, coder_inst->pack_temporary_bits+=nbits; while (nbits) { - if (pattern & mask1) - coder_inst->pack_temporary|=mask2; - nbits--; - mask1<<=1; - mask2>>=1; + if (pattern & mask1) + coder_inst->pack_temporary|=mask2; + nbits--; + mask1<<=1; + mask2>>=1; } Ptngc_out8bits(coder_inst,output); } /* Write up to 24 bits */ -TNG_INLINE void Ptngc_writebits(struct coder *coder_inst, +TNG_INLINE void DECLSPECDLLEXPORT Ptngc_writebits(struct coder *coder_inst, unsigned int value, int nbits, unsigned char **output_ptr) { @@ -93,7 +93,7 @@ TNG_INLINE void Ptngc_writebits(struct coder *coder_inst, } /* Write up to 32 bits */ -void Ptngc_write32bits(struct coder *coder_inst,unsigned int value, +void DECLSPECDLLEXPORT Ptngc_write32bits(struct coder *coder_inst,unsigned int value, int nbits, unsigned char **output_ptr) { unsigned int mask; @@ -116,15 +116,15 @@ void Ptngc_write32bits(struct coder *coder_inst,unsigned int value, } /* Write "arbitrary" number of bits */ -void Ptngc_writemanybits(struct coder *coder_inst, unsigned char *value, +void DECLSPECDLLEXPORT Ptngc_writemanybits(struct coder *coder_inst, unsigned char *value, int nbits, unsigned char **output_ptr) { int vptr=0; while (nbits>=24) { unsigned int v=((((unsigned int)value[vptr])<<16)| - (((unsigned int)value[vptr+1])<<8)| - (((unsigned int)value[vptr+2]))); + (((unsigned int)value[vptr+1])<<8)| + (((unsigned int)value[vptr+2]))); Ptngc_writebits(coder_inst,v,24,output_ptr); vptr+=3; nbits-=24; @@ -151,8 +151,8 @@ static int write_stop_bit_code(struct coder *coder_inst, unsigned int s, s>>=coding_parameter; if (s) { - this|=1U; - coder_inst->stat_overflow++; + this|=1U; + coder_inst->stat_overflow++; } coder_inst->pack_temporary<<=(coding_parameter+1); coder_inst->pack_temporary_bits+=coding_parameter+1; @@ -160,9 +160,9 @@ static int write_stop_bit_code(struct coder *coder_inst, unsigned int s, Ptngc_out8bits(coder_inst,output); if (s) { - coding_parameter>>=1; - if (coding_parameter<1) - coding_parameter=1; + coding_parameter>>=1; + if (coding_parameter<1) + coding_parameter=1; } } while (s); coder_inst->stat_numval++; @@ -175,15 +175,15 @@ static int pack_stopbits_item(struct coder *coder_inst,int item, /* Find this symbol in table. */ int s=0; if (item>0) - s=1+(item-1)*2; + s=1+(item-1)*2; else if (item<0) - s=2+(-item-1)*2; + s=2+(-item-1)*2; return write_stop_bit_code(coder_inst,s,coding_parameter,output); } static int pack_triplet(struct coder *coder_inst, unsigned int *s, unsigned char **output, int coding_parameter, - unsigned int max_base, int maxbits) + unsigned int max_base, int maxbits) { /* Determine base for this triplet. */ unsigned int min_base=1U<<coding_parameter; @@ -194,14 +194,14 @@ static int pack_triplet(struct coder *coder_inst, unsigned int *s, for (i=0; i<3; i++) while (s[i]>=this_base) { - this_base*=2; - jbase++; + this_base*=2; + jbase++; } bits_per_value=coding_parameter+jbase; if (jbase>=3) { if (this_base>max_base) - return 1; + return 1; bits_per_value=maxbits; jbase=3; } @@ -215,14 +215,14 @@ static int pack_triplet(struct coder *coder_inst, unsigned int *s, return 0; } -void Ptngc_pack_flush(struct coder *coder_inst,unsigned char **output) +void DECLSPECDLLEXPORT Ptngc_pack_flush(struct coder *coder_inst,unsigned char **output) { /* Zero-fill just enough. */ if (coder_inst->pack_temporary_bits>0) Ptngc_write_pattern(coder_inst,0,8-coder_inst->pack_temporary_bits,output); } -unsigned char *Ptngc_pack_array(struct coder *coder_inst, +unsigned char DECLSPECDLLEXPORT *Ptngc_pack_array(struct coder *coder_inst, int *input, int *length, int coding, int coding_parameter, int natoms, int speed) { @@ -235,25 +235,25 @@ unsigned char *Ptngc_pack_array(struct coder *coder_inst, int cnt=0; int most_negative=2147483647; for (i=0; i<n; i++) - if (input[i]<most_negative) - most_negative=input[i]; + if (input[i]<most_negative) + most_negative=input[i]; most_negative=-most_negative; output[0]=((unsigned int)most_negative)&0xFFU; output[1]=(((unsigned int)most_negative)>>8)&0xFFU; output[2]=(((unsigned int)most_negative)>>16)&0xFFU; output[3]=(((unsigned int)most_negative)>>24)&0xFFU; for (i=0; i<natoms; i++) - for (j=0; j<3; j++) - for (k=0; k<nframes; k++) - { - int item=input[k*3*natoms+i*3+j]; - pval[cnt++]=(unsigned int)(item+most_negative); + for (j=0; j<3; j++) + for (k=0; k<nframes; k++) + { + int item=input[k*3*natoms+i*3+j]; + pval[cnt++]=(unsigned int)(item+most_negative); - } + } if (speed>=5) - bwlzh_compress(pval,n,output+4,length); + bwlzh_compress(pval,n,output+4,length); else - bwlzh_compress_no_lz77(pval,n,output+4,length); + bwlzh_compress_no_lz77(pval,n,output+4,length); (*length)+=4; free(pval); return output; @@ -275,64 +275,64 @@ unsigned char *Ptngc_pack_array(struct coder *coder_inst, output=warnmalloc(8* *length*sizeof *output); output_ptr=output; if ((coding==TNG_COMPRESS_ALGO_TRIPLET) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) - { - /* Pack triplets. */ - int ntriplets=*length/3; - /* Determine max base and maxbits */ - unsigned int max_base=1U<<coding_parameter; - unsigned int maxbits=coding_parameter; - unsigned int intmax=0; - for (i=0; i<*length; i++) - { - int item=input[i]; - unsigned int s=0; - if (item>0) - s=1+(item-1)*2; - else if (item<0) - s=2+(-item-1)*2; - if (s>intmax) - intmax=s; - } - /* Store intmax */ - coder_inst->pack_temporary_bits=32; - coder_inst->pack_temporary=intmax; - Ptngc_out8bits(coder_inst,&output_ptr); - while (intmax>=max_base) - { - max_base*=2; - maxbits++; - } - for (i=0; i<ntriplets; i++) - { - int j; - unsigned int s[3]; - for (j=0; j<3; j++) - { - int item=input[i*3+j]; - /* Find this symbol in table. */ - s[j]=0; - if (item>0) - s[j]=1+(item-1)*2; - else if (item<0) - s[j]=2+(-item-1)*2; - } - if (pack_triplet(coder_inst, s, &output_ptr, + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) + { + /* Pack triplets. */ + int ntriplets=*length/3; + /* Determine max base and maxbits */ + unsigned int max_base=1U<<coding_parameter; + unsigned int maxbits=coding_parameter; + unsigned int intmax=0; + for (i=0; i<*length; i++) + { + int item=input[i]; + unsigned int s=0; + if (item>0) + s=1+(item-1)*2; + else if (item<0) + s=2+(-item-1)*2; + if (s>intmax) + intmax=s; + } + /* Store intmax */ + coder_inst->pack_temporary_bits=32; + coder_inst->pack_temporary=intmax; + Ptngc_out8bits(coder_inst,&output_ptr); + while (intmax>=max_base) + { + max_base*=2; + maxbits++; + } + for (i=0; i<ntriplets; i++) + { + int j; + unsigned int s[3]; + for (j=0; j<3; j++) + { + int item=input[i*3+j]; + /* Find this symbol in table. */ + s[j]=0; + if (item>0) + s[j]=1+(item-1)*2; + else if (item<0) + s[j]=2+(-item-1)*2; + } + if (pack_triplet(coder_inst, s, &output_ptr, coding_parameter, max_base,maxbits)) - { - free(output); - return NULL; - } - } - } + { + free(output); + return NULL; + } + } + } else - for (i=0; i<*length; i++) - if (pack_stopbits_item(coder_inst,input[i],&output_ptr,coding_parameter)) - { - free(output); - return NULL; - } + for (i=0; i<*length; i++) + if (pack_stopbits_item(coder_inst,input[i],&output_ptr,coding_parameter)) + { + free(output); + return NULL; + } Ptngc_pack_flush(coder_inst,&output_ptr); output_length=(int)(output_ptr-output); *length=output_length; @@ -357,39 +357,39 @@ static int unpack_array_stop_bits(struct coder *coder_inst, unsigned int insert_mask=1U<<(numbits-1); int inserted_bits=numbits; do { - for (j=0; j<numbits; j++) - { - bit=*ptr & extract_mask; - if (bit) - pattern|=insert_mask; - insert_mask>>=1; - extract_mask>>=1; - if (!extract_mask) - { - extract_mask=0x80; - ptr++; - } - } - /* Check stop bit */ - bit=*ptr & extract_mask; - extract_mask>>=1; - if (!extract_mask) - { - extract_mask=0x80; - ptr++; - } - if (bit) - { - numbits>>=1; - if (numbits<1) - numbits=1; - inserted_bits+=numbits; - insert_mask=1U<<(inserted_bits-1); - } + for (j=0; j<numbits; j++) + { + bit=*ptr & extract_mask; + if (bit) + pattern|=insert_mask; + insert_mask>>=1; + extract_mask>>=1; + if (!extract_mask) + { + extract_mask=0x80; + ptr++; + } + } + /* Check stop bit */ + bit=*ptr & extract_mask; + extract_mask>>=1; + if (!extract_mask) + { + extract_mask=0x80; + ptr++; + } + if (bit) + { + numbits>>=1; + if (numbits<1) + numbits=1; + inserted_bits+=numbits; + insert_mask=1U<<(inserted_bits-1); + } } while (bit); s=(pattern+1)/2; if ((pattern%2)==0) - s=-s; + s=-s; output[i]=s; } return 0; @@ -426,45 +426,45 @@ static int unpack_array_triplet(struct coder *coder_inst, unsigned int numbits; unsigned int bit; for (j=0; j<2; j++) - { - bit=*ptr & extract_mask; - jbase<<=1; - if (bit) - jbase|=1U; - extract_mask>>=1; - if (!extract_mask) - { - extract_mask=0x80; - ptr++; - } - } + { + bit=*ptr & extract_mask; + jbase<<=1; + if (bit) + jbase|=1U; + extract_mask>>=1; + if (!extract_mask) + { + extract_mask=0x80; + ptr++; + } + } if (jbase==3) - numbits=maxbits; + numbits=maxbits; else - numbits=coding_parameter+jbase; + numbits=coding_parameter+jbase; for (j=0; j<3; j++) - { - int s; - unsigned int jbit; - unsigned int pattern=0; - for (jbit=0; jbit<numbits; jbit++) - { - bit=*ptr & extract_mask; - pattern<<=1; - if (bit) - pattern|=1U; - extract_mask>>=1; - if (!extract_mask) - { - extract_mask=0x80; - ptr++; - } - } - s=(pattern+1)/2; - if ((pattern%2)==0) - s=-s; - output[i*3+j]=s; - } + { + int s; + unsigned int jbit; + unsigned int pattern=0; + for (jbit=0; jbit<numbits; jbit++) + { + bit=*ptr & extract_mask; + pattern<<=1; + if (bit) + pattern|=1U; + extract_mask>>=1; + if (!extract_mask) + { + extract_mask=0x80; + ptr++; + } + } + s=(pattern+1)/2; + if ((pattern%2)==0) + s=-s; + output[i*3+j]=s; + } } return 0; } @@ -478,23 +478,23 @@ static int unpack_array_bwlzh(struct coder *coder_inst, int nframes=n/natoms/3; int cnt=0; int most_negative=(int)(((unsigned int)packed[0]) | - (((unsigned int)packed[1])<<8) | - (((unsigned int)packed[2])<<16) | - (((unsigned int)packed[3])<<24)); + (((unsigned int)packed[1])<<8) | + (((unsigned int)packed[2])<<16) | + (((unsigned int)packed[3])<<24)); (void) coder_inst; bwlzh_decompress(packed+4,length,pval); for (i=0; i<natoms; i++) for (j=0; j<3; j++) for (k=0; k<nframes; k++) - { - unsigned int s=pval[cnt++]; - output[k*3*natoms+i*3+j]=(int)s-most_negative; - } + { + unsigned int s=pval[cnt++]; + output[k*3*natoms+i*3+j]=(int)s-most_negative; + } free(pval); return 0; } -int Ptngc_unpack_array(struct coder *coder_inst, +int DECLSPECDLLEXPORT Ptngc_unpack_array(struct coder *coder_inst, unsigned char *packed, int *output, int length, int coding, int coding_parameter, int natoms) @@ -503,8 +503,8 @@ int Ptngc_unpack_array(struct coder *coder_inst, (coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER)) return unpack_array_stop_bits(coder_inst, packed, output, length, coding_parameter); else if ((coding==TNG_COMPRESS_ALGO_TRIPLET) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) return unpack_array_triplet(coder_inst, packed, output, length, coding_parameter); else if (coding==TNG_COMPRESS_ALGO_POS_XTC2) return Ptngc_unpack_array_xtc2(coder_inst, packed, output, length); diff --git a/src/compression/dict.c b/src/compression/dict.c index ea6cad9..4744949 100644 --- a/src/compression/dict.c +++ b/src/compression/dict.c @@ -23,8 +23,8 @@ void Ptngc_comp_canonical_dict(unsigned int *dict, int *ndict) } void Ptngc_comp_make_dict_hist(unsigned int *vals, int nvals, - unsigned int *dict, int *ndict, - unsigned int *hist) + unsigned int *dict, int *ndict, + unsigned int *hist) { int i; int j=0; @@ -37,9 +37,9 @@ void Ptngc_comp_make_dict_hist(unsigned int *vals, int nvals, for (i=0; i<0x20004; i++) if (hist[i]!=0) { - hist[j]=hist[i]; - dict[j]=dict[i]; - j++; + hist[j]=hist[i]; + dict[j]=dict[i]; + j++; } *ndict=j; } diff --git a/src/compression/huffman.c b/src/compression/huffman.c index 8f98755..26dc347 100644 --- a/src/compression/huffman.c +++ b/src/compression/huffman.c @@ -69,10 +69,10 @@ static int comp_htree(const void *leafptr1, const void *leafptr2, const void *pr } static void assign_codes(union htree_nodeleaf *htree, - struct codelength *codelength, - unsigned int code, - int length, - int top) + struct codelength *codelength, + unsigned int code, + int length, + int top) { #if 0 printf("Assign codes called with code %d length %d\n",code,length); @@ -83,18 +83,18 @@ static void assign_codes(union htree_nodeleaf *htree, codelength[htree->leaf.idict].code=(code<<1)|htree->leaf.bit; #if 0 printf("I am a leaf: %d %d\n", - codelength[htree->leaf.idict].length, - codelength[htree->leaf.idict].code); + codelength[htree->leaf.idict].length, + codelength[htree->leaf.idict].code); #endif } else { if (!top) - { - code<<=1; - code|=htree->node.bit; - length++; - } + { + code<<=1; + code|=htree->node.bit; + length++; + } #if 0 printf("I am a node length: %d\n",length); printf("I am a node code: %d\n",code); @@ -109,14 +109,14 @@ static void free_nodes(union htree_nodeleaf *htree, int top) if (htree->nodeleaf==htree_leaf) { if (!top) - free(htree); + free(htree); } else { free_nodes(htree->node.n1,0); free_nodes(htree->node.n2,0); if (!top) - free(htree); + free(htree); } } @@ -174,12 +174,12 @@ static unsigned int readbits(int length, unsigned char **input, int *bitptr) *bitptr=(*bitptr)+1; extract_mask>>=1; if (!extract_mask) - { - extract_mask=0x80U; - *input=(*input)+1; - *bitptr=0; - thisval=**input; - } + { + extract_mask=0x80U; + *input=(*input)+1; + *bitptr=0; + thisval=**input; + } } return val; } @@ -219,14 +219,14 @@ static int comp_codes_value(const void *codeptr1, const void *codeptr2, const vo huffman_dict_unpacked array should be 131077 long (note five longer than 0x20000) */ void Ptngc_comp_conv_to_huffman(unsigned int *vals, int nvals, - unsigned int *dict, int ndict, - unsigned int *prob, - unsigned char *huffman, - int *huffman_len, - unsigned char *huffman_dict, - int *huffman_dictlen, - unsigned int *huffman_dict_unpacked, - int *huffman_dict_unpackedlen) + unsigned int *dict, int ndict, + unsigned int *prob, + unsigned char *huffman, + int *huffman_len, + unsigned char *huffman_dict, + int *huffman_dictlen, + unsigned int *huffman_dict_unpacked, + int *huffman_dict_unpackedlen) { int i; int nleft; @@ -239,121 +239,121 @@ void Ptngc_comp_conv_to_huffman(unsigned int *vals, int nvals, while (longcodes) { /* Create array of leafs (will be array of nodes/trees during - buildup of tree. */ + buildup of tree. */ htree=warnmalloc(ndict*sizeof *htree); codelength=warnmalloc(ndict*sizeof *codelength); bitptr=0; huffman_ptr=huffman; for (i=0; i<ndict; i++) - { - htree[i].nodeleaf=htree_leaf; - htree[i].leaf.idict=i; - htree[i].leaf.prob=prob[i]; - } + { + htree[i].nodeleaf=htree_leaf; + htree[i].leaf.idict=i; + htree[i].leaf.prob=prob[i]; + } /* Sort the leafs wrt probability. */ Ptngc_merge_sort(htree,ndict,sizeof *htree,comp_htree,NULL); #if 0 for (i=0; i<ndict; i++) - { - printf("%d %d\n",dict[htree[i].leaf.idict],htree[i].leaf.prob); - } + { + printf("%d %d\n",dict[htree[i].leaf.idict],htree[i].leaf.prob); + } #endif /* Build tree. */ if (ndict==1) - { - codelength[0].code=1; - codelength[0].length=1; - } + { + codelength[0].code=1; + codelength[0].length=1; + } else - { - /* Nodes and leafs left. */ - nleft=ndict; + { + /* Nodes and leafs left. */ + nleft=ndict; - /* Take the two least probable symbols (which are at the end of the - array and combine them until there is nothing left. */ - while (nleft>1) - { - union htree_nodeleaf *n1=warnmalloc(sizeof *n1); - union htree_nodeleaf *n2=warnmalloc(sizeof *n2); - int new_place; - int p1,p2, new_prob; - *n1=htree[nleft-1]; - *n2=htree[nleft-2]; - if (n1->nodeleaf==htree_leaf) - { - p1=n1->leaf.prob; - n1->leaf.bit=0; - } - else - { - p1=n1->node.prob; - n1->node.bit=0; - } - if (n2->nodeleaf==htree_leaf) - { - p2=n2->leaf.prob; - n2->leaf.bit=1; - } - else - { - p2=n2->node.prob; - n2->node.bit=1; - } - nleft--; - /* Create a new node */ - htree[nleft-1].nodeleaf=htree_node; - htree[nleft-1].node.n1=n1; - htree[nleft-1].node.n2=n2; - new_prob=p1+p2; - htree[nleft-1].node.prob=new_prob; - /* Use insertion sort to place this in the correct place in the - array. */ - /* Where should it be inserted? */ - new_place=nleft; - while (new_place>0) - { - int pc; - if (htree[new_place-1].nodeleaf==htree_node) - pc=htree[new_place-1].node.prob; - else - pc=htree[new_place-1].leaf.prob; - if (new_prob<pc) - break; - else - new_place--; - } - if (new_place!=nleft) - { - /* Shift array (overlapping regions!) */ - union htree_nodeleaf nodecopy=htree[nleft-1]; - memmove(htree+new_place+1, - htree+new_place, - (nleft-1-new_place)*sizeof *htree); - htree[new_place]=nodecopy; - } - } - } + /* Take the two least probable symbols (which are at the end of the + array and combine them until there is nothing left. */ + while (nleft>1) + { + union htree_nodeleaf *n1=warnmalloc(sizeof *n1); + union htree_nodeleaf *n2=warnmalloc(sizeof *n2); + int new_place; + int p1,p2, new_prob; + *n1=htree[nleft-1]; + *n2=htree[nleft-2]; + if (n1->nodeleaf==htree_leaf) + { + p1=n1->leaf.prob; + n1->leaf.bit=0; + } + else + { + p1=n1->node.prob; + n1->node.bit=0; + } + if (n2->nodeleaf==htree_leaf) + { + p2=n2->leaf.prob; + n2->leaf.bit=1; + } + else + { + p2=n2->node.prob; + n2->node.bit=1; + } + nleft--; + /* Create a new node */ + htree[nleft-1].nodeleaf=htree_node; + htree[nleft-1].node.n1=n1; + htree[nleft-1].node.n2=n2; + new_prob=p1+p2; + htree[nleft-1].node.prob=new_prob; + /* Use insertion sort to place this in the correct place in the + array. */ + /* Where should it be inserted? */ + new_place=nleft; + while (new_place>0) + { + int pc; + if (htree[new_place-1].nodeleaf==htree_node) + pc=htree[new_place-1].node.prob; + else + pc=htree[new_place-1].leaf.prob; + if (new_prob<pc) + break; + else + new_place--; + } + if (new_place!=nleft) + { + /* Shift array (overlapping regions!) */ + union htree_nodeleaf nodecopy=htree[nleft-1]; + memmove(htree+new_place+1, + htree+new_place, + (nleft-1-new_place)*sizeof *htree); + htree[new_place]=nodecopy; + } + } + } /* Create codes from tree */ assign_codes(htree,codelength,0,0,1); /* Canonicalize */ /* First put values into to the codelength array for sorting. */ for (i=0; i<ndict; i++) - { - codelength[i].dict=dict[i]; - codelength[i].prob=prob[i]; - } + { + codelength[i].dict=dict[i]; + codelength[i].prob=prob[i]; + } /* Sort codes wrt length/value */ Ptngc_merge_sort(codelength,ndict,sizeof *codelength,comp_codes,NULL); /* Canonicalize codes. */ code=0; for (i=0; i<ndict; i++) - { - codelength[i].code=code; - if (i<ndict-1) - code=(code+1)<<(codelength[i+1].length-codelength[i].length); - } + { + codelength[i].code=code; + if (i<ndict-1) + code=(code+1)<<(codelength[i+1].length-codelength[i].length); + } /* Free all nodes / leaves. */ free_nodes(htree,1); /* Free array that held nodes/leaves. */ @@ -362,44 +362,44 @@ void Ptngc_comp_conv_to_huffman(unsigned int *vals, int nvals, longcodes=0; /* Check if there is a too long huffman code. */ for (i=0; i<ndict; i++) - if (codelength[i].length>MAX_HUFFMAN_LEN) - longcodes=1; + if (codelength[i].length>MAX_HUFFMAN_LEN) + longcodes=1; /* If the codes are too long alter the probabilities. */ if (longcodes) - { - for (i=0; i<ndict; i++) - { - prob[i]>>=1; - if (prob[i]==0) - prob[i]=1; - } + { + for (i=0; i<ndict; i++) + { + prob[i]>>=1; + if (prob[i]==0) + prob[i]=1; + } - /* Free codelength. We will compute a new one. */ - free(codelength); - } + /* Free codelength. We will compute a new one. */ + free(codelength); + } } #if 0 { for (i=0; i<ndict; i++) { - printf("%d %d\t\t %d %d ",codelength[i].dict,codelength[i].prob,codelength[i].length,codelength[i].code); - { - unsigned int c=codelength[i].code; - int j; - unsigned int mask=1<<(codelength[i].length-1); - for (j=codelength[i].length-1; j>=0; j--) - { - int bit=c&mask; - if (bit) - printf("1"); - else - printf("0"); - mask>>=1; - } - printf("\n"); - } + printf("%d %d\t\t %d %d ",codelength[i].dict,codelength[i].prob,codelength[i].length,codelength[i].code); + { + unsigned int c=codelength[i].code; + int j; + unsigned int mask=1<<(codelength[i].length-1); + for (j=codelength[i].length-1; j>=0; j--) + { + int bit=c&mask; + if (bit) + printf("1"); + else + printf("0"); + mask>>=1; + } + printf("\n"); + } } } #endif @@ -409,8 +409,8 @@ void Ptngc_comp_conv_to_huffman(unsigned int *vals, int nvals, { int r; for (r=0; r<ndict; r++) - if (codelength[r].dict==vals[i]) - break; + if (codelength[r].dict==vals[i]) + break; writebits(codelength[r].code,codelength[r].length,&huffman_ptr,&bitptr); } if (bitptr) @@ -440,20 +440,20 @@ void Ptngc_comp_conv_to_huffman(unsigned int *vals, int nvals, int ihave=0; int j; for (j=0; j<ndict; j++) - if (codelength[j].dict==(unsigned int)i) - { + if (codelength[j].dict==(unsigned int)i) + { - ihave=1; - writebits(1,1,&huffman_ptr,&bitptr); - writebits(codelength[j].length,5,&huffman_ptr,&bitptr); - huffman_dict_unpacked[3+i]=codelength[j].length; - break; - } + ihave=1; + writebits(1,1,&huffman_ptr,&bitptr); + writebits(codelength[j].length,5,&huffman_ptr,&bitptr); + huffman_dict_unpacked[3+i]=codelength[j].length; + break; + } if (!ihave) - { - writebits(0,1,&huffman_ptr,&bitptr); - huffman_dict_unpacked[3+i]=0; - } + { + writebits(0,1,&huffman_ptr,&bitptr); + huffman_dict_unpacked[3+i]=0; + } } if (bitptr) writebits(0,8-bitptr,&huffman_ptr,&bitptr); @@ -465,12 +465,12 @@ void Ptngc_comp_conv_to_huffman(unsigned int *vals, int nvals, } void Ptngc_comp_conv_from_huffman(unsigned char *huffman, - unsigned int *vals, int nvals, - int ndict, - unsigned char *huffman_dict, - int huffman_dictlen, - unsigned int *huffman_dict_unpacked, - int huffman_dict_unpackedlen) + unsigned int *vals, int nvals, + int ndict, + unsigned char *huffman_dict, + int huffman_dictlen, + unsigned int *huffman_dict_unpacked, + int huffman_dict_unpackedlen) { struct codelength *codelength=warnmalloc(ndict*sizeof *codelength); int i,j; @@ -485,19 +485,19 @@ void Ptngc_comp_conv_from_huffman(unsigned char *huffman, maxdict=huffman_dict_unpacked[0]|(huffman_dict_unpacked[1]<<8)|(huffman_dict_unpacked[2]<<16); j=0; for(i=0; i<=maxdict; i++) - { - if (huffman_dict_unpacked[3+i]!=0) - { - codelength[j].length=huffman_dict_unpacked[3+i]; - codelength[j].dict=i; + { + if (huffman_dict_unpacked[3+i]!=0) + { + codelength[j].length=huffman_dict_unpacked[3+i]; + codelength[j].dict=i; #if 0 - printf("%d %d\n", - codelength[j].length, - codelength[j].dict); + printf("%d %d\n", + codelength[j].length, + codelength[j].dict); #endif - j++; - } - } + j++; + } + } } else { @@ -507,20 +507,20 @@ void Ptngc_comp_conv_from_huffman(unsigned char *huffman, bitptr=0; j=0; for(i=0; i<=maxdict; i++) - { - int bit=readbits(1,&huffman_ptr,&bitptr); - if (bit) - { - codelength[j].length=readbits(5,&huffman_ptr,&bitptr); - codelength[j].dict=i; + { + int bit=readbits(1,&huffman_ptr,&bitptr); + if (bit) + { + codelength[j].length=readbits(5,&huffman_ptr,&bitptr); + codelength[j].dict=i; #if 0 - printf("%d %d\n", - codelength[j].length, - codelength[j].dict); + printf("%d %d\n", + codelength[j].length, + codelength[j].dict); #endif - j++; - } - } + j++; + } + } } /* Sort codes wrt length/value. */ Ptngc_merge_sort(codelength,ndict,sizeof *codelength,comp_codes,NULL); @@ -530,28 +530,28 @@ void Ptngc_comp_conv_from_huffman(unsigned char *huffman, { codelength[i].code=code; if (i<ndict-1) - code=(code+1)<<(codelength[i+1].length-codelength[i].length); + code=(code+1)<<(codelength[i+1].length-codelength[i].length); } #if 0 { for (i=0; i<ndict; i++) { - printf("%d -\t\t %d %d ",codelength[i].dict,codelength[i].length,codelength[i].code); - { - unsigned int c=codelength[i].code; - int j; - unsigned int mask=1<<(codelength[i].length-1); - for (j=codelength[i].length-1; j>=0; j--) - { - int bit=c&mask; - if (bit) - printf("1"); - else - printf("0"); - mask>>=1; - } - printf("\n"); - } + printf("%d -\t\t %d %d ",codelength[i].dict,codelength[i].length,codelength[i].code); + { + unsigned int c=codelength[i].code; + int j; + unsigned int mask=1<<(codelength[i].length-1); + for (j=codelength[i].length-1; j>=0; j--) + { + int bit=c&mask; + if (bit) + printf("1"); + else + printf("0"); + mask>>=1; + } + printf("\n"); + } } } #endif @@ -565,17 +565,17 @@ void Ptngc_comp_conv_from_huffman(unsigned char *huffman, symbol=readbits(len,&huffman_ptr,&bitptr); j=0; while (symbol!=codelength[j].code) - { - int newlen; - j++; - newlen=codelength[j].length; - if (newlen!=len) - { - symbol<<=(newlen-len); - symbol|=readbits(newlen-len,&huffman_ptr,&bitptr); - len=newlen; - } - } + { + int newlen; + j++; + newlen=codelength[j].length; + if (newlen!=len) + { + symbol<<=(newlen-len); + symbol|=readbits(newlen-len,&huffman_ptr,&bitptr); + len=newlen; + } + } vals[i]=codelength[j].dict; } /* Free info about codes and length. */ diff --git a/src/compression/huffmem.c b/src/compression/huffmem.c index 2232b7e..dec6837 100644 --- a/src/compression/huffmem.c +++ b/src/compression/huffmem.c @@ -28,10 +28,10 @@ int Ptngc_comp_huff_buflen(int nvals) /* the value pointed to by chosen_algo should be sent as -1 for autodetect. */ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, - unsigned char *huffman, int *huffman_len, - int *huffdatalen, - int *huffman_lengths,int *chosen_algo, - int isvals16) + unsigned char *huffman, int *huffman_len, + int *huffdatalen, + int *huffman_lengths,int *chosen_algo, + int isvals16) { unsigned int *dict=warnmalloc(0x20005*sizeof *dict); unsigned int *hist=warnmalloc(0x20005*sizeof *hist); @@ -68,9 +68,9 @@ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, /* First compress the data using huffman coding (place it ready for output at 14 (code for algorithm+length etc.). */ Ptngc_comp_conv_to_huffman(vals,nvals,dict,ndict,hist, - huffman+14,&nhuff, - huffdict,&nhuffdict, - huffdictunpack,&nhuffdictunpack); + huffman+14,&nhuff, + huffdict,&nhuffdict, + huffdictunpack,&nhuffdictunpack); *huffdatalen=nhuff; /* Algorithm 0 stores the huffman dictionary directly (+ a code for @@ -83,30 +83,30 @@ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, Ptngc_comp_make_dict_hist(huffdictunpack,nhuffdictunpack,dict,&ndict1,hist); /* Pack huffman dictionary */ Ptngc_comp_conv_to_huffman(huffdictunpack,nhuffdictunpack, - dict,ndict1,hist, - huffman1,&nhuff1, - huffdict1,&nhuffdict1, - huffdictunpack1,&nhuffdictunpack1); + dict,ndict1,hist, + huffman1,&nhuff1, + huffdict1,&nhuffdict1, + huffdictunpack1,&nhuffdictunpack1); huffman_lengths[1]=nhuff+nhuff1+nhuffdict1+1*2+3*4+3+3+3+3+3; /* ... and rle + huffman coding ... (algorithm 2) Pack any repetetitive patterns. */ Ptngc_comp_conv_to_rle(huffdictunpack,nhuffdictunpack, - huffdictrle,&nhuffrle,1); + huffdictrle,&nhuffrle,1); /* Determine probabilities. */ Ptngc_comp_make_dict_hist(huffdictrle,nhuffrle,dict,&ndict2,hist); /* Pack huffman dictionary */ Ptngc_comp_conv_to_huffman(huffdictrle,nhuffrle, - dict,ndict2,hist, - huffman2,&nhuff2, - huffdict2,&nhuffdict2, - huffdictunpack2,&nhuffdictunpack2); + dict,ndict2,hist, + huffman2,&nhuff2, + huffdict2,&nhuffdict2, + huffdictunpack2,&nhuffdictunpack2); huffman_lengths[2]=nhuff+nhuff2+nhuffdict2+1*2+3*4+3+3+3+3+3+3; /* Choose the best algorithm and output the data. */ if ((*chosen_algo==0) || ((*chosen_algo==-1) && - (((huffman_lengths[0]<huffman_lengths[1]) && - (huffman_lengths[0]<huffman_lengths[2]))))) + (((huffman_lengths[0]<huffman_lengths[1]) && + (huffman_lengths[0]<huffman_lengths[2]))))) { *chosen_algo=0; *huffman_len=huffman_lengths[0]; @@ -131,10 +131,10 @@ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, huffman[18+nhuff]=(((unsigned int)ndict)>>8)&0xFFU; huffman[19+nhuff]=(((unsigned int)ndict)>>16)&0xFFU; for (i=0; i<nhuffdict; i++) - huffman[20+nhuff+i]=huffdict[i]; + huffman[20+nhuff+i]=huffdict[i]; } else if ((*chosen_algo==1) || ((*chosen_algo==-1) && - ((huffman_lengths[1]<huffman_lengths[2])))) + ((huffman_lengths[1]<huffman_lengths[2])))) { *chosen_algo=1; *huffman_len=huffman_lengths[1]; @@ -168,9 +168,9 @@ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, huffman[27+nhuff]=(((unsigned int)ndict1)>>8)&0xFFU; huffman[28+nhuff]=(((unsigned int)ndict1)>>16)&0xFFU; for (i=0; i<nhuff1; i++) - huffman[29+nhuff+i]=huffman1[i]; + huffman[29+nhuff+i]=huffman1[i]; for (i=0; i<nhuffdict1; i++) - huffman[29+nhuff+nhuff1+i]=huffdict1[i]; + huffman[29+nhuff+nhuff1+i]=huffdict1[i]; } else { @@ -209,9 +209,9 @@ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, huffman[30+nhuff]=(((unsigned int)ndict2)>>8)&0xFFU; huffman[31+nhuff]=(((unsigned int)ndict2)>>16)&0xFFU; for (i=0; i<nhuff2; i++) - huffman[32+nhuff+i]=huffman2[i]; + huffman[32+nhuff+i]=huffman2[i]; for (i=0; i<nhuffdict2; i++) - huffman[32+nhuff+nhuff2+i]=huffdict2[i]; + huffman[32+nhuff+nhuff2+i]=huffdict2[i]; } if (!isvals16) free(vals16); @@ -230,36 +230,36 @@ void Ptngc_comp_huff_compress_verbose(unsigned int *vals, int nvals, } void Ptngc_comp_huff_compress(unsigned int *vals, int nvals, - unsigned char *huffman, int *huffman_len) + unsigned char *huffman, int *huffman_len) { int huffman_lengths[N_HUFFMAN_ALGO]; int algo=-1; int huffdatalen; Ptngc_comp_huff_compress_verbose(vals,nvals,huffman,huffman_len,&huffdatalen, - huffman_lengths,&algo,0); + huffman_lengths,&algo,0); } void Ptngc_comp_huff_decompress(unsigned char *huffman, int huffman_len, - unsigned int *vals) + unsigned int *vals) { int isvals16=(int)huffman[0]; unsigned int *vals16=NULL; int algo=(int)huffman[1]; int nvals16=(int)((unsigned int)huffman[2]| - (((unsigned int)huffman[3])<<8)| - (((unsigned int)huffman[4])<<16)| - (((unsigned int)huffman[5])<<24)); + (((unsigned int)huffman[3])<<8)| + (((unsigned int)huffman[4])<<16)| + (((unsigned int)huffman[5])<<24)); int nvals=(int)((unsigned int)huffman[6]| - (((unsigned int)huffman[7])<<8)| - (((unsigned int)huffman[8])<<16)| - (((unsigned int)huffman[9])<<24)); + (((unsigned int)huffman[7])<<8)| + (((unsigned int)huffman[8])<<16)| + (((unsigned int)huffman[9])<<24)); int nhuff=(int)((unsigned int)huffman[10]| - (((unsigned int)huffman[11])<<8)| - (((unsigned int)huffman[12])<<16)| - (((unsigned int)huffman[13])<<24)); + (((unsigned int)huffman[11])<<8)| + (((unsigned int)huffman[12])<<16)| + (((unsigned int)huffman[13])<<24)); int ndict=(int)((unsigned int)huffman[17+nhuff]| - (((unsigned int)huffman[18+nhuff])<<8)| - (((unsigned int)huffman[19+nhuff])<<16)); + (((unsigned int)huffman[18+nhuff])<<8)| + (((unsigned int)huffman[19+nhuff])<<16)); (void)huffman_len; if (!isvals16) vals16=warnmalloc(nvals16*sizeof *vals16); @@ -271,33 +271,33 @@ void Ptngc_comp_huff_decompress(unsigned char *huffman, int huffman_len, if (algo==0) { int nhuffdict=(int)((unsigned int)huffman[14+nhuff]| - (((unsigned int)huffman[15+nhuff])<<8)| - (((unsigned int)huffman[16+nhuff])<<16)); + (((unsigned int)huffman[15+nhuff])<<8)| + (((unsigned int)huffman[16+nhuff])<<16)); Ptngc_comp_conv_from_huffman(huffman+14,vals16,nvals16,ndict, - huffman+20+nhuff,nhuffdict,NULL,0); + huffman+20+nhuff,nhuffdict,NULL,0); } else if (algo==1) { unsigned int *huffdictunpack=warnmalloc(0x20005*sizeof *huffdictunpack); /* First the dictionary needs to be uncompressed. */ int nhuffdictunpack=(int)((unsigned int)huffman[14+nhuff]| - (((unsigned int)huffman[15+nhuff])<<8)| - (((unsigned int)huffman[16+nhuff])<<16)); + (((unsigned int)huffman[15+nhuff])<<8)| + (((unsigned int)huffman[16+nhuff])<<16)); int nhuff1=(int)((unsigned int)huffman[20+nhuff]| - (((unsigned int)huffman[21+nhuff])<<8)| - (((unsigned int)huffman[22+nhuff])<<16)); + (((unsigned int)huffman[21+nhuff])<<8)| + (((unsigned int)huffman[22+nhuff])<<16)); int nhuffdict1=(int)((unsigned int)huffman[23+nhuff]| - (((unsigned int)huffman[24+nhuff])<<8)| - (((unsigned int)huffman[25+nhuff])<<16)); + (((unsigned int)huffman[24+nhuff])<<8)| + (((unsigned int)huffman[25+nhuff])<<16)); int ndict1=(int)((unsigned int)huffman[26+nhuff]| - (((unsigned int)huffman[27+nhuff])<<8)| - (((unsigned int)huffman[28+nhuff])<<16)); + (((unsigned int)huffman[27+nhuff])<<8)| + (((unsigned int)huffman[28+nhuff])<<16)); Ptngc_comp_conv_from_huffman(huffman+29+nhuff,huffdictunpack, - nhuffdictunpack,ndict1, - huffman+29+nhuff+nhuff1,nhuffdict1,NULL,0); + nhuffdictunpack,ndict1, + huffman+29+nhuff+nhuff1,nhuffdict1,NULL,0); /* Then decompress the "real" data. */ Ptngc_comp_conv_from_huffman(huffman+14,vals16,nvals16,ndict, - NULL,0,huffdictunpack,nhuffdictunpack); + NULL,0,huffdictunpack,nhuffdictunpack); free(huffdictunpack); } else if (algo==2) @@ -306,28 +306,28 @@ void Ptngc_comp_huff_decompress(unsigned char *huffman, int huffman_len, unsigned int *huffdictrle=warnmalloc((3*0x20005+3)*sizeof *huffdictrle); /* First the dictionary needs to be uncompressed. */ int nhuffdictunpack=(int)((unsigned int)huffman[14+nhuff]| - (((unsigned int)huffman[15+nhuff])<<8)| - (((unsigned int)huffman[16+nhuff])<<16)); + (((unsigned int)huffman[15+nhuff])<<8)| + (((unsigned int)huffman[16+nhuff])<<16)); int nhuffrle=(int)((unsigned int)huffman[20+nhuff]| - (((unsigned int)huffman[21+nhuff])<<8)| - (((unsigned int)huffman[22+nhuff])<<16)); + (((unsigned int)huffman[21+nhuff])<<8)| + (((unsigned int)huffman[22+nhuff])<<16)); int nhuff2=(int)((unsigned int)huffman[23+nhuff]| - (((unsigned int)huffman[24+nhuff])<<8)| - (((unsigned int)huffman[25+nhuff])<<16)); + (((unsigned int)huffman[24+nhuff])<<8)| + (((unsigned int)huffman[25+nhuff])<<16)); int nhuffdict2=(int)((unsigned int)huffman[26+nhuff]| - (((unsigned int)huffman[27+nhuff])<<8)| - (((unsigned int)huffman[28+nhuff])<<16)); + (((unsigned int)huffman[27+nhuff])<<8)| + (((unsigned int)huffman[28+nhuff])<<16)); int ndict2=(int)((unsigned int)huffman[29+nhuff]| - (((unsigned int)huffman[30+nhuff])<<8)| - (((unsigned int)huffman[31+nhuff])<<16)); + (((unsigned int)huffman[30+nhuff])<<8)| + (((unsigned int)huffman[31+nhuff])<<16)); Ptngc_comp_conv_from_huffman(huffman+32+nhuff,huffdictrle, - nhuffrle,ndict2, - huffman+32+nhuff+nhuff2,nhuffdict2,NULL,0); + nhuffrle,ndict2, + huffman+32+nhuff+nhuff2,nhuffdict2,NULL,0); /* Then uncompress the rle data */ Ptngc_comp_conv_from_rle(huffdictrle,huffdictunpack,nhuffdictunpack); /* Then decompress the "real" data. */ Ptngc_comp_conv_from_huffman(huffman+14,vals16,nvals16,ndict, - NULL,0,huffdictunpack,nhuffdictunpack); + NULL,0,huffdictunpack,nhuffdictunpack); free(huffdictrle); free(huffdictunpack); } diff --git a/src/compression/lz77.c b/src/compression/lz77.c index 5ea63cf..dba6400 100644 --- a/src/compression/lz77.c +++ b/src/compression/lz77.c @@ -32,7 +32,7 @@ The second value is the "inverse" of the first value and can be used to find the locations of similar strings. */ static void sort_strings(unsigned int *vals, int nvals, - unsigned int *output) + unsigned int *output) { int i; int *indices=warnmalloc(2*nvals*sizeof *indices); @@ -55,79 +55,79 @@ static void sort_strings(unsigned int *vals, int nvals, for (i=0; i<nvals; i++) { /* If we have not already found a repeating string we must find - it. */ + it. */ if (!nrepeat[i]) - { - int maxrepeat=nvals*2; - int j,k,m; - int good_j=-1, good_k=0; - int kmax=16; - /* Track repeating patterns. - k=1 corresponds to AAAAA... - k=2 corresponds to ABABAB... - k=3 corresponds to ABCABCABCABC... - k=4 corresponds to ABCDABCDABCD... - etc. */ - for (k=kmax; k>=1; k--) - { - try_next_k: - if (k>=1) - { - for (j=k; j<maxrepeat; j+=k) - { - int is_equal=1; - for (m=0; m<k; m++) - if (vals[(i+m)%nvals]!=vals[(i+j+m)%nvals]) - { - is_equal=0; - break; - } - if (is_equal) - { - int new_j=j+k; - if (new_j>maxrepeat) - new_j=j; - if ((new_j>good_j) || ((new_j==good_j) && (k<good_k))) - { - good_j=new_j; /* We have found that - the strings repeat - for this length... */ - good_k=k; /* ...and with this - length of the - repeating - pattern. */ - } - } - else - { - /* We know that it is no point in trying - with more than m */ - if (j==0) - { - k=m; - } - else - k--; - goto try_next_k; - } - } - } - } - /* From good_j and good_k we know the repeat for a large - number of strings. The very last repeat length should not - be assigned, since it can be much longer if a new test is - done. */ - for (m=0; (m+good_k<good_j) && (i+m<nvals); m+=good_k) - { - int repeat=good_j-m; - if (repeat>nvals) - repeat=nvals; - nrepeat[i+m]=((unsigned int) (good_k)) | (((unsigned int) (repeat))<<8); - } - /* If no repetition was found for this value signal that here. */ - if (!nrepeat[i]) - nrepeat[i+m]=257U; /* This is 1<<8 | 1 */ - } + { + int maxrepeat=nvals*2; + int j,k,m; + int good_j=-1, good_k=0; + int kmax=16; + /* Track repeating patterns. + k=1 corresponds to AAAAA... + k=2 corresponds to ABABAB... + k=3 corresponds to ABCABCABCABC... + k=4 corresponds to ABCDABCDABCD... + etc. */ + for (k=kmax; k>=1; k--) + { + try_next_k: + if (k>=1) + { + for (j=k; j<maxrepeat; j+=k) + { + int is_equal=1; + for (m=0; m<k; m++) + if (vals[(i+m)%nvals]!=vals[(i+j+m)%nvals]) + { + is_equal=0; + break; + } + if (is_equal) + { + int new_j=j+k; + if (new_j>maxrepeat) + new_j=j; + if ((new_j>good_j) || ((new_j==good_j) && (k<good_k))) + { + good_j=new_j; /* We have found that + the strings repeat + for this length... */ + good_k=k; /* ...and with this + length of the + repeating + pattern. */ + } + } + else + { + /* We know that it is no point in trying + with more than m */ + if (j==0) + { + k=m; + } + else + k--; + goto try_next_k; + } + } + } + } + /* From good_j and good_k we know the repeat for a large + number of strings. The very last repeat length should not + be assigned, since it can be much longer if a new test is + done. */ + for (m=0; (m+good_k<good_j) && (i+m<nvals); m+=good_k) + { + int repeat=good_j-m; + if (repeat>nvals) + repeat=nvals; + nrepeat[i+m]=((unsigned int) (good_k)) | (((unsigned int) (repeat))<<8); + } + /* If no repetition was found for this value signal that here. */ + if (!nrepeat[i]) + nrepeat[i+m]=257U; /* This is 1<<8 | 1 */ + } } /* Sort cyclic shift matrix. */ @@ -155,19 +155,19 @@ static void add_circular(int *previous,int v, int i) { previous[(NUM_PREVIOUS+3)*v]++; if (previous[(NUM_PREVIOUS+3)*v]>NUM_PREVIOUS) - previous[(NUM_PREVIOUS+3)*v]=NUM_PREVIOUS; + previous[(NUM_PREVIOUS+3)*v]=NUM_PREVIOUS; previous[(NUM_PREVIOUS+3)*v+3+previous[(NUM_PREVIOUS+3)*v+1]]=i; previous[(NUM_PREVIOUS+3)*v+1]++; if (previous[(NUM_PREVIOUS+3)*v+1]>=NUM_PREVIOUS) - previous[(NUM_PREVIOUS+3)*v+1]=0; + previous[(NUM_PREVIOUS+3)*v+1]=0; } previous[(NUM_PREVIOUS+3)*v+2]=i; } void Ptngc_comp_to_lz77(unsigned int *vals, int nvals, - unsigned int *data, int *ndata, - unsigned int *len, int *nlens, - unsigned int *offsets, int *noffsets) + unsigned int *data, int *ndata, + unsigned int *len, int *nlens, + unsigned int *offsets, int *noffsets) { int noff=0; int ndat=0; @@ -192,112 +192,112 @@ void Ptngc_comp_to_lz77(unsigned int *vals, int nvals, #endif int firstoffset=i-MAX_OFFSET; if (firstoffset<0) - firstoffset=0; + firstoffset=0; if (i!=0) - { - int largest_len=0; - int largest_offset=0; - int icirc, ncirc; - /* Is this identical to a previous offset? Prefer close - values for offset. Search through circular buffer for the - possible values for the start of this string. */ - ncirc=previous[(NUM_PREVIOUS+3)*vals[i]]; - for (icirc=0; icirc<ncirc; icirc++) - { - int iptr=previous[(NUM_PREVIOUS+3)*vals[i]+1]-icirc-1; - if (iptr<0) - iptr+=NUM_PREVIOUS; - j=previous[(NUM_PREVIOUS+3)*vals[i]+3+iptr]; - if (j<firstoffset) - break; + { + int largest_len=0; + int largest_offset=0; + int icirc, ncirc; + /* Is this identical to a previous offset? Prefer close + values for offset. Search through circular buffer for the + possible values for the start of this string. */ + ncirc=previous[(NUM_PREVIOUS+3)*vals[i]]; + for (icirc=0; icirc<ncirc; icirc++) + { + int iptr=previous[(NUM_PREVIOUS+3)*vals[i]+1]-icirc-1; + if (iptr<0) + iptr+=NUM_PREVIOUS; + j=previous[(NUM_PREVIOUS+3)*vals[i]+3+iptr]; + if (j<firstoffset) + break; #if 0 - fprintf(stderr,"Starting search for %d at %d. Found %d\n",vals[i],j,vals[j]); + fprintf(stderr,"Starting search for %d at %d. Found %d\n",vals[i],j,vals[j]); #endif - while ((j<i) && (vals[j]==vals[i])) - { - if (j>=firstoffset) - { - for (k=0; i+k<nvals; k++) - if (vals[j+k]!=vals[i+k]) - break; - if ((k>largest_len) && ((k>=(i-j)+16) || ((k>4) && (i-j==1)))) - { - largest_len=k; - largest_offset=j; - } - } - j++; - } - } + while ((j<i) && (vals[j]==vals[i])) + { + if (j>=firstoffset) + { + for (k=0; i+k<nvals; k++) + if (vals[j+k]!=vals[i+k]) + break; + if ((k>largest_len) && ((k>=(i-j)+16) || ((k>4) && (i-j==1)))) + { + largest_len=k; + largest_offset=j; + } + } + j++; + } + } #if 0 - /* Search in sorted string buffer too. */ - kmin=info[i*2+1]-MAX_STRING_SEARCH; - kmax=info[i*2+1]+MAX_STRING_SEARCH; - if (kmin<0) - kmin=0; - if (kmax>=nvals) - kmax=nvals; - for (k=kmin; k<kmax; k++) - { - int m; - int s=info[k*2]; - if ((s<i) && (s+MAX_OFFSET>=i)) - { - for (m=0; i+m<nvals; m++) - if (vals[i+m]!=vals[(s+m)%nvals]) - break; - if ((m>largest_len) && (m>4) && (m+2>=(i-s))) - { - largest_len=m; - largest_offset=s; + /* Search in sorted string buffer too. */ + kmin=info[i*2+1]-MAX_STRING_SEARCH; + kmax=info[i*2+1]+MAX_STRING_SEARCH; + if (kmin<0) + kmin=0; + if (kmax>=nvals) + kmax=nvals; + for (k=kmin; k<kmax; k++) + { + int m; + int s=info[k*2]; + if ((s<i) && (s+MAX_OFFSET>=i)) + { + for (m=0; i+m<nvals; m++) + if (vals[i+m]!=vals[(s+m)%nvals]) + break; + if ((m>largest_len) && (m>4) && (m+2>=(i-s))) + { + largest_len=m; + largest_offset=s; #if 0 - fprintf(stderr,"Offset: %d %d\n",m,i-s); + fprintf(stderr,"Offset: %d %d\n",m,i-s); #endif - } - } - } + } + } + } #endif - /* Check how to write this info. */ - if (largest_len>MAX_LEN) - largest_len=MAX_LEN; - if (largest_len) - { - if (i-largest_offset==1) - { - data[ndat++]=0; - } - else - { - data[ndat++]=1; - offsets[noff++]=i-largest_offset; - } - len[nlen++]=largest_len; + /* Check how to write this info. */ + if (largest_len>MAX_LEN) + largest_len=MAX_LEN; + if (largest_len) + { + if (i-largest_offset==1) + { + data[ndat++]=0; + } + else + { + data[ndat++]=1; + offsets[noff++]=i-largest_offset; + } + len[nlen++]=largest_len; #if 0 - fprintf(stderr,"l:o: %d:%d data=%d i=%d\n",largest_len,i-largest_offset,ndat,i); - fflush(stderr); + fprintf(stderr,"l:o: %d:%d data=%d i=%d\n",largest_len,i-largest_offset,ndat,i); + fflush(stderr); #endif #if 0 - fprintf(stderr,"Found largest len %d at %d.\n",largest_len,i-largest_offset); + fprintf(stderr,"Found largest len %d at %d.\n",largest_len,i-largest_offset); #endif - /* Add these values to the circular buffer. */ - for (k=0; k<largest_len; k++) - add_circular(previous,vals[i+k],i+k); - i+=largest_len-1; - } - else - { - data[ndat++]=vals[i]+2; - /* Add this value to circular buffer. */ - add_circular(previous,vals[i],i); - } - } + /* Add these values to the circular buffer. */ + for (k=0; k<largest_len; k++) + add_circular(previous,vals[i+k],i+k); + i+=largest_len-1; + } + else + { + data[ndat++]=vals[i]+2; + /* Add this value to circular buffer. */ + add_circular(previous,vals[i],i); + } + } else - { - data[ndat++]=vals[i]+2; - /* Add this value to circular buffer. */ - add_circular(previous,vals[i],i); - } + { + data[ndat++]=vals[i]+2; + /* Add this value to circular buffer. */ + add_circular(previous,vals[i],i); + } } *noffsets=noff; *ndata=ndat; @@ -309,9 +309,9 @@ void Ptngc_comp_to_lz77(unsigned int *vals, int nvals, } void Ptngc_comp_from_lz77(unsigned int *data, int ndata, - unsigned int *len, int nlens, - unsigned int *offsets, int noffsets, - unsigned int *vals, int nvals) + unsigned int *len, int nlens, + unsigned int *offsets, int noffsets, + unsigned int *vals, int nvals) { int i=0; int joff=0; @@ -324,27 +324,27 @@ void Ptngc_comp_from_lz77(unsigned int *data, int ndata, { unsigned int v=data[jdat++]; if (v<2) - { - int offset=1; - int k; - int length=(int)len[jlen++]; + { + int offset=1; + int k; + int length=(int)len[jlen++]; #if 0 - fprintf(stderr,"len=%d off=%d i=%d\n",length,offset,i); + fprintf(stderr,"len=%d off=%d i=%d\n",length,offset,i); #endif - if (v==1) - offset=offsets[joff++]; - for (k=0; k<length; k++) - { - vals[i]=vals[i-offset]; - if (i>=nvals) - { - fprintf(stderr,"too many vals.\n"); - exit(EXIT_FAILURE); - } - i++; - } - } + if (v==1) + offset=offsets[joff++]; + for (k=0; k<length; k++) + { + vals[i]=vals[i-offset]; + if (i>=nvals) + { + fprintf(stderr,"too many vals.\n"); + exit(EXIT_FAILURE); + } + i++; + } + } else - vals[i++]=v-2; + vals[i++]=v-2; } } diff --git a/src/compression/merge_sort.c b/src/compression/merge_sort.c index 204243b..1d49903 100644 --- a/src/compression/merge_sort.c +++ b/src/compression/merge_sort.c @@ -18,12 +18,12 @@ #include "compression/merge_sort.h" static void ms_inner(void *base, size_t size, - size_t start, size_t end, - int (*compar)(const void *v1,const void *v2,const void *private), - const void *private, - char *workarray) + size_t start, size_t end, + int (*compar)(const void *v1,const void *v2,const void *private), + const void *private, + char *workarray) { - int middle; + size_t middle; if ((end-start)>1) { char *cbase=(char *)base; @@ -32,59 +32,59 @@ static void ms_inner(void *base, size_t size, printf("For start %d end %d obtained new middle: %d\n",start,end,middle); #endif ms_inner(base,size, - start,middle, - compar,private,workarray); + start,middle, + compar,private,workarray); ms_inner(base,size, - middle,end, - compar,private,workarray); + middle,end, + compar,private,workarray); #if 0 printf("For start %d end %d Before merge: Comparing element %d with %d\n",start,end,middle-1,middle); #endif if (compar(cbase+(middle-1)*size,cbase+middle*size,private)>0) - { - /* Merge to work array. */ - int i, n=end-start; - int ileft=start; - int iright=middle; - for (i=0; i<n; i++) - { - if (ileft==middle) - { - memcpy(workarray+i*size,cbase+iright*size,size); - iright++; - } - else if (iright==end) - { - memcpy(workarray+i*size,cbase+ileft*size,size); - ileft++; - } - else - { -#if 0 - printf("For start %d end %d In merge: Comparing element %d with %d\n",start,end,ileft,iright); -#endif - if (compar(cbase+ileft*size,cbase+iright*size,private)>0) - { - memcpy(workarray+i*size,cbase+iright*size,size); - iright++; - } - else - { - memcpy(workarray+i*size,cbase+ileft*size,size); - ileft++; - } - } - } - /* Copy result back. */ - memcpy(cbase+start*size,workarray,(end-start)*size); - } + { + /* Merge to work array. */ + size_t i, n=end-start; + size_t ileft=start; + size_t iright=middle; + for (i=0; i<n; i++) + { + if (ileft==middle) + { + memcpy(workarray+i*size,cbase+iright*size,size); + iright++; + } + else if (iright==end) + { + memcpy(workarray+i*size,cbase+ileft*size,size); + ileft++; + } + else + { + #if 0 + printf("For start %d end %d In merge: Comparing element %d with %d\n",start,end,ileft,iright); + #endif + if (compar(cbase+ileft*size,cbase+iright*size,private)>0) + { + memcpy(workarray+i*size,cbase+iright*size,size); + iright++; + } + else + { + memcpy(workarray+i*size,cbase+ileft*size,size); + ileft++; + } + } + } + /* Copy result back. */ + memcpy(cbase+start*size,workarray,(end-start)*size); + } } } void Ptngc_merge_sort(void *base, size_t nmemb, size_t size, - int (*compar)(const void *v1,const void *v2,const void *private), - void *private) + int (*compar)(const void *v1,const void *v2,const void *private), + void *private) { char *warr=warnmalloc(nmemb*size); ms_inner(base,size,0,nmemb,compar,private,warr); diff --git a/src/compression/mtf.c b/src/compression/mtf.c index a2ba3cf..1d74af3 100644 --- a/src/compression/mtf.c +++ b/src/compression/mtf.c @@ -19,7 +19,7 @@ /* Move to front coding. Acceptable inputs are max 8 bits (0-0xFF) */ static void comp_conv_to_mtf_byte(unsigned char *vals, int nvals, - unsigned char *valsmtf) + unsigned char *valsmtf) { int i; /* Indices into a linked list */ @@ -41,27 +41,27 @@ static void comp_conv_to_mtf_byte(unsigned char *vals, int nvals, int oldptr=-1; int r=0; while (dict[ptr]!=v) - { - oldptr=ptr; - ptr=list[ptr]; - r++; - } + { + oldptr=ptr; + ptr=list[ptr]; + r++; + } valsmtf[i]=(unsigned char)r; /* Move it to front in list */ /* Is it the head? Then it is already at the front. */ if (oldptr!=-1) - { - /* Remove it from inside the list */ - list[oldptr]=list[ptr]; - /* Move it to the front. */ - list[ptr]=head; - head=ptr; - } + { + /* Remove it from inside the list */ + list[oldptr]=list[ptr]; + /* Move it to the front. */ + list[ptr]=head; + head=ptr; + } } } void Ptngc_comp_conv_to_mtf_partial(unsigned int *vals, int nvals, - unsigned int *valsmtf) + unsigned int *valsmtf) { unsigned char *tmp=warnmalloc(nvals*2); int i, j; @@ -70,23 +70,23 @@ void Ptngc_comp_conv_to_mtf_partial(unsigned int *vals, int nvals, for (j=0; j<3; j++) { for (i=0; i<nvals; i++) - tmp[i]=(unsigned char)((vals[i]>>(8*j))&0xFF); + tmp[i]=(unsigned char)((vals[i]>>(8*j))&0xFF); comp_conv_to_mtf_byte(tmp,nvals,tmp+nvals); for (i=0; i<nvals; i++) - valsmtf[i]|=(((unsigned int)(tmp[nvals+i]))<<(8*j)); + valsmtf[i]|=(((unsigned int)(tmp[nvals+i]))<<(8*j)); } free(tmp); } void Ptngc_comp_conv_to_mtf_partial3(unsigned int *vals, int nvals, - unsigned char *valsmtf) + unsigned char *valsmtf) { unsigned char *tmp=warnmalloc(nvals); int i, j; for (j=0; j<3; j++) { for (i=0; i<nvals; i++) - tmp[i]=(unsigned char)((vals[i]>>(8*j))&0xFF); + tmp[i]=(unsigned char)((vals[i]>>(8*j))&0xFF); comp_conv_to_mtf_byte(tmp,nvals,valsmtf+j*nvals); } free(tmp); @@ -94,7 +94,7 @@ void Ptngc_comp_conv_to_mtf_partial3(unsigned int *vals, int nvals, /* Move to front decoding */ static void comp_conv_from_mtf_byte(unsigned char *valsmtf, int nvals, - unsigned char *vals) + unsigned char *vals) { int i; /* Indices into a linked list */ @@ -116,27 +116,27 @@ static void comp_conv_from_mtf_byte(unsigned char *valsmtf, int nvals, int oldptr=-1; int cnt=0; while (cnt<r) - { - oldptr=ptr; - ptr=list[ptr]; - cnt++; - } + { + oldptr=ptr; + ptr=list[ptr]; + cnt++; + } vals[i]=(unsigned int)dict[ptr]; /* Move it to front in list */ /* Is it the head? Then it is already at the front. */ if (oldptr!=-1) - { - /* Remove it from inside the list */ - list[oldptr]=list[ptr]; - /* Move it to the front. */ - list[ptr]=head; - head=ptr; - } + { + /* Remove it from inside the list */ + list[oldptr]=list[ptr]; + /* Move it to the front. */ + list[ptr]=head; + head=ptr; + } } } void Ptngc_comp_conv_from_mtf_partial(unsigned int *valsmtf, int nvals, - unsigned int *vals) + unsigned int *vals) { unsigned char *tmp=warnmalloc(nvals*2); int i, j; @@ -145,16 +145,16 @@ void Ptngc_comp_conv_from_mtf_partial(unsigned int *valsmtf, int nvals, for (j=0; j<3; j++) { for (i=0; i<nvals; i++) - tmp[i]=(unsigned char)((valsmtf[i]>>(8*j))&0xFF); + tmp[i]=(unsigned char)((valsmtf[i]>>(8*j))&0xFF); comp_conv_from_mtf_byte(tmp,nvals,tmp+nvals); for (i=0; i<nvals; i++) - vals[i]|=(((unsigned int)(tmp[nvals+i]))<<(8*j)); + vals[i]|=(((unsigned int)(tmp[nvals+i]))<<(8*j)); } free(tmp); } void Ptngc_comp_conv_from_mtf_partial3(unsigned char *valsmtf, int nvals, - unsigned int *vals) + unsigned int *vals) { unsigned char *tmp=warnmalloc(nvals); int i, j; @@ -164,7 +164,7 @@ void Ptngc_comp_conv_from_mtf_partial3(unsigned char *valsmtf, int nvals, { comp_conv_from_mtf_byte(valsmtf+j*nvals,nvals,tmp); for (i=0; i<nvals; i++) - vals[i]|=(((unsigned int)(tmp[i]))<<(8*j)); + vals[i]|=(((unsigned int)(tmp[i]))<<(8*j)); } free(tmp); } @@ -172,8 +172,8 @@ void Ptngc_comp_conv_from_mtf_partial3(unsigned char *valsmtf, int nvals, /* Move to front coding. Acceptable inputs are max 24 bits (0-0xFFFFFF) */ void Ptngc_comp_conv_to_mtf(unsigned int *vals, int nvals, - unsigned int *dict, int ndict, - unsigned int *valsmtf) + unsigned int *dict, int ndict, + unsigned int *valsmtf) { int i; /* Indices into a linked list */ @@ -192,30 +192,30 @@ void Ptngc_comp_conv_to_mtf(unsigned int *vals, int nvals, int oldptr=-1; int r=0; while (dict[ptr]!=v) - { - oldptr=ptr; - ptr=list[ptr]; - r++; - } + { + oldptr=ptr; + ptr=list[ptr]; + r++; + } valsmtf[i]=r; /* Move it to front in list */ /* Is it the head? Then it is already at the front. */ if (oldptr!=-1) - { - /* Remove it from inside the list */ - list[oldptr]=list[ptr]; - /* Move it to the front. */ - list[ptr]=head; - head=ptr; - } + { + /* Remove it from inside the list */ + list[oldptr]=list[ptr]; + /* Move it to the front. */ + list[ptr]=head; + head=ptr; + } } free(list); } /* Move to front decoding */ void Ptngc_comp_conv_from_mtf(unsigned int *valsmtf, int nvals, - unsigned int *dict, int ndict, - unsigned int *vals) + unsigned int *dict, int ndict, + unsigned int *vals) { int i; /* Indices into a linked list */ @@ -234,22 +234,22 @@ void Ptngc_comp_conv_from_mtf(unsigned int *valsmtf, int nvals, int oldptr=-1; int cnt=0; while (cnt<r) - { - oldptr=ptr; - ptr=list[ptr]; - cnt++; - } + { + oldptr=ptr; + ptr=list[ptr]; + cnt++; + } vals[i]=dict[ptr]; /* Move it to front in list */ /* Is it the head? Then it is already at the front. */ if (oldptr!=-1) - { - /* Remove it from inside the list */ - list[oldptr]=list[ptr]; - /* Move it to the front. */ - list[ptr]=head; - head=ptr; - } + { + /* Remove it from inside the list */ + list[oldptr]=list[ptr]; + /* Move it to the front. */ + list[ptr]=head; + head=ptr; + } } free(list); } diff --git a/src/compression/rle.c b/src/compression/rle.c index 2adeae2..ee708ed 100644 --- a/src/compression/rle.c +++ b/src/compression/rle.c @@ -14,21 +14,21 @@ #include "compression/rle.h" static void add_rle(unsigned int *rle, - int v,int nsim, - int *j,int min_rle) + int v,int nsim, + int *j,int min_rle) { if (nsim>min_rle) { /* Insert run-length */ unsigned int run=((unsigned int)nsim); while (run>1) - { - if (run&0x1U) - rle[(*j)++]=1; - else - rle[(*j)++]=0; - run>>=1; - } + { + if (run&0x1U) + rle[(*j)++]=1; + else + rle[(*j)++]=0; + run>>=1; + } nsim=1; } while (nsim--) @@ -39,8 +39,8 @@ static void add_rle(unsigned int *rle, Acceptable inputs are about 16 bits (0-0xFFFF) If input is 0-N output will be be values of 0-(N+2) */ void Ptngc_comp_conv_to_rle(unsigned int *vals, int nvals, - unsigned int *rle, int *nrle, - int min_rle) + unsigned int *rle, int *nrle, + int min_rle) { int i; int j=0; @@ -49,21 +49,21 @@ void Ptngc_comp_conv_to_rle(unsigned int *vals, int nvals, for (i=0; i<nvals; i++) { if (!nsim) - { - v=vals[i]; - nsim=1; - } + { + v=vals[i]; + nsim=1; + } else - { - if (v==vals[i]) - nsim++; - else - { - add_rle(rle,v,nsim,&j,min_rle); - nsim=1; - v=vals[i]; - } - } + { + if (v==vals[i]) + nsim++; + else + { + add_rle(rle,v,nsim,&j,min_rle); + nsim=1; + v=vals[i]; + } + } } if (nsim!=0) add_rle(rle,v,nsim,&j,min_rle); @@ -71,7 +71,7 @@ void Ptngc_comp_conv_to_rle(unsigned int *vals, int nvals, } void Ptngc_comp_conv_from_rle(unsigned int *rle, - unsigned int *vals, int nvals) + unsigned int *vals, int nvals) { int i=0; int j=0; @@ -83,19 +83,19 @@ void Ptngc_comp_conv_from_rle(unsigned int *rle, unsigned int v=rle[j++]; unsigned int hasrle=0; while (v<2) - { - if (v) - len|=mask; - mask<<=1; - hasrle=1; - v=rle[j++]; - } + { + if (v) + len|=mask; + mask<<=1; + hasrle=1; + v=rle[j++]; + } if (!hasrle) - len=1; + len=1; else - len|=mask; + len|=mask; for (k=0; k<(int)len; k++) - vals[i++]=v-2; + vals[i++]=v-2; } } diff --git a/src/compression/tng_compress.c b/src/compression/tng_compress.c index a63e7a1..62c6326 100644 --- a/src/compression/tng_compress.c +++ b/src/compression/tng_compress.c @@ -24,7 +24,7 @@ #define MAGIC_INT_VEL 0x56474E54 #define SPEED_DEFAULT 2 /* Default to relatively fast compression. For very good compression it makes sense to - choose speed=4 or speed=5 */ + choose speed=4 or speed=5 */ #define PRECISION(hi,lo) (Ptngc_i32x2_to_d(hi,lo)) @@ -36,16 +36,16 @@ static int verify_input_data(double *x, int natoms, int nframes, double precisio for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) - goto error; + if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) + goto error; return 0; error: #if 0 for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) - printf("ERROR. Too large value: %d %d %d: %g %g %g\n",iframe,i,j,x[iframe*natoms*3+i*3+j],precision,x[iframe*natoms*3+i*3+j]/precision/MAX_FVAL); + if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) + printf("ERROR. Too large value: %d %d %d: %g %g %g\n",iframe,i,j,x[iframe*natoms*3+i*3+j],precision,x[iframe*natoms*3+i*3+j]/precision/MAX_FVAL); #endif return 1; } @@ -56,46 +56,46 @@ static int verify_input_data_float(float *x, int natoms, int nframes, float prec for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) - goto error; + if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) + goto error; return 0; error: #if 0 for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) - printf("ERROR. Too large value: %d %d %d: %g %g %g\n",iframe,i,j,x[iframe*natoms*3+i*3+j],precision,x[iframe*natoms*3+i*3+j]/precision/MAX_FVAL); + if (fabs(x[iframe*natoms*3+i*3+j]/precision+0.5)>=MAX_FVAL) + printf("ERROR. Too large value: %d %d %d: %g %g %g\n",iframe,i,j,x[iframe*natoms*3+i*3+j],precision,x[iframe*natoms*3+i*3+j]/precision/MAX_FVAL); #endif return 1; } static int quantize(double *x, int natoms, int nframes, - double precision, - int *quant) + double precision, + int *quant) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - quant[iframe*natoms*3+i*3+j]=(int)floor((x[iframe*natoms*3+i*3+j]/precision)+0.5); + quant[iframe*natoms*3+i*3+j]=(int)floor((x[iframe*natoms*3+i*3+j]/precision)+0.5); return verify_input_data(x,natoms,nframes,precision); } static int quantize_float(float *x, int natoms, int nframes, - float precision, - int *quant) + float precision, + int *quant) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - quant[iframe*natoms*3+i*3+j]=(int)floor((x[iframe*natoms*3+i*3+j]/precision)+0.5); + quant[iframe*natoms*3+i*3+j]=(int)floor((x[iframe*natoms*3+i*3+j]/precision)+0.5); return verify_input_data_float(x,natoms,nframes,precision); } static void quant_inter_differences(int *quant, int natoms, int nframes, - int *quant_inter) + int *quant_inter) { int iframe, i, j; /* The first frame is used for absolute positions. */ @@ -106,97 +106,97 @@ static void quant_inter_differences(int *quant, int natoms, int nframes, for (iframe=1; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - quant_inter[iframe*natoms*3+i*3+j]=quant[iframe*natoms*3+i*3+j]-quant[(iframe-1)*natoms*3+i*3+j]; + quant_inter[iframe*natoms*3+i*3+j]=quant[iframe*natoms*3+i*3+j]-quant[(iframe-1)*natoms*3+i*3+j]; } static void quant_intra_differences(int *quant, int natoms, int nframes, - int *quant_intra) + int *quant_intra) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) { /* The first atom is used with its absolute position. */ for (j=0; j<3; j++) - quant_intra[iframe*natoms*3+j]=quant[iframe*natoms*3+j]; + quant_intra[iframe*natoms*3+j]=quant[iframe*natoms*3+j]; /* For all other atoms the intraframe differences are computed. */ for (i=1; i<natoms; i++) - for (j=0; j<3; j++) - quant_intra[iframe*natoms*3+i*3+j]=quant[iframe*natoms*3+i*3+j]-quant[iframe*natoms*3+(i-1)*3+j]; + for (j=0; j<3; j++) + quant_intra[iframe*natoms*3+i*3+j]=quant[iframe*natoms*3+i*3+j]-quant[iframe*natoms*3+(i-1)*3+j]; } } static void unquantize(double *x, int natoms, int nframes, - double precision, - int *quant) + double precision, + int *quant) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - x[iframe*natoms*3+i*3+j]=(double)quant[iframe*natoms*3+i*3+j]*precision; + x[iframe*natoms*3+i*3+j]=(double)quant[iframe*natoms*3+i*3+j]*precision; } static void unquantize_float(float *x, int natoms, int nframes, - float precision, - int *quant) + float precision, + int *quant) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) for (i=0; i<natoms; i++) for (j=0; j<3; j++) - x[iframe*natoms*3+i*3+j]=(float)quant[iframe*natoms*3+i*3+j]*precision; + x[iframe*natoms*3+i*3+j]=(float)quant[iframe*natoms*3+i*3+j]*precision; } static void unquantize_inter_differences(double *x, int natoms, int nframes, - double precision, - int *quant) + double precision, + int *quant) { int iframe, i, j; for (i=0; i<natoms; i++) for (j=0; j<3; j++) { - int q=quant[i*3+j]; /* First value. */ - x[i*3+j]=(double)q*precision; - for (iframe=1; iframe<nframes; iframe++) - { - q+=quant[iframe*natoms*3+i*3+j]; - x[iframe*natoms*3+i*3+j]=(double)q*precision; - } + int q=quant[i*3+j]; /* First value. */ + x[i*3+j]=(double)q*precision; + for (iframe=1; iframe<nframes; iframe++) + { + q+=quant[iframe*natoms*3+i*3+j]; + x[iframe*natoms*3+i*3+j]=(double)q*precision; + } } } static void unquantize_inter_differences_float(float *x, int natoms, int nframes, - float precision, - int *quant) + float precision, + int *quant) { int iframe, i, j; for (i=0; i<natoms; i++) for (j=0; j<3; j++) { - int q=quant[i*3+j]; /* First value. */ - x[i*3+j]=(float)q*precision; - for (iframe=1; iframe<nframes; iframe++) - { - q+=quant[iframe*natoms*3+i*3+j]; - x[iframe*natoms*3+i*3+j]=(float)q*precision; - } + int q=quant[i*3+j]; /* First value. */ + x[i*3+j]=(float)q*precision; + for (iframe=1; iframe<nframes; iframe++) + { + q+=quant[iframe*natoms*3+i*3+j]; + x[iframe*natoms*3+i*3+j]=(float)q*precision; + } } } static void unquantize_inter_differences_int(int *x, int natoms, int nframes, - int *quant) + int *quant) { int iframe, i, j; for (i=0; i<natoms; i++) for (j=0; j<3; j++) { - int q=quant[i*3+j]; /* First value. */ - x[i*3+j]=q; - for (iframe=1; iframe<nframes; iframe++) - { - q+=quant[iframe*natoms*3+i*3+j]; - x[iframe*natoms*3+i*3+j]=q; - } + int q=quant[i*3+j]; /* First value. */ + x[i*3+j]=q; + for (iframe=1; iframe<nframes; iframe++) + { + q+=quant[iframe*natoms*3+i*3+j]; + x[iframe*natoms*3+i*3+j]=q; + } } } @@ -209,23 +209,23 @@ static void unquant_intra_differences_first_frame(int *quant, int natoms) { int q=quant[j]; for (i=1; i<natoms; i++) - { - q+=quant[i*3+j]; - quant[i*3+j]=q; - } + { + q+=quant[i*3+j]; + quant[i*3+j]=q; + } } #if 0 for (j=0; j<3; j++) for (i=0; i<natoms; i++) { - printf("UQ: %d %d %d: %d\n",0,j,i,quant[i*3+j]); + printf("UQ: %d %d %d: %d\n",0,j,i,quant[i*3+j]); } #endif } static void unquantize_intra_differences(double *x, int natoms, int nframes, - double precision, - int *quant) + double precision, + int *quant) { int iframe, i, j; #if 0 @@ -234,48 +234,48 @@ static void unquantize_intra_differences(double *x, int natoms, int nframes, for (iframe=0; iframe<nframes; iframe++) for (j=0; j<3; j++) { - int q=quant[iframe*natoms*3+j]; - x[iframe*natoms*3+j]=(double)q*precision; - for (i=1; i<natoms; i++) - { - q+=quant[iframe*natoms*3+i*3+j]; - x[iframe*natoms*3+i*3+j]=(double)q*precision; - } + int q=quant[iframe*natoms*3+j]; + x[iframe*natoms*3+j]=(double)q*precision; + for (i=1; i<natoms; i++) + { + q+=quant[iframe*natoms*3+i*3+j]; + x[iframe*natoms*3+i*3+j]=(double)q*precision; + } } } static void unquantize_intra_differences_float(float *x, int natoms, int nframes, - float precision, - int *quant) + float precision, + int *quant) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) for (j=0; j<3; j++) { - int q=quant[iframe*natoms*3+j]; - x[iframe*natoms*3+j]=(float)q*precision; - for (i=1; i<natoms; i++) - { - q+=quant[iframe*natoms*3+i*3+j]; - x[iframe*natoms*3+i*3+j]=(float)q*precision; - } + int q=quant[iframe*natoms*3+j]; + x[iframe*natoms*3+j]=(float)q*precision; + for (i=1; i<natoms; i++) + { + q+=quant[iframe*natoms*3+i*3+j]; + x[iframe*natoms*3+i*3+j]=(float)q*precision; + } } } static void unquantize_intra_differences_int(int *x, int natoms, int nframes, - int *quant) + int *quant) { int iframe, i, j; for (iframe=0; iframe<nframes; iframe++) for (j=0; j<3; j++) { - int q=quant[iframe*natoms*3+j]; - x[iframe*natoms*3+j]=q; - for (i=1; i<natoms; i++) - { - q+=quant[iframe*natoms*3+i*3+j]; - x[iframe*natoms*3+i*3+j]=q; - } + int q=quant[iframe*natoms*3+j]; + x[iframe*natoms*3+j]=q; + for (i=1; i<natoms; i++) + { + q+=quant[iframe*natoms*3+i*3+j]; + x[iframe*natoms*3+i*3+j]=q; + } } } @@ -310,13 +310,13 @@ static fix_t readbufferfix(unsigned char *buf, int num) /* Perform position compression from the quantized data. */ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intra, - int natoms, int nframes, - int speed, - int initial_coding, int initial_coding_parameter, - int coding, int coding_parameter, - fix_t prec_hi, fix_t prec_lo, - int *nitems, - char *data) + int natoms, int nframes, + int speed, + int initial_coding, int initial_coding_parameter, + int coding, int coding_parameter, + fix_t prec_hi, fix_t prec_lo, + int *nitems, + char *data) { int bufloc=0; char *datablock=NULL; @@ -364,16 +364,16 @@ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intr struct coder *coder=Ptngc_coder_init(); length=natoms*3; datablock=(char*)Ptngc_pack_array(coder,quant,&length, - initial_coding,initial_coding_parameter,natoms,speed); + initial_coding,initial_coding_parameter,natoms,speed); Ptngc_coder_deinit(coder); } else if ((initial_coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || - (initial_coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) + (initial_coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) { struct coder *coder=Ptngc_coder_init(); length=natoms*3; datablock=(char*)Ptngc_pack_array(coder,quant_intra,&length, - initial_coding,initial_coding_parameter,natoms,speed); + initial_coding,initial_coding_parameter,natoms,speed); Ptngc_coder_deinit(coder); } /* Block length. */ @@ -391,42 +391,42 @@ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intr datablock=NULL; /* Inter-frame compression? */ if ((coding==TNG_COMPRESS_ALGO_POS_STOPBIT_INTER) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTER) || - (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTER)) - { - struct coder *coder=Ptngc_coder_init(); - length=natoms*3*(nframes-1); - datablock=(char*)Ptngc_pack_array(coder,quant_inter+natoms*3,&length, - coding,coding_parameter,natoms,speed); - Ptngc_coder_deinit(coder); - } + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTER) || + (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTER)) + { + struct coder *coder=Ptngc_coder_init(); + length=natoms*3*(nframes-1); + datablock=(char*)Ptngc_pack_array(coder,quant_inter+natoms*3,&length, + coding,coding_parameter,natoms,speed); + Ptngc_coder_deinit(coder); + } /* One-to-one compression? */ else if ((coding==TNG_COMPRESS_ALGO_POS_XTC2) || - (coding==TNG_COMPRESS_ALGO_POS_XTC3) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) - { - struct coder *coder=Ptngc_coder_init(); - length=natoms*3*(nframes-1); - datablock=(char*)Ptngc_pack_array(coder,quant+natoms*3,&length, - coding,coding_parameter,natoms,speed); - Ptngc_coder_deinit(coder); - } + (coding==TNG_COMPRESS_ALGO_POS_XTC3) || + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) + { + struct coder *coder=Ptngc_coder_init(); + length=natoms*3*(nframes-1); + datablock=(char*)Ptngc_pack_array(coder,quant+natoms*3,&length, + coding,coding_parameter,natoms,speed); + Ptngc_coder_deinit(coder); + } /* Intra-frame compression? */ else if ((coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || - (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) - { - struct coder *coder=Ptngc_coder_init(); - length=natoms*3*(nframes-1); - datablock=(char*)Ptngc_pack_array(coder,quant_intra+natoms*3,&length, - coding,coding_parameter,natoms,speed); - Ptngc_coder_deinit(coder); - } + (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) + { + struct coder *coder=Ptngc_coder_init(); + length=natoms*3*(nframes-1); + datablock=(char*)Ptngc_pack_array(coder,quant_intra+natoms*3,&length, + coding,coding_parameter,natoms,speed); + Ptngc_coder_deinit(coder); + } /* Block length. */ if (data) - bufferfix((unsigned char*)data+bufloc,(fix_t)length,4); + bufferfix((unsigned char*)data+bufloc,(fix_t)length,4); bufloc+=4; if (data) - memcpy(data+bufloc,datablock,length); + memcpy(data+bufloc,datablock,length); free(datablock); bufloc+=length; } @@ -435,13 +435,13 @@ static void compress_quantized_pos(int *quant, int *quant_inter, int *quant_intr /* Perform velocity compression from vel into the data block */ static void compress_quantized_vel(int *quant, int *quant_inter, - int natoms, int nframes, - int speed, - int initial_coding, int initial_coding_parameter, - int coding, int coding_parameter, - fix_t prec_hi, fix_t prec_lo, - int *nitems, - char *data) + int natoms, int nframes, + int speed, + int initial_coding, int initial_coding_parameter, + int coding, int coding_parameter, + fix_t prec_hi, fix_t prec_lo, + int *nitems, + char *data) { int bufloc=0; char *datablock=NULL; @@ -490,7 +490,7 @@ static void compress_quantized_vel(int *quant, int *quant_inter, { struct coder *coder=Ptngc_coder_init(); datablock=(char*)Ptngc_pack_array(coder,quant,&length, - initial_coding,initial_coding_parameter,natoms,speed); + initial_coding,initial_coding_parameter,natoms,speed); Ptngc_coder_deinit(coder); } /* Block length. */ @@ -510,32 +510,32 @@ static void compress_quantized_vel(int *quant, int *quant_inter, datablock=NULL; /* Inter-frame compression? */ if ((coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_INTER) || - (coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER) || - (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_INTER)) - { - struct coder *coder=Ptngc_coder_init(); - length=natoms*3*(nframes-1); - datablock=(char*)Ptngc_pack_array(coder,quant_inter+natoms*3,&length, - coding,coding_parameter,natoms,speed); - Ptngc_coder_deinit(coder); - } + (coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER) || + (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_INTER)) + { + struct coder *coder=Ptngc_coder_init(); + length=natoms*3*(nframes-1); + datablock=(char*)Ptngc_pack_array(coder,quant_inter+natoms*3,&length, + coding,coding_parameter,natoms,speed); + Ptngc_coder_deinit(coder); + } /* One-to-one compression? */ else if ((coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE) || - (coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE) || - (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) - { - struct coder *coder=Ptngc_coder_init(); - length=natoms*3*(nframes-1); - datablock=(char*)Ptngc_pack_array(coder,quant+natoms*3,&length, - coding,coding_parameter,natoms,speed); - Ptngc_coder_deinit(coder); - } + (coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE) || + (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) + { + struct coder *coder=Ptngc_coder_init(); + length=natoms*3*(nframes-1); + datablock=(char*)Ptngc_pack_array(coder,quant+natoms*3,&length, + coding,coding_parameter,natoms,speed); + Ptngc_coder_deinit(coder); + } /* Block length. */ if (data) - bufferfix((unsigned char*)data+bufloc,(fix_t)length,4); + bufferfix((unsigned char*)data+bufloc,(fix_t)length,4); bufloc+=4; if (data) - memcpy(data+bufloc,datablock,length); + memcpy(data+bufloc,datablock,length); free(datablock); bufloc+=length; } @@ -543,7 +543,7 @@ static void compress_quantized_vel(int *quant, int *quant_inter, } static int determine_best_coding_stop_bits(struct coder *coder,int *input, int *length, - int *coding_parameter, int natoms) + int *coding_parameter, int natoms) { int bits; unsigned char *packed; @@ -554,16 +554,16 @@ static int determine_best_coding_stop_bits(struct coder *coder,int *input, int * { io_length=*length; packed=Ptngc_pack_array(coder,input,&io_length, - TNG_COMPRESS_ALGO_STOPBIT,bits,natoms,0); + TNG_COMPRESS_ALGO_STOPBIT,bits,natoms,0); if (packed) - { - if ((new_parameter==-1) || (io_length<best_length)) - { - new_parameter=bits; - best_length=io_length; - } - free(packed); - } + { + if ((new_parameter==-1) || (io_length<best_length)) + { + new_parameter=bits; + best_length=io_length; + } + free(packed); + } } if (new_parameter==-1) return 1; @@ -574,7 +574,7 @@ static int determine_best_coding_stop_bits(struct coder *coder,int *input, int * } static int determine_best_coding_triple(struct coder *coder,int *input, int *length, - int *coding_parameter, int natoms) + int *coding_parameter, int natoms) { int bits; unsigned char *packed; @@ -585,16 +585,16 @@ static int determine_best_coding_triple(struct coder *coder,int *input, int *len { io_length=*length; packed=Ptngc_pack_array(coder,input,&io_length, - TNG_COMPRESS_ALGO_TRIPLET,bits,natoms,0); + TNG_COMPRESS_ALGO_TRIPLET,bits,natoms,0); if (packed) - { - if ((new_parameter==-1) || (io_length<best_length)) - { - new_parameter=bits; - best_length=io_length; - } - free(packed); - } + { + if ((new_parameter==-1) || (io_length<best_length)) + { + new_parameter=bits; + best_length=io_length; + } + free(packed); + } } if (new_parameter==-1) return 1; @@ -605,8 +605,8 @@ static int determine_best_coding_triple(struct coder *coder,int *input, int *len } static void determine_best_pos_initial_coding(int *quant, int *quant_intra, int natoms, int speed, - fix_t prec_hi, fix_t prec_lo, - int *initial_coding, int *initial_coding_parameter) + fix_t prec_hi, fix_t prec_lo, + int *initial_coding, int *initial_coding_parameter) { if (*initial_coding==-1) { @@ -622,8 +622,8 @@ static void determine_best_pos_initial_coding(int *quant, int *quant_intra, int current_coding=TNG_COMPRESS_ALGO_POS_XTC2; current_coding_parameter=0; compress_quantized_pos(quant,NULL,quant_intra,natoms,1,speed, - current_coding,current_coding_parameter, - 0,0,prec_hi,prec_lo,¤t_code_size,NULL); + current_coding,current_coding_parameter, + 0,0,prec_hi,prec_lo,¤t_code_size,NULL); best_coding=current_coding; best_coding_parameter=current_coding_parameter; best_code_size=current_code_size; @@ -634,14 +634,14 @@ static void determine_best_pos_initial_coding(int *quant, int *quant_intra, int current_code_size=natoms*3; current_coding_parameter=0; if (!determine_best_coding_triple(coder,quant_intra,¤t_code_size,¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); /* Determine best parameter for triplet one-to-one. */ @@ -650,76 +650,76 @@ static void determine_best_pos_initial_coding(int *quant, int *quant_intra, int current_code_size=natoms*3; current_coding_parameter=0; if (!determine_best_coding_triple(coder,quant,¤t_code_size,¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); if (speed>=2) - { - current_coding=TNG_COMPRESS_ALGO_POS_XTC3; - current_coding_parameter=0; - compress_quantized_pos(quant,NULL,quant_intra,natoms,1,speed, - current_coding,current_coding_parameter, - 0,0,prec_hi,prec_lo,¤t_code_size,NULL); - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + { + current_coding=TNG_COMPRESS_ALGO_POS_XTC3; + current_coding_parameter=0; + compress_quantized_pos(quant,NULL,quant_intra,natoms,1,speed, + current_coding,current_coding_parameter, + 0,0,prec_hi,prec_lo,¤t_code_size,NULL); + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } /* Test BWLZH intra */ if (speed>=6) - { - current_coding=TNG_COMPRESS_ALGO_POS_BWLZH_INTRA; - current_coding_parameter=0; - compress_quantized_pos(quant,NULL,quant_intra,natoms,1,speed, - current_coding,current_coding_parameter, - 0,0,prec_hi,prec_lo,¤t_code_size,NULL); - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - } - } + { + current_coding=TNG_COMPRESS_ALGO_POS_BWLZH_INTRA; + current_coding_parameter=0; + compress_quantized_pos(quant,NULL,quant_intra,natoms,1,speed, + current_coding,current_coding_parameter, + 0,0,prec_hi,prec_lo,¤t_code_size,NULL); + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + } + } *initial_coding=best_coding; *initial_coding_parameter=best_coding_parameter; } else { if (*initial_coding_parameter==-1) - { - if ((*initial_coding==TNG_COMPRESS_ALGO_POS_XTC2) || - (*initial_coding==TNG_COMPRESS_ALGO_POS_XTC3) || - (*initial_coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) - *initial_coding_parameter=0; - else if (*initial_coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3; - determine_best_coding_triple(coder,quant_intra,¤t_code_size,initial_coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } - else if (*initial_coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3; - determine_best_coding_triple(coder,quant,¤t_code_size,initial_coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } - } + { + if ((*initial_coding==TNG_COMPRESS_ALGO_POS_XTC2) || + (*initial_coding==TNG_COMPRESS_ALGO_POS_XTC3) || + (*initial_coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) + *initial_coding_parameter=0; + else if (*initial_coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3; + determine_best_coding_triple(coder,quant_intra,¤t_code_size,initial_coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } + else if (*initial_coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE) + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3; + determine_best_coding_triple(coder,quant,¤t_code_size,initial_coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } + } } } static void determine_best_pos_coding(int *quant, int *quant_inter, int *quant_intra, int natoms, int nframes, int speed, - fix_t prec_hi, fix_t prec_lo, - int *coding, int *coding_parameter) + fix_t prec_hi, fix_t prec_lo, + int *coding, int *coding_parameter) { if (*coding==-1) { @@ -734,16 +734,16 @@ static void determine_best_pos_coding(int *quant, int *quant_inter, int *quant_i struct coder *coder; /* Always use XTC2 for the initial coding. */ compress_quantized_pos(quant,quant_inter,quant_intra,natoms,1,speed, - TNG_COMPRESS_ALGO_POS_XTC2,0, - 0,0, - prec_hi,prec_lo,&initial_code_size,NULL); + TNG_COMPRESS_ALGO_POS_XTC2,0, + 0,0, + prec_hi,prec_lo,&initial_code_size,NULL); /* Start with XTC2, it should always work. */ current_coding=TNG_COMPRESS_ALGO_POS_XTC2; current_coding_parameter=0; compress_quantized_pos(quant,quant_inter,quant_intra,natoms,nframes,speed, - TNG_COMPRESS_ALGO_POS_XTC2,0, - current_coding,current_coding_parameter, - prec_hi,prec_lo,¤t_code_size,NULL); + TNG_COMPRESS_ALGO_POS_XTC2,0, + current_coding,current_coding_parameter, + prec_hi,prec_lo,¤t_code_size,NULL); best_coding=current_coding; best_coding_parameter=current_coding_parameter; best_code_size=current_code_size-initial_code_size; /* Correct for the use of XTC2 for the first frame. */ @@ -754,15 +754,15 @@ static void determine_best_pos_coding(int *quant, int *quant_inter, int *quant_i current_code_size=natoms*3*(nframes-1); current_coding_parameter=0; if (!determine_best_coding_stop_bits(coder,quant_inter+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); /* Determine best parameter for triplet interframe coding. */ @@ -771,15 +771,15 @@ static void determine_best_pos_coding(int *quant, int *quant_inter, int *quant_i current_code_size=natoms*3*(nframes-1); current_coding_parameter=0; if (!determine_best_coding_triple(coder,quant_inter+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); /* Determine best parameter for triplet intraframe coding. */ @@ -788,15 +788,15 @@ static void determine_best_pos_coding(int *quant, int *quant_inter, int *quant_i current_code_size=natoms*3*(nframes-1); current_coding_parameter=0; if (!determine_best_coding_triple(coder,quant_intra+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); /* Determine best parameter for triplet one-to-one coding. */ @@ -805,99 +805,99 @@ static void determine_best_pos_coding(int *quant, int *quant_inter, int *quant_i current_code_size=natoms*3*(nframes-1); current_coding_parameter=0; if (!determine_best_coding_triple(coder,quant+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); /* Test BWLZH inter */ if (speed>=4) - { - current_coding=TNG_COMPRESS_ALGO_POS_BWLZH_INTER; - current_coding_parameter=0; - compress_quantized_pos(quant,quant_inter,quant_intra,natoms,nframes,speed, - TNG_COMPRESS_ALGO_POS_XTC2,0, - current_coding,current_coding_parameter, - prec_hi,prec_lo,¤t_code_size,NULL); - current_code_size-=initial_code_size; /* Correct for the use of XTC2 for the first frame. */ - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + { + current_coding=TNG_COMPRESS_ALGO_POS_BWLZH_INTER; + current_coding_parameter=0; + compress_quantized_pos(quant,quant_inter,quant_intra,natoms,nframes,speed, + TNG_COMPRESS_ALGO_POS_XTC2,0, + current_coding,current_coding_parameter, + prec_hi,prec_lo,¤t_code_size,NULL); + current_code_size-=initial_code_size; /* Correct for the use of XTC2 for the first frame. */ + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } /* Test BWLZH intra */ if (speed>=6) - { - current_coding=TNG_COMPRESS_ALGO_POS_BWLZH_INTRA; - current_coding_parameter=0; - compress_quantized_pos(quant,quant_inter,quant_intra,natoms,nframes,speed, - TNG_COMPRESS_ALGO_POS_XTC2,0, - current_coding,current_coding_parameter, - prec_hi,prec_lo,¤t_code_size,NULL); - current_code_size-=initial_code_size; /* Correct for the use of XTC2 for the first frame. */ - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - } - } + { + current_coding=TNG_COMPRESS_ALGO_POS_BWLZH_INTRA; + current_coding_parameter=0; + compress_quantized_pos(quant,quant_inter,quant_intra,natoms,nframes,speed, + TNG_COMPRESS_ALGO_POS_XTC2,0, + current_coding,current_coding_parameter, + prec_hi,prec_lo,¤t_code_size,NULL); + current_code_size-=initial_code_size; /* Correct for the use of XTC2 for the first frame. */ + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + } + } *coding=best_coding; *coding_parameter=best_coding_parameter; } else if (*coding_parameter==-1) { if ((*coding==TNG_COMPRESS_ALGO_POS_XTC2) || - (*coding==TNG_COMPRESS_ALGO_POS_XTC3) || - (*coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTER) || - (*coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) - *coding_parameter=0; + (*coding==TNG_COMPRESS_ALGO_POS_XTC3) || + (*coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTER) || + (*coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) + *coding_parameter=0; else if (*coding==TNG_COMPRESS_ALGO_POS_STOPBIT_INTER) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_stop_bits(coder,quant_inter+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_stop_bits(coder,quant_inter+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTER) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_triple(coder,quant_inter+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_triple(coder,quant_inter+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_triple(coder,quant_intra+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_triple(coder,quant_intra+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_triple(coder,quant+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_triple(coder,quant+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } } } static void determine_best_vel_initial_coding(int *quant, int natoms, int speed, - fix_t prec_hi, fix_t prec_lo, - int *initial_coding, int *initial_coding_parameter) + fix_t prec_hi, fix_t prec_lo, + int *initial_coding, int *initial_coding_parameter) { if (*initial_coding==-1) { @@ -915,12 +915,12 @@ static void determine_best_vel_initial_coding(int *quant, int natoms, int speed, current_coding_parameter=0; coder=Ptngc_coder_init(); if (!determine_best_coding_stop_bits(coder,quant,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } + ¤t_coding_parameter,natoms)) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } Ptngc_coder_deinit(coder); /* Determine best parameter for triplet one-to-one. */ @@ -929,58 +929,58 @@ static void determine_best_vel_initial_coding(int *quant, int natoms, int speed, current_code_size=natoms*3; current_coding_parameter=0; if (!determine_best_coding_triple(coder,quant,¤t_code_size,¤t_coding_parameter,natoms)) - { - if ((best_coding==-1) || (current_code_size<best_code_size)) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - best_code_size=current_code_size; - } - } + { + if ((best_coding==-1) || (current_code_size<best_code_size)) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + best_code_size=current_code_size; + } + } Ptngc_coder_deinit(coder); /* Test BWLZH one-to-one */ if (speed>=4) - { - current_coding=TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE; - current_coding_parameter=0; - compress_quantized_vel(quant,NULL,natoms,1,speed, - current_coding,current_coding_parameter, - 0,0,prec_hi,prec_lo,¤t_code_size,NULL); - if ((best_coding==-1) || (current_code_size<best_code_size)) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - } - } + { + current_coding=TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE; + current_coding_parameter=0; + compress_quantized_vel(quant,NULL,natoms,1,speed, + current_coding,current_coding_parameter, + 0,0,prec_hi,prec_lo,¤t_code_size,NULL); + if ((best_coding==-1) || (current_code_size<best_code_size)) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + } + } *initial_coding=best_coding; *initial_coding_parameter=best_coding_parameter; } else if (*initial_coding_parameter==-1) { if (*initial_coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE) - *initial_coding_parameter=0; + *initial_coding_parameter=0; else if (*initial_coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3; - determine_best_coding_stop_bits(coder,quant,¤t_code_size, - initial_coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3; + determine_best_coding_stop_bits(coder,quant,¤t_code_size, + initial_coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*initial_coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3; - determine_best_coding_triple(coder,quant,¤t_code_size,initial_coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3; + determine_best_coding_triple(coder,quant,¤t_code_size,initial_coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } } } static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms, int nframes, int speed, - fix_t prec_hi, fix_t prec_lo, - int *coding, int *coding_parameter) + fix_t prec_hi, fix_t prec_lo, + int *coding, int *coding_parameter) { if (*coding==-1) { @@ -996,8 +996,8 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms, struct coder *coder; /* Use stopbits one-to-one coding for the initial coding. */ compress_quantized_vel(quant,NULL,natoms,1,speed, - TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE,initial_numbits, - 0,0,prec_hi,prec_lo,&initial_code_size,NULL); + TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE,initial_numbits, + 0,0,prec_hi,prec_lo,&initial_code_size,NULL); /* Test stopbit one-to-one */ current_coding=TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE; @@ -1005,7 +1005,7 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms, current_coding_parameter=0; coder=Ptngc_coder_init(); determine_best_coding_stop_bits(coder,quant+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms); + ¤t_coding_parameter,natoms); Ptngc_coder_deinit(coder); best_coding=current_coding; best_code_size=current_code_size; @@ -1017,15 +1017,15 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms, current_coding_parameter=0; coder=Ptngc_coder_init(); if (!determine_best_coding_triple(coder,quant_inter+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_code_size=current_code_size; - best_coding_parameter=current_coding_parameter; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_code_size=current_code_size; + best_coding_parameter=current_coding_parameter; + } + } Ptngc_coder_deinit(coder); /* Test triplet one-to-one */ @@ -1034,15 +1034,15 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms, current_coding_parameter=0; coder=Ptngc_coder_init(); if (!determine_best_coding_triple(coder,quant+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_code_size=current_code_size; - best_coding_parameter=current_coding_parameter; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_code_size=current_code_size; + best_coding_parameter=current_coding_parameter; + } + } Ptngc_coder_deinit(coder); /* Test stopbit interframe */ @@ -1051,99 +1051,99 @@ static void determine_best_vel_coding(int *quant, int *quant_inter, int natoms, current_coding_parameter=0; coder=Ptngc_coder_init(); if (!determine_best_coding_stop_bits(coder,quant_inter+natoms*3,¤t_code_size, - ¤t_coding_parameter,natoms)) - { - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_code_size=current_code_size; - best_coding_parameter=current_coding_parameter; - } - } + ¤t_coding_parameter,natoms)) + { + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_code_size=current_code_size; + best_coding_parameter=current_coding_parameter; + } + } Ptngc_coder_deinit(coder); if (speed>=4) - { - /* Test BWLZH inter */ - current_coding=TNG_COMPRESS_ALGO_VEL_BWLZH_INTER; - current_coding_parameter=0; - compress_quantized_vel(quant,quant_inter,natoms,nframes,speed, - TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE,initial_numbits, - current_coding,current_coding_parameter, - prec_hi,prec_lo,¤t_code_size,NULL); - current_code_size-=initial_code_size; /* Correct for the initial frame */ - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_code_size=current_code_size; - best_coding_parameter=current_coding_parameter; - } - - /* Test BWLZH one-to-one */ - current_coding=TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE; - current_coding_parameter=0; - compress_quantized_vel(quant,quant_inter,natoms,nframes,speed, - TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE,initial_numbits, - current_coding,current_coding_parameter, - prec_hi,prec_lo,¤t_code_size,NULL); - current_code_size-=initial_code_size; /* Correct for the initial frame */ - if (current_code_size<best_code_size) - { - best_coding=current_coding; - best_coding_parameter=current_coding_parameter; - } - } + { + /* Test BWLZH inter */ + current_coding=TNG_COMPRESS_ALGO_VEL_BWLZH_INTER; + current_coding_parameter=0; + compress_quantized_vel(quant,quant_inter,natoms,nframes,speed, + TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE,initial_numbits, + current_coding,current_coding_parameter, + prec_hi,prec_lo,¤t_code_size,NULL); + current_code_size-=initial_code_size; /* Correct for the initial frame */ + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_code_size=current_code_size; + best_coding_parameter=current_coding_parameter; + } + + /* Test BWLZH one-to-one */ + current_coding=TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE; + current_coding_parameter=0; + compress_quantized_vel(quant,quant_inter,natoms,nframes,speed, + TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE,initial_numbits, + current_coding,current_coding_parameter, + prec_hi,prec_lo,¤t_code_size,NULL); + current_code_size-=initial_code_size; /* Correct for the initial frame */ + if (current_code_size<best_code_size) + { + best_coding=current_coding; + best_coding_parameter=current_coding_parameter; + } + } *coding=best_coding; *coding_parameter=best_coding_parameter; } else if (*coding_parameter==-1) { if ((*coding==TNG_COMPRESS_ALGO_VEL_BWLZH_INTER) || - (*coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) - *coding_parameter=0; + (*coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) + *coding_parameter=0; else if (*coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_stop_bits(coder,quant+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_stop_bits(coder,quant+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_INTER) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_triple(coder,quant_inter+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_triple(coder,quant_inter+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_triple(coder,quant+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_triple(coder,quant+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } else if (*coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER) - { - struct coder *coder=Ptngc_coder_init(); - int current_code_size=natoms*3*(nframes-1); - determine_best_coding_stop_bits(coder,quant_inter+natoms*3,¤t_code_size, - coding_parameter,natoms); - Ptngc_coder_deinit(coder); - } + { + struct coder *coder=Ptngc_coder_init(); + int current_code_size=natoms*3*(nframes-1); + determine_best_coding_stop_bits(coder,quant_inter+natoms*3,¤t_code_size, + coding_parameter,natoms); + Ptngc_coder_deinit(coder); + } } } char DECLSPECDLLEXPORT *tng_compress_pos_int(int *pos, int natoms, int nframes, - unsigned long prec_hi, unsigned long prec_lo, - int speed,int *algo, - int *nitems) + unsigned long prec_hi, unsigned long prec_lo, + int speed,int *algo, + int *nitems) { char *data=malloc(natoms*nframes*14+11*4); /* 12 bytes are required to store 4 32 bit integers - This is 17% extra. The final 11*4 is to store information - needed for decompression. */ + This is 17% extra. The final 11*4 is to store information + needed for decompression. */ int *quant=pos; /* Already quantized positions. */ int *quant_intra=malloc(natoms*nframes*3*sizeof *quant_intra); int *quant_inter=malloc(natoms*nframes*3*sizeof *quant_inter); @@ -1170,12 +1170,12 @@ char DECLSPECDLLEXPORT *tng_compress_pos_int(int *pos, int natoms, int nframes, { initial_coding_parameter=-1; determine_best_pos_initial_coding(quant,quant_intra,natoms,speed,prec_hi,prec_lo, - &initial_coding,&initial_coding_parameter); + &initial_coding,&initial_coding_parameter); } else if (initial_coding_parameter==-1) { determine_best_pos_initial_coding(quant,quant_intra,natoms,speed,prec_hi,prec_lo, - &initial_coding,&initial_coding_parameter); + &initial_coding,&initial_coding_parameter); } if (nframes==1) @@ -1187,22 +1187,22 @@ char DECLSPECDLLEXPORT *tng_compress_pos_int(int *pos, int natoms, int nframes, if (nframes>1) { if (coding==-1) - { - coding_parameter=-1; - determine_best_pos_coding(quant,quant_inter,quant_intra,natoms,nframes,speed,prec_hi,prec_lo, - &coding,&coding_parameter); - } + { + coding_parameter=-1; + determine_best_pos_coding(quant,quant_inter,quant_intra,natoms,nframes,speed,prec_hi,prec_lo, + &coding,&coding_parameter); + } else if (coding_parameter==-1) - { - determine_best_pos_coding(quant,quant_inter,quant_intra,natoms,nframes,speed,prec_hi,prec_lo, - &coding,&coding_parameter); - } + { + determine_best_pos_coding(quant,quant_inter,quant_intra,natoms,nframes,speed,prec_hi,prec_lo, + &coding,&coding_parameter); + } } compress_quantized_pos(quant,quant_inter,quant_intra,natoms,nframes,speed, - initial_coding,initial_coding_parameter, - coding,coding_parameter, - prec_hi,prec_lo,nitems,data); + initial_coding,initial_coding_parameter, + coding,coding_parameter, + prec_hi,prec_lo,nitems,data); free(quant_inter); free(quant_intra); if (algo[0]==-1) @@ -1217,9 +1217,9 @@ char DECLSPECDLLEXPORT *tng_compress_pos_int(int *pos, int natoms, int nframes, } char DECLSPECDLLEXPORT *tng_compress_pos(double *pos, int natoms, int nframes, - double desired_precision, - int speed,int *algo, - int *nitems) + double desired_precision, + int speed,int *algo, + int *nitems) { int *quant=malloc(natoms*nframes*3*sizeof *quant); char *data; @@ -1235,9 +1235,9 @@ char DECLSPECDLLEXPORT *tng_compress_pos(double *pos, int natoms, int nframes, } char DECLSPECDLLEXPORT *tng_compress_pos_float(float *pos, int natoms, int nframes, - float desired_precision, - int speed,int *algo, - int *nitems) + float desired_precision, + int speed,int *algo, + int *nitems) { int *quant=malloc(natoms*nframes*3*sizeof *quant); char *data; @@ -1253,10 +1253,10 @@ char DECLSPECDLLEXPORT *tng_compress_pos_float(float *pos, int natoms, int nfram } char DECLSPECDLLEXPORT *tng_compress_pos_find_algo(double *pos, int natoms, int nframes, - double desired_precision, - int speed, - int *algo, - int *nitems) + double desired_precision, + int speed, + int *algo, + int *nitems) { algo[0]=-1; algo[1]=-1; @@ -1266,10 +1266,10 @@ char DECLSPECDLLEXPORT *tng_compress_pos_find_algo(double *pos, int natoms, int } char DECLSPECDLLEXPORT *tng_compress_pos_float_find_algo(float *pos, int natoms, int nframes, - float desired_precision, - int speed, - int *algo, - int *nitems) + float desired_precision, + int speed, + int *algo, + int *nitems) { algo[0]=-1; algo[1]=-1; @@ -1279,9 +1279,9 @@ char DECLSPECDLLEXPORT *tng_compress_pos_float_find_algo(float *pos, int natoms, } char DECLSPECDLLEXPORT *tng_compress_pos_int_find_algo(int *pos, int natoms, int nframes, - unsigned long prec_hi, unsigned long prec_lo, - int speed,int *algo, - int *nitems) + unsigned long prec_hi, unsigned long prec_lo, + int speed,int *algo, + int *nitems) { algo[0]=-1; algo[1]=-1; @@ -1303,13 +1303,13 @@ int DECLSPECDLLEXPORT tng_compress_nalgo(void) } char DECLSPECDLLEXPORT *tng_compress_vel_int(int *vel, int natoms, int nframes, - unsigned long prec_hi, unsigned long prec_lo, - int speed, int *algo, - int *nitems) + unsigned long prec_hi, unsigned long prec_lo, + int speed, int *algo, + int *nitems) { char *data=malloc(natoms*nframes*14+11*4); /* 12 bytes are required to store 4 32 bit integers - This is 17% extra. The final 11*4 is to store information - needed for decompression. */ + This is 17% extra. The final 11*4 is to store information + needed for decompression. */ int *quant=vel; int *quant_inter=malloc(natoms*nframes*3*sizeof *quant_inter); @@ -1334,12 +1334,12 @@ char DECLSPECDLLEXPORT *tng_compress_vel_int(int *vel, int natoms, int nframes, { initial_coding_parameter=-1; determine_best_vel_initial_coding(quant,natoms,speed,prec_hi,prec_lo, - &initial_coding,&initial_coding_parameter); + &initial_coding,&initial_coding_parameter); } else if (initial_coding_parameter==-1) { determine_best_vel_initial_coding(quant,natoms,speed,prec_hi,prec_lo, - &initial_coding,&initial_coding_parameter); + &initial_coding,&initial_coding_parameter); } if (nframes==1) @@ -1351,22 +1351,22 @@ char DECLSPECDLLEXPORT *tng_compress_vel_int(int *vel, int natoms, int nframes, if (nframes>1) { if (coding==-1) - { - coding_parameter=-1; - determine_best_vel_coding(quant,quant_inter,natoms,nframes,speed,prec_hi,prec_lo, - &coding,&coding_parameter); - } + { + coding_parameter=-1; + determine_best_vel_coding(quant,quant_inter,natoms,nframes,speed,prec_hi,prec_lo, + &coding,&coding_parameter); + } else if (coding_parameter==-1) - { - determine_best_vel_coding(quant,quant_inter,natoms,nframes,speed,prec_hi,prec_lo, - &coding,&coding_parameter); - } + { + determine_best_vel_coding(quant,quant_inter,natoms,nframes,speed,prec_hi,prec_lo, + &coding,&coding_parameter); + } } compress_quantized_vel(quant,quant_inter,natoms,nframes,speed, - initial_coding,initial_coding_parameter, - coding,coding_parameter, - prec_hi,prec_lo,nitems,data); + initial_coding,initial_coding_parameter, + coding,coding_parameter, + prec_hi,prec_lo,nitems,data); free(quant_inter); if (algo[0]==-1) algo[0]=initial_coding; @@ -1380,9 +1380,9 @@ char DECLSPECDLLEXPORT *tng_compress_vel_int(int *vel, int natoms, int nframes, } char DECLSPECDLLEXPORT *tng_compress_vel(double *vel, int natoms, int nframes, - double desired_precision, - int speed, int *algo, - int *nitems) + double desired_precision, + int speed, int *algo, + int *nitems) { int *quant=malloc(natoms*nframes*3*sizeof *quant); char *data; @@ -1397,9 +1397,9 @@ char DECLSPECDLLEXPORT *tng_compress_vel(double *vel, int natoms, int nframes, } char DECLSPECDLLEXPORT *tng_compress_vel_float(float *vel, int natoms, int nframes, - float desired_precision, - int speed, int *algo, - int *nitems) + float desired_precision, + int speed, int *algo, + int *nitems) { int *quant=malloc(natoms*nframes*3*sizeof *quant); char *data; @@ -1414,10 +1414,10 @@ char DECLSPECDLLEXPORT *tng_compress_vel_float(float *vel, int natoms, int nfram } char DECLSPECDLLEXPORT *tng_compress_vel_find_algo(double *vel, int natoms, int nframes, - double desired_precision, - int speed, - int *algo, - int *nitems) + double desired_precision, + int speed, + int *algo, + int *nitems) { algo[0]=-1; algo[1]=-1; @@ -1427,10 +1427,10 @@ char DECLSPECDLLEXPORT *tng_compress_vel_find_algo(double *vel, int natoms, int } char DECLSPECDLLEXPORT *tng_compress_vel_float_find_algo(float *vel, int natoms, int nframes, - float desired_precision, - int speed, - int *algo, - int *nitems) + float desired_precision, + int speed, + int *algo, + int *nitems) { algo[0]=-1; algo[1]=-1; @@ -1440,10 +1440,10 @@ char DECLSPECDLLEXPORT *tng_compress_vel_float_find_algo(float *vel, int natoms, } char DECLSPECDLLEXPORT *tng_compress_vel_int_find_algo(int *vel, int natoms, int nframes, - unsigned long prec_hi, unsigned long prec_lo, - int speed, - int *algo, - int *nitems) + unsigned long prec_hi, unsigned long prec_lo, + int speed, + int *algo, + int *nitems) { algo[0]=-1; algo[1]=-1; @@ -1453,8 +1453,8 @@ char DECLSPECDLLEXPORT *tng_compress_vel_int_find_algo(int *vel, int natoms, int } int DECLSPECDLLEXPORT tng_compress_inquire(char *data,int *vel, int *natoms, - int *nframes, double *precision, - int *algo) + int *nframes, double *precision, + int *algo) { int bufloc=0; fix_t prec_hi, prec_lo; @@ -1549,7 +1549,7 @@ static int tng_compress_uncompress_pos_gen(char *data,double *posd,float *posf,i /* The initial frame */ coder=Ptngc_coder_init(); rval=Ptngc_unpack_array(coder,(unsigned char*)data+bufloc,quant,natoms*3, - initial_coding,initial_coding_parameter,natoms); + initial_coding,initial_coding_parameter,natoms); Ptngc_coder_deinit(coder); if (rval) goto error; @@ -1561,21 +1561,21 @@ static int tng_compress_uncompress_pos_gen(char *data,double *posd,float *posf,i (initial_coding==TNG_COMPRESS_ALGO_POS_XTC3)) { if (posd) - unquantize(posd,natoms,1,PRECISION(*prec_hi,*prec_lo),quant); + unquantize(posd,natoms,1,PRECISION(*prec_hi,*prec_lo),quant); else if (posf) - unquantize_float(posf,natoms,1,(float)PRECISION(*prec_hi,*prec_lo),quant); + unquantize_float(posf,natoms,1,(float)PRECISION(*prec_hi,*prec_lo),quant); else if (posi) - memcpy(posi,quant,natoms*3*sizeof *posi); + memcpy(posi,quant,natoms*3*sizeof *posi); } else if ((initial_coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || - (initial_coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) + (initial_coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) { if (posd) - unquantize_intra_differences(posd,natoms,1,PRECISION(*prec_hi,*prec_lo),quant); + unquantize_intra_differences(posd,natoms,1,PRECISION(*prec_hi,*prec_lo),quant); else if (posf) - unquantize_intra_differences_float(posf,natoms,1,(float)PRECISION(*prec_hi,*prec_lo),quant); + unquantize_intra_differences_float(posf,natoms,1,(float)PRECISION(*prec_hi,*prec_lo),quant); else if (posi) - unquantize_intra_differences_int(posi,natoms,1,quant); + unquantize_intra_differences_int(posi,natoms,1,quant); unquant_intra_differences_first_frame(quant,natoms); } /* The remaining frames. */ @@ -1584,45 +1584,45 @@ static int tng_compress_uncompress_pos_gen(char *data,double *posd,float *posf,i bufloc+=4; coder=Ptngc_coder_init(); rval=Ptngc_unpack_array(coder,(unsigned char *)data+bufloc,quant+natoms*3,(nframes-1)*natoms*3, - coding,coding_parameter,natoms); + coding,coding_parameter,natoms); Ptngc_coder_deinit(coder); if (rval) - goto error; + goto error; if ((coding==TNG_COMPRESS_ALGO_POS_STOPBIT_INTER) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTER) || - (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTER)) - { - /* This requires that the first frame is already in one-to-one format, even if intra-frame - compression was done there. Therefore the unquant_intra_differences_first_frame should be called - before to convert it correctly. */ - if (posd) - unquantize_inter_differences(posd,natoms,nframes,PRECISION(*prec_hi,*prec_lo),quant); - else if (posf) - unquantize_inter_differences_float(posf,natoms,nframes,(float)PRECISION(*prec_hi,*prec_lo),quant); - else if (posi) - unquantize_inter_differences_int(posi,natoms,nframes,quant); - } + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTER) || + (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTER)) + { + /* This requires that the first frame is already in one-to-one format, even if intra-frame + compression was done there. Therefore the unquant_intra_differences_first_frame should be called + before to convert it correctly. */ + if (posd) + unquantize_inter_differences(posd,natoms,nframes,PRECISION(*prec_hi,*prec_lo),quant); + else if (posf) + unquantize_inter_differences_float(posf,natoms,nframes,(float)PRECISION(*prec_hi,*prec_lo),quant); + else if (posi) + unquantize_inter_differences_int(posi,natoms,nframes,quant); + } else if ((coding==TNG_COMPRESS_ALGO_POS_XTC2) || - (coding==TNG_COMPRESS_ALGO_POS_XTC3) || - (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) - { - if (posd) - unquantize(posd+natoms*3,natoms,nframes-1,PRECISION(*prec_hi,*prec_lo),quant+natoms*3); - else if (posf) - unquantize_float(posf+natoms*3,natoms,nframes-1,(float)PRECISION(*prec_hi,*prec_lo),quant+natoms*3); - else if (posi) - memcpy(posi+natoms*3,quant+natoms*3,natoms*3*(nframes-1)*sizeof *posi); - } + (coding==TNG_COMPRESS_ALGO_POS_XTC3) || + (coding==TNG_COMPRESS_ALGO_POS_TRIPLET_ONETOONE)) + { + if (posd) + unquantize(posd+natoms*3,natoms,nframes-1,PRECISION(*prec_hi,*prec_lo),quant+natoms*3); + else if (posf) + unquantize_float(posf+natoms*3,natoms,nframes-1,(float)PRECISION(*prec_hi,*prec_lo),quant+natoms*3); + else if (posi) + memcpy(posi+natoms*3,quant+natoms*3,natoms*3*(nframes-1)*sizeof *posi); + } else if ((coding==TNG_COMPRESS_ALGO_POS_TRIPLET_INTRA) || - (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) - { - if (posd) - unquantize_intra_differences(posd+natoms*3,natoms,nframes-1,PRECISION(*prec_hi,*prec_lo),quant+natoms*3); - else if (posf) - unquantize_intra_differences_float(posf+natoms*3,natoms,nframes-1,(float)PRECISION(*prec_hi,*prec_lo),quant+natoms*3); - else if (posi) - unquantize_intra_differences_int(posi+natoms*3,natoms,nframes-1,quant+natoms*3); - } + (coding==TNG_COMPRESS_ALGO_POS_BWLZH_INTRA)) + { + if (posd) + unquantize_intra_differences(posd+natoms*3,natoms,nframes-1,PRECISION(*prec_hi,*prec_lo),quant+natoms*3); + else if (posf) + unquantize_intra_differences_float(posf+natoms*3,natoms,nframes-1,(float)PRECISION(*prec_hi,*prec_lo),quant+natoms*3); + else if (posi) + unquantize_intra_differences_int(posi+natoms*3,natoms,nframes-1,quant+natoms*3); + } } error: free(quant); @@ -1696,7 +1696,7 @@ static int tng_compress_uncompress_vel_gen(char *data,double *veld,float *velf,i /* The initial frame */ coder=Ptngc_coder_init(); rval=Ptngc_unpack_array(coder,(unsigned char*)data+bufloc,quant,natoms*3, - initial_coding,initial_coding_parameter,natoms); + initial_coding,initial_coding_parameter,natoms); Ptngc_coder_deinit(coder); if (rval) goto error; @@ -1708,11 +1708,11 @@ static int tng_compress_uncompress_vel_gen(char *data,double *veld,float *velf,i (initial_coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) { if (veld) - unquantize(veld,natoms,1,PRECISION(*prec_hi,*prec_lo),quant); + unquantize(veld,natoms,1,PRECISION(*prec_hi,*prec_lo),quant); else if (velf) - unquantize_float(velf,natoms,1,(float)PRECISION(*prec_hi,*prec_lo),quant); + unquantize_float(velf,natoms,1,(float)PRECISION(*prec_hi,*prec_lo),quant); else if (veli) - memcpy(veli,quant,natoms*3*sizeof *veli); + memcpy(veli,quant,natoms*3*sizeof *veli); } /* The remaining frames. */ if (nframes>1) @@ -1720,35 +1720,35 @@ static int tng_compress_uncompress_vel_gen(char *data,double *veld,float *velf,i bufloc+=4; coder=Ptngc_coder_init(); rval=Ptngc_unpack_array(coder,(unsigned char *)data+bufloc,quant+natoms*3,(nframes-1)*natoms*3, - coding,coding_parameter,natoms); + coding,coding_parameter,natoms); Ptngc_coder_deinit(coder); if (rval) - goto error; + goto error; /* Inter-frame compression? */ if ((coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_INTER) || - (coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER) || - (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_INTER)) - { - /* This requires that the first frame is already in one-to-one format. */ - if (veld) - unquantize_inter_differences(veld,natoms,nframes,PRECISION(*prec_hi,*prec_lo),quant); - else if (velf) - unquantize_inter_differences_float(velf,natoms,nframes,(float)PRECISION(*prec_hi,*prec_lo),quant); - else if (veli) - unquantize_inter_differences_int(veli,natoms,nframes,quant); - } + (coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_INTER) || + (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_INTER)) + { + /* This requires that the first frame is already in one-to-one format. */ + if (veld) + unquantize_inter_differences(veld,natoms,nframes,PRECISION(*prec_hi,*prec_lo),quant); + else if (velf) + unquantize_inter_differences_float(velf,natoms,nframes,(float)PRECISION(*prec_hi,*prec_lo),quant); + else if (veli) + unquantize_inter_differences_int(veli,natoms,nframes,quant); + } /* One-to-one compression? */ else if ((coding==TNG_COMPRESS_ALGO_VEL_STOPBIT_ONETOONE) || - (coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE) || - (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) - { - if (veld) - unquantize(veld+natoms*3,natoms,nframes-1,PRECISION(*prec_hi,*prec_lo),quant+natoms*3); - else if (velf) - unquantize_float(velf+natoms*3,natoms,nframes-1,(float)PRECISION(*prec_hi,*prec_lo),quant+natoms*3); - else if (veli) - memcpy(veli+natoms*3,quant+natoms*3,natoms*3*(nframes-1)*sizeof *veli); - } + (coding==TNG_COMPRESS_ALGO_VEL_TRIPLET_ONETOONE) || + (coding==TNG_COMPRESS_ALGO_VEL_BWLZH_ONETOONE)) + { + if (veld) + unquantize(veld+natoms*3,natoms,nframes-1,PRECISION(*prec_hi,*prec_lo),quant+natoms*3); + else if (velf) + unquantize_float(velf+natoms*3,natoms,nframes-1,(float)PRECISION(*prec_hi,*prec_lo),quant+natoms*3); + else if (veli) + memcpy(veli+natoms*3,quant+natoms*3,natoms*3*(nframes-1)*sizeof *veli); + } } error: free(quant); @@ -1811,15 +1811,15 @@ int DECLSPECDLLEXPORT tng_compress_uncompress_int(char *data,int *posvel, unsign } void DECLSPECDLLEXPORT tng_compress_int_to_double(int *posvel_int,unsigned long prec_hi, unsigned long prec_lo, - int natoms,int nframes, - double *posvel_double) + int natoms,int nframes, + double *posvel_double) { unquantize(posvel_double,natoms,nframes,PRECISION(prec_hi,prec_lo),posvel_int); } void DECLSPECDLLEXPORT tng_compress_int_to_float(int *posvel_int,unsigned long prec_hi, unsigned long prec_lo, - int natoms,int nframes, - float *posvel_float) + int natoms,int nframes, + float *posvel_float) { unquantize_float(posvel_float,natoms,nframes,(float)PRECISION(prec_hi,prec_lo),posvel_int); } diff --git a/src/compression/vals16.c b/src/compression/vals16.c index df44d8d..10a1cd8 100644 --- a/src/compression/vals16.c +++ b/src/compression/vals16.c @@ -16,29 +16,29 @@ /* Coding 32 bit ints in sequences of 16 bit ints. Worst case the output is 3*nvals long. */ void Ptngc_comp_conv_to_vals16(unsigned int *vals,int nvals, - unsigned int *vals16, int *nvals16) + unsigned int *vals16, int *nvals16) { int i; int j=0; for (i=0; i<nvals; i++) { if (vals[i]<=0x7FFFU) - vals16[j++]=vals[i]; + vals16[j++]=vals[i]; else - { - unsigned int lo=(vals[i]&0x7FFFU)|0x8000U; - unsigned int hi=vals[i]>>15; - vals16[j++]=lo; - if (hi<=0x7FFFU) - vals16[j++]=hi; - else - { - unsigned int lohi=(hi&0x7FFFU)|0x8000U; - unsigned int hihi=hi>>15; - vals16[j++]=lohi; - vals16[j++]=hihi; - } - } + { + unsigned int lo=(vals[i]&0x7FFFU)|0x8000U; + unsigned int hi=vals[i]>>15; + vals16[j++]=lo; + if (hi<=0x7FFFU) + vals16[j++]=hi; + else + { + unsigned int lohi=(hi&0x7FFFU)|0x8000U; + unsigned int hihi=hi>>15; + vals16[j++]=lohi; + vals16[j++]=hihi; + } + } } #if 0 /* Test that things that detect that this is bad really works. */ @@ -48,26 +48,26 @@ void Ptngc_comp_conv_to_vals16(unsigned int *vals,int nvals, } void Ptngc_comp_conv_from_vals16(unsigned int *vals16,int nvals16, - unsigned int *vals, int *nvals) + unsigned int *vals, int *nvals) { int i=0; int j=0; while (i<nvals16) { if (vals16[i]<=0x7FFFU) - vals[j++]=vals16[i++]; + vals[j++]=vals16[i++]; else - { - unsigned int lo=vals16[i++]; - unsigned int hi=vals16[i++]; - if (hi<=0x7FFFU) - vals[j++]=(lo&0x7FFFU)|(hi<<15); - else - { - unsigned int hihi=vals16[i++]; - vals[j++]=(lo&0x7FFFU)|((hi&0x7FFFU)<<15)|(hihi<<30); - } - } + { + unsigned int lo=vals16[i++]; + unsigned int hi=vals16[i++]; + if (hi<=0x7FFFU) + vals[j++]=(lo&0x7FFFU)|(hi<<15); + else + { + unsigned int hihi=vals16[i++]; + vals[j++]=(lo&0x7FFFU)|((hi&0x7FFFU)<<15)|(hihi<<30); + } + } } *nvals=j; } diff --git a/src/compression/widemuldiv.c b/src/compression/widemuldiv.c index dfc88b5..44d4adb 100644 --- a/src/compression/widemuldiv.c +++ b/src/compression/widemuldiv.c @@ -38,9 +38,9 @@ void Ptngc_widemul(unsigned int i1, unsigned int i2, unsigned int *ohi, unsigned { #if defined(TRAJNG_X86_GCC_INLINE_MULDIV) __asm__ __volatile__ ("mull %%edx\n\t" - : "=a" (i1), "=d" (i2) - : "a" (i1),"d" (i2) - : "cc"); + : "=a" (i1), "=d" (i2) + : "a" (i1),"d" (i2) + : "cc"); *ohi=i2; *olo=i1; #else /* TRAJNG X86 GCC INLINE MULDIV */ @@ -56,7 +56,7 @@ void Ptngc_widemul(unsigned int i1, unsigned int i2, unsigned int *ohi, unsigned unsigned int a_U,a_L; /* upper and lower half of a */ unsigned int b_U,b_L; /* upper and lower half of b */ - + unsigned int x_UU,x_UL,x_LU,x_LL; /* temporary storage. */ unsigned int x,x_U,x_L; /* temporary storage. */ @@ -70,11 +70,11 @@ void Ptngc_widemul(unsigned int i1, unsigned int i2, unsigned int *ohi, unsigned x=a_L*b_L; x_LL=x & L_m; x_LU=x>>bits; - + x=a_U*b_L; x_LU+=x & L_m; x_UL=x>>bits; - + x=a_L*b_U; x_LU+=x & L_m; x_UL+=x>>bits; @@ -105,9 +105,9 @@ void Ptngc_widediv(unsigned int hi, unsigned int lo, unsigned int i, unsigned in { #if defined(TRAJNG_X86_GCC_INLINE_MULDIV) __asm__ __volatile__ ("divl %%ecx\n\t" - : "=a" (lo), "=d" (hi) - : "a" (lo),"d" (hi), "c" (i) - : "cc"); + : "=a" (lo), "=d" (hi) + : "a" (lo),"d" (hi), "c" (i) + : "cc"); *result=lo; *remainder=hi; #else /* TRAJNG X86 GCC INLINE MULDIV */ @@ -126,7 +126,7 @@ void Ptngc_widediv(unsigned int hi, unsigned int lo, unsigned int i, unsigned in unsigned int hibit=bits2-1U; unsigned int hibit_mask=1U<<hibit; unsigned int allbits=(hibit_mask-1U)|hibit_mask; - + /* Do division. */ rmask=hibit_mask; res=0; @@ -135,26 +135,26 @@ void Ptngc_widediv(unsigned int hi, unsigned int lo, unsigned int i, unsigned in while (rmask) { if ((s_U<hi) || ((s_U==hi) && (s_L<=lo))) - { - /* Subtract */ - hi-=s_U; - if (s_L>lo) - { - unsigned int t; - hi--; /* Borrow */ - t=allbits-s_L; - lo+=t+1; - } - else - lo-=s_L; - - /* Set bit. */ - res|=rmask; - } + { + /* Subtract */ + hi-=s_U; + if (s_L>lo) + { + unsigned int t; + hi--; /* Borrow */ + t=allbits-s_L; + lo+=t+1; + } + else + lo-=s_L; + + /* Set bit. */ + res|=rmask; + } rmask>>=1; s_L>>=1; if (s_U & 1U) - s_L|=hibit_mask; + s_L|=hibit_mask; s_U>>=1; } *remainder=lo; @@ -179,7 +179,7 @@ static void largeint_add_gen(unsigned int v1, unsigned int *largeint, int n, int v2=(largeint[j]+carry)&0xFFFFFFFFU; carry=0; if ((((unsigned int)-1)&0xFFFFFFFFU) -1<largeint[j]) - carry=1; + carry=1; largeint[j]=v2; j++; } @@ -200,13 +200,13 @@ void Ptngc_largeint_mul(unsigned int v1, unsigned int *largeint_in, unsigned int for (i=0; i<n; i++) { if (largeint_in[i]!=0U) - { - unsigned int lo,hi; - Ptngc_widemul(v1,largeint_in[i],&hi,&lo); /* 32x32->64 mul */ - largeint_add_gen(lo,largeint_out,n,i); - if (i+1<n) - largeint_add_gen(hi,largeint_out,n,i+1); - } + { + unsigned int lo,hi; + Ptngc_widemul(v1,largeint_in[i],&hi,&lo); /* 32x32->64 mul */ + largeint_add_gen(lo,largeint_out,n,i); + if (i+1<n) + largeint_add_gen(hi,largeint_out,n,i+1); + } } } diff --git a/src/compression/xtc2.c b/src/compression/xtc2.c index 635faf5..d0e0c09 100644 --- a/src/compression/xtc2.c +++ b/src/compression/xtc2.c @@ -222,17 +222,17 @@ static const int seq_instr[MAXINSTR][2]= { { 1,1 }, /* 1 : one large atom + runlength encoded small integers. Use same settings as before. */ { 0,2 }, /* 00 : set base and runlength in next four bits (x). base (increase/keep/decrease)=x%3-1. runlength=1+x/3. - The special value 1111 in the four bits means runlength=6 and base change=0 */ + The special value 1111 in the four bits means runlength=6 and base change=0 */ { 4,4 }, /* 0100 : next only a large atom comes. */ { 5,4 }, /* 0101 : next only runlength encoded small integers. Use same settings as before. */ { 6,4 }, /* 0110 : Large change in base. Change is encoded in the - following 2 bits. change direction (sign) is the first - bit. The next bit +1 is the actual change. This - allows the change of up to +/- 2 indices. */ + following 2 bits. change direction (sign) is the first + bit. The next bit +1 is the actual change. This + allows the change of up to +/- 2 indices. */ { 14,5 }, /* 01110 : flip whether integers should be modified to compress water better */ { 15,5 }, /* 01111 : Large rle. The next 4 bits encode how many - large atoms are in the following sequence: 3-18. (2 is - more efficiently coded with two large instructions. */ + large atoms are in the following sequence: 3-18. (2 is + more efficiently coded with two large instructions. */ }; static void write_instruction(struct coder *coder,int instr,unsigned char **output_ptr) @@ -258,13 +258,13 @@ static unsigned int readbits(unsigned char **ptr, int *bitptr, int nbits) *bitptr=(*bitptr)+1; extract_mask>>=1; if (!extract_mask) - { - extract_mask=0x80U; - *ptr=(*ptr)+1; - *bitptr=0; - if (nbits) - thisval=**ptr; - } + { + extract_mask=0x80U; + *ptr=(*ptr)+1; + *bitptr=0; + if (nbits) + thisval=**ptr; + } } #ifdef SHOWIT fprintf(stderr,"%x\n",val); @@ -301,25 +301,25 @@ static int read_instruction(unsigned char **ptr, int *bitptr) { bits=readbits(ptr,bitptr,1); if (!bits) - instr=INSTR_BASE_RUNLENGTH; + instr=INSTR_BASE_RUNLENGTH; else - { - bits=readbits(ptr,bitptr,2); - if (bits==0) - instr=INSTR_ONLY_LARGE; - else if (bits==1) - instr=INSTR_ONLY_SMALL; - else if (bits==2) - instr=INSTR_LARGE_BASE_CHANGE; - else if (bits==3) - { - bits=readbits(ptr,bitptr,1); - if (bits==0) - instr=INSTR_FLIP; - else - instr=INSTR_LARGE_RLE; - } - } + { + bits=readbits(ptr,bitptr,2); + if (bits==0) + instr=INSTR_ONLY_LARGE; + else if (bits==1) + instr=INSTR_ONLY_SMALL; + else if (bits==2) + instr=INSTR_LARGE_BASE_CHANGE; + else if (bits==3) + { + bits=readbits(ptr,bitptr,1); + if (bits==0) + instr=INSTR_FLIP; + else + instr=INSTR_LARGE_RLE; + } + } } return instr; } @@ -346,12 +346,12 @@ static void swap_is_better(int *input, int *minint, int *sum_normal, int *sum_sw normal[2]=input[6+i]-input[3+i]; /* minint[i]-minint[i] cancels out */ swap_ints(normal,swapped); for (j=1; j<3; j++) - { - if (positive_int(normal[j])>(unsigned int)normal_max) - normal_max=positive_int(normal[j]); - if (positive_int(swapped[j])>(unsigned int)swapped_max) - swapped_max=positive_int(swapped[j]); - } + { + if (positive_int(normal[j])>(unsigned int)normal_max) + normal_max=positive_int(normal[j]); + if (positive_int(swapped[j])>(unsigned int)swapped_max) + swapped_max=positive_int(swapped[j]); + } } if (normal_max==0) normal_max=1; @@ -381,21 +381,21 @@ static void swapdecide(struct coder *coder, int *input,int *swapatoms, int *larg ((normal<swapped) && (fabs((double)normal/swapped)<iflipgaincheck))) { if (swapped<normal) - { - if (!*swapatoms) - { - *swapatoms=1; - didswap=1; - } - } + { + if (!*swapatoms) + { + *swapatoms=1; + didswap=1; + } + } else - { - if (*swapatoms) - { - *swapatoms=0; - didswap=1; - } - } + { + if (*swapatoms) + { + *swapatoms=0; + didswap=1; + } + } } if (didswap) { @@ -417,12 +417,12 @@ static int compute_magic_bits(int *index) for (i=0; i<3; i++) { if (i!=0) - { - /* We must do the multiplication of the largeint with the integer base */ - Ptngc_largeint_mul(magic[index[i]],largeint,largeint_tmp,4); - for (j=0; j<4; j++) - largeint[j]=largeint_tmp[j]; - } + { + /* We must do the multiplication of the largeint with the integer base */ + Ptngc_largeint_mul(magic[index[i]],largeint,largeint_tmp,4); + for (j=0; j<4; j++) + largeint[j]=largeint_tmp[j]; + } Ptngc_largeint_add(magic[index[i]]-1,largeint,4); } /* Find last bit. */ @@ -433,7 +433,7 @@ static int compute_magic_bits(int *index) for (i=0; i<3; i++) for (j=0; j<32; j++) if (largeint[i]&(1U<<j)) - onebit=i*32+j+1; + onebit=i*32+j+1; return onebit; } @@ -451,12 +451,12 @@ static void trajcoder_base_compress(int *input, int n, int *index, unsigned char for (i=0; i<n; i++) { if (i!=0) - { - /* We must do the multiplication of the largeint with the integer base */ - Ptngc_largeint_mul(magic[index[i%3]],largeint,largeint_tmp,19); - for (j=0; j<19; j++) - largeint[j]=largeint_tmp[j]; - } + { + /* We must do the multiplication of the largeint with the integer base */ + Ptngc_largeint_mul(magic[index[i%3]],largeint,largeint_tmp,19); + for (j=0; j<19; j++) + largeint[j]=largeint_tmp[j]; + } Ptngc_largeint_add(input[i],largeint,19); } if (largeint[18]) @@ -475,10 +475,10 @@ static void trajcoder_base_compress(int *input, int n, int *index, unsigned char { int shift=0; for (j=0; j<4; j++) - { - result[i*4+j]=(largeint[i]>>shift) & 0xFFU; - shift+=8; - } + { + result[i*4+j]=(largeint[i]>>shift) & 0xFFU; + shift+=8; + } } } @@ -494,10 +494,10 @@ static void trajcoder_base_decompress(unsigned char *input, int n, int *index, i int shift=0; largeint[i]=0U; for (j=0; j<4; j++) - { - largeint[i]|=((unsigned int)input[i*4+j])<<shift; - shift+=8; - } + { + largeint[i]|=((unsigned int)input[i*4+j])<<shift; + shift+=8; + } } largeint[18]=0U; #if 0 @@ -516,7 +516,7 @@ static void trajcoder_base_decompress(unsigned char *input, int n, int *index, i #endif #if 0 for (j=0; j<19; j++) - largeint[j]=largeint_tmp[j]; + largeint[j]=largeint_tmp[j]; #endif memcpy(largeint,largeint_tmp,19*sizeof *largeint); output[i]=remainder; @@ -535,11 +535,11 @@ static int is_quite_large(int *input, int small_index, int max_large_index) else { for (i=0; i<3; i++) - if (positive_int(input[i])>magic[small_index+QUITE_LARGE]) - { - is=1; - break; - } + if (positive_int(input[i])>magic[small_index+QUITE_LARGE]) + { + is=1; + break; + } } return is; } @@ -574,21 +574,21 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord,int { int i; for (i=0; i<startenc; i++) - { - tmp_prevcoord[0]+=encode_ints[i*3]; - tmp_prevcoord[1]+=encode_ints[i*3+1]; - tmp_prevcoord[2]+=encode_ints[i*3+2]; + { + tmp_prevcoord[0]+=encode_ints[i*3]; + tmp_prevcoord[1]+=encode_ints[i*3+1]; + tmp_prevcoord[2]+=encode_ints[i*3+2]; #ifdef SHOWIT - fprintf(stderr,"%6d: %6d %6d %6d\t\t%6d %6d %6d\t\t%6d %6d %6d\n",i*3, - tmp_prevcoord[0],tmp_prevcoord[1],tmp_prevcoord[2], - encode_ints[i*3], - encode_ints[i*3+1], - encode_ints[i*3+2], - positive_int(encode_ints[i*3]), - positive_int(encode_ints[i*3+1]), - positive_int(encode_ints[i*3+2])); -#endif - } + fprintf(stderr,"%6d: %6d %6d %6d\t\t%6d %6d %6d\t\t%6d %6d %6d\n",i*3, + tmp_prevcoord[0],tmp_prevcoord[1],tmp_prevcoord[2], + encode_ints[i*3], + encode_ints[i*3+1], + encode_ints[i*3+2], + positive_int(encode_ints[i*3]), + positive_int(encode_ints[i*3+1]), + positive_int(encode_ints[i*3+2])); +#endif + } } #ifdef SHOWIT @@ -601,15 +601,15 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord,int encode_ints[nencode+2]=input_ptr[nencode+2]-minint[2]-tmp_prevcoord[2]; #ifdef SHOWIT fprintf(stderr,"%6d: %6d %6d %6d\t\t%6d %6d %6d\t\t%6d %6d %6d\n",nencode, - input_ptr[nencode]-minint[0], - input_ptr[nencode+1]-minint[1], - input_ptr[nencode+2]-minint[2], - encode_ints[nencode], - encode_ints[nencode+1], - encode_ints[nencode+2], - positive_int(encode_ints[nencode]), - positive_int(encode_ints[nencode+1]), - positive_int(encode_ints[nencode+2])); + input_ptr[nencode]-minint[0], + input_ptr[nencode+1]-minint[1], + input_ptr[nencode+2]-minint[2], + encode_ints[nencode], + encode_ints[nencode+1], + encode_ints[nencode+2], + positive_int(encode_ints[nencode]), + positive_int(encode_ints[nencode+1]), + positive_int(encode_ints[nencode+2])); #endif tmp_prevcoord[0]=input_ptr[nencode]-minint[0]; tmp_prevcoord[1]=input_ptr[nencode+1]-minint[1]; @@ -620,17 +620,17 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord,int } static void flush_large(struct coder *coder, int *has_large, int *has_large_ints, int n, - int *large_index, int large_nbits, unsigned char *compress_buffer, - unsigned char **output_ptr) + int *large_index, int large_nbits, unsigned char *compress_buffer, + unsigned char **output_ptr) { int i; if (n<3) { for (i=0; i<n; i++) - { - write_instruction(coder,INSTR_ONLY_LARGE,output_ptr); - write_three_large(coder,has_large_ints+i*3,large_index,large_nbits,compress_buffer,output_ptr); - } + { + write_instruction(coder,INSTR_ONLY_LARGE,output_ptr); + write_three_large(coder,has_large_ints+i*3,large_index,large_nbits,compress_buffer,output_ptr); + } } else { @@ -640,26 +640,26 @@ static void flush_large(struct coder *coder, int *has_large, int *has_large_ints write_instruction(coder,INSTR_LARGE_RLE,output_ptr); Ptngc_writebits(coder,n-3,4,output_ptr); /* Number of large atoms in this sequence: 3 to 18 */ for (i=0; i<n; i++) - write_three_large(coder,has_large_ints+i*3,large_index,large_nbits,compress_buffer,output_ptr); + write_three_large(coder,has_large_ints+i*3,large_index,large_nbits,compress_buffer,output_ptr); } if (((*has_large)-n)!=0) { int j; for (i=0; i<(*has_large)-n; i++) - for (j=0; j<3; j++) - has_large_ints[i*3+j]=has_large_ints[(i+n)*3+j]; + for (j=0; j<3; j++) + has_large_ints[i*3+j]=has_large_ints[(i+n)*3+j]; } *has_large=(*has_large)-n; /* Number of remaining large atoms in buffer */ } static void buffer_large(struct coder *coder, int *has_large, int *has_large_ints, int *new_large_ints, - int *large_index, int large_nbits, unsigned char *compress_buffer, - unsigned char **output_ptr) + int *large_index, int large_nbits, unsigned char *compress_buffer, + unsigned char **output_ptr) { /* If it is full we must write them all. */ if (*has_large==18) flush_large(coder,has_large,has_large_ints, *has_large, - large_index,large_nbits,compress_buffer,output_ptr); /* Flush them all. */ + large_index,large_nbits,compress_buffer,output_ptr); /* Flush them all. */ has_large_ints[(*has_large)*3]=new_large_ints[0]; has_large_ints[(*has_large)*3+1]=new_large_ints[1]; has_large_ints[(*has_large)*3+2]=new_large_ints[2]; @@ -687,10 +687,10 @@ unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length int has_large_ints[54]; /* Large cache. Up to 18 large atoms. */ int prevcoord[3]; int runlength=0; /* Initial runlength. "Stupidly" set to zero for - simplicity and explicity */ + simplicity and explicity */ int swapatoms=0; /* Initial guess is that we should not swap the - first two atoms in each large+small - transition */ + first two atoms in each large+small + transition */ int didswap; /* Whether swapping was actually done. */ int *input_ptr=input; int encode_ints[21]; /* Up to 3 large + 18 small ints can be encoded at once */ @@ -713,10 +713,10 @@ unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length for (i=1; i<ntriplets; i++) for (j=0; j<3; j++) { - if (input[i*3+j]>maxint[j]) - maxint[j]=input[i*3+j]; - if (input[i*3+j]<minint[j]) - minint[j]=input[i*3+j]; + if (input[i*3+j]>maxint[j]) + maxint[j]=input[i*3+j]; + if (input[i*3+j]<minint[j]) + minint[j]=input[i*3+j]; } large_index[0]=Ptngc_find_magic_index(maxint[0]-minint[0]+1); @@ -732,7 +732,7 @@ unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length #ifdef SHOWIT for (j=0; j<3; j++) fprintf(stderr,"minint[%d]=%d. maxint[%d]=%d large_index[%d]=%d value=%d\n",j,minint[j],j,maxint[j], - j,large_index[j],magic[large_index[j]]); + j,large_index[j],magic[large_index[j]]); fprintf(stderr,"large_nbits=%d\n",large_nbits); #endif @@ -749,8 +749,8 @@ unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length int item=input[i]; int s=positive_int(item); if (s>intmax) - if (s<max_small) - intmax=s; + if (s<max_small) + intmax=s; } /* This value is not critical, since if I guess wrong, the code will just insert instructions to increase this value. */ @@ -788,10 +788,10 @@ unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length #ifdef SHOWIT for (i=0; i<ntriplets_left; i++) fprintf(stderr,"VALUE:%d %6d %6d %6d\n", - i, - input_ptr[i*3], - input_ptr[i*3+1], - input_ptr[i*3+2]); + i, + input_ptr[i*3], + input_ptr[i*3+1], + input_ptr[i*3+2]); #endif #endif @@ -803,455 +803,455 @@ unsigned char *Ptngc_pack_array_xtc2(struct coder *coder,int *input, int *length while (ntriplets_left) { if (ntriplets_left<0) - { - fprintf(stderr,"TRAJNG: BUG! ntriplets_left<0!\n"); - exit(EXIT_FAILURE); - } + { + fprintf(stderr,"TRAJNG: BUG! ntriplets_left<0!\n"); + exit(EXIT_FAILURE); + } /* If only less than three atoms left we just write them all as large integers. Here no swapping is done! */ if (ntriplets_left<3) - { - for (ienc=0; ienc<ntriplets_left; ienc++) - { - int jenc; - for (jenc=0; jenc<3; jenc++) - encode_ints[jenc]=input_ptr[ienc*3+jenc]-minint[jenc]; - buffer_large(coder,&has_large,has_large_ints,encode_ints,large_index,large_nbits,compress_buffer,&output_ptr); - input_ptr+=3; - ntriplets_left--; - } - flush_large(coder,&has_large,has_large_ints,has_large,large_index,large_nbits,compress_buffer,&output_ptr); - } + { + for (ienc=0; ienc<ntriplets_left; ienc++) + { + int jenc; + for (jenc=0; jenc<3; jenc++) + encode_ints[jenc]=input_ptr[ienc*3+jenc]-minint[jenc]; + buffer_large(coder,&has_large,has_large_ints,encode_ints,large_index,large_nbits,compress_buffer,&output_ptr); + input_ptr+=3; + ntriplets_left--; + } + flush_large(coder,&has_large,has_large_ints,has_large,large_index,large_nbits,compress_buffer,&output_ptr); + } else - { - int min_runlength=0; - int largest_required_base; - int largest_runlength_base; - int largest_required_index; - int largest_runlength_index; - int new_runlength; - int new_small_index; - int iter_runlength; - int iter_small_index; - int rle_index_dep; - didswap=0; - /* Insert the next batch of integers to be encoded into the buffer */ + { + int min_runlength=0; + int largest_required_base; + int largest_runlength_base; + int largest_required_index; + int largest_runlength_index; + int new_runlength; + int new_small_index; + int iter_runlength; + int iter_small_index; + int rle_index_dep; + didswap=0; + /* Insert the next batch of integers to be encoded into the buffer */ #ifdef SHOWIT - fprintf(stderr,"Initial batch\n"); -#endif - insert_batch(input_ptr,ntriplets_left,prevcoord,minint,encode_ints,0,&nencode); - - /* First we must decide if the next value is large (does not reasonably fit in current small encoding) - Also, if we have not written any values yet, we must begin by writing a large atom. */ - if ((input_ptr==input) || (is_quite_large(encode_ints,small_index,max_large_index)) || (refused)) - { - /* If any of the next two atoms are large we should probably write them as large and not swap them */ - int no_swap=0; - if ((is_quite_large(encode_ints+3,small_index,max_large_index)) || (is_quite_large(encode_ints+6,small_index,max_large_index))) - no_swap=1; - if (!no_swap) - { - /* Next we must decide if we should swap the first - two values. */ + fprintf(stderr,"Initial batch\n"); +#endif + insert_batch(input_ptr,ntriplets_left,prevcoord,minint,encode_ints,0,&nencode); + + /* First we must decide if the next value is large (does not reasonably fit in current small encoding) + Also, if we have not written any values yet, we must begin by writing a large atom. */ + if ((input_ptr==input) || (is_quite_large(encode_ints,small_index,max_large_index)) || (refused)) + { + /* If any of the next two atoms are large we should probably write them as large and not swap them */ + int no_swap=0; + if ((is_quite_large(encode_ints+3,small_index,max_large_index)) || (is_quite_large(encode_ints+6,small_index,max_large_index))) + no_swap=1; + if (!no_swap) + { + /* Next we must decide if we should swap the first + two values. */ #if 1 - swapdecide(coder,input_ptr,&swapatoms,large_index,minint,&output_ptr); + swapdecide(coder,input_ptr,&swapatoms,large_index,minint,&output_ptr); #else - swapatoms=0; -#endif - /* If we should do the integer swapping manipulation we should do it now */ - if (swapatoms) - { - didswap=1; - for (i=0; i<3; i++) - { - int in[3], out[3]; - in[0]=input_ptr[i]-minint[i]; - in[1]=input_ptr[3+i]-input_ptr[i]; /* minint[i]-minint[i] cancels out */ - in[2]=input_ptr[6+i]-input_ptr[3+i]; /* minint[i]-minint[i] cancels out */ - swap_ints(in,out); - encode_ints[i]=out[0]; - encode_ints[3+i]=out[1]; - encode_ints[6+i]=out[2]; - } - /* We have swapped atoms, so the minimum run-length is 2 */ + swapatoms=0; +#endif + /* If we should do the integer swapping manipulation we should do it now */ + if (swapatoms) + { + didswap=1; + for (i=0; i<3; i++) + { + int in[3], out[3]; + in[0]=input_ptr[i]-minint[i]; + in[1]=input_ptr[3+i]-input_ptr[i]; /* minint[i]-minint[i] cancels out */ + in[2]=input_ptr[6+i]-input_ptr[3+i]; /* minint[i]-minint[i] cancels out */ + swap_ints(in,out); + encode_ints[i]=out[0]; + encode_ints[3+i]=out[1]; + encode_ints[6+i]=out[2]; + } + /* We have swapped atoms, so the minimum run-length is 2 */ #ifdef SHOWIT - fprintf(stderr,"Swap atoms results in:\n"); - for (i=0; i<3; i++) - fprintf(stderr,"%d: %6d %6d %6d\t\t%6d %6d %6d\n",i*3, - encode_ints[i*3], - encode_ints[i*3+1], - encode_ints[i*3+2], - positive_int(encode_ints[i*3]), - positive_int(encode_ints[i*3+1]), - positive_int(encode_ints[i*3+2])); - -#endif - min_runlength=2; - } - } - if ((swapatoms) && (didswap)) - { - for (ienc=0; ienc<3; ienc++) - prevcoord[ienc]=encode_ints[ienc]; - } - else - { - for (ienc=0; ienc<3; ienc++) - prevcoord[ienc]=input_ptr[ienc]-minint[ienc]; - } - /* Cache large value for later possible combination with - a sequence of small integers. */ - buffer_large(coder,&has_large,has_large_ints,prevcoord,large_index,large_nbits,compress_buffer,&output_ptr); + fprintf(stderr,"Swap atoms results in:\n"); + for (i=0; i<3; i++) + fprintf(stderr,"%d: %6d %6d %6d\t\t%6d %6d %6d\n",i*3, + encode_ints[i*3], + encode_ints[i*3+1], + encode_ints[i*3+2], + positive_int(encode_ints[i*3]), + positive_int(encode_ints[i*3+1]), + positive_int(encode_ints[i*3+2])); + +#endif + min_runlength=2; + } + } + if ((swapatoms) && (didswap)) + { + for (ienc=0; ienc<3; ienc++) + prevcoord[ienc]=encode_ints[ienc]; + } + else + { + for (ienc=0; ienc<3; ienc++) + prevcoord[ienc]=input_ptr[ienc]-minint[ienc]; + } + /* Cache large value for later possible combination with + a sequence of small integers. */ + buffer_large(coder,&has_large,has_large_ints,prevcoord,large_index,large_nbits,compress_buffer,&output_ptr); #ifdef SHOWIT - fprintf(stderr,"Prevcoord after packing of large: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Prevcoord after packing of large: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); #endif - /* We have written a large integer so we have one less atoms to worry about */ - input_ptr+=3; - ntriplets_left--; + /* We have written a large integer so we have one less atoms to worry about */ + input_ptr+=3; + ntriplets_left--; - refused=0; + refused=0; - /* Insert the next batch of integers to be encoded into the buffer */ + /* Insert the next batch of integers to be encoded into the buffer */ #ifdef SHOWIT - fprintf(stderr,"Update batch due to large int.\n"); -#endif - if ((swapatoms) && (didswap)) - { - /* Keep swapped values. */ - for (i=0; i<2; i++) - for (ienc=0; ienc<3; ienc++) - encode_ints[i*3+ienc]=encode_ints[(i+1)*3+ienc]; - } - insert_batch(input_ptr,ntriplets_left,prevcoord,minint,encode_ints,min_runlength,&nencode); - } - /* Here we should only have differences for the atom coordinates. */ - /* Convert the ints to positive ints */ - for (ienc=0; ienc<nencode; ienc++) - { - int pint=positive_int(encode_ints[ienc]); - encode_ints[ienc]=pint; - } - /* Now we must decide what base and runlength to do. If we have swapped atoms it will be at least 2. - If even the next atom is large, we will not do anything. */ - largest_required_base=0; - /* Determine required base */ - for (ienc=0; ienc<min_runlength*3; ienc++) - if (encode_ints[ienc]>largest_required_base) - largest_required_base=encode_ints[ienc]; - /* Also compute what the largest base is for the current runlength setting! */ - largest_runlength_base=0; - for (ienc=0; (ienc<runlength*3) && (ienc<nencode); ienc++) - if (encode_ints[ienc]>largest_runlength_base) - largest_runlength_base=encode_ints[ienc]; - - largest_required_index=Ptngc_find_magic_index(largest_required_base); - largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); - - if (largest_required_index<largest_runlength_index) - { - new_runlength=min_runlength; - new_small_index=largest_required_index; - } - else - { - new_runlength=runlength; - new_small_index=largest_runlength_index; - } - - /* Only allow increase of runlength wrt min_runlength */ - if (new_runlength<min_runlength) - new_runlength=min_runlength; - - /* If the current runlength is longer than the number of - triplets left stop it from being so. */ - if (new_runlength>ntriplets_left) - new_runlength=ntriplets_left; - - /* We must at least try to get some small integers going. */ - if (new_runlength==0) - { - new_runlength=1; - new_small_index=small_index; - } - - iter_runlength=new_runlength; - iter_small_index=new_small_index; - - /* Iterate to find optimal encoding and runlength */ + fprintf(stderr,"Update batch due to large int.\n"); +#endif + if ((swapatoms) && (didswap)) + { + /* Keep swapped values. */ + for (i=0; i<2; i++) + for (ienc=0; ienc<3; ienc++) + encode_ints[i*3+ienc]=encode_ints[(i+1)*3+ienc]; + } + insert_batch(input_ptr,ntriplets_left,prevcoord,minint,encode_ints,min_runlength,&nencode); + } + /* Here we should only have differences for the atom coordinates. */ + /* Convert the ints to positive ints */ + for (ienc=0; ienc<nencode; ienc++) + { + int pint=positive_int(encode_ints[ienc]); + encode_ints[ienc]=pint; + } + /* Now we must decide what base and runlength to do. If we have swapped atoms it will be at least 2. + If even the next atom is large, we will not do anything. */ + largest_required_base=0; + /* Determine required base */ + for (ienc=0; ienc<min_runlength*3; ienc++) + if (encode_ints[ienc]>largest_required_base) + largest_required_base=encode_ints[ienc]; + /* Also compute what the largest base is for the current runlength setting! */ + largest_runlength_base=0; + for (ienc=0; (ienc<runlength*3) && (ienc<nencode); ienc++) + if (encode_ints[ienc]>largest_runlength_base) + largest_runlength_base=encode_ints[ienc]; + + largest_required_index=Ptngc_find_magic_index(largest_required_base); + largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); + + if (largest_required_index<largest_runlength_index) + { + new_runlength=min_runlength; + new_small_index=largest_required_index; + } + else + { + new_runlength=runlength; + new_small_index=largest_runlength_index; + } + + /* Only allow increase of runlength wrt min_runlength */ + if (new_runlength<min_runlength) + new_runlength=min_runlength; + + /* If the current runlength is longer than the number of + triplets left stop it from being so. */ + if (new_runlength>ntriplets_left) + new_runlength=ntriplets_left; + + /* We must at least try to get some small integers going. */ + if (new_runlength==0) + { + new_runlength=1; + new_small_index=small_index; + } + + iter_runlength=new_runlength; + iter_small_index=new_small_index; + + /* Iterate to find optimal encoding and runlength */ #ifdef SHOWIT - fprintf(stderr,"Entering iterative loop.\n"); - fflush(stderr); + fprintf(stderr,"Entering iterative loop.\n"); + fflush(stderr); #endif - do { - new_runlength=iter_runlength; - new_small_index=iter_small_index; + do { + new_runlength=iter_runlength; + new_small_index=iter_small_index; #ifdef SHOWIT - fprintf(stderr,"Test new_small_index=%d Base=%d\n",new_small_index,magic[new_small_index]); -#endif - /* What is the largest runlength - we can do with the currently - selected encoding? Also the max supported runlength is 6 triplets! */ - for (ienc=0; ienc<nencode && ienc<18; ienc++) - { - int test_index=Ptngc_find_magic_index(encode_ints[ienc]); - if (test_index>new_small_index) - break; - } - if (ienc/3>new_runlength) - { - iter_runlength=ienc/3; + fprintf(stderr,"Test new_small_index=%d Base=%d\n",new_small_index,magic[new_small_index]); +#endif + /* What is the largest runlength + we can do with the currently + selected encoding? Also the max supported runlength is 6 triplets! */ + for (ienc=0; ienc<nencode && ienc<18; ienc++) + { + int test_index=Ptngc_find_magic_index(encode_ints[ienc]); + if (test_index>new_small_index) + break; + } + if (ienc/3>new_runlength) + { + iter_runlength=ienc/3; #ifdef SHOWIT - fprintf(stderr,"I found a new possible runlength: %d\n",iter_runlength); -#endif - } - - /* How large encoding do we have to use? */ - largest_runlength_base=0; - for (ienc=0; ienc<iter_runlength*3; ienc++) - if (encode_ints[ienc]>largest_runlength_base) - largest_runlength_base=encode_ints[ienc]; - largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); - if (largest_runlength_index!=new_small_index) - { - iter_small_index=largest_runlength_index; + fprintf(stderr,"I found a new possible runlength: %d\n",iter_runlength); +#endif + } + + /* How large encoding do we have to use? */ + largest_runlength_base=0; + for (ienc=0; ienc<iter_runlength*3; ienc++) + if (encode_ints[ienc]>largest_runlength_base) + largest_runlength_base=encode_ints[ienc]; + largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); + if (largest_runlength_index!=new_small_index) + { + iter_small_index=largest_runlength_index; #ifdef SHOWIT - fprintf(stderr,"I found a new possible small index: %d Base=%d\n",iter_small_index,magic[iter_small_index]); + fprintf(stderr,"I found a new possible small index: %d Base=%d\n",iter_small_index,magic[iter_small_index]); #endif - } - } while ((new_runlength!=iter_runlength) || - (new_small_index!=iter_small_index)); + } + } while ((new_runlength!=iter_runlength) || + (new_small_index!=iter_small_index)); #ifdef SHOWIT - fprintf(stderr,"Exit iterative loop.\n"); - fflush(stderr); -#endif - - /* Verify that we got something good. We may have caught a - substantially larger atom. If so we should just bail - out and let the loop get on another lap. We may have a - minimum runlength though and then we have to fulfill - the request to write out these atoms! */ - rle_index_dep=0; - if (new_runlength<3) - rle_index_dep=IS_LARGE; - else if (new_runlength<6) - rle_index_dep=QUITE_LARGE; - if ((min_runlength) - || ((new_small_index<small_index+IS_LARGE) && (new_small_index+rle_index_dep<max_large_index)) + fprintf(stderr,"Exit iterative loop.\n"); + fflush(stderr); +#endif + + /* Verify that we got something good. We may have caught a + substantially larger atom. If so we should just bail + out and let the loop get on another lap. We may have a + minimum runlength though and then we have to fulfill + the request to write out these atoms! */ + rle_index_dep=0; + if (new_runlength<3) + rle_index_dep=IS_LARGE; + else if (new_runlength<6) + rle_index_dep=QUITE_LARGE; + if ((min_runlength) + || ((new_small_index<small_index+IS_LARGE) && (new_small_index+rle_index_dep<max_large_index)) #if 1 - || (new_small_index+IS_LARGE<max_large_index) + || (new_small_index+IS_LARGE<max_large_index) #endif ) - { - int nbits; - if ((new_runlength!=runlength) || (new_small_index!=small_index)) - { - int change=new_small_index-small_index; - - if (new_small_index<=0) - change=0; - - if (change<0) - { - int ixx; - for (ixx=0; ixx<new_runlength; ixx++) - { - int rejected; - do { - int ixyz; - double isum=0.; /* ints can be almost 32 bit so multiplication will overflow. So do doubles. */ - for (ixyz=0; ixyz<3; ixyz++) - { - /* encode_ints is already positive (and multiplied by 2 versus the original, just as magic ints) */ - double id=encode_ints[ixx*3+ixyz]; - isum+=id*id; - } - rejected=0; + { + int nbits; + if ((new_runlength!=runlength) || (new_small_index!=small_index)) + { + int change=new_small_index-small_index; + + if (new_small_index<=0) + change=0; + + if (change<0) + { + int ixx; + for (ixx=0; ixx<new_runlength; ixx++) + { + int rejected; + do { + int ixyz; + double isum=0.; /* ints can be almost 32 bit so multiplication will overflow. So do doubles. */ + for (ixyz=0; ixyz<3; ixyz++) + { + /* encode_ints is already positive (and multiplied by 2 versus the original, just as magic ints) */ + double id=encode_ints[ixx*3+ixyz]; + isum+=id*id; + } + rejected=0; #ifdef SHOWIT - fprintf(stderr,"Tested decrease %d of index: %g>=%g?\n",change,isum,(double)magic[small_index+change]*(double)magic[small_index+change]); + fprintf(stderr,"Tested decrease %d of index: %g>=%g?\n",change,isum,(double)magic[small_index+change]*(double)magic[small_index+change]); #endif - if (isum>(double)magic[small_index+change]*(double)magic[small_index+change]) - { + if (isum>(double)magic[small_index+change]*(double)magic[small_index+change]) + { #ifdef SHOWIT - fprintf(stderr,"Rejected decrease %d of index due to length of vector: %g>=%g\n",change,isum,(double)magic[small_index+change]*(double)magic[small_index+change]); -#endif - rejected=1; - change++; - } - } while ((change<0) && (rejected)); - if (change==0) - break; - } - } - - /* If the only thing to do is to change the base by - only one -1 it is probably not worth it. */ - if (!((change==-1) && (runlength==new_runlength))) - { - /* If we have a very short runlength we do not - want to do large base changes. It costs 6 - extra bits to do -2. We gain 2/3 - bits per value to decrease the index by -2, - ie 2 bits, so to any changes down we must - have a runlength of 3 or more to do it for - one molecule! If we have several molecules we - will gain of course, so not be so strict. */ - if ((change==-2) && (new_runlength<3)) - { - if (runlength==new_runlength) - change=0; - else - change=-1; + fprintf(stderr,"Rejected decrease %d of index due to length of vector: %g>=%g\n",change,isum,(double)magic[small_index+change]*(double)magic[small_index+change]); +#endif + rejected=1; + change++; + } + } while ((change<0) && (rejected)); + if (change==0) + break; + } + } + + /* If the only thing to do is to change the base by + only one -1 it is probably not worth it. */ + if (!((change==-1) && (runlength==new_runlength))) + { + /* If we have a very short runlength we do not + want to do large base changes. It costs 6 + extra bits to do -2. We gain 2/3 + bits per value to decrease the index by -2, + ie 2 bits, so to any changes down we must + have a runlength of 3 or more to do it for + one molecule! If we have several molecules we + will gain of course, so not be so strict. */ + if ((change==-2) && (new_runlength<3)) + { + if (runlength==new_runlength) + change=0; + else + change=-1; #ifdef SHOWIT - fprintf(stderr,"Rejected change by -2 due to too short runlenght. Change set to %d\n",change); -#endif - } - - /* First adjust base using large base change instruction (if necessary) */ - while ((change>1) || (change<-1) || ((new_runlength==6) && (change))) - { - unsigned int code=0U; - int this_change=change; - if (this_change>2) - this_change=2; - if (this_change<-2) - this_change=-2; - change-=this_change; + fprintf(stderr,"Rejected change by -2 due to too short runlenght. Change set to %d\n",change); +#endif + } + + /* First adjust base using large base change instruction (if necessary) */ + while ((change>1) || (change<-1) || ((new_runlength==6) && (change))) + { + unsigned int code=0U; + int this_change=change; + if (this_change>2) + this_change=2; + if (this_change<-2) + this_change=-2; + change-=this_change; #ifdef SHOWIT - fprintf(stderr,"Large base change: %d.\n",this_change); -#endif - small_index+=this_change; - if (this_change<0) - { - code|=2U; - this_change=-this_change; - } - code|=(unsigned int)(this_change-1); - write_instruction(coder,INSTR_LARGE_BASE_CHANGE,&output_ptr); - Ptngc_writebits(coder,code,2,&output_ptr); - } - /* If there is still base change or runlength changes to do, we do them now. */ - if ((new_runlength!=runlength) || (change)) - { - unsigned int ichange=(unsigned int)(change+1); - unsigned int code=0U; - unsigned int irun=(unsigned int)((new_runlength-1)*3); - if (new_runlength==6) - ichange=0; /* Means no change. The change has been taken care of explicitly by a large - base change instruction above. */ - code=ichange+irun; + fprintf(stderr,"Large base change: %d.\n",this_change); +#endif + small_index+=this_change; + if (this_change<0) + { + code|=2U; + this_change=-this_change; + } + code|=(unsigned int)(this_change-1); + write_instruction(coder,INSTR_LARGE_BASE_CHANGE,&output_ptr); + Ptngc_writebits(coder,code,2,&output_ptr); + } + /* If there is still base change or runlength changes to do, we do them now. */ + if ((new_runlength!=runlength) || (change)) + { + unsigned int ichange=(unsigned int)(change+1); + unsigned int code=0U; + unsigned int irun=(unsigned int)((new_runlength-1)*3); + if (new_runlength==6) + ichange=0; /* Means no change. The change has been taken care of explicitly by a large + base change instruction above. */ + code=ichange+irun; #ifdef SHOWIT - fprintf(stderr,"Small base change: %d Runlength change: %d\n",change,new_runlength); -#endif - small_index+=change; - write_instruction(coder,INSTR_BASE_RUNLENGTH,&output_ptr); - Ptngc_writebits(coder,code,4,&output_ptr); - runlength=new_runlength; - } - } + fprintf(stderr,"Small base change: %d Runlength change: %d\n",change,new_runlength); +#endif + small_index+=change; + write_instruction(coder,INSTR_BASE_RUNLENGTH,&output_ptr); + Ptngc_writebits(coder,code,4,&output_ptr); + runlength=new_runlength; + } + } #ifdef SHOWIT - else - fprintf(stderr,"Rejected base change due to only change==-1\n"); + else + fprintf(stderr,"Rejected base change due to only change==-1\n"); #endif #ifdef SHOWIT - fprintf(stderr,"Current small index: %d Base=%d\n",small_index,magic[small_index]); -#endif - } - /* If we have a large previous integer we can combine it with a sequence of small ints. */ - if (has_large) - { - /* If swapatoms is set to 1 but we did actually not - do any swapping, we must first write out the - large atom and then the small. If swapatoms is 1 - and we did swapping we can use the efficient - encoding. */ - if ((swapatoms) && (!didswap)) - { + fprintf(stderr,"Current small index: %d Base=%d\n",small_index,magic[small_index]); +#endif + } + /* If we have a large previous integer we can combine it with a sequence of small ints. */ + if (has_large) + { + /* If swapatoms is set to 1 but we did actually not + do any swapping, we must first write out the + large atom and then the small. If swapatoms is 1 + and we did swapping we can use the efficient + encoding. */ + if ((swapatoms) && (!didswap)) + { #ifdef SHOWIT - fprintf(stderr,"Swapatoms was set to 1 but we did not do swapping!\n"); - fprintf(stderr,"Only one large integer.\n"); + fprintf(stderr,"Swapatoms was set to 1 but we did not do swapping!\n"); + fprintf(stderr,"Only one large integer.\n"); #endif - /* Flush all large atoms. */ - flush_large(coder,&has_large,has_large_ints,has_large,large_index,large_nbits,compress_buffer,&output_ptr); + /* Flush all large atoms. */ + flush_large(coder,&has_large,has_large_ints,has_large,large_index,large_nbits,compress_buffer,&output_ptr); #ifdef SHOWIT - fprintf(stderr,"Sequence of only small integers.\n"); + fprintf(stderr,"Sequence of only small integers.\n"); #endif - write_instruction(coder,INSTR_ONLY_SMALL,&output_ptr); - } - else - { + write_instruction(coder,INSTR_ONLY_SMALL,&output_ptr); + } + else + { #ifdef SHOWIT - fprintf(stderr,"Sequence of one large and small integers (good compression).\n"); -#endif - /* Flush all large atoms but one! */ - if (has_large>1) - flush_large(coder,&has_large,has_large_ints,has_large-1,large_index,large_nbits,compress_buffer,&output_ptr); - write_instruction(coder,INSTR_DEFAULT,&output_ptr); - write_three_large(coder,has_large_ints,large_index,large_nbits,compress_buffer,&output_ptr); - has_large=0; - } - } - else - { + fprintf(stderr,"Sequence of one large and small integers (good compression).\n"); +#endif + /* Flush all large atoms but one! */ + if (has_large>1) + flush_large(coder,&has_large,has_large_ints,has_large-1,large_index,large_nbits,compress_buffer,&output_ptr); + write_instruction(coder,INSTR_DEFAULT,&output_ptr); + write_three_large(coder,has_large_ints,large_index,large_nbits,compress_buffer,&output_ptr); + has_large=0; + } + } + else + { #ifdef SHOWIT - fprintf(stderr,"Sequence of only small integers.\n"); -#endif - write_instruction(coder,INSTR_ONLY_SMALL,&output_ptr); - } - /* Base compress small integers using the current parameters. */ - nbits=magic_bits[small_index][runlength-1]; - /* The same base is used for the small changes. */ - small_idx[0]=small_index; - small_idx[1]=small_index; - small_idx[2]=small_index; - trajcoder_base_compress(encode_ints,runlength*3,small_idx,compress_buffer); + fprintf(stderr,"Sequence of only small integers.\n"); +#endif + write_instruction(coder,INSTR_ONLY_SMALL,&output_ptr); + } + /* Base compress small integers using the current parameters. */ + nbits=magic_bits[small_index][runlength-1]; + /* The same base is used for the small changes. */ + small_idx[0]=small_index; + small_idx[1]=small_index; + small_idx[2]=small_index; + trajcoder_base_compress(encode_ints,runlength*3,small_idx,compress_buffer); #ifdef SHOWIT - fprintf(stderr,"nbits=%d (%g)\n",nbits,nbits/(runlength*3.)); - nbits_sum+=nbits; - nvalues_sum+=runlength*3; - fprintf(stderr,"Runlength encoded small integers. runlength=%d\n",runlength); + fprintf(stderr,"nbits=%d (%g)\n",nbits,nbits/(runlength*3.)); + nbits_sum+=nbits; + nvalues_sum+=runlength*3; + fprintf(stderr,"Runlength encoded small integers. runlength=%d\n",runlength); #endif - /* write out base compressed small integers */ - Ptngc_writemanybits(coder,compress_buffer,nbits,&output_ptr); + /* write out base compressed small integers */ + Ptngc_writemanybits(coder,compress_buffer,nbits,&output_ptr); #ifdef SHOWIT - for (ienc=0; ienc<runlength; ienc++) - fprintf(stderr,"Small: %d %d %d\n", - encode_ints[ienc*3], - encode_ints[ienc*3+1], - encode_ints[ienc*3+2]); -#endif - /* Update prevcoord. */ - for (ienc=0; ienc<runlength; ienc++) - { + for (ienc=0; ienc<runlength; ienc++) + fprintf(stderr,"Small: %d %d %d\n", + encode_ints[ienc*3], + encode_ints[ienc*3+1], + encode_ints[ienc*3+2]); +#endif + /* Update prevcoord. */ + for (ienc=0; ienc<runlength; ienc++) + { #ifdef SHOWIT - fprintf(stderr,"Prevcoord in packing: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Prevcoord in packing: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); #endif - prevcoord[0]+=unpositive_int(encode_ints[ienc*3]); - prevcoord[1]+=unpositive_int(encode_ints[ienc*3+1]); - prevcoord[2]+=unpositive_int(encode_ints[ienc*3+2]); - } + prevcoord[0]+=unpositive_int(encode_ints[ienc*3]); + prevcoord[1]+=unpositive_int(encode_ints[ienc*3+1]); + prevcoord[2]+=unpositive_int(encode_ints[ienc*3+2]); + } #ifdef SHOWIT - fprintf(stderr,"Prevcoord in packing: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Prevcoord in packing: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); #endif - input_ptr+=3*runlength; - ntriplets_left-=runlength; - } - else - { + input_ptr+=3*runlength; + ntriplets_left-=runlength; + } + else + { #ifdef SHOWIT - fprintf(stderr,"Refused value: %d old is %d max is %d\n",new_small_index,small_index,max_large_index); - fflush(stderr); + fprintf(stderr,"Refused value: %d old is %d max is %d\n",new_small_index,small_index,max_large_index); + fflush(stderr); #endif - refused=1; - } - } + refused=1; + } + } #ifdef SHOWIT fprintf(stderr,"Number of triplets left is %d\n",ntriplets_left); #endif @@ -1315,184 +1315,184 @@ int Ptngc_unpack_array_xtc2(struct coder *coder,unsigned char *packed,int *outpu int instr=read_instruction(&ptr,&bitptr); #ifdef SHOWIT if ((instr>=0) && (instr<MAXINSTR)) - fprintf(stderr,"Decoded instruction %s\n",instrnames[instr]); + fprintf(stderr,"Decoded instruction %s\n",instrnames[instr]); #endif if ((instr==INSTR_DEFAULT) /* large+small */ - || (instr==INSTR_ONLY_LARGE) /* only large */ - || (instr==INSTR_ONLY_SMALL)) /* only small */ - { - int large_ints[3]={0,0,0}; - if (instr!=INSTR_ONLY_SMALL) - { - /* Clear the compress buffer. */ - int i; - for (i=0; i<18*4; i++) - compress_buffer[i]=0; - /* Get the large value. */ - readmanybits(&ptr,&bitptr,large_nbits,compress_buffer); - trajcoder_base_decompress(compress_buffer,3,large_index,encode_ints); - large_ints[0]=encode_ints[0]; - large_ints[1]=encode_ints[1]; - large_ints[2]=encode_ints[2]; + || (instr==INSTR_ONLY_LARGE) /* only large */ + || (instr==INSTR_ONLY_SMALL)) /* only small */ + { + int large_ints[3]={0,0,0}; + if (instr!=INSTR_ONLY_SMALL) + { + /* Clear the compress buffer. */ + int i; + for (i=0; i<18*4; i++) + compress_buffer[i]=0; + /* Get the large value. */ + readmanybits(&ptr,&bitptr,large_nbits,compress_buffer); + trajcoder_base_decompress(compress_buffer,3,large_index,encode_ints); + large_ints[0]=encode_ints[0]; + large_ints[1]=encode_ints[1]; + large_ints[2]=encode_ints[2]; #ifdef SHOWIT - fprintf(stderr,"large ints: %d %d %d\n",large_ints[0],large_ints[1],large_ints[2]); -#endif - } - if (instr!=INSTR_ONLY_LARGE) - { - int small_idx[3]; - int i; - /* The same base is used for the small changes. */ - small_idx[0]=small_index; - small_idx[1]=small_index; - small_idx[2]=small_index; - /* Clear the compress buffer. */ - for (i=0; i<18*4; i++) - compress_buffer[i]=0; - /* Get the small values. */ - readmanybits(&ptr,&bitptr,magic_bits[small_index][runlength-1],compress_buffer); - trajcoder_base_decompress(compress_buffer,3*runlength,small_idx,encode_ints); + fprintf(stderr,"large ints: %d %d %d\n",large_ints[0],large_ints[1],large_ints[2]); +#endif + } + if (instr!=INSTR_ONLY_LARGE) + { + int small_idx[3]; + int i; + /* The same base is used for the small changes. */ + small_idx[0]=small_index; + small_idx[1]=small_index; + small_idx[2]=small_index; + /* Clear the compress buffer. */ + for (i=0; i<18*4; i++) + compress_buffer[i]=0; + /* Get the small values. */ + readmanybits(&ptr,&bitptr,magic_bits[small_index][runlength-1],compress_buffer); + trajcoder_base_decompress(compress_buffer,3*runlength,small_idx,encode_ints); #ifdef SHOWIT - for (i=0; i<runlength; i++) - fprintf(stderr,"small ints: %d %d %d\n",encode_ints[i*3+0],encode_ints[i*3+1],encode_ints[i*3+2]); -#endif - } - if (instr==INSTR_DEFAULT) - { - /* Check for swapped atoms */ - if (swapatoms) - { - /* Unswap the atoms. */ - int i; - for (i=0; i<3; i++) - { - int in[3], out[3]; - in[0]=large_ints[i]; - in[1]=unpositive_int(encode_ints[i]); - in[2]=unpositive_int(encode_ints[3+i]); - swap_ints(in,out); - large_ints[i]=out[0]; - encode_ints[i]=positive_int(out[1]); - encode_ints[3+i]=positive_int(out[2]); - } - } - } - /* Output result. */ - if (instr!=INSTR_ONLY_SMALL) - { - /* Output large value */ - *output++=large_ints[0]+minint[0]; - *output++=large_ints[1]+minint[1]; - *output++=large_ints[2]+minint[2]; - prevcoord[0]=large_ints[0]; - prevcoord[1]=large_ints[1]; - prevcoord[2]=large_ints[2]; + for (i=0; i<runlength; i++) + fprintf(stderr,"small ints: %d %d %d\n",encode_ints[i*3+0],encode_ints[i*3+1],encode_ints[i*3+2]); +#endif + } + if (instr==INSTR_DEFAULT) + { + /* Check for swapped atoms */ + if (swapatoms) + { + /* Unswap the atoms. */ + int i; + for (i=0; i<3; i++) + { + int in[3], out[3]; + in[0]=large_ints[i]; + in[1]=unpositive_int(encode_ints[i]); + in[2]=unpositive_int(encode_ints[3+i]); + swap_ints(in,out); + large_ints[i]=out[0]; + encode_ints[i]=positive_int(out[1]); + encode_ints[3+i]=positive_int(out[2]); + } + } + } + /* Output result. */ + if (instr!=INSTR_ONLY_SMALL) + { + /* Output large value */ + *output++=large_ints[0]+minint[0]; + *output++=large_ints[1]+minint[1]; + *output++=large_ints[2]+minint[2]; + prevcoord[0]=large_ints[0]; + prevcoord[1]=large_ints[1]; + prevcoord[2]=large_ints[2]; #ifdef SHOWIT - fprintf(stderr,"Prevcoord after unpacking of large: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); - fprintf(stderr,"VALUE:%d %6d %6d %6d\n", - length/3-ntriplets_left, - prevcoord[0]+minint[0], - prevcoord[1]+minint[1], - prevcoord[2]+minint[2]); -#endif - ntriplets_left--; - } - if (instr!=INSTR_ONLY_LARGE) - { - /* Output small values */ - int i; + fprintf(stderr,"Prevcoord after unpacking of large: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"VALUE:%d %6d %6d %6d\n", + length/3-ntriplets_left, + prevcoord[0]+minint[0], + prevcoord[1]+minint[1], + prevcoord[2]+minint[2]); +#endif + ntriplets_left--; + } + if (instr!=INSTR_ONLY_LARGE) + { + /* Output small values */ + int i; #ifdef SHOWIT - fprintf(stderr,"Prevcoord before unpacking of small: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); -#endif - for (i=0; i<runlength; i++) - { - int v[3]; - v[0]=unpositive_int(encode_ints[i*3]); - v[1]=unpositive_int(encode_ints[i*3+1]); - v[2]=unpositive_int(encode_ints[i*3+2]); - prevcoord[0]+=v[0]; - prevcoord[1]+=v[1]; - prevcoord[2]+=v[2]; + fprintf(stderr,"Prevcoord before unpacking of small: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); +#endif + for (i=0; i<runlength; i++) + { + int v[3]; + v[0]=unpositive_int(encode_ints[i*3]); + v[1]=unpositive_int(encode_ints[i*3+1]); + v[2]=unpositive_int(encode_ints[i*3+2]); + prevcoord[0]+=v[0]; + prevcoord[1]+=v[1]; + prevcoord[2]+=v[2]; #ifdef SHOWIT - fprintf(stderr,"Prevcoord after unpacking of small: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); - fprintf(stderr,"Unpacked small values: %6d %6d %6d\t\t%6d %6d %6d\n",v[0],v[1],v[2],prevcoord[0],prevcoord[1],prevcoord[2]); - fprintf(stderr,"VALUE:%d %6d %6d %6d\n", - length/3-(ntriplets_left-i), - prevcoord[0]+minint[0], - prevcoord[1]+minint[1], - prevcoord[2]+minint[2]); -#endif - *output++=prevcoord[0]+minint[0]; - *output++=prevcoord[1]+minint[1]; - *output++=prevcoord[2]+minint[2]; - } - ntriplets_left-=runlength; - } - } + fprintf(stderr,"Prevcoord after unpacking of small: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Unpacked small values: %6d %6d %6d\t\t%6d %6d %6d\n",v[0],v[1],v[2],prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"VALUE:%d %6d %6d %6d\n", + length/3-(ntriplets_left-i), + prevcoord[0]+minint[0], + prevcoord[1]+minint[1], + prevcoord[2]+minint[2]); +#endif + *output++=prevcoord[0]+minint[0]; + *output++=prevcoord[1]+minint[1]; + *output++=prevcoord[2]+minint[2]; + } + ntriplets_left-=runlength; + } + } else if (instr==INSTR_LARGE_RLE) - { - int i,j; - int large_ints[3]; - /* How many large atoms in this sequence? */ - int n=(int)readbits(&ptr,&bitptr,4)+3; /* 3-18 large atoms */ - for (i=0; i<n; i++) - { - /* Clear the compress buffer. */ - for (j=0; j<18*4; j++) - compress_buffer[j]=0; - /* Get the large value. */ - readmanybits(&ptr,&bitptr,large_nbits,compress_buffer); - trajcoder_base_decompress(compress_buffer,3,large_index,encode_ints); - large_ints[0]=encode_ints[0]; - large_ints[1]=encode_ints[1]; - large_ints[2]=encode_ints[2]; - /* Output large value */ - *output++=large_ints[0]+minint[0]; - *output++=large_ints[1]+minint[1]; - *output++=large_ints[2]+minint[2]; - prevcoord[0]=large_ints[0]; - prevcoord[1]=large_ints[1]; - prevcoord[2]=large_ints[2]; - } - ntriplets_left-=n; - } + { + int i,j; + int large_ints[3]; + /* How many large atoms in this sequence? */ + int n=(int)readbits(&ptr,&bitptr,4)+3; /* 3-18 large atoms */ + for (i=0; i<n; i++) + { + /* Clear the compress buffer. */ + for (j=0; j<18*4; j++) + compress_buffer[j]=0; + /* Get the large value. */ + readmanybits(&ptr,&bitptr,large_nbits,compress_buffer); + trajcoder_base_decompress(compress_buffer,3,large_index,encode_ints); + large_ints[0]=encode_ints[0]; + large_ints[1]=encode_ints[1]; + large_ints[2]=encode_ints[2]; + /* Output large value */ + *output++=large_ints[0]+minint[0]; + *output++=large_ints[1]+minint[1]; + *output++=large_ints[2]+minint[2]; + prevcoord[0]=large_ints[0]; + prevcoord[1]=large_ints[1]; + prevcoord[2]=large_ints[2]; + } + ntriplets_left-=n; + } else if (instr==INSTR_BASE_RUNLENGTH) - { - unsigned int code=readbits(&ptr,&bitptr,4); - int change; - if (code==15) - { - change=0; - runlength=6; - } - else - { - int ichange=code%3; - runlength=code/3+1; - change=ichange-1; - } - small_index+=change; - } + { + unsigned int code=readbits(&ptr,&bitptr,4); + int change; + if (code==15) + { + change=0; + runlength=6; + } + else + { + int ichange=code%3; + runlength=code/3+1; + change=ichange-1; + } + small_index+=change; + } else if (instr==INSTR_FLIP) - { - swapatoms=1-swapatoms; - } + { + swapatoms=1-swapatoms; + } else if (instr==INSTR_LARGE_BASE_CHANGE) - { - unsigned int ichange=readbits(&ptr,&bitptr,2); - int change=(int)(ichange&0x1U)+1; - if (ichange&0x2U) - change=-change; - small_index+=change; - } + { + unsigned int ichange=readbits(&ptr,&bitptr,2); + int change=(int)(ichange&0x1U)+1; + if (ichange&0x2U) + change=-change; + small_index+=change; + } else - { - fprintf(stderr,"TRAJNG: BUG! Encoded unknown instruction.\n"); - exit(EXIT_FAILURE); - } + { + fprintf(stderr,"TRAJNG: BUG! Encoded unknown instruction.\n"); + exit(EXIT_FAILURE); + } #ifdef SHOWIT fprintf(stderr,"Number of triplets left is %d\n",ntriplets_left); #endif diff --git a/src/compression/xtc3.c b/src/compression/xtc3.c index f9993e6..edeb93d 100644 --- a/src/compression/xtc3.c +++ b/src/compression/xtc3.c @@ -35,16 +35,16 @@ static const double iflipgaincheck=0.89089871814033927; /* 1./(2**(1./6)) */ #define MAX_SMALL_RLE 12 /* Maximum number of small atoms in one group. */ #define TRESHOLD_INTRA_INTER_DIRECT 1.5 /* How much larger can the direct - frame deltas for the small - triplets be and be accepted anyway - as better than the intra/inter frame - deltas. For better instructions/RLEs. */ + frame deltas for the small + triplets be and be accepted anyway + as better than the intra/inter frame + deltas. For better instructions/RLEs. */ #define TRESHOLD_INTER_INTRA 5.0 /* How much larger can the intra - frame deltas for the small - triplets be and be accepted anyway - as better than the inter frame - deltas. */ + frame deltas for the small + triplets be and be accepted anyway + as better than the inter frame + deltas. */ /* Difference in indices used for determining whether to store as large or small. A fun detail in this compression algorithm is that @@ -116,7 +116,7 @@ struct xtc3_context int has_large; int has_large_ints[MAX_LARGE_RLE*3]; /* Large cache. */ int has_large_type[MAX_LARGE_RLE]; /* What kind of type this large - int is. */ + int is. */ int current_large_type; }; @@ -176,12 +176,12 @@ static void swap_is_better(int *input, int *minint, int *sum_normal, int *sum_sw normal[2]=input[6+i]-input[3+i]; /* minint[i]-minint[i] cancels out */ swap_ints(normal,swapped); for (j=1; j<3; j++) - { - if (positive_int(normal[j])>(unsigned int)normal_max) - normal_max=positive_int(normal[j]); - if (positive_int(swapped[j])>(unsigned int)swapped_max) - swapped_max=positive_int(swapped[j]); - } + { + if (positive_int(normal[j])>(unsigned int)normal_max) + normal_max=positive_int(normal[j]); + if (positive_int(swapped[j])>(unsigned int)swapped_max) + swapped_max=positive_int(swapped[j]); + } } if (normal_max==0) normal_max=1; @@ -202,8 +202,8 @@ static void allocate_enough_memory(unsigned int **ptr, int *nele, int *nele_allo } static void insert_value_in_array(unsigned int **ptr, int *nele, int *nele_alloc, - unsigned int value, - char *arrayname) + unsigned int value, + char *arrayname) { #ifndef SHOWIT (void)arrayname; @@ -237,21 +237,21 @@ static void swapdecide(struct xtc3_context *xtc3_context, int *input,int *swapat ((normal<swapped) && (fabs((double)normal/swapped)<iflipgaincheck))) { if (swapped<normal) - { - if (!*swapatoms) - { - *swapatoms=1; - didswap=1; - } - } + { + if (!*swapatoms) + { + *swapatoms=1; + didswap=1; + } + } else - { - if (*swapatoms) - { - *swapatoms=0; - didswap=1; - } - } + { + if (*swapatoms) + { + *swapatoms=0; + didswap=1; + } + } } if (didswap) { @@ -259,9 +259,9 @@ static void swapdecide(struct xtc3_context *xtc3_context, int *input,int *swapat fprintf(stderr,"Flip. Swapatoms is now %d\n",*swapatoms); #endif insert_value_in_array(&xtc3_context->instructions, - &xtc3_context->ninstr, - &xtc3_context->ninstr_alloc, - INSTR_FLIP,"instr"); + &xtc3_context->ninstr, + &xtc3_context->ninstr_alloc, + INSTR_FLIP,"instr"); } } @@ -277,11 +277,11 @@ static int is_quite_large(int *input, int small_index, int max_large_index) else { for (i=0; i<3; i++) - if (positive_int(input[i])>(unsigned int)Ptngc_magic(small_index+QUITE_LARGE)) - { - is=1; - break; - } + if (positive_int(input[i])>(unsigned int)Ptngc_magic(small_index+QUITE_LARGE)) + { + is=1; + break; + } } return is; } @@ -304,21 +304,21 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord, int { int i; for (i=0; i<startenc; i++) - { - tmp_prevcoord[0]+=encode_ints[i*3]; - tmp_prevcoord[1]+=encode_ints[i*3+1]; - tmp_prevcoord[2]+=encode_ints[i*3+2]; + { + tmp_prevcoord[0]+=encode_ints[i*3]; + tmp_prevcoord[1]+=encode_ints[i*3+1]; + tmp_prevcoord[2]+=encode_ints[i*3+2]; #ifdef SHOWIT - fprintf(stderr,"%6d: %6d %6d %6d\t\t%6d %6d %6d\t\t%6d %6d %6d\n",i*3, - tmp_prevcoord[0],tmp_prevcoord[1],tmp_prevcoord[2], - encode_ints[i*3], - encode_ints[i*3+1], - encode_ints[i*3+2], - positive_int(encode_ints[i*3]), - positive_int(encode_ints[i*3+1]), - positive_int(encode_ints[i*3+2])); -#endif - } + fprintf(stderr,"%6d: %6d %6d %6d\t\t%6d %6d %6d\t\t%6d %6d %6d\n",i*3, + tmp_prevcoord[0],tmp_prevcoord[1],tmp_prevcoord[2], + encode_ints[i*3], + encode_ints[i*3+1], + encode_ints[i*3+2], + positive_int(encode_ints[i*3]), + positive_int(encode_ints[i*3+1]), + positive_int(encode_ints[i*3+2])); +#endif + } } #ifdef SHOWIT @@ -331,15 +331,15 @@ static void insert_batch(int *input_ptr, int ntriplets_left, int *prevcoord, int encode_ints[nencode+2]=input_ptr[nencode+2]-tmp_prevcoord[2]; #ifdef SHOWIT fprintf(stderr,"%6d: %6d %6d %6d\t\t%6d %6d %6d\t\t%6d %6d %6d\n",nencode, - input_ptr[nencode], - input_ptr[nencode+1], - input_ptr[nencode+2], - encode_ints[nencode], - encode_ints[nencode+1], - encode_ints[nencode+2], - positive_int(encode_ints[nencode]), - positive_int(encode_ints[nencode+1]), - positive_int(encode_ints[nencode+2])); + input_ptr[nencode], + input_ptr[nencode+1], + input_ptr[nencode+2], + encode_ints[nencode], + encode_ints[nencode+1], + encode_ints[nencode+2], + positive_int(encode_ints[nencode]), + positive_int(encode_ints[nencode+1]), + positive_int(encode_ints[nencode+2])); #endif tmp_prevcoord[0]=input_ptr[nencode]; tmp_prevcoord[1]=input_ptr[nencode+1]; @@ -358,50 +358,50 @@ static void large_instruction_change(struct xtc3_context *xtc3_context, int i) unsigned int instr; xtc3_context->current_large_type=xtc3_context->has_large_type[i]; if (xtc3_context->current_large_type==0) - instr=INSTR_LARGE_DIRECT; + instr=INSTR_LARGE_DIRECT; else if (xtc3_context->current_large_type==1) - instr=INSTR_LARGE_INTRA_DELTA; + instr=INSTR_LARGE_INTRA_DELTA; else - instr=INSTR_LARGE_INTER_DELTA; + instr=INSTR_LARGE_INTER_DELTA; insert_value_in_array(&xtc3_context->instructions, - &xtc3_context->ninstr, - &xtc3_context->ninstr_alloc, - instr,"instr"); + &xtc3_context->ninstr, + &xtc3_context->ninstr_alloc, + instr,"instr"); } } static void write_three_large(struct xtc3_context *xtc3_context, - int i) + int i) { int m; if (xtc3_context->current_large_type==0) { for (m=0; m<3; m++) - insert_value_in_array(&xtc3_context->large_direct, - &xtc3_context->nlargedir, - &xtc3_context->nlargedir_alloc, - xtc3_context->has_large_ints[i*3+m],"large direct"); + insert_value_in_array(&xtc3_context->large_direct, + &xtc3_context->nlargedir, + &xtc3_context->nlargedir_alloc, + xtc3_context->has_large_ints[i*3+m],"large direct"); } else if (xtc3_context->current_large_type==1) { for (m=0; m<3; m++) - insert_value_in_array(&xtc3_context->large_intra_delta, - &xtc3_context->nlargeintra, - &xtc3_context->nlargeintra_alloc, - xtc3_context->has_large_ints[i*3+m],"large intra"); + insert_value_in_array(&xtc3_context->large_intra_delta, + &xtc3_context->nlargeintra, + &xtc3_context->nlargeintra_alloc, + xtc3_context->has_large_ints[i*3+m],"large intra"); } else { for (m=0; m<3; m++) - insert_value_in_array(&xtc3_context->large_inter_delta, - &xtc3_context->nlargeinter, - &xtc3_context->nlargeinter_alloc, - xtc3_context->has_large_ints[i*3+m],"large inter"); + insert_value_in_array(&xtc3_context->large_inter_delta, + &xtc3_context->nlargeinter, + &xtc3_context->nlargeinter_alloc, + xtc3_context->has_large_ints[i*3+m],"large inter"); } } static void flush_large(struct xtc3_context *xtc3_context, - int n) /* How many to flush. */ + int n) /* How many to flush. */ { int i; i=0; @@ -409,48 +409,48 @@ static void flush_large(struct xtc3_context *xtc3_context, { int j,k; /* If the first large is of a different kind than the currently used we must - emit an "instruction" to change the large type. */ + emit an "instruction" to change the large type. */ large_instruction_change(xtc3_context,i); /* How many large of the same kind in a row? */ for (j=0; - (i+j<n) && - (xtc3_context->has_large_type[i+j]==xtc3_context->has_large_type[i]); - j++); + (i+j<n) && + (xtc3_context->has_large_type[i+j]==xtc3_context->has_large_type[i]); + j++); if (j<3) - { - for (k=0; k<j; k++) - { - insert_value_in_array(&xtc3_context->instructions, - &xtc3_context->ninstr, - &xtc3_context->ninstr_alloc, - INSTR_ONLY_LARGE,"instr"); - write_three_large(xtc3_context,i+k); - } - } + { + for (k=0; k<j; k++) + { + insert_value_in_array(&xtc3_context->instructions, + &xtc3_context->ninstr, + &xtc3_context->ninstr_alloc, + INSTR_ONLY_LARGE,"instr"); + write_three_large(xtc3_context,i+k); + } + } else - { - insert_value_in_array(&xtc3_context->instructions, - &xtc3_context->ninstr, - &xtc3_context->ninstr_alloc, - INSTR_LARGE_RLE,"instr"); - insert_value_in_array(&xtc3_context->rle, - &xtc3_context->nrle, - &xtc3_context->nrle_alloc, - (unsigned int)j,"rle (large)"); - for (k=0; k<j; k++) - write_three_large(xtc3_context,i+k); - } + { + insert_value_in_array(&xtc3_context->instructions, + &xtc3_context->ninstr, + &xtc3_context->ninstr_alloc, + INSTR_LARGE_RLE,"instr"); + insert_value_in_array(&xtc3_context->rle, + &xtc3_context->nrle, + &xtc3_context->nrle_alloc, + (unsigned int)j,"rle (large)"); + for (k=0; k<j; k++) + write_three_large(xtc3_context,i+k); + } i+=j; } if ((xtc3_context->has_large-n)!=0) { int j; for (i=0; i<xtc3_context->has_large-n; i++) - { - xtc3_context->has_large_type[i]=xtc3_context->has_large_type[i+n]; - for (j=0; j<3; j++) - xtc3_context->has_large_ints[i*3+j]=xtc3_context->has_large_ints[(i+n)*3+j]; - } + { + xtc3_context->has_large_type[i]=xtc3_context->has_large_type[i+n]; + for (j=0; j<3; j++) + xtc3_context->has_large_ints[i*3+j]=xtc3_context->has_large_ints[(i+n)*3+j]; + } } xtc3_context->has_large-=n; /* Number of remaining large atoms in buffer */ } @@ -467,7 +467,7 @@ static double compute_intlen(unsigned int *ints) } static void buffer_large(struct xtc3_context *xtc3_context, int *input, int inpdata, - int natoms, int intradelta_ok) + int natoms, int intradelta_ok) { unsigned int direct[3], intradelta[3]={0,}, interdelta[3]={0,}; double minlen; @@ -495,10 +495,10 @@ static void buffer_large(struct xtc3_context *xtc3_context, int *input, int inpd intradelta[2]=positive_int(input[inpdata+2]-input[inpdata-1]); thislen=compute_intlen(intradelta); if (thislen*TRESHOLD_INTRA_INTER_DIRECT<minlen) - { - minlen=thislen; - best_type=1; /* Intra delta */ - } + { + minlen=thislen; + best_type=1; /* Intra delta */ + } } #endif #if 1 @@ -511,9 +511,9 @@ static void buffer_large(struct xtc3_context *xtc3_context, int *input, int inpd interdelta[2]=positive_int(input[inpdata+2]-input[inpdata-natoms*3+2]); thislen=compute_intlen(interdelta); if (thislen*TRESHOLD_INTRA_INTER_DIRECT<minlen) - { - best_type=2; /* Inter delta */ - } + { + best_type=2; /* Inter delta */ + } } #endif xtc3_context->has_large_type[xtc3_context->has_large]=best_type; @@ -581,18 +581,18 @@ static int base_bytes(unsigned int base, int n) for (i=0; i<n; i++) { if (i!=0) - { - Ptngc_largeint_mul(base,largeint,largeint_tmp,n+1); - for (j=0; j<n+1; j++) - largeint[j]=largeint_tmp[j]; - } + { + Ptngc_largeint_mul(base,largeint,largeint_tmp,n+1); + for (j=0; j<n+1; j++) + largeint[j]=largeint_tmp[j]; + } Ptngc_largeint_add(base-1U,largeint,n+1); } for (i=0; i<n; i++) if (largeint[i]) for (j=0; j<4; j++) - if ((largeint[i]>>(j*8))&0xFFU) - numbytes=i*4+j+1; + if ((largeint[i]>>(j*8))&0xFFU) + numbytes=i*4+j+1; return numbytes; } @@ -615,89 +615,89 @@ static void base_compress(unsigned int *data, int len, unsigned char *output, in int nvals=0; int basegiven=0; for (j=0; j<MAXBASEVALS+1; j++) - largeint[j]=0U; + largeint[j]=0U; for (i=ixyz; i<len; i+=3) - { + { if (nvals==0) { int basecheckvals=0; int k; - if (basegiven==0) - { - base=0U; - /* Find the largest value for this particular coordinate. */ - for (k=i; k<len; k+=3) - { - if (data[k]>base) - base=data[k]; - basecheckvals++; - if (basecheckvals==MAXBASEVALS*BASEINTERVAL) - break; - } - /* The base is one larger than the largest values. */ - base++; - if (base<2) - base=2; - /* Store the base in the output. */ - output[nwrittenout++]=(unsigned char)(base&0xFFU); - output[nwrittenout++]=(unsigned char)((base>>8)&0xFFU); - output[nwrittenout++]=(unsigned char)((base>>16)&0xFFU); - output[nwrittenout++]=(unsigned char)((base>>24)&0xFFU); - basegiven=BASEINTERVAL; - /* How many bytes is needed to store MAXBASEVALS values using this base? */ - numbytes=base_bytes(base,MAXBASEVALS); - } - basegiven--; + if (basegiven==0) + { + base=0U; + /* Find the largest value for this particular coordinate. */ + for (k=i; k<len; k+=3) + { + if (data[k]>base) + base=data[k]; + basecheckvals++; + if (basecheckvals==MAXBASEVALS*BASEINTERVAL) + break; + } + /* The base is one larger than the largest values. */ + base++; + if (base<2) + base=2; + /* Store the base in the output. */ + output[nwrittenout++]=(unsigned char)(base&0xFFU); + output[nwrittenout++]=(unsigned char)((base>>8)&0xFFU); + output[nwrittenout++]=(unsigned char)((base>>16)&0xFFU); + output[nwrittenout++]=(unsigned char)((base>>24)&0xFFU); + basegiven=BASEINTERVAL; + /* How many bytes is needed to store MAXBASEVALS values using this base? */ + numbytes=base_bytes(base,MAXBASEVALS); + } + basegiven--; #ifdef SHOWIT fprintf(stderr,"Base for %d is %u. I need %d bytes for %d values.\n",ixyz,base,numbytes,MAXBASEVALS); #endif } - if (nvals!=0) - { - Ptngc_largeint_mul(base,largeint,largeint_tmp,MAXBASEVALS+1); - for (j=0; j<MAXBASEVALS+1; j++) - largeint[j]=largeint_tmp[j]; - } - Ptngc_largeint_add(data[i],largeint,MAXBASEVALS+1); + if (nvals!=0) + { + Ptngc_largeint_mul(base,largeint,largeint_tmp,MAXBASEVALS+1); + for (j=0; j<MAXBASEVALS+1; j++) + largeint[j]=largeint_tmp[j]; + } + Ptngc_largeint_add(data[i],largeint,MAXBASEVALS+1); #ifdef SHOWIT - fprintf(stderr,"outputting value %u\n",data[i]); + fprintf(stderr,"outputting value %u\n",data[i]); #endif - nvals++; - if (nvals==MAXBASEVALS) - { + nvals++; + if (nvals==MAXBASEVALS) + { #ifdef SHOWIT - fprintf(stderr,"Writing largeint: "); + fprintf(stderr,"Writing largeint: "); #endif - for (j=0; j<numbytes; j++) - { - int ilarge=j/4; - int ibyte=j%4; - output[nwrittenout++]=(unsigned char)((largeint[ilarge]>>(ibyte*8))&(0xFFU)); + for (j=0; j<numbytes; j++) + { + int ilarge=j/4; + int ibyte=j%4; + output[nwrittenout++]=(unsigned char)((largeint[ilarge]>>(ibyte*8))&(0xFFU)); #ifdef SHOWIT - fprintf(stderr,"%02x",(unsigned int)output[nwrittenout-1]); + fprintf(stderr,"%02x",(unsigned int)output[nwrittenout-1]); #endif - } + } #ifdef SHOWIT - fprintf(stderr,"\n"); + fprintf(stderr,"\n"); #endif - nvals=0; - for (j=0; j<MAXBASEVALS+1; j++) - largeint[j]=0U; - } - } + nvals=0; + for (j=0; j<MAXBASEVALS+1; j++) + largeint[j]=0U; + } + } if (nvals) - { - numbytes=base_bytes(base,nvals); + { + numbytes=base_bytes(base,nvals); #ifdef SHOWIT - fprintf(stderr,"Base for %d is %u. I need %d bytes for %d values.\n",ixyz,base,numbytes,nvals); -#endif - for (j=0; j<numbytes; j++) - { - int ilarge=j/4; - int ibyte=j%4; - output[nwrittenout++]=(unsigned char)((largeint[ilarge]>>(ibyte*8))&(0xFFU)); - } - } + fprintf(stderr,"Base for %d is %u. I need %d bytes for %d values.\n",ixyz,base,numbytes,nvals); +#endif + for (j=0; j<numbytes; j++) + { + int ilarge=j/4; + int ibyte=j%4; + output[nwrittenout++]=(unsigned char)((largeint[ilarge]>>(ibyte*8))&(0xFFU)); + } + } } *outlen=nwrittenout; } @@ -712,8 +712,8 @@ static void base_decompress(unsigned char *input, int len, unsigned int *output) if (maxbasevals>(int)MAXMAXBASEVALS) { fprintf(stderr,"Read a larger maxbasevals value from the file than I can handle. Fix" - " by increasing MAXMAXBASEVALS to at least %d. Although, this is" - " probably a bug in TRAJNG, since MAXMAXBASEVALS should already be insanely large enough.\n",maxbasevals); + " by increasing MAXMAXBASEVALS to at least %d. Although, this is" + " probably a bug in TRAJNG, since MAXMAXBASEVALS should already be insanely large enough.\n",maxbasevals); exit(EXIT_FAILURE); } input+=3; @@ -728,65 +728,65 @@ static void base_decompress(unsigned char *input, int len, unsigned int *output) fprintf(stderr,"Base for %d is %u. I need %d bytes for %d values.\n",ixyz,base,numbytes,maxbasevals); #endif while (nvals_left) - { - int n; - if (basegiven==0) - { - base=(unsigned int)(input[0])| - (((unsigned int)(input[1]))<<8)| - (((unsigned int)(input[2]))<<16)| - (((unsigned int)(input[3]))<<24); - input+=4; - basegiven=baseinterval; - /* How many bytes is needed to store maxbasevals values using this base? */ - numbytes=base_bytes(base,maxbasevals); - } - basegiven--; - if (nvals_left<maxbasevals) - { - numbytes=base_bytes(base,nvals_left); + { + int n; + if (basegiven==0) + { + base=(unsigned int)(input[0])| + (((unsigned int)(input[1]))<<8)| + (((unsigned int)(input[2]))<<16)| + (((unsigned int)(input[3]))<<24); + input+=4; + basegiven=baseinterval; + /* How many bytes is needed to store maxbasevals values using this base? */ + numbytes=base_bytes(base,maxbasevals); + } + basegiven--; + if (nvals_left<maxbasevals) + { + numbytes=base_bytes(base,nvals_left); #ifdef SHOWIT - fprintf(stderr,"Base for %d is %u. I need %d bytes for %d values.\n",ixyz,base,numbytes,nvals_left); + fprintf(stderr,"Base for %d is %u. I need %d bytes for %d values.\n",ixyz,base,numbytes,nvals_left); #endif - } - for (j=0; j<maxbasevals+1; j++) - largeint[j]=0U; + } + for (j=0; j<maxbasevals+1; j++) + largeint[j]=0U; #ifdef SHOWIT - fprintf(stderr,"Reading largeint: "); + fprintf(stderr,"Reading largeint: "); #endif if (numbytes/4 < maxbasevals+1) { - for (j=0; j<numbytes; j++) - { - int ilarge=j/4; - int ibyte=j%4; - largeint[ilarge]|=((unsigned int)input[j])<<(ibyte*8); + for (j=0; j<numbytes; j++) + { + int ilarge=j/4; + int ibyte=j%4; + largeint[ilarge]|=((unsigned int)input[j])<<(ibyte*8); #ifdef SHOWIT - fprintf(stderr,"%02x",(unsigned int)input[j]); + fprintf(stderr,"%02x",(unsigned int)input[j]); #endif } - } + } #ifdef SHOWIT - fprintf(stderr,"\n"); -#endif - input+=numbytes; - /* Do the long division required to get the output values. */ - n=maxbasevals; - if (n>nvals_left) - n=nvals_left; - for (i=n-1; i>=0; i--) - { - output[outvals+i*3]=Ptngc_largeint_div(base,largeint,largeint_tmp,maxbasevals+1); - for (j=0; j<maxbasevals+1; j++) - largeint[j]=largeint_tmp[j]; - } + fprintf(stderr,"\n"); +#endif + input+=numbytes; + /* Do the long division required to get the output values. */ + n=maxbasevals; + if (n>nvals_left) + n=nvals_left; + for (i=n-1; i>=0; i--) + { + output[outvals+i*3]=Ptngc_largeint_div(base,largeint,largeint_tmp,maxbasevals+1); + for (j=0; j<maxbasevals+1; j++) + largeint[j]=largeint_tmp[j]; + } #ifdef SHOWIT - for (i=0; i<n; i++) - fprintf(stderr,"outputting value %u\n",output[outvals+i*3]); + for (i=0; i<n; i++) + fprintf(stderr,"outputting value %u\n",output[outvals+i*3]); #endif - outvals+=n*3; - nvals_left-=n; - } + outvals+=n*3; + nvals_left-=n; + } } } @@ -824,10 +824,10 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp int large_index[3]; int prevcoord[3]; int runlength=0; /* Initial runlength. "Stupidly" set to zero for - simplicity and explicity */ + simplicity and explicity */ int swapatoms=0; /* Initial guess is that we should not swap the - first two atoms in each large+small - transition */ + first two atoms in each large+small + transition */ int didswap; /* Whether swapping was actually done. */ int inpdata=0; int encode_ints[3+MAX_SMALL_RLE*3]; /* Up to 3 large + 24 small ints can be encoded at once */ @@ -866,10 +866,10 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp for (i=1; i<ntriplets; i++) for (j=0; j<3; j++) { - if (input[i*3+j]>xtc3_context.maxint[j]) - xtc3_context.maxint[j]=input[i*3+j]; - if (input[i*3+j]<xtc3_context.minint[j]) - xtc3_context.minint[j]=input[i*3+j]; + if (input[i*3+j]>xtc3_context.maxint[j]) + xtc3_context.maxint[j]=input[i*3+j]; + if (input[i*3+j]<xtc3_context.minint[j]) + xtc3_context.minint[j]=input[i*3+j]; } large_index[0]=Ptngc_find_magic_index(xtc3_context.maxint[0]-xtc3_context.minint[0]+1); @@ -884,7 +884,7 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp #ifdef SHOWIT for (j=0; j<3; j++) fprintf(stderr,"minint[%d]=%d. maxint[%d]=%d large_index[%d]=%d value=%d\n",j,xtc3_context.minint[j],j,xtc3_context.maxint[j], - j,large_index[j],Ptngc_magic(large_index[j])); + j,large_index[j],Ptngc_magic(large_index[j])); #endif /* Guess initial small index */ @@ -899,8 +899,8 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp int item=input[i]; int s=positive_int(item); if (s>intmax) - if (s<max_small) - intmax=s; + if (s<max_small) + intmax=s; } /* This value is not critical, since if I guess wrong, the code will just insert instructions to increase this value. */ @@ -917,10 +917,10 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp #ifdef SHOWIT for (i=0; i<ntriplets_left; i++) fprintf(stderr,"VALUE:%d %6d %6d %6d\n", - i, - input[inpdata+i*3], - input[inpdata+i*3+1], - input[inpdata+i*3+2]); + i, + input[inpdata+i*3], + input[inpdata+i*3+1], + input[inpdata+i*3+2]); #endif #endif @@ -932,494 +932,494 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp while (ntriplets_left) { if (ntriplets_left<0) - { - fprintf(stderr,"TRAJNG: BUG! ntriplets_left<0!\n"); - exit(EXIT_FAILURE); - } + { + fprintf(stderr,"TRAJNG: BUG! ntriplets_left<0!\n"); + exit(EXIT_FAILURE); + } /* If only less than three atoms left we just write them all as large integers. Here no swapping is done! */ if (ntriplets_left<3) - { - for (ienc=0; ienc<ntriplets_left; ienc++) - { - buffer_large(&xtc3_context,input,inpdata,natoms,1); - inpdata+=3; - ntriplets_left--; - } - flush_large(&xtc3_context,xtc3_context.has_large); /* Flush all */ - } + { + for (ienc=0; ienc<ntriplets_left; ienc++) + { + buffer_large(&xtc3_context,input,inpdata,natoms,1); + inpdata+=3; + ntriplets_left--; + } + flush_large(&xtc3_context,xtc3_context.has_large); /* Flush all */ + } else - { - int min_runlength=0; - int largest_required_base; - int largest_runlength_base; - int largest_required_index; - int largest_runlength_index; - int new_runlength; - int new_small_index; - int iter_runlength; - int iter_small_index; - int rle_index_dep; - didswap=0; - /* Insert the next batch of integers to be encoded into the buffer */ + { + int min_runlength=0; + int largest_required_base; + int largest_runlength_base; + int largest_required_index; + int largest_runlength_index; + int new_runlength; + int new_small_index; + int iter_runlength; + int iter_small_index; + int rle_index_dep; + didswap=0; + /* Insert the next batch of integers to be encoded into the buffer */ #ifdef SHOWIT - fprintf(stderr,"Initial batch\n"); -#endif - insert_batch(input+inpdata,ntriplets_left,prevcoord,encode_ints,0,&nencode); - - /* First we must decide if the next value is large (does not reasonably fit in current small encoding) - Also, if we have not written any values yet, we must begin by writing a large atom. */ - if ((inpdata==0) || (is_quite_large(encode_ints,small_index,max_large_index)) || (refused)) - { - /* If any of the next two atoms are large we should probably write them as large and not swap them */ - int no_swap=0; - if ((is_quite_large(encode_ints+3,small_index,max_large_index)) || (is_quite_large(encode_ints+6,small_index,max_large_index))) - no_swap=1; + fprintf(stderr,"Initial batch\n"); +#endif + insert_batch(input+inpdata,ntriplets_left,prevcoord,encode_ints,0,&nencode); + + /* First we must decide if the next value is large (does not reasonably fit in current small encoding) + Also, if we have not written any values yet, we must begin by writing a large atom. */ + if ((inpdata==0) || (is_quite_large(encode_ints,small_index,max_large_index)) || (refused)) + { + /* If any of the next two atoms are large we should probably write them as large and not swap them */ + int no_swap=0; + if ((is_quite_large(encode_ints+3,small_index,max_large_index)) || (is_quite_large(encode_ints+6,small_index,max_large_index))) + no_swap=1; #if 1 - if (!no_swap) - { - /* If doing inter-frame coding results in smaller values we should not do any swapping either. */ - int frame=inpdata/(natoms*3); - if (frame>0) - { - unsigned int delta[3], delta2[3]; - delta[0]=positive_int(input[inpdata+3]-input[inpdata-natoms*3+3]); - delta[1]=positive_int(input[inpdata+4]-input[inpdata-natoms*3+4]); - delta[2]=positive_int(input[inpdata+5]-input[inpdata-natoms*3+5]); - delta2[0]=positive_int(encode_ints[3]); - delta2[1]=positive_int(encode_ints[4]); - delta2[2]=positive_int(encode_ints[5]); + if (!no_swap) + { + /* If doing inter-frame coding results in smaller values we should not do any swapping either. */ + int frame=inpdata/(natoms*3); + if (frame>0) + { + unsigned int delta[3], delta2[3]; + delta[0]=positive_int(input[inpdata+3]-input[inpdata-natoms*3+3]); + delta[1]=positive_int(input[inpdata+4]-input[inpdata-natoms*3+4]); + delta[2]=positive_int(input[inpdata+5]-input[inpdata-natoms*3+5]); + delta2[0]=positive_int(encode_ints[3]); + delta2[1]=positive_int(encode_ints[4]); + delta2[2]=positive_int(encode_ints[5]); #ifdef SHOWIT - fprintf(stderr,"A1: inter delta: %u %u %u. intra delta=%u %u %u\n", - delta[0],delta[1],delta[2], - delta2[0],delta2[1],delta2[2]); -#endif - if (compute_intlen(delta)*TRESHOLD_INTER_INTRA<compute_intlen(delta2)) - { - delta[0]=positive_int(input[inpdata+6]-input[inpdata-natoms*3+6]); - delta[1]=positive_int(input[inpdata+7]-input[inpdata-natoms*3+7]); - delta[2]=positive_int(input[inpdata+8]-input[inpdata-natoms*3+8]); - delta2[0]=positive_int(encode_ints[6]); - delta2[1]=positive_int(encode_ints[7]); - delta2[2]=positive_int(encode_ints[8]); + fprintf(stderr,"A1: inter delta: %u %u %u. intra delta=%u %u %u\n", + delta[0],delta[1],delta[2], + delta2[0],delta2[1],delta2[2]); +#endif + if (compute_intlen(delta)*TRESHOLD_INTER_INTRA<compute_intlen(delta2)) + { + delta[0]=positive_int(input[inpdata+6]-input[inpdata-natoms*3+6]); + delta[1]=positive_int(input[inpdata+7]-input[inpdata-natoms*3+7]); + delta[2]=positive_int(input[inpdata+8]-input[inpdata-natoms*3+8]); + delta2[0]=positive_int(encode_ints[6]); + delta2[1]=positive_int(encode_ints[7]); + delta2[2]=positive_int(encode_ints[8]); #ifdef SHOWIT - fprintf(stderr,"A2: inter delta: %u %u %u. intra delta=%u %u %u\n", - delta[0],delta[1],delta[2], - delta2[0],delta2[1],delta2[2]); + fprintf(stderr,"A2: inter delta: %u %u %u. intra delta=%u %u %u\n", + delta[0],delta[1],delta[2], + delta2[0],delta2[1],delta2[2]); #endif - if (compute_intlen(delta)*TRESHOLD_INTER_INTRA<compute_intlen(delta2)) - { - no_swap=1; + if (compute_intlen(delta)*TRESHOLD_INTER_INTRA<compute_intlen(delta2)) + { + no_swap=1; #ifdef SHOWIT - fprintf(stderr,"SETTING NO SWAP!\n"); + fprintf(stderr,"SETTING NO SWAP!\n"); #endif - } - } - } - } + } + } + } + } #endif - if (!no_swap) - { - /* Next we must decide if we should swap the first - two values. */ + if (!no_swap) + { + /* Next we must decide if we should swap the first + two values. */ #if 1 - swapdecide(&xtc3_context,input+inpdata,&swapatoms,large_index,xtc3_context.minint); + swapdecide(&xtc3_context,input+inpdata,&swapatoms,large_index,xtc3_context.minint); #else - swapatoms=0; -#endif - /* If we should do the integer swapping manipulation we should do it now */ - if (swapatoms) - { - didswap=1; - for (i=0; i<3; i++) - { - int in[3], out[3]; - in[0]=input[inpdata+i]; - in[1]=input[inpdata+3+i]-input[inpdata+i]; - in[2]=input[inpdata+6+i]-input[inpdata+3+i]; - swap_ints(in,out); - encode_ints[i]=out[0]; - encode_ints[3+i]=out[1]; - encode_ints[6+i]=out[2]; - } - /* We have swapped atoms, so the minimum run-length is 2 */ + swapatoms=0; +#endif + /* If we should do the integer swapping manipulation we should do it now */ + if (swapatoms) + { + didswap=1; + for (i=0; i<3; i++) + { + int in[3], out[3]; + in[0]=input[inpdata+i]; + in[1]=input[inpdata+3+i]-input[inpdata+i]; + in[2]=input[inpdata+6+i]-input[inpdata+3+i]; + swap_ints(in,out); + encode_ints[i]=out[0]; + encode_ints[3+i]=out[1]; + encode_ints[6+i]=out[2]; + } + /* We have swapped atoms, so the minimum run-length is 2 */ #ifdef SHOWIT - fprintf(stderr,"Swap atoms results in:\n"); - for (i=0; i<3; i++) - fprintf(stderr,"%d: %6d %6d %6d\t\t%6d %6d %6d\n",i*3, - encode_ints[i*3], - encode_ints[i*3+1], - encode_ints[i*3+2], - positive_int(encode_ints[i*3]), - positive_int(encode_ints[i*3+1]), - positive_int(encode_ints[i*3+2])); - -#endif - min_runlength=2; - } - } - /* Cache large value for later possible combination with - a sequence of small integers. */ - if ((swapatoms) && (didswap)) - { - buffer_large(&xtc3_context,input,inpdata+3,natoms,0); /* This is a swapped integer, so inpdata is one atom later and - intra coding is not ok. */ - for (ienc=0; ienc<3; ienc++) - prevcoord[ienc]=input[inpdata+3+ienc]; - } - else - { - buffer_large(&xtc3_context,input,inpdata,natoms,1); - for (ienc=0; ienc<3; ienc++) - prevcoord[ienc]=input[inpdata+ienc]; - } + fprintf(stderr,"Swap atoms results in:\n"); + for (i=0; i<3; i++) + fprintf(stderr,"%d: %6d %6d %6d\t\t%6d %6d %6d\n",i*3, + encode_ints[i*3], + encode_ints[i*3+1], + encode_ints[i*3+2], + positive_int(encode_ints[i*3]), + positive_int(encode_ints[i*3+1]), + positive_int(encode_ints[i*3+2])); + +#endif + min_runlength=2; + } + } + /* Cache large value for later possible combination with + a sequence of small integers. */ + if ((swapatoms) && (didswap)) + { + buffer_large(&xtc3_context,input,inpdata+3,natoms,0); /* This is a swapped integer, so inpdata is one atom later and + intra coding is not ok. */ + for (ienc=0; ienc<3; ienc++) + prevcoord[ienc]=input[inpdata+3+ienc]; + } + else + { + buffer_large(&xtc3_context,input,inpdata,natoms,1); + for (ienc=0; ienc<3; ienc++) + prevcoord[ienc]=input[inpdata+ienc]; + } #ifdef SHOWIT - fprintf(stderr,"Prevcoord after packing of large: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Prevcoord after packing of large: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); #endif - /* We have written a large integer so we have one less atoms to worry about */ - inpdata+=3; - ntriplets_left--; + /* We have written a large integer so we have one less atoms to worry about */ + inpdata+=3; + ntriplets_left--; - refused=0; + refused=0; - /* Insert the next batch of integers to be encoded into the buffer */ + /* Insert the next batch of integers to be encoded into the buffer */ #ifdef SHOWIT - fprintf(stderr,"Update batch due to large int.\n"); -#endif - if ((swapatoms) && (didswap)) - { - /* Keep swapped values. */ - for (i=0; i<2; i++) - for (ienc=0; ienc<3; ienc++) - encode_ints[i*3+ienc]=encode_ints[(i+1)*3+ienc]; - } - insert_batch(input+inpdata,ntriplets_left,prevcoord,encode_ints,min_runlength,&nencode); - } - /* Here we should only have differences for the atom coordinates. */ - /* Convert the ints to positive ints */ - for (ienc=0; ienc<nencode; ienc++) - { - int pint=positive_int(encode_ints[ienc]); - encode_ints[ienc]=pint; - } - /* Now we must decide what base and runlength to do. If we have swapped atoms it will be at least 2. - If even the next atom is large, we will not do anything. */ - largest_required_base=0; - /* Determine required base */ - for (ienc=0; ienc<min_runlength*3; ienc++) - if (encode_ints[ienc]>largest_required_base) - largest_required_base=encode_ints[ienc]; - /* Also compute what the largest base is for the current runlength setting! */ - largest_runlength_base=0; - for (ienc=0; (ienc<runlength*3) && (ienc<nencode); ienc++) - if (encode_ints[ienc]>largest_runlength_base) - largest_runlength_base=encode_ints[ienc]; - - largest_required_index=Ptngc_find_magic_index(largest_required_base); - largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); - - if (largest_required_index<largest_runlength_index) - { - new_runlength=min_runlength; - new_small_index=largest_required_index; - } - else - { - new_runlength=runlength; - new_small_index=largest_runlength_index; - } - - /* Only allow increase of runlength wrt min_runlength */ - if (new_runlength<min_runlength) - new_runlength=min_runlength; - - /* If the current runlength is longer than the number of - triplets left stop it from being so. */ - if (new_runlength>ntriplets_left) - new_runlength=ntriplets_left; - - /* We must at least try to get some small integers going. */ - if (new_runlength==0) - { - new_runlength=1; - new_small_index=small_index; - } - - iter_runlength=new_runlength; - iter_small_index=new_small_index; - - /* Iterate to find optimal encoding and runlength */ + fprintf(stderr,"Update batch due to large int.\n"); +#endif + if ((swapatoms) && (didswap)) + { + /* Keep swapped values. */ + for (i=0; i<2; i++) + for (ienc=0; ienc<3; ienc++) + encode_ints[i*3+ienc]=encode_ints[(i+1)*3+ienc]; + } + insert_batch(input+inpdata,ntriplets_left,prevcoord,encode_ints,min_runlength,&nencode); + } + /* Here we should only have differences for the atom coordinates. */ + /* Convert the ints to positive ints */ + for (ienc=0; ienc<nencode; ienc++) + { + int pint=positive_int(encode_ints[ienc]); + encode_ints[ienc]=pint; + } + /* Now we must decide what base and runlength to do. If we have swapped atoms it will be at least 2. + If even the next atom is large, we will not do anything. */ + largest_required_base=0; + /* Determine required base */ + for (ienc=0; ienc<min_runlength*3; ienc++) + if (encode_ints[ienc]>largest_required_base) + largest_required_base=encode_ints[ienc]; + /* Also compute what the largest base is for the current runlength setting! */ + largest_runlength_base=0; + for (ienc=0; (ienc<runlength*3) && (ienc<nencode); ienc++) + if (encode_ints[ienc]>largest_runlength_base) + largest_runlength_base=encode_ints[ienc]; + + largest_required_index=Ptngc_find_magic_index(largest_required_base); + largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); + + if (largest_required_index<largest_runlength_index) + { + new_runlength=min_runlength; + new_small_index=largest_required_index; + } + else + { + new_runlength=runlength; + new_small_index=largest_runlength_index; + } + + /* Only allow increase of runlength wrt min_runlength */ + if (new_runlength<min_runlength) + new_runlength=min_runlength; + + /* If the current runlength is longer than the number of + triplets left stop it from being so. */ + if (new_runlength>ntriplets_left) + new_runlength=ntriplets_left; + + /* We must at least try to get some small integers going. */ + if (new_runlength==0) + { + new_runlength=1; + new_small_index=small_index; + } + + iter_runlength=new_runlength; + iter_small_index=new_small_index; + + /* Iterate to find optimal encoding and runlength */ #ifdef SHOWIT - fprintf(stderr,"Entering iterative loop.\n"); - fflush(stderr); + fprintf(stderr,"Entering iterative loop.\n"); + fflush(stderr); #endif - do { - new_runlength=iter_runlength; - new_small_index=iter_small_index; + do { + new_runlength=iter_runlength; + new_small_index=iter_small_index; #ifdef SHOWIT - fprintf(stderr,"Test new_small_index=%d Base=%d\n",new_small_index,Ptngc_magic(new_small_index)); -#endif - /* What is the largest runlength - we can do with the currently - selected encoding? Also the max supported runlength is MAX_SMALL_RLE triplets! */ - for (ienc=0; ienc<nencode && ienc<MAX_SMALL_RLE*3; ienc++) - { - int test_index=Ptngc_find_magic_index(encode_ints[ienc]); - if (test_index>new_small_index) - break; - } - if (ienc/3>new_runlength) - { - iter_runlength=ienc/3; + fprintf(stderr,"Test new_small_index=%d Base=%d\n",new_small_index,Ptngc_magic(new_small_index)); +#endif + /* What is the largest runlength + we can do with the currently + selected encoding? Also the max supported runlength is MAX_SMALL_RLE triplets! */ + for (ienc=0; ienc<nencode && ienc<MAX_SMALL_RLE*3; ienc++) + { + int test_index=Ptngc_find_magic_index(encode_ints[ienc]); + if (test_index>new_small_index) + break; + } + if (ienc/3>new_runlength) + { + iter_runlength=ienc/3; #ifdef SHOWIT - fprintf(stderr,"I found a new possible runlength: %d\n",iter_runlength); -#endif - } - - /* How large encoding do we have to use? */ - largest_runlength_base=0; - for (ienc=0; ienc<iter_runlength*3; ienc++) - if (encode_ints[ienc]>largest_runlength_base) - largest_runlength_base=encode_ints[ienc]; - largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); - if (largest_runlength_index!=new_small_index) - { - iter_small_index=largest_runlength_index; + fprintf(stderr,"I found a new possible runlength: %d\n",iter_runlength); +#endif + } + + /* How large encoding do we have to use? */ + largest_runlength_base=0; + for (ienc=0; ienc<iter_runlength*3; ienc++) + if (encode_ints[ienc]>largest_runlength_base) + largest_runlength_base=encode_ints[ienc]; + largest_runlength_index=Ptngc_find_magic_index(largest_runlength_base); + if (largest_runlength_index!=new_small_index) + { + iter_small_index=largest_runlength_index; #ifdef SHOWIT - fprintf(stderr,"I found a new possible small index: %d Base=%d\n",iter_small_index,Ptngc_magic(iter_small_index)); + fprintf(stderr,"I found a new possible small index: %d Base=%d\n",iter_small_index,Ptngc_magic(iter_small_index)); #endif - } - } while ((new_runlength!=iter_runlength) || - (new_small_index!=iter_small_index)); + } + } while ((new_runlength!=iter_runlength) || + (new_small_index!=iter_small_index)); #ifdef SHOWIT - fprintf(stderr,"Exit iterative loop.\n"); - fflush(stderr); -#endif - - /* Verify that we got something good. We may have caught a - substantially larger atom. If so we should just bail - out and let the loop get on another lap. We may have a - minimum runlength though and then we have to fulfill - the request to write out these atoms! */ - rle_index_dep=0; - if (new_runlength<3) - rle_index_dep=IS_LARGE; - else if (new_runlength<6) - rle_index_dep=QUITE_LARGE; - if ((min_runlength) - || ((new_small_index<small_index+IS_LARGE) && (new_small_index+rle_index_dep<max_large_index)) + fprintf(stderr,"Exit iterative loop.\n"); + fflush(stderr); +#endif + + /* Verify that we got something good. We may have caught a + substantially larger atom. If so we should just bail + out and let the loop get on another lap. We may have a + minimum runlength though and then we have to fulfill + the request to write out these atoms! */ + rle_index_dep=0; + if (new_runlength<3) + rle_index_dep=IS_LARGE; + else if (new_runlength<6) + rle_index_dep=QUITE_LARGE; + if ((min_runlength) + || ((new_small_index<small_index+IS_LARGE) && (new_small_index+rle_index_dep<max_large_index)) #if 1 - || (new_small_index+IS_LARGE<max_large_index) + || (new_small_index+IS_LARGE<max_large_index) #endif ) - { - /* If doing inter-frame coding of large integers results - in smaller values than the small value we should not - produce a sequence of small values here. */ - int frame=inpdata/(natoms*3); - int numsmaller=0; + { + /* If doing inter-frame coding of large integers results + in smaller values than the small value we should not + produce a sequence of small values here. */ + int frame=inpdata/(natoms*3); + int numsmaller=0; #if 1 - if ((!swapatoms) && (frame>0)) - { - for (i=0; i<new_runlength; i++) - { - unsigned int delta[3]; - unsigned int delta2[3]; - delta[0]=positive_int(input[inpdata+i*3]-input[inpdata-natoms*3+i*3]); - delta[1]=positive_int(input[inpdata+i*3+1]-input[inpdata-natoms*3+i*3+1]); - delta[2]=positive_int(input[inpdata+i*3+2]-input[inpdata-natoms*3+i*3+2]); - delta2[0]=positive_int(encode_ints[i*3]); - delta2[1]=positive_int(encode_ints[i*3+1]); - delta2[2]=positive_int(encode_ints[i*3+2]); - if (compute_intlen(delta)*TRESHOLD_INTER_INTRA<compute_intlen(delta2)) - numsmaller++; - } - } -#endif - /* Most of the values should become smaller, otherwise - we should encode them with intra coding. */ - if ((!swapatoms) && (numsmaller>=2*new_runlength/3)) - { - /* Put all the values in large arrays, instead of the small array */ - if (new_runlength) - { - for (i=0; i<new_runlength; i++) - buffer_large(&xtc3_context,input,inpdata+i*3,natoms,1); - for (i=0; i<3; i++) - prevcoord[i]=input[inpdata+(new_runlength-1)*3+i]; - inpdata+=3*new_runlength; - ntriplets_left-=new_runlength; - } - } - else - { - if ((new_runlength!=runlength) || (new_small_index!=small_index)) - { - int change=new_small_index-small_index; - - if (new_small_index<=0) - change=0; - - if (change<0) - { - int ixx; - for (ixx=0; ixx<new_runlength; ixx++) - { - int rejected; - do { - int ixyz; - double isum=0.; /* ints can be almost 32 bit so multiplication will overflow. So do doubles. */ - for (ixyz=0; ixyz<3; ixyz++) - { - /* encode_ints is already positive (and multiplied by 2 versus the original, just as magic ints) */ - double id=encode_ints[ixx*3+ixyz]; - isum+=id*id; - } - rejected=0; + if ((!swapatoms) && (frame>0)) + { + for (i=0; i<new_runlength; i++) + { + unsigned int delta[3]; + unsigned int delta2[3]; + delta[0]=positive_int(input[inpdata+i*3]-input[inpdata-natoms*3+i*3]); + delta[1]=positive_int(input[inpdata+i*3+1]-input[inpdata-natoms*3+i*3+1]); + delta[2]=positive_int(input[inpdata+i*3+2]-input[inpdata-natoms*3+i*3+2]); + delta2[0]=positive_int(encode_ints[i*3]); + delta2[1]=positive_int(encode_ints[i*3+1]); + delta2[2]=positive_int(encode_ints[i*3+2]); + if (compute_intlen(delta)*TRESHOLD_INTER_INTRA<compute_intlen(delta2)) + numsmaller++; + } + } +#endif + /* Most of the values should become smaller, otherwise + we should encode them with intra coding. */ + if ((!swapatoms) && (numsmaller>=2*new_runlength/3)) + { + /* Put all the values in large arrays, instead of the small array */ + if (new_runlength) + { + for (i=0; i<new_runlength; i++) + buffer_large(&xtc3_context,input,inpdata+i*3,natoms,1); + for (i=0; i<3; i++) + prevcoord[i]=input[inpdata+(new_runlength-1)*3+i]; + inpdata+=3*new_runlength; + ntriplets_left-=new_runlength; + } + } + else + { + if ((new_runlength!=runlength) || (new_small_index!=small_index)) + { + int change=new_small_index-small_index; + + if (new_small_index<=0) + change=0; + + if (change<0) + { + int ixx; + for (ixx=0; ixx<new_runlength; ixx++) + { + int rejected; + do { + int ixyz; + double isum=0.; /* ints can be almost 32 bit so multiplication will overflow. So do doubles. */ + for (ixyz=0; ixyz<3; ixyz++) + { + /* encode_ints is already positive (and multiplied by 2 versus the original, just as magic ints) */ + double id=encode_ints[ixx*3+ixyz]; + isum+=id*id; + } + rejected=0; #ifdef SHOWIT - fprintf(stderr,"Tested decrease %d of index: %g>=%g?\n",change,isum,(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change)); + fprintf(stderr,"Tested decrease %d of index: %g>=%g?\n",change,isum,(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change)); #endif - if (isum>(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change)) - { + if (isum>(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change)) + { #ifdef SHOWIT - fprintf(stderr,"Rejected decrease %d of index due to length of vector: %g>=%g\n",change,isum,(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change)); -#endif - rejected=1; - change++; - } - } while ((change<0) && (rejected)); - if (change==0) - break; - } - } - - /* Always accept the new small indices here. */ - small_index=new_small_index; - /* If we have a new runlength emit it */ - if (runlength!=new_runlength) - { - runlength=new_runlength; - insert_value_in_array(&xtc3_context.instructions, - &xtc3_context.ninstr, - &xtc3_context.ninstr_alloc, - INSTR_SMALL_RUNLENGTH,"instr"); - insert_value_in_array(&xtc3_context.rle, - &xtc3_context.nrle, - &xtc3_context.nrle_alloc, - (unsigned int)runlength,"rle (small)"); - } + fprintf(stderr,"Rejected decrease %d of index due to length of vector: %g>=%g\n",change,isum,(double)Ptngc_magic(small_index+change)*(double)Ptngc_magic(small_index+change)); +#endif + rejected=1; + change++; + } + } while ((change<0) && (rejected)); + if (change==0) + break; + } + } + + /* Always accept the new small indices here. */ + small_index=new_small_index; + /* If we have a new runlength emit it */ + if (runlength!=new_runlength) + { + runlength=new_runlength; + insert_value_in_array(&xtc3_context.instructions, + &xtc3_context.ninstr, + &xtc3_context.ninstr_alloc, + INSTR_SMALL_RUNLENGTH,"instr"); + insert_value_in_array(&xtc3_context.rle, + &xtc3_context.nrle, + &xtc3_context.nrle_alloc, + (unsigned int)runlength,"rle (small)"); + } #ifdef SHOWIT - fprintf(stderr,"Current small index: %d Base=%d\n",small_index,Ptngc_magic(small_index)); -#endif - } - /* If we have a large previous integer we can combine it with a sequence of small ints. */ - if (xtc3_context.has_large) - { - /* If swapatoms is set to 1 but we did actually not - do any swapping, we must first write out the - large atom and then the small. If swapatoms is 1 - and we did swapping we can use the efficient - encoding. */ - if ((swapatoms) && (!didswap)) - { + fprintf(stderr,"Current small index: %d Base=%d\n",small_index,Ptngc_magic(small_index)); +#endif + } + /* If we have a large previous integer we can combine it with a sequence of small ints. */ + if (xtc3_context.has_large) + { + /* If swapatoms is set to 1 but we did actually not + do any swapping, we must first write out the + large atom and then the small. If swapatoms is 1 + and we did swapping we can use the efficient + encoding. */ + if ((swapatoms) && (!didswap)) + { #ifdef SHOWIT - fprintf(stderr,"Swapatoms was set to 1 but we did not do swapping!\n"); - fprintf(stderr,"Only one large integer.\n"); + fprintf(stderr,"Swapatoms was set to 1 but we did not do swapping!\n"); + fprintf(stderr,"Only one large integer.\n"); #endif - /* Flush all large atoms. */ - flush_large(&xtc3_context,xtc3_context.has_large); + /* Flush all large atoms. */ + flush_large(&xtc3_context,xtc3_context.has_large); #ifdef SHOWIT - fprintf(stderr,"Sequence of only small integers.\n"); + fprintf(stderr,"Sequence of only small integers.\n"); #endif - insert_value_in_array(&xtc3_context.instructions, - &xtc3_context.ninstr, - &xtc3_context.ninstr_alloc, - INSTR_ONLY_SMALL,"instr"); - } - else - { + insert_value_in_array(&xtc3_context.instructions, + &xtc3_context.ninstr, + &xtc3_context.ninstr_alloc, + INSTR_ONLY_SMALL,"instr"); + } + else + { #ifdef SHOWIT - fprintf(stderr,"Sequence of one large and small integers (good compression).\n"); -#endif - /* Flush all large atoms but one! */ - if (xtc3_context.has_large>1) - flush_large(&xtc3_context,xtc3_context.has_large-1); - - /* Here we must check if we should emit a large - type change instruction. */ - large_instruction_change(&xtc3_context,0); - - insert_value_in_array(&xtc3_context.instructions, - &xtc3_context.ninstr, - &xtc3_context.ninstr_alloc, - INSTR_DEFAULT,"instr"); - - write_three_large(&xtc3_context,0); - xtc3_context.has_large=0; - } - } - else - { + fprintf(stderr,"Sequence of one large and small integers (good compression).\n"); +#endif + /* Flush all large atoms but one! */ + if (xtc3_context.has_large>1) + flush_large(&xtc3_context,xtc3_context.has_large-1); + + /* Here we must check if we should emit a large + type change instruction. */ + large_instruction_change(&xtc3_context,0); + + insert_value_in_array(&xtc3_context.instructions, + &xtc3_context.ninstr, + &xtc3_context.ninstr_alloc, + INSTR_DEFAULT,"instr"); + + write_three_large(&xtc3_context,0); + xtc3_context.has_large=0; + } + } + else + { #ifdef SHOWIT - fprintf(stderr,"Sequence of only small integers.\n"); -#endif - insert_value_in_array(&xtc3_context.instructions, - &xtc3_context.ninstr, - &xtc3_context.ninstr_alloc, - INSTR_ONLY_SMALL,"instr"); - } - /* Insert the small integers into the small integer array. */ - for (ienc=0; ienc<runlength*3; ienc++) - insert_value_in_array(&xtc3_context.smallintra, - &xtc3_context.nsmallintra, - &xtc3_context.nsmallintra_alloc, - (unsigned int)encode_ints[ienc],"smallintra"); + fprintf(stderr,"Sequence of only small integers.\n"); +#endif + insert_value_in_array(&xtc3_context.instructions, + &xtc3_context.ninstr, + &xtc3_context.ninstr_alloc, + INSTR_ONLY_SMALL,"instr"); + } + /* Insert the small integers into the small integer array. */ + for (ienc=0; ienc<runlength*3; ienc++) + insert_value_in_array(&xtc3_context.smallintra, + &xtc3_context.nsmallintra, + &xtc3_context.nsmallintra_alloc, + (unsigned int)encode_ints[ienc],"smallintra"); #ifdef SHOWIT - for (ienc=0; ienc<runlength; ienc++) - fprintf(stderr,"Small: %d %d %d\n", - encode_ints[ienc*3], - encode_ints[ienc*3+1], - encode_ints[ienc*3+2]); -#endif - /* Update prevcoord. */ - for (ienc=0; ienc<runlength; ienc++) - { + for (ienc=0; ienc<runlength; ienc++) + fprintf(stderr,"Small: %d %d %d\n", + encode_ints[ienc*3], + encode_ints[ienc*3+1], + encode_ints[ienc*3+2]); +#endif + /* Update prevcoord. */ + for (ienc=0; ienc<runlength; ienc++) + { #ifdef SHOWIT - fprintf(stderr,"Prevcoord in packing: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Prevcoord in packing: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); #endif - prevcoord[0]+=unpositive_int(encode_ints[ienc*3]); - prevcoord[1]+=unpositive_int(encode_ints[ienc*3+1]); - prevcoord[2]+=unpositive_int(encode_ints[ienc*3+2]); - } + prevcoord[0]+=unpositive_int(encode_ints[ienc*3]); + prevcoord[1]+=unpositive_int(encode_ints[ienc*3+1]); + prevcoord[2]+=unpositive_int(encode_ints[ienc*3+2]); + } #ifdef SHOWIT - fprintf(stderr,"Prevcoord in packing: %d %d %d\n", - prevcoord[0],prevcoord[1],prevcoord[2]); + fprintf(stderr,"Prevcoord in packing: %d %d %d\n", + prevcoord[0],prevcoord[1],prevcoord[2]); #endif - inpdata+=3*runlength; - ntriplets_left-=runlength; + inpdata+=3*runlength; + ntriplets_left-=runlength; #if 1 - } + } #endif - } - else - { + } + else + { #ifdef SHOWIT - fprintf(stderr,"Refused value: %d old is %d max is %d\n",new_small_index,small_index,max_large_index); - fflush(stderr); + fprintf(stderr,"Refused value: %d old is %d max is %d\n",new_small_index,small_index,max_large_index); + fflush(stderr); #endif - refused=1; - } - } + refused=1; + } + } #ifdef SHOWIT fprintf(stderr,"Number of triplets left is %d\n",ntriplets_left); #endif @@ -1456,9 +1456,9 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp { bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.ninstr)); if (speed>=5) - bwlzh_compress(xtc3_context.instructions,xtc3_context.ninstr,bwlzh_buf,&bwlzh_buf_len); + bwlzh_compress(xtc3_context.instructions,xtc3_context.ninstr,bwlzh_buf,&bwlzh_buf_len); else - bwlzh_compress_no_lz77(xtc3_context.instructions,xtc3_context.ninstr,bwlzh_buf,&bwlzh_buf_len); + bwlzh_compress_no_lz77(xtc3_context.instructions,xtc3_context.ninstr,bwlzh_buf,&bwlzh_buf_len); output_int(output,&outdata,(unsigned int)bwlzh_buf_len); memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); outdata+=bwlzh_buf_len; @@ -1474,9 +1474,9 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp { bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nrle)); if (speed>=5) - bwlzh_compress(xtc3_context.rle,xtc3_context.nrle,bwlzh_buf,&bwlzh_buf_len); + bwlzh_compress(xtc3_context.rle,xtc3_context.nrle,bwlzh_buf,&bwlzh_buf_len); else - bwlzh_compress_no_lz77(xtc3_context.rle,xtc3_context.nrle,bwlzh_buf,&bwlzh_buf_len); + bwlzh_compress_no_lz77(xtc3_context.rle,xtc3_context.nrle,bwlzh_buf,&bwlzh_buf_len); output_int(output,&outdata,(unsigned int)bwlzh_buf_len); memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); outdata+=bwlzh_buf_len; @@ -1491,18 +1491,18 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp if (xtc3_context.nlargedir) { if ((speed<=2) || ((speed<=5) && (!heuristic_bwlzh(xtc3_context.large_direct,xtc3_context.nlargedir)))) - { - bwlzh_buf=NULL; - bwlzh_buf_len=INT_MAX; - } + { + bwlzh_buf=NULL; + bwlzh_buf_len=INT_MAX; + } else - { - bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nlargedir)); - if (speed>=5) - bwlzh_compress(xtc3_context.large_direct,xtc3_context.nlargedir,bwlzh_buf,&bwlzh_buf_len); - else - bwlzh_compress_no_lz77(xtc3_context.large_direct,xtc3_context.nlargedir,bwlzh_buf,&bwlzh_buf_len); - } + { + bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nlargedir)); + if (speed>=5) + bwlzh_compress(xtc3_context.large_direct,xtc3_context.nlargedir,bwlzh_buf,&bwlzh_buf_len); + else + bwlzh_compress_no_lz77(xtc3_context.large_direct,xtc3_context.nlargedir,bwlzh_buf,&bwlzh_buf_len); + } /* If this can be written smaller using base compression we should do that. */ base_buf=warnmalloc((xtc3_context.nlargedir+3)*sizeof(int)); base_compress(xtc3_context.large_direct,xtc3_context.nlargedir,base_buf,&base_buf_len); @@ -1510,19 +1510,19 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp fprintf(stderr,"Large direct: Base len=%d. BWLZH len=%d\n",base_buf_len,bwlzh_buf_len); #endif if (base_buf_len<bwlzh_buf_len) - { - output[outdata++]=0U; - output_int(output,&outdata,(unsigned int)base_buf_len); - memcpy(output+outdata,base_buf,base_buf_len); - outdata+=base_buf_len; - } + { + output[outdata++]=0U; + output_int(output,&outdata,(unsigned int)base_buf_len); + memcpy(output+outdata,base_buf,base_buf_len); + outdata+=base_buf_len; + } else - { - output[outdata++]=1U; - output_int(output,&outdata,(unsigned int)bwlzh_buf_len); - memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); - outdata+=bwlzh_buf_len; - } + { + output[outdata++]=1U; + output_int(output,&outdata,(unsigned int)bwlzh_buf_len); + memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); + outdata+=bwlzh_buf_len; + } free(bwlzh_buf); free(base_buf); } @@ -1535,18 +1535,18 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp if (xtc3_context.nlargeintra) { if ((speed<=2) || ((speed<=5) && (!heuristic_bwlzh(xtc3_context.large_intra_delta,xtc3_context.nlargeintra)))) - { - bwlzh_buf=NULL; - bwlzh_buf_len=INT_MAX; - } + { + bwlzh_buf=NULL; + bwlzh_buf_len=INT_MAX; + } else - { - bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nlargeintra)); - if (speed>=5) - bwlzh_compress(xtc3_context.large_intra_delta,xtc3_context.nlargeintra,bwlzh_buf,&bwlzh_buf_len); - else - bwlzh_compress_no_lz77(xtc3_context.large_intra_delta,xtc3_context.nlargeintra,bwlzh_buf,&bwlzh_buf_len); - } + { + bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nlargeintra)); + if (speed>=5) + bwlzh_compress(xtc3_context.large_intra_delta,xtc3_context.nlargeintra,bwlzh_buf,&bwlzh_buf_len); + else + bwlzh_compress_no_lz77(xtc3_context.large_intra_delta,xtc3_context.nlargeintra,bwlzh_buf,&bwlzh_buf_len); + } /* If this can be written smaller using base compression we should do that. */ base_buf=warnmalloc((xtc3_context.nlargeintra+3)*sizeof(int)); base_compress(xtc3_context.large_intra_delta,xtc3_context.nlargeintra,base_buf,&base_buf_len); @@ -1554,19 +1554,19 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp fprintf(stderr,"Large intra: Base len=%d. BWLZH len=%d\n",base_buf_len,bwlzh_buf_len); #endif if (base_buf_len<bwlzh_buf_len) - { - output[outdata++]=0U; - output_int(output,&outdata,(unsigned int)base_buf_len); - memcpy(output+outdata,base_buf,base_buf_len); - outdata+=base_buf_len; - } + { + output[outdata++]=0U; + output_int(output,&outdata,(unsigned int)base_buf_len); + memcpy(output+outdata,base_buf,base_buf_len); + outdata+=base_buf_len; + } else - { - output[outdata++]=1U; - output_int(output,&outdata,(unsigned int)bwlzh_buf_len); - memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); - outdata+=bwlzh_buf_len; - } + { + output[outdata++]=1U; + output_int(output,&outdata,(unsigned int)bwlzh_buf_len); + memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); + outdata+=bwlzh_buf_len; + } free(bwlzh_buf); free(base_buf); } @@ -1579,18 +1579,18 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp if (xtc3_context.nlargeinter) { if ((speed<=2) || ((speed<=5) && (!heuristic_bwlzh(xtc3_context.large_inter_delta,xtc3_context.nlargeinter)))) - { - bwlzh_buf=NULL; - bwlzh_buf_len=INT_MAX; - } + { + bwlzh_buf=NULL; + bwlzh_buf_len=INT_MAX; + } else - { - bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nlargeinter)); - if (speed>=5) - bwlzh_compress(xtc3_context.large_inter_delta,xtc3_context.nlargeinter,bwlzh_buf,&bwlzh_buf_len); - else - bwlzh_compress_no_lz77(xtc3_context.large_inter_delta,xtc3_context.nlargeinter,bwlzh_buf,&bwlzh_buf_len); - } + { + bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nlargeinter)); + if (speed>=5) + bwlzh_compress(xtc3_context.large_inter_delta,xtc3_context.nlargeinter,bwlzh_buf,&bwlzh_buf_len); + else + bwlzh_compress_no_lz77(xtc3_context.large_inter_delta,xtc3_context.nlargeinter,bwlzh_buf,&bwlzh_buf_len); + } /* If this can be written smaller using base compression we should do that. */ base_buf=warnmalloc((xtc3_context.nlargeinter+3)*sizeof(int)); base_compress(xtc3_context.large_inter_delta,xtc3_context.nlargeinter,base_buf,&base_buf_len); @@ -1598,19 +1598,19 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp fprintf(stderr,"Large inter: Base len=%d. BWLZH len=%d\n",base_buf_len,bwlzh_buf_len); #endif if (base_buf_len<bwlzh_buf_len) - { - output[outdata++]=0U; - output_int(output,&outdata,(unsigned int)base_buf_len); - memcpy(output+outdata,base_buf,base_buf_len); - outdata+=base_buf_len; - } + { + output[outdata++]=0U; + output_int(output,&outdata,(unsigned int)base_buf_len); + memcpy(output+outdata,base_buf,base_buf_len); + outdata+=base_buf_len; + } else - { - output[outdata++]=1U; - output_int(output,&outdata,(unsigned int)bwlzh_buf_len); - memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); - outdata+=bwlzh_buf_len; - } + { + output[outdata++]=1U; + output_int(output,&outdata,(unsigned int)bwlzh_buf_len); + memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); + outdata+=bwlzh_buf_len; + } free(bwlzh_buf); free(base_buf); } @@ -1623,18 +1623,18 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp if (xtc3_context.nsmallintra) { if ((speed<=2) || ((speed<=5) && (!heuristic_bwlzh(xtc3_context.smallintra,xtc3_context.nsmallintra)))) - { - bwlzh_buf=NULL; - bwlzh_buf_len=INT_MAX; - } + { + bwlzh_buf=NULL; + bwlzh_buf_len=INT_MAX; + } else - { - bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nsmallintra)); - if (speed>=5) - bwlzh_compress(xtc3_context.smallintra,xtc3_context.nsmallintra,bwlzh_buf,&bwlzh_buf_len); - else - bwlzh_compress_no_lz77(xtc3_context.smallintra,xtc3_context.nsmallintra,bwlzh_buf,&bwlzh_buf_len); - } + { + bwlzh_buf=warnmalloc(bwlzh_get_buflen(xtc3_context.nsmallintra)); + if (speed>=5) + bwlzh_compress(xtc3_context.smallintra,xtc3_context.nsmallintra,bwlzh_buf,&bwlzh_buf_len); + else + bwlzh_compress_no_lz77(xtc3_context.smallintra,xtc3_context.nsmallintra,bwlzh_buf,&bwlzh_buf_len); + } /* If this can be written smaller using base compression we should do that. */ base_buf=warnmalloc((xtc3_context.nsmallintra+3)*sizeof(int)); base_compress(xtc3_context.smallintra,xtc3_context.nsmallintra,base_buf,&base_buf_len); @@ -1642,19 +1642,19 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp fprintf(stderr,"Small intra: Base len=%d. BWLZH len=%d\n",base_buf_len,bwlzh_buf_len); #endif if (base_buf_len<bwlzh_buf_len) - { - output[outdata++]=0U; - output_int(output,&outdata,(unsigned int)base_buf_len); - memcpy(output+outdata,base_buf,base_buf_len); - outdata+=base_buf_len; - } + { + output[outdata++]=0U; + output_int(output,&outdata,(unsigned int)base_buf_len); + memcpy(output+outdata,base_buf,base_buf_len); + outdata+=base_buf_len; + } else - { - output[outdata++]=1U; - output_int(output,&outdata,(unsigned int)bwlzh_buf_len); - memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); - outdata+=bwlzh_buf_len; - } + { + output[outdata++]=1U; + output_int(output,&outdata,(unsigned int)bwlzh_buf_len); + memcpy(output+outdata,bwlzh_buf,bwlzh_buf_len); + outdata+=bwlzh_buf_len; + } free(bwlzh_buf); free(base_buf); } @@ -1665,13 +1665,13 @@ unsigned char *Ptngc_pack_array_xtc3(int *input, int *length, int natoms, int sp } static void decompress_bwlzh_block(unsigned char **ptr, - int nvals, - unsigned int **vals) + int nvals, + unsigned int **vals) { int bwlzh_buf_len=(int)(((unsigned int)(*ptr)[0]) | - (((unsigned int)(*ptr)[1])<<8) | - (((unsigned int)(*ptr)[2])<<16) | - (((unsigned int)(*ptr)[3])<<24)); + (((unsigned int)(*ptr)[1])<<8) | + (((unsigned int)(*ptr)[2])<<16) | + (((unsigned int)(*ptr)[3])<<24)); (*ptr)+=4; *vals=warnmalloc(nvals*sizeof (**vals)); bwlzh_decompress(*ptr,nvals,*vals); @@ -1679,13 +1679,13 @@ static void decompress_bwlzh_block(unsigned char **ptr, } static void decompress_base_block(unsigned char **ptr, - int nvals, - unsigned int **vals) + int nvals, + unsigned int **vals) { int base_buf_len=(int)(((unsigned int)(*ptr)[0]) | - (((unsigned int)(*ptr)[1])<<8) | - (((unsigned int)(*ptr)[2])<<16) | - (((unsigned int)(*ptr)[3])<<24)); + (((unsigned int)(*ptr)[1])<<8) | + (((unsigned int)(*ptr)[2])<<16) | + (((unsigned int)(*ptr)[3])<<24)); (*ptr)+=4; *vals=warnmalloc(nvals*sizeof (**vals)); base_decompress(*ptr,nvals,*vals); @@ -1693,11 +1693,11 @@ static void decompress_base_block(unsigned char **ptr, } static void unpack_one_large(struct xtc3_context *xtc3_context, - int *ilargedir, int *ilargeintra, - int *ilargeinter, int *prevcoord, - int *minint, int *output, - int outdata, int didswap, - int natoms, int current_large_type) + int *ilargedir, int *ilargeintra, + int *ilargeinter, int *prevcoord, + int *minint, int *output, + int outdata, int didswap, + int natoms, int current_large_type) { int large_ints[3]={0,0,0}; if (current_large_type==0 && xtc3_context->large_direct) @@ -1717,11 +1717,11 @@ static void unpack_one_large(struct xtc3_context *xtc3_context, else if (xtc3_context->large_inter_delta) { large_ints[0]=unpositive_int(xtc3_context->large_inter_delta[(*ilargeinter)]) - +output[outdata-natoms*3+didswap*3]; + +output[outdata-natoms*3+didswap*3]; large_ints[1]=unpositive_int(xtc3_context->large_inter_delta[(*ilargeinter)+1]) - +output[outdata-natoms*3+1+didswap*3]; + +output[outdata-natoms*3+1+didswap*3]; large_ints[2]=unpositive_int(xtc3_context->large_inter_delta[(*ilargeinter)+2]) - +output[outdata-natoms*3+2+didswap*3]; + +output[outdata-natoms*3+2+didswap*3]; (*ilargeinter)+=3; } prevcoord[0]=large_ints[0]; @@ -1760,78 +1760,78 @@ int Ptngc_unpack_array_xtc3(unsigned char *packed,int *output, int length, int n for (i=0; i<3; i++) { minint[i]=unpositive_int((int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24))); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24))); ptr+=4; } xtc3_context.ninstr=(int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24)); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24)); ptr+=4; if (xtc3_context.ninstr) decompress_bwlzh_block(&ptr,xtc3_context.ninstr,&xtc3_context.instructions); xtc3_context.nrle=(int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24)); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24)); ptr+=4; if (xtc3_context.nrle) decompress_bwlzh_block(&ptr,xtc3_context.nrle,&xtc3_context.rle); xtc3_context.nlargedir=(int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24)); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24)); ptr+=4; if (xtc3_context.nlargedir) { if (*ptr++==1) - decompress_bwlzh_block(&ptr,xtc3_context.nlargedir,&xtc3_context.large_direct); + decompress_bwlzh_block(&ptr,xtc3_context.nlargedir,&xtc3_context.large_direct); else - decompress_base_block(&ptr,xtc3_context.nlargedir,&xtc3_context.large_direct); + decompress_base_block(&ptr,xtc3_context.nlargedir,&xtc3_context.large_direct); } xtc3_context.nlargeintra=(int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24)); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24)); ptr+=4; if (xtc3_context.nlargeintra) { if (*ptr++==1) - decompress_bwlzh_block(&ptr,xtc3_context.nlargeintra,&xtc3_context.large_intra_delta); + decompress_bwlzh_block(&ptr,xtc3_context.nlargeintra,&xtc3_context.large_intra_delta); else - decompress_base_block(&ptr,xtc3_context.nlargeintra,&xtc3_context.large_intra_delta); + decompress_base_block(&ptr,xtc3_context.nlargeintra,&xtc3_context.large_intra_delta); } xtc3_context.nlargeinter=(int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24)); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24)); ptr+=4; if (xtc3_context.nlargeinter) { if (*ptr++==1) - decompress_bwlzh_block(&ptr,xtc3_context.nlargeinter,&xtc3_context.large_inter_delta); + decompress_bwlzh_block(&ptr,xtc3_context.nlargeinter,&xtc3_context.large_inter_delta); else - decompress_base_block(&ptr,xtc3_context.nlargeinter,&xtc3_context.large_inter_delta); + decompress_base_block(&ptr,xtc3_context.nlargeinter,&xtc3_context.large_inter_delta); } xtc3_context.nsmallintra=(int)(((unsigned int)ptr[0]) | - (((unsigned int)ptr[1])<<8) | - (((unsigned int)ptr[2])<<16) | - (((unsigned int)ptr[3])<<24)); + (((unsigned int)ptr[1])<<8) | + (((unsigned int)ptr[2])<<16) | + (((unsigned int)ptr[3])<<24)); ptr+=4; if (xtc3_context.nsmallintra) { if (*ptr++==1) - decompress_bwlzh_block(&ptr,xtc3_context.nsmallintra,&xtc3_context.smallintra); + decompress_bwlzh_block(&ptr,xtc3_context.nsmallintra,&xtc3_context.smallintra); else - decompress_base_block(&ptr,xtc3_context.nsmallintra,&xtc3_context.smallintra); + decompress_base_block(&ptr,xtc3_context.nsmallintra,&xtc3_context.smallintra); } /* Initial prevcoord is the minimum integers. */ @@ -1849,103 +1849,103 @@ int Ptngc_unpack_array_xtc3(unsigned char *packed,int *output, int length, int n fprintf(stderr,"ntriplets left=%d\n",ntriplets_left); #endif if ((instr==INSTR_DEFAULT) /* large+small */ - || (instr==INSTR_ONLY_LARGE) /* only large */ - || (instr==INSTR_ONLY_SMALL)) /* only small */ - { - if (instr!=INSTR_ONLY_SMALL) - { - int didswap=0; - if ((instr==INSTR_DEFAULT) && (swapatoms)) - didswap=1; - unpack_one_large(&xtc3_context,&ilargedir, &ilargeintra, &ilargeinter, - prevcoord, minint, output, outdata, didswap, - natoms, current_large_type); - ntriplets_left--; - outdata+=3; - } - if (instr!=INSTR_ONLY_LARGE) - { - for (i=0; i<runlength; i++) - { - prevcoord[0]+=unpositive_int(xtc3_context.smallintra[ismallintra]); - prevcoord[1]+=unpositive_int(xtc3_context.smallintra[ismallintra+1]); - prevcoord[2]+=unpositive_int(xtc3_context.smallintra[ismallintra+2]); - ismallintra+=3; - output[outdata+i*3]=prevcoord[0]; - output[outdata+i*3+1]=prevcoord[1]; - output[outdata+i*3+2]=prevcoord[2]; + || (instr==INSTR_ONLY_LARGE) /* only large */ + || (instr==INSTR_ONLY_SMALL)) /* only small */ + { + if (instr!=INSTR_ONLY_SMALL) + { + int didswap=0; + if ((instr==INSTR_DEFAULT) && (swapatoms)) + didswap=1; + unpack_one_large(&xtc3_context,&ilargedir, &ilargeintra, &ilargeinter, + prevcoord, minint, output, outdata, didswap, + natoms, current_large_type); + ntriplets_left--; + outdata+=3; + } + if (instr!=INSTR_ONLY_LARGE) + { + for (i=0; i<runlength; i++) + { + prevcoord[0]+=unpositive_int(xtc3_context.smallintra[ismallintra]); + prevcoord[1]+=unpositive_int(xtc3_context.smallintra[ismallintra+1]); + prevcoord[2]+=unpositive_int(xtc3_context.smallintra[ismallintra+2]); + ismallintra+=3; + output[outdata+i*3]=prevcoord[0]; + output[outdata+i*3+1]=prevcoord[1]; + output[outdata+i*3+2]=prevcoord[2]; #ifdef SHOWIT - fprintf(stderr,"Unpack small: %d %d %d\n",prevcoord[0],prevcoord[1],prevcoord[2]); -#endif - } - if ((instr==INSTR_DEFAULT) && (swapatoms)) - { - for (i=0; i<3; i++) - { - int tmp=output[outdata-3+i]; - output[outdata-3+i]=output[outdata+i]; - output[outdata+i]=tmp; - } + fprintf(stderr,"Unpack small: %d %d %d\n",prevcoord[0],prevcoord[1],prevcoord[2]); +#endif + } + if ((instr==INSTR_DEFAULT) && (swapatoms)) + { + for (i=0; i<3; i++) + { + int tmp=output[outdata-3+i]; + output[outdata-3+i]=output[outdata+i]; + output[outdata+i]=tmp; + } #ifdef SHOWIT - fprintf(stderr,"Unswap results in\n"); - for (i=0; i<3; i++) - fprintf(stderr,"%d %d %d\n",output[outdata-3+i*3],output[outdata-2+i*3],output[outdata-1+i*3]); -#endif - } - ntriplets_left-=runlength; - outdata+=runlength*3; - } - } + fprintf(stderr,"Unswap results in\n"); + for (i=0; i<3; i++) + fprintf(stderr,"%d %d %d\n",output[outdata-3+i*3],output[outdata-2+i*3],output[outdata-1+i*3]); +#endif + } + ntriplets_left-=runlength; + outdata+=runlength*3; + } + } else if (instr==INSTR_LARGE_RLE && irle<xtc3_context.nrle) - { - int large_rle=xtc3_context.rle[irle++]; + { + int large_rle=xtc3_context.rle[irle++]; #ifdef SHOWIT - fprintf(stderr,"large_rle=%d @ %d\n",large_rle,irle-1); -#endif - for (i=0; i<large_rle; i++) - { - unpack_one_large(&xtc3_context,&ilargedir, &ilargeintra, &ilargeinter, - prevcoord, minint, output, outdata, 0, - natoms, current_large_type); - ntriplets_left--; - outdata+=3; - } - } + fprintf(stderr,"large_rle=%d @ %d\n",large_rle,irle-1); +#endif + for (i=0; i<large_rle; i++) + { + unpack_one_large(&xtc3_context,&ilargedir, &ilargeintra, &ilargeinter, + prevcoord, minint, output, outdata, 0, + natoms, current_large_type); + ntriplets_left--; + outdata+=3; + } + } else if (instr==INSTR_SMALL_RUNLENGTH && irle<xtc3_context.nrle) - { - runlength=xtc3_context.rle[irle++]; + { + runlength=xtc3_context.rle[irle++]; #ifdef SHOWIT - fprintf(stderr,"small_rle=%d @ %d\n",runlength,irle-1); + fprintf(stderr,"small_rle=%d @ %d\n",runlength,irle-1); #endif - } + } else if (instr==INSTR_FLIP) - { - swapatoms=1-swapatoms; + { + swapatoms=1-swapatoms; #ifdef SHOWIT - fprintf(stderr,"new flip=%d\n",swapatoms); + fprintf(stderr,"new flip=%d\n",swapatoms); #endif - } + } else if (instr==INSTR_LARGE_DIRECT) - { - current_large_type=0; + { + current_large_type=0; #ifdef SHOWIT - fprintf(stderr,"large direct\n"); + fprintf(stderr,"large direct\n"); #endif - } + } else if (instr==INSTR_LARGE_INTRA_DELTA) - { - current_large_type=1; + { + current_large_type=1; #ifdef SHOWIT - fprintf(stderr,"large intra delta\n"); + fprintf(stderr,"large intra delta\n"); #endif - } + } else if (instr==INSTR_LARGE_INTER_DELTA) - { - current_large_type=2; + { + current_large_type=2; #ifdef SHOWIT - fprintf(stderr,"large inter delta\n"); + fprintf(stderr,"large inter delta\n"); #endif - } + } } if (ntriplets_left<0) { diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index 5229c2f..27d57a8 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -258,7 +258,7 @@ struct tng_trajectory { /** A handle to the input file */ FILE *input_file; /** The length of the input file */ - long int input_file_len; + long input_file_len; /** The path of the output trajectory file */ char *output_file_path; /** A handle to the output file */ @@ -343,9 +343,9 @@ struct tng_trajectory { /** The currently active frame set */ struct tng_trajectory_frame_set current_trajectory_frame_set; /** The pos in the src file of the current frame set */ - long int current_trajectory_frame_set_input_file_pos; + long current_trajectory_frame_set_input_file_pos; /** The pos in the dest file of the current frame set */ - long int current_trajectory_frame_set_output_file_pos; + long current_trajectory_frame_set_output_file_pos; /** The number of frame sets in the trajectory N.B. Not saved in file and * cannot be trusted to be up-to-date */ int64_t n_trajectory_frame_sets; @@ -2947,7 +2947,7 @@ static tng_function_status tng_frame_set_block_read tng_gen_block_t block, const char hash_mode) { - long int file_pos; + long file_pos; int offset = 0; int64_t i, prev_n_particles; tng_bool same_hash; @@ -2985,7 +2985,7 @@ static tng_function_status tng_frame_set_block_read /* FIXME: Does not check if the size of the contents matches the expected * size or if the contents can be read. */ - file_pos = (long int)ftell(tng_data->input_file) - + file_pos = (int64_t)ftell(tng_data->input_file) - (block->block_contents_size + block->header_contents_size); if(hash_mode == TNG_USE_HASH) @@ -3863,7 +3863,7 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, { dest = tng_compress_pos_float_find_algo(start_pos, (int)n_particles, (int)algo_find_n_frames, - 0.001, 0, + (float)0.001, 0, tng_data-> compress_algo_pos, &new_len); @@ -3871,7 +3871,7 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, if(algo_find_n_frames < n_frames) { dest = tng_compress_pos_float(start_pos, (int)n_particles, - (int)n_frames, 0.001, 0, + (int)n_frames, (float)0.001, 0, tng_data->compress_algo_pos, &new_len); } @@ -3898,7 +3898,7 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, if(type == TNG_FLOAT_DATA) { dest = tng_compress_pos_float(start_pos, (int)n_particles, - (int)n_frames, 0.001, 0, + (int)n_frames, (float)0.001, 0, tng_data->compress_algo_pos, &new_len); } else @@ -3930,14 +3930,14 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, { dest = tng_compress_vel_float_find_algo(start_pos, (int)n_particles, (int)algo_find_n_frames, - 0.001, 0, + (float)0.001, 0, tng_data-> compress_algo_vel, &new_len); if(algo_find_n_frames < n_frames) { dest = tng_compress_vel_float(start_pos, (int)n_particles, - (int)n_frames, 0.001, 0, + (int)n_frames, (float)0.001, 0, tng_data->compress_algo_pos, &new_len); } @@ -3964,7 +3964,7 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, if(type == TNG_FLOAT_DATA) { dest = tng_compress_vel_float(start_pos, (int)n_particles, - (int)n_frames, 0.001, 0, + (int)n_frames, (float)0.001, 0, tng_data-> compress_algo_vel, &new_len); @@ -3985,7 +3985,7 @@ static tng_function_status tng_compress(tng_trajectory_t tng_data, return(TNG_FAILURE); } - offset = (char *)start_pos - block->block_contents; + offset = (unsigned long)((char *)start_pos - block->block_contents); block->block_contents_size = new_len + offset; @@ -4022,7 +4022,8 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, char *temp; double *d_dest = 0; float *f_dest = 0; - int offset, result; + unsigned long offset; + int result; (void)tng_data; if(block->id != TNG_TRAJ_POSITIONS && @@ -4068,10 +4069,10 @@ static tng_function_status tng_uncompress(tng_trajectory_t tng_data, return(TNG_FAILURE); } - offset = (char *)start_pos - (char *)block->block_contents; + offset = (unsigned long)((char *)start_pos - (char *)block->block_contents); - block->block_contents_size = uncompressed_len + offset; + block->block_contents_size = (int64_t)(uncompressed_len + offset); temp = realloc(block->block_contents, uncompressed_len + offset); if(!temp) @@ -4120,7 +4121,7 @@ static tng_function_status tng_gzip_compress(tng_trajectory_t tng_data, { Bytef *dest; char *temp; - uLong max_len, stat, offset; + unsigned long max_len, stat, offset; (void)tng_data; max_len = compressBound(len); @@ -4593,7 +4594,7 @@ static tng_function_status tng_particle_data_read if(codec_id != TNG_UNCOMPRESSED) { - data_size = n_frames_div * size * n_particles * n_values; + data_size = (unsigned long)(n_frames_div * size * n_particles * n_values); switch(codec_id) { case TNG_XTC_COMPRESSION: @@ -4739,10 +4740,10 @@ static tng_function_status tng_particle_data_block_write const char hash_mode) { int64_t n_particles, num_first_particle, n_frames, stride_length; - int64_t frame_step; + int64_t frame_step, data_start_pos; int64_t i, j, k; - int offset = 0, size, data_start_pos; - unsigned int len; + int offset = 0, size; + size_t len; char dependency, temp, *temp_name; double multiplier; char ***first_dim_values, **second_dim_values; @@ -4803,7 +4804,7 @@ static tng_function_status tng_particle_data_block_write temp_name = realloc(block->name, len); if(!temp_name) { - printf("Cannot allocate memory (%d bytes). %s: %d\n", len, + printf("Cannot allocate memory (%ld bytes). %s: %d\n", len, __FILE__, __LINE__); free(block->name); block->name = 0; @@ -5096,7 +5097,7 @@ static tng_function_status tng_particle_data_block_write { for(i = offset; i < block->block_contents_size; i+=size) { - *(float *)(block->block_contents + i) *= multiplier; + *(float *)(block->block_contents + i) *= (float)multiplier; if(tng_data->input_endianness_swap_func_32 && tng_data->input_endianness_swap_func_32(tng_data, (int32_t *)(block->block_contents + i)) @@ -5183,7 +5184,7 @@ static tng_function_status tng_particle_data_block_write data->codec_id = TNG_UNCOMPRESSED; break; case TNG_TNG_COMPRESSION: - printf("TEST: frame_step: %"PRId64", n_particles: %"PRId64", block_contents_size: %"PRId64", start_pos: %d\n", + printf("TEST: frame_step: %"PRId64", n_particles: %"PRId64", block_contents_size: %"PRId64", start_pos: %"PRId64"\n", frame_step, n_particles, block->block_contents_size, data_start_pos); stat = tng_compress(tng_data, block, frame_step, n_particles, data->datatype, @@ -7059,8 +7060,7 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_get tng_molecule_t molecule, int64_t *cnt) { - int index = -1; - int64_t i; + int64_t i, index = -1; for(i = tng_data->n_molecules; i--;) { @@ -7084,8 +7084,7 @@ tng_function_status DECLSPECDLLEXPORT tng_molecule_cnt_set tng_molecule_t molecule, const int64_t cnt) { - int index = -1; - int64_t i, old_cnt; + int64_t i, old_cnt, index = -1; for(i = tng_data->n_molecules; i--;) { @@ -7307,8 +7306,7 @@ tng_function_status DECLSPECDLLEXPORT tng_chain_residue_w_id_add const int64_t id, tng_residue_t *residue) { - int64_t i; - int curr_index; + int64_t i, curr_index; tng_residue_t new_residues, temp_residue, last_residue; tng_molecule_t molecule = chain->molecule; tng_function_status stat = TNG_SUCCESS; @@ -9585,7 +9583,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_nr_find fseek(tng_data->input_file, (long)file_pos, SEEK_SET); - tng_data->current_trajectory_frame_set_input_file_pos = file_pos; + tng_data->current_trajectory_frame_set_input_file_pos = (long)file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -9912,7 +9910,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_of_frame_find fseek(tng_data->input_file, (long)file_pos, SEEK_SET); - tng_data->current_trajectory_frame_set_input_file_pos = file_pos; + tng_data->current_trajectory_frame_set_input_file_pos = (long)file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -10435,7 +10433,7 @@ tng_function_status DECLSPECDLLEXPORT tng_block_read_next(tng_trajectory_t tng_d tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next(tng_trajectory_t tng_data, const char hash_mode) { - long int file_pos; + long file_pos; tng_gen_block_t block; tng_function_status stat = TNG_SUCCESS; @@ -10444,11 +10442,11 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_read_next(tng_trajectory_t t return(TNG_CRITICAL); } - file_pos = (long int)tng_data->current_trajectory_frame_set.next_frame_set_file_pos; + file_pos = (long)tng_data->current_trajectory_frame_set.next_frame_set_file_pos; if(file_pos < 0 && tng_data->current_trajectory_frame_set_input_file_pos <= 0) { - file_pos = tng_data->first_trajectory_frame_set_input_file_pos; + file_pos = (long)tng_data->first_trajectory_frame_set_input_file_pos; } if(file_pos > 0) @@ -10535,8 +10533,9 @@ tng_function_status tng_frame_set_write(tng_trajectory_t tng_data, tng_function_status stat; tng_data->current_trajectory_frame_set_output_file_pos = - tng_data->last_trajectory_frame_set_output_file_pos = ftell(tng_data->output_file); + tng_data->last_trajectory_frame_set_output_file_pos = + tng_data->current_trajectory_frame_set_output_file_pos; if(tng_data->current_trajectory_frame_set_output_file_pos <= 0) { @@ -10613,7 +10612,7 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_set_new const int64_t first_frame, const int64_t n_frames) { - int i; + int64_t i; tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; tng_particle_mapping_t mapping; @@ -10987,7 +10986,8 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_block_add const int64_t codec_id, void *new_data) { - int i, j, k, size, len; + int i, size, len; + int64_t j, k; int64_t tot_n_particles, n_frames_div; char ***first_dim_values, **second_dim_values; tng_trajectory_frame_set_t frame_set; @@ -11140,8 +11140,9 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_data_write { int64_t header_pos, file_pos; int64_t output_file_len, n_values_per_frame, size, contents_size; - int64_t header_size, temp_first, temp_last, temp_current; + int64_t header_size, temp_first, temp_last; int64_t i, last_frame; + long temp_current; tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; FILE *temp = tng_data->input_file; @@ -11553,9 +11554,10 @@ tng_function_status DECLSPECDLLEXPORT tng_frame_particle_data_write { int64_t header_pos, file_pos, tot_n_particles; int64_t output_file_len, n_values_per_frame, size, contents_size; - int64_t header_size, temp_first, temp_last, temp_current; + int64_t header_size, temp_first, temp_last; int64_t mapping_block_end_pos, num_first_particle, block_n_particles; int64_t i, last_frame; + long temp_current; tng_gen_block_t block; tng_trajectory_frame_set_t frame_set; FILE *temp = tng_data->input_file; @@ -12134,7 +12136,7 @@ tng_function_status DECLSPECDLLEXPORT tng_data_values_free const int64_t n_values_per_frame, const char type) { - int i, j; + int64_t i, j; (void)tng_data; if(values) @@ -12244,7 +12246,7 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_values_free const int64_t n_values_per_frame, const char type) { - int i, j, k; + int64_t i, j, k; (void)tng_data; if(values) @@ -12289,8 +12291,8 @@ tng_function_status DECLSPECDLLEXPORT tng_data_get int64_t *n_values_per_frame, char *type) { - int64_t file_pos, block_index; - int i, j, len, size; + int64_t i, j, file_pos, block_index; + int len, size; tng_non_particle_data_t data; tng_trajectory_frame_set_t frame_set = &tng_data->current_trajectory_frame_set; @@ -12531,7 +12533,8 @@ tng_function_status DECLSPECDLLEXPORT tng_data_interval_get { int64_t i, j, n_frames, file_pos, current_frame_pos, first_frame; int64_t block_index; - int len, size; + int size; + size_t len; tng_non_particle_data_t data; tng_trajectory_frame_set_t frame_set; tng_gen_block_t block; @@ -13267,7 +13270,8 @@ tng_function_status DECLSPECDLLEXPORT tng_particle_data_interval_get { int64_t i, j, k, mapping, n_frames, file_pos, current_frame_pos, i_step; int64_t first_frame, block_index; - int len, size; + int size; + size_t len; tng_particle_data_t data; tng_trajectory_frame_set_t frame_set; tng_gen_block_t block; |