SURFEX v7.3
General documentation of Surfex
 All Classes Files Functions Variables Typedefs
/home/dasprezs/EXPORT_v7_3/src/LIB/LFI_COMPRESS/srcc/bitbuff.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #ifdef VPP
00006 # include <sys/types.h>
00007 typedef __uint64_t WORD;
00008 #else
00009 #ifdef SX5
00010 typedef unsigned long uint64_t;
00011 #else
00012 # include <inttypes.h>
00013 #endif
00014 typedef uint64_t WORD;
00015 #endif
00016 
00017 #define WORDSIZE 64
00018 
00019 #ifdef NO_UNDERSCORE
00020 # define SET_FILLIDX set_fillidx
00021 # define GET_FILLIDX get_fillidx
00022 # define FILL_BBUFF  fill_bbuff
00023 # define SET_EXTRACTIDX set_extractidx
00024 # define GET_EXTRACTIDX get_extractidx
00025 # define EXTRACT_BBUFF  extract_bbuff
00026 #else
00027 # define SET_FILLIDX set_fillidx_
00028 # define GET_FILLIDX get_fillidx_
00029 # define FILL_BBUFF  fill_bbuff_
00030 # define SET_EXTRACTIDX set_extractidx_
00031 # define GET_EXTRACTIDX get_extractidx_
00032 # define EXTRACT_BBUFF  extract_bbuff_
00033 #endif
00034 
00035 int outidx = 0;
00036 int outbrem = WORDSIZE ;
00037 
00038 int inidx = 0;
00039 int inbrem = WORDSIZE;
00040 
00041 void SET_FILLIDX(unsigned *idx, unsigned *bitoffset){
00042     inidx  = *idx;
00043     inidx += (*bitoffset/WORDSIZE);
00044     inbrem = WORDSIZE - (*bitoffset%WORDSIZE);
00045 }
00046 
00047 void GET_FILLIDX(unsigned *idx, unsigned *bitoffset){
00048     *idx    = inidx;
00049     *bitoffset = WORDSIZE - inbrem;
00050 }
00051 
00052 void FILL_BBUFF(WORD *out, int *n, unsigned *val){
00053     /* inidx = index of the current buffer elt to fill */
00054     /* inbrem = number of bits remaining on buffer elt out[idx] */
00055 
00056     /* fill buffer out with n low bits of val */
00057 
00058     if (inbrem >= *n){
00059         inbrem = inbrem - *n;
00060         /* turn to 0 the n bits of out */
00061         out[inidx] &= ~(~(~(WORD)0 << *n) << inbrem);
00062         /* now set the n bits of out to val */
00063         out[inidx] |= (*val & ~(~(WORD)0 << *n)) << inbrem;
00064         return;
00065     } else {
00066         int nex = *n - inbrem; /* number of bits that will be filled later */
00067         if (inbrem != 0){
00068             /* turn to 0 the inbrem lower bits of out */
00069             out[inidx] &= (~(WORD)0 << inbrem) ;
00070             /* now set the inbrem lower bits of out with val */
00071             out[inidx] |= ((*val >> nex) & ~(~(WORD)0 << inbrem));
00072         }
00073         inidx++;
00074         inbrem = WORDSIZE;
00075         FILL_BBUFF(out, &nex, val);
00076     }
00077 
00078 }
00079 
00080 void SET_EXTRACTIDX(unsigned *idx, unsigned *bitoffset) {
00081     outidx = *idx;
00082     outidx += (*bitoffset/WORDSIZE);
00083     outbrem = WORDSIZE-(*bitoffset%WORDSIZE);
00084 }
00085 
00086 void GET_EXTRACTIDX(unsigned *idx, unsigned *bitoffset){
00087     *idx = outidx;
00088     *bitoffset = WORDSIZE - outbrem;
00089 }
00090 
00091     
00092 void extract_bbuff_rec(WORD *buff, int *n, unsigned *val) {
00093     
00094     if (outbrem >= *n){
00095         outbrem = outbrem - *n;
00096         *val = (*val << *n) | (unsigned)((buff[outidx]>>outbrem) & ~(~(WORD)0 << *n));
00097         return;
00098     } else {
00099         int nex = *n - outbrem;
00100         if (outbrem != 0){
00101             *val = (*val << outbrem)| (unsigned)(buff[outidx] & ~(~(WORD)0 << outbrem));
00102 
00103         }
00104         outidx++;
00105         outbrem=WORDSIZE;
00106         extract_bbuff_rec(buff,&nex,val);
00107     }
00108 }
00109 
00110 void EXTRACT_BBUFF(WORD *buff, int *n, unsigned *val) {
00111     
00112     unsigned tmpval;
00113 
00114     tmpval=0;
00115     extract_bbuff_rec(buff,n,&tmpval);
00116     *val = tmpval;
00117 }
00118