contrib/python-zstandard/zstd/common/bitstream.h
changeset 40121 73fef626dae3
parent 37495 b1fb341d8a61
child 42070 675775c33ab6
equal deleted inserted replaced
40120:89742f1fa6cb 40121:73fef626dae3
     1 /* ******************************************************************
     1 /* ******************************************************************
     2    bitstream
     2    bitstream
     3    Part of FSE library
     3    Part of FSE library
     4    header file (to include)
     4    Copyright (C) 2013-present, Yann Collet.
     5    Copyright (C) 2013-2017, Yann Collet.
       
     6 
     5 
     7    BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
     6    BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
     8 
     7 
     9    Redistribution and use in source and binary forms, with or without
     8    Redistribution and use in source and binary forms, with or without
    10    modification, are permitted provided that the following conditions are
     9    modification, are permitted provided that the following conditions are
    47 
    46 
    48 /*-****************************************
    47 /*-****************************************
    49 *  Dependencies
    48 *  Dependencies
    50 ******************************************/
    49 ******************************************/
    51 #include "mem.h"            /* unaligned access routines */
    50 #include "mem.h"            /* unaligned access routines */
       
    51 #include "debug.h"          /* assert(), DEBUGLOG(), RAWLOG() */
    52 #include "error_private.h"  /* error codes and messages */
    52 #include "error_private.h"  /* error codes and messages */
    53 
       
    54 
       
    55 /*-*************************************
       
    56 *  Debug
       
    57 ***************************************/
       
    58 #if defined(BIT_DEBUG) && (BIT_DEBUG>=1)
       
    59 #  include <assert.h>
       
    60 #else
       
    61 #  ifndef assert
       
    62 #    define assert(condition) ((void)0)
       
    63 #  endif
       
    64 #endif
       
    65 
    53 
    66 
    54 
    67 /*=========================================
    55 /*=========================================
    68 *  Target specific
    56 *  Target specific
    69 =========================================*/
    57 =========================================*/
    81 ********************************************/
    69 ********************************************/
    82 /* bitStream can mix input from multiple sources.
    70 /* bitStream can mix input from multiple sources.
    83  * A critical property of these streams is that they encode and decode in **reverse** direction.
    71  * A critical property of these streams is that they encode and decode in **reverse** direction.
    84  * So the first bit sequence you add will be the last to be read, like a LIFO stack.
    72  * So the first bit sequence you add will be the last to be read, like a LIFO stack.
    85  */
    73  */
    86 typedef struct
    74 typedef struct {
    87 {
       
    88     size_t bitContainer;
    75     size_t bitContainer;
    89     unsigned bitPos;
    76     unsigned bitPos;
    90     char*  startPtr;
    77     char*  startPtr;
    91     char*  ptr;
    78     char*  ptr;
    92     char*  endPtr;
    79     char*  endPtr;
   116 
   103 
   117 
   104 
   118 /*-********************************************
   105 /*-********************************************
   119 *  bitStream decoding API (read backward)
   106 *  bitStream decoding API (read backward)
   120 **********************************************/
   107 **********************************************/
   121 typedef struct
   108 typedef struct {
   122 {
       
   123     size_t   bitContainer;
   109     size_t   bitContainer;
   124     unsigned bitsConsumed;
   110     unsigned bitsConsumed;
   125     const char* ptr;
   111     const char* ptr;
   126     const char* start;
   112     const char* start;
   127     const char* limitPtr;
   113     const char* limitPtr;
   234     bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
   220     bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
   235     bitC->bitPos += nbBits;
   221     bitC->bitPos += nbBits;
   236 }
   222 }
   237 
   223 
   238 /*! BIT_addBitsFast() :
   224 /*! BIT_addBitsFast() :
   239  *  works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
   225  *  works only if `value` is _clean_,
       
   226  *  meaning all high bits above nbBits are 0 */
   240 MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
   227 MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC,
   241                                 size_t value, unsigned nbBits)
   228                                 size_t value, unsigned nbBits)
   242 {
   229 {
   243     assert((value>>nbBits) == 0);
   230     assert((value>>nbBits) == 0);
   244     assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
   231     assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);