1  /**************************************************************************


2  *


3  * bitio_m.h  Macros for bitio


4  * Copyright (C) 1994 Neil Sharman and Alistair Moffat


5  *


6  * This program is free software; you can redistribute it and/or modify


7  * it under the terms of the GNU General Public License as published by


8  * the Free Software Foundation; either version 2 of the License, or


9  * (at your option) any later version.


10  *


11  * This program is distributed in the hope that it will be useful,


12  * but WITHOUT ANY WARRANTY; without even the implied warranty of


13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


14  * GNU General Public License for more details.


15  *


16  * You should have received a copy of the GNU General Public License


17  * along with this program; if not, write to the Free Software


18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


19  *


20  **************************************************************************/


21 


22 


23  #ifndef H_BITIO_M


24  #define H_BITIO_M


25 


26  /* ==================================== */


27 


28  #ifndef BIO_ENCODE_PROLOGUE


29  #define BIO_ENCODE_PROLOGUE


30  #endif


31 


32  #ifndef BIO_DECODE_PROLOGUE


33  #define BIO_DECODE_PROLOGUE


34  #endif


35 


36  #ifndef BIO_ENCODE_EPILOGUE


37  #define BIO_ENCODE_EPILOGUE


38  #endif


39 


40  #ifndef BIO_DECODE_EPILOGUE


41  #define BIO_DECODE_EPILOGUE


42  #endif


43 


44  #ifndef DECODE_ADD


45  #define DECODE_ADD(b) (b) += (b) + DECODE_BIT


46  #endif


47 


48  /* ==================================== */


49 


50  #define POSITIVE(f, x) \


51  if ((x)<=0) \


52  fprintf(stderr,"Error: Cannot "#f" encode %lu\n",(unsigned long)x),exit(1);


53 


54 


55  #define CEILLOG_2(x,v) \


56  do { \


57  register int _B_x = (x)  1; \


58  (v) = 0; \


59  for (; _B_x ; _B_x>>=1, (v)++); \


60  } while(0)


61 


62 


63  #define FLOORLOG_2(x,v) \


64  do { \


65  register int _B_x = (x); \


66  (v) = 1; \


67  for (; _B_x ; _B_x>>=1, (v)++); \


68  } while(0)


69 


70 


71  /****************************************************************************/


72 


73 


74 


75  #define UNARY_ENCODE(x) \


76  do { \


77  register unsigned long _B_x = (x); \


78  BIO_ENCODE_PROLOGUE; \


79  POSITIVE(unary, _B_x); \


80  while(_B_x) ENCODE_BIT(0); \


81  ENCODE_BIT(1); \


82  BIO_ENCODE_EPILOGUE; \


83  } while(0)


84 


85  #define UNARY_ENCODE_L(x, count) \


86  do { \


87  register unsigned long _B_x = (x); \


88  BIO_ENCODE_PROLOGUE; \


89  POSITIVE(unary, _B_x); \


90  (count) += _B_x; \


91  while(_B_x) ENCODE_BIT(0); \


92  ENCODE_BIT(1); \


93  BIO_ENCODE_EPILOGUE; \


94  } while(0)


95 


96  #define UNARY_DECODE(x) \


97  do { \


98  BIO_DECODE_PROLOGUE; \


99  (x) = 1; \


100  while (!DECODE_BIT) (x)++; \


101  BIO_DECODE_EPILOGUE; \


102  } while(0)


103 


104  #define UNARY_DECODE_L(x, count) \


105  do { \


106  BIO_DECODE_PROLOGUE; \


107  (x) = 1; \


108  while (!DECODE_BIT) (x)++; \


109  (count) += (x); \


110  BIO_DECODE_EPILOGUE; \


111  } while(0)


112 


113 


114  #define UNARY_LENGTH(x, count) \


115  do { \


116  POSITIVE(unary, x); \


117  (count) = (x); \


118  } while(0)


119 


120 


121  /****************************************************************************/


122 


123 


124  #define BINARY_ENCODE(x, b) \


125  do { \


126  register unsigned long _B_x = (x); \


127  register unsigned long _B_b = (b); \


128  register int _B_nbits, _B_logofb, _B_thresh; \


129  BIO_ENCODE_PROLOGUE; \


130  POSITIVE(binary, _B_x); \


131  CEILLOG_2(_B_b, _B_logofb); \


132  _B_thresh = (1<<_B_logofb)  _B_b; \


133  if (_B_x < _B_thresh) \


134  _B_nbits = _B_logofb1; \


135  else \


136  { \


137  _B_nbits = _B_logofb; \


138  _B_x += _B_thresh; \


139  } \


140  while (_B_nbits>=0) \


141  ENCODE_BIT((_B_x>>_B_nbits) & 0x1); \


142  BIO_ENCODE_EPILOGUE; \


143  } while(0)


144 


145  #define BINARY_ENCODE_L(x, b, count) \


146  do { \


147  register unsigned long _B_x = (x); \


148  register unsigned long _B_b = (b); \


149  register int _B_nbits, _B_logofb, _B_thresh; \


150  BIO_ENCODE_PROLOGUE; \


151  POSITIVE(binary, _B_x); \


152  CEILLOG_2(_B_b, _B_logofb); \


153  _B_thresh = (1<<_B_logofb)  _B_b; \


154  if (_B_x < _B_thresh) \


155  _B_nbits = _B_logofb1; \


156  else \


157  { \


158  _B_nbits = _B_logofb; \


159  _B_x += _B_thresh; \


160  } \


161  (count) += _B_nbits; \


162  while (_B_nbits>=0) \


163  ENCODE_BIT((_B_x>>_B_nbits) & 0x1); \


164  BIO_ENCODE_EPILOGUE; \


165  } while(0)


166 


167 


168  #define BINARY_DECODE(x, b) \


169  do { \


170  register unsigned long _B_x = 0; \


171  register unsigned long _B_b = (b); \


172  register int _B_i, _B_logofb, _B_thresh; \


173  BIO_DECODE_PROLOGUE; \


174  if (_B_b != 1) \


175  { \


176  CEILLOG_2(_B_b, _B_logofb); \


177  _B_thresh = (1<<_B_logofb)  _B_b; \


178  _B_logofb; \


179  for (_B_i=0; _B_i < _B_logofb; _B_i++) \


180  DECODE_ADD(_B_x); \


181  if (_B_x >= _B_thresh) \


182  { \


183  DECODE_ADD(_B_x); \


184  _B_x = _B_thresh; \


185  } \


186  (x) = _B_x+1; \


187  } \


188  else \


189  (x) = 1; \


190  BIO_DECODE_EPILOGUE; \


191  } while(0)


192 


193  #define BINARY_DECODE_L(x, b, count) \


194  do { \


195  register unsigned long _B_x = 0; \


196  register unsigned long _B_b = (b); \


197  register int _B_i, _B_logofb, _B_thresh; \


198  BIO_DECODE_PROLOGUE; \


199  if (_B_b != 1) \


200  { \


201  CEILLOG_2(_B_b, _B_logofb); \


202  _B_thresh = (1<<_B_logofb)  _B_b; \


203  _B_logofb; \


204  (count) += _B_logofb; \


205  for (_B_i=0; _B_i < _B_logofb; _B_i++) \


206  DECODE_ADD(_B_x); \


207  if (_B_x >= _B_thresh) \


208  { \


209  DECODE_ADD(_B_x); \


210  _B_x = _B_thresh; \


211  (count)++; \


212  } \


213  (x) = _B_x+1; \


214  } \


215  else \


216  (x) = 1; \


217  BIO_DECODE_EPILOGUE; \


218  } while(0)


219 


220  #define BINARY_LENGTH(x, b, count) \


221  do { \


222  register unsigned long _B_x = (x); \


223  register unsigned long _B_b = (b); \


224  register int _B_nbits, _B_logofb, _B_thresh; \


225  POSITIVE(binary, _B_x); \


226  CEILLOG_2(_B_b, _B_logofb); \


227  _B_thresh = (1<<_B_logofb)  _B_b; \


228  if (_B_x < _B_thresh) \


229  _B_nbits = _B_logofb1; \


230  else \


231  _B_nbits = _B_logofb; \


232  (count) = _B_nbits; \


233  } while(0)


234 


235  /****************************************************************************/


236 


237 


238 


239 


240  #define GAMMA_ENCODE(x) \


241  do { \


242  register unsigned long _B_xx = (x); \


243  register int _B_logofb; \


244  register int _B_nbits; \


245  BIO_ENCODE_PROLOGUE; \


246  POSITIVE(gamma, _B_xx); \


247  FLOORLOG_2(_B_xx, _B_logofb); \


248  _B_nbits = _B_logofb+1; \


249  while(_B_logofb) ENCODE_BIT(0); \


250  while (_B_nbits>=0) \


251  ENCODE_BIT((_B_xx>>_B_nbits) & 0x1); \


252  BIO_ENCODE_EPILOGUE; \


253  } while (0)


254 


255  #define GAMMA_ENCODE_L(x, count) \


256  do { \


257  register unsigned long _B_xx = (x); \


258  register int _B_logofb; \


259  register int _B_nbits; \


260  BIO_ENCODE_PROLOGUE; \


261  POSITIVE(gamma, _B_xx); \


262  FLOORLOG_2(_B_xx, _B_logofb); \


263  _B_nbits = _B_logofb+1; \


264  (count) += _B_logofb*2+1; \


265  while(_B_logofb) ENCODE_BIT(0); \


266  while (_B_nbits>=0) \


267  ENCODE_BIT((_B_xx>>_B_nbits) & 0x1); \


268  BIO_ENCODE_EPILOGUE; \


269  } while (0)


270 


271  #define GAMMA_DECODE(x) \


272  do { \


273  register unsigned long _B_xx = 1; \


274  register int _B_nbits = 0; \


275  BIO_DECODE_PROLOGUE; \


276  while(!DECODE_BIT) _B_nbits++; \


277  while (_B_nbits > 0) \


278  DECODE_ADD(_B_xx); \


279  (x) = _B_xx; \


280  BIO_DECODE_EPILOGUE; \


281  } while (0)


282 


283  #define GAMMA_DECODE_L(x, count) \


284  do { \


285  register unsigned long _B_xx = 1; \


286  register int _B_nbits = 0; \


287  BIO_DECODE_PROLOGUE; \


288  while(!DECODE_BIT) _B_nbits++; \


289  (count) += _B_nbits*2+1; \


290  while (_B_nbits > 0) \


291  DECODE_ADD(_B_xx); \


292  (x) = _B_xx; \


293  BIO_DECODE_EPILOGUE; \


294  } while (0)


295 


296  #define GAMMA_LENGTH(x, count) \


297  do { \


298  register unsigned long _B_xx = (x); \


299  register int _B_logofb; \


300  POSITIVE(gamma, _B_xx); \


301  FLOORLOG_2(_B_xx, _B_logofb); \


302  (count) = _B_logofb*2+1; \


303  } while (0)


304 


305 


306 


307  /****************************************************************************/


308 


309 


310  #define DELTA_ENCODE(x) \


311  do { \


312  register unsigned long _B_xxx = (x); \


313  register int _B_logx; \


314  FLOORLOG_2(_B_xxx, _B_logx); \


315  GAMMA_ENCODE(_B_logx+1); \


316  BINARY_ENCODE(_B_xxx+1, 1<<_B_logx); \


317  } while (0)


318 


319  #define DELTA_ENCODE_L(x, count) \


320  do { \


321  register unsigned long _B_xxx = (x); \


322  register int _B_logx; \


323  FLOORLOG_2(_B_xxx, _B_logx); \


324  GAMMA_ENCODE_L(_B_logx+1, count); \


325  BINARY_ENCODE_L(_B_xxx+1, 1<<_B_logx, count); \


326  } while (0)


327 


328 


329  #define DELTA_DECODE(x) \


330  do { \


331  register unsigned long _B_xxx; \


332  register int _B_logx; \


333  GAMMA_DECODE(_B_logx); _B_logx; \


334  BINARY_DECODE(_B_xxx, 1<<_B_logx); _B_xxx; \


335  (x) = _B_xxx + (1<<_B_logx); \


336  } while (0)


337 


338  #define DELTA_DECODE_L(x, count) \


339  do { \


340  register unsigned long _B_xxx; \


341  register int _B_logx; \


342  GAMMA_DECODE_L(_B_logx, count); _B_logx; \


343  BINARY_DECODE_L(_B_xxx, 1<<_B_logx, count); _B_xxx; \


344  (x) = _B_xxx + (1<<_B_logx); \


345  } while (0)


346 


347  #define DELTA_LENGTH(x, count) \


348  do { \


349  register unsigned long _B_xxx = (x); \


350  register int _B_logx, _B_dcount; \


351  FLOORLOG_2(_B_xxx, _B_logx); \


352  GAMMA_LENGTH(_B_logx+1, count); \


353  BINARY_LENGTH(_B_xxx+1, 1<<_B_logx, _B_dcount); \


354  (count) += _B_dcount; \


355  } while (0)


356 


357 


358  /****************************************************************************/


359 


360 


361 


362 


363  #define ELIAS_ENCODE(x, b, s) \


364  do { \


365  register unsigned long _B_xx = (x); \


366  register unsigned long _B_b = (b); \


367  register double _B_s = (s); \


368  register int _B_lower=1, _B_upper=1; \


369  register int _B_k=0; \


370  register double _B_pow=1.0; \


371  POSITIVE(elias, _B_xx); \


372  while (_B_xx>_B_upper) \


373  { \


374  _B_k++; \


375  _B_lower = _B_upper+1; \


376  _B_pow *= _B_s; \


377  _B_upper += _B_b*_B_pow; \


378  } \


379  UNARY_ENCODE(_B_k+1); \


380  BINARY_ENCODE(_B_xx_B_lower+1, _B_upper_B_lower+1); \


381  } while (0)


382 


383  #define ELIAS_ENCODE_L(x, b, s, count) \


384  do { \


385  register unsigned long _B_xx = (x); \


386  register unsigned long _B_b = (b); \


387  register double _B_s = (s); \


388  register int _B_lower=1, _B_upper=1; \


389  register int _B_k=0; \


390  register double _B_pow=1.0; \


391  POSITIVE(elias, _B_xx); \


392  while (_B_xx>_B_upper) \


393  { \


394  _B_k++; \


395  _B_lower = _B_upper+1; \


396  _B_pow *= _B_s; \


397  _B_upper += _B_b*_B_pow; \


398  } \


399  UNARY_ENCODE_L(_B_k+1, count); \


400  BINARY_ENCODE_L(_B_xx_B_lower+1, _B_upper_B_lower+1, count); \


401  } while (0)


402 


403  #define ELIAS_DECODE(x, b, s) \


404  do { \


405  register unsigned long _B_xx; \


406  register unsigned long _B_b = (b); \


407  register double _B_s = (s); \


408  register int _B_lower=1, _B_upper=1; \


409  register int _B_k; \


410  register double _B_pow=1.0; \


411  UNARY_DECODE(_B_k); _B_k; \


412  while (_B_k) \


413  { \


414  _B_lower = _B_upper+1; \


415  _B_pow *= _B_s; \


416  _B_upper += _B_b*_B_pow; \


417  } \


418  BINARY_DECODE(_B_xx, _B_upper_B_lower+1); _B_xx; \


419  POSITIVE(gamma, _B_xx+_B_lower); \


420  (x) = _B_xx+_B_lower; \


421  } while (0)


422 


423  #define ELIAS_DECODE_L(x, b, s, count) \


424  do { \


425  register unsigned long _B_xx; \


426  register unsigned long _B_b = (b); \


427  register double _B_s = (s); \


428  register int _B_lower=1, _B_upper=1; \


429  register int _B_k; \


430  register double _B_pow=1.0; \


431  UNARY_DECODE_L(_B_k, count); _B_k; \


432  while (_B_k) \


433  { \


434  _B_lower = _B_upper+1; \


435  _B_pow *= _B_s; \


436  _B_upper += _B_b*_B_pow; \


437  } \


438  BINARY_DECODE_L(_B_xx, _B_upper_B_lower+1, count); _B_xx; \


439  POSITIVE(gamma, _B_xx+_B_lower); \


440  (x) = _B_xx+_B_lower; \


441  } while (0)


442 


443  #define ELIAS_LENGTH(x, b, s, count) \


444  do { \


445  register unsigned long _B_xx = (x); \


446  register unsigned long _B_b = (b); \


447  register double _B_s = (s); \


448  register int _B_lower=1, _B_upper=1; \


449  register int _B_k=0, _B_ecount; \


450  register double _B_pow=1.0; \


451  POSITIVE(gamma, _B_xx); \


452  while (_B_xx>_B_upper) \


453  { \


454  _B_k++; \


455  _B_lower = _B_upper+1; \


456  _B_pow *= _B_s; \


457  _B_upper += _B_b*_B_pow; \


458  } \


459  UNARY_LENGTH(_B_k+1, count); \


460  BINARY_LENGTH(_B_xx_B_lower+1, _B_upper_B_lower+1, _B_ecount); \


461  (count) += _B_ecount; \


462  } while (0)


463 


464 


465  /****************************************************************************/


466 


467 


468  #define BBLOCK_ENCODE(x, b) \


469  do { \


470  register unsigned long _B_xx = (x); \


471  register unsigned long _B_bb = (b); \


472  register int _B_xdivb = 0; \


473  POSITIVE(bblock, _B_xx); \


474  _B_xx; \


475  while (_B_xx >= _B_bb) \


476  { \


477  _B_xdivb++; \


478  _B_xx = _B_bb; \


479  } \


480  UNARY_ENCODE(_B_xdivb+1); \


481  BINARY_ENCODE(_B_xx+1, _B_bb); \


482  } while (0)


483 


484  #define BBLOCK_ENCODE_L(x, b, count) \


485  do { \


486  register unsigned long _B_xx = (x); \


487  register unsigned long _B_bb = (b); \


488  register int _B_xdivb = 0; \


489  POSITIVE(bblock, _B_xx); \


490  _B_xx; \


491  while (_B_xx >= _B_bb) \


492  { \


493  _B_xdivb++; \


494  _B_xx = _B_bb; \


495  } \


496  UNARY_ENCODE_L(_B_xdivb+1, count); \


497  BINARY_ENCODE_L(_B_xx+1, _B_bb, count); \


498  } while (0)


499 


500  #define BBLOCK_DECODE(x, b) \


501  do { \


502  register unsigned long _B_x1, _B_xx = 0; \


503  register unsigned long _B_bb = (b); \


504  register int _B_xdivb; \


505  UNARY_DECODE(_B_xdivb); _B_xdivb; \


506  while (_B_xdivb) \


507  _B_xx += _B_bb; \


508  BINARY_DECODE(_B_x1, _B_bb); \


509  (x) = _B_xx+_B_x1; \


510  } while (0)


511 


512  #define BBLOCK_DECODE_L(x, b, count) \


513  do { \


514  register unsigned long _B_x1, _B_xx = 0; \


515  register unsigned long _B_bb = (b); \


516  register int _B_xdivb; \


517  UNARY_DECODE_L(_B_xdivb, count); _B_xdivb; \


518  while (_B_xdivb) \


519  _B_xx += _B_bb; \


520  BINARY_DECODE_L(_B_x1, _B_bb, count); \


521  (x) = _B_xx+_B_x1; \


522  } while (0)


523 


524  #define BBLOCK_LENGTH(x, b, count) \


525  do { \


526  register unsigned long _B_bcount, _B_xx = (x); \


527  register unsigned long _B_bb = (b); \


528  register int _B_xdivb = 0; \


529  POSITIVE(bblock, _B_xx); \


530  _B_xx; \


531  while (_B_xx >= _B_bb) \


532  { \


533  _B_xdivb++; \


534  _B_xx = _B_bb; \


535  } \


536  UNARY_LENGTH(_B_xdivb+1, count); \


537  BINARY_LENGTH(_B_xx+1, _B_bb, _B_bcount); \


538  (count) += _B_bcount; \


539  } while (0)


540 


541  #endif

