@@ -583,18 +583,6 @@ int32_t td5d(const unsigned char *inVals, unsigned char *outVals, const uint32_t
583
583
}
584
584
} // end td5d
585
585
586
- static inline void esmOutputBits (unsigned char * outVals , const uint32_t nBits , const uint32_t bitVal , uint32_t * nextOutIx , uint32_t * nextOutBit )
587
- {
588
- // output 1 to 8 bits
589
- outVals [* nextOutIx ] |= (unsigned char )(bitVal << * nextOutBit );
590
- * nextOutBit += nBits ;
591
- if (* nextOutBit >= 8 )
592
- {
593
- * nextOutBit -= 8 ;
594
- outVals [++ (* nextOutIx )] = (unsigned char )bitVal >> (nBits - * nextOutBit );
595
- }
596
- } // end esmOutputBits
597
-
598
586
static uint32_t textNBitsTable [MAX_PREDEFINED_FREQUENCY_CHAR_COUNT ]= {
599
587
3 , 3 , 4 , 4 ,
600
588
5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ,
@@ -704,6 +692,7 @@ int32_t encodeAdaptiveTextMode(const unsigned char *inVals, unsigned char *outVa
704
692
uint32_t inVal ;
705
693
uint32_t nextOutIx = 1 ;
706
694
uint32_t nextOutBit = 0 ;
695
+ uint64_t outBits = 0 ; // store 64 bits before writing
707
696
uint32_t eVal ;
708
697
const uint32_t * textEncodingArray = extendedTextEncoding ;
709
698
const uint32_t output7or8 = highBitclear ? 7 : 8 ;
@@ -714,22 +703,21 @@ int32_t encodeAdaptiveTextMode(const unsigned char *inVals, unsigned char *outVa
714
703
setAdaptiveChars (val256 , outVals , nValues , & textEncodingArray );
715
704
if (highBitclear )
716
705
outVals [0 ] |= 128 ; // set high bit of info byte to indicate 7-bit values
717
- outVals [1 ] = 0 ; // init first value used by esmOutputBits
718
706
while (pInVal < pLastInValPlusOne )
719
707
{
720
708
eVal = textEncodingArray [(inVal = (unsigned char )* (pInVal ++ ))];
721
709
if (eVal < MAX_PREDEFINED_FREQUENCY_CHAR_COUNT )
722
710
{
723
711
// encode predefined chars and adaptive chars
724
- esmOutputBits (outVals , textNBitsTable [eVal ], textBitValTable [eVal ], & nextOutIx , & nextOutBit );
712
+ thisOutIx2 (outVals , textNBitsTable [eVal ], textBitValTable [eVal ], & nextOutIx , & nextOutBit , & outBits );
725
713
}
726
714
else
727
715
{
728
716
// output char not predefined or adaptive
729
717
if (nextOutIx > maxBytes )
730
718
return 0 ; // requested compression not met
731
- esmOutputBits (outVals , 3 , 0x5 , & nextOutIx , & nextOutBit );
732
- esmOutputBits (outVals , output7or8 , inVal , & nextOutIx , & nextOutBit ); // output 7 bits if high bit clear, else 8
719
+ thisOutIx2 (outVals , 3 , 0x5 , & nextOutIx , & nextOutBit , & outBits );
720
+ thisOutIx2 (outVals , output7or8 , inVal , & nextOutIx , & nextOutBit , & outBits ); // output 7 bits if high bit clear, else 8
733
721
#ifdef TD64_TEST_MODE
734
722
if (textEncodingArray == extendedTextEncoding )
735
723
g_td64Text8bitCount ++ ;
@@ -738,7 +726,8 @@ int32_t encodeAdaptiveTextMode(const unsigned char *inVals, unsigned char *outVa
738
726
#endif
739
727
}
740
728
}
741
- return nextOutIx * 8 + nextOutBit ;
729
+ esmOutputRemainder (outVals , & nextOutIx , & nextOutBit , & outBits );
730
+ return nextOutIx * 8 ;
742
731
} // end encodeAdaptiveTextMode
743
732
744
733
int32_t encodeSingleValueMode (const unsigned char * inVals , unsigned char * outVals , const uint32_t nValues , int32_t singleValue , const uint32_t compressNSV )
@@ -940,6 +929,7 @@ int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, co
940
929
//uint32_t nextOutIx=nUniquesIn + 1; // start of encoding past uniques written from outer loop;
941
930
uint32_t nextOutIx ; // round for now
942
931
uint32_t nextOutBit = 1 ; // first bit indicates 1 or 2 uniques in first two input values
932
+ uint64_t outBits ; // store 64 bits before writing
943
933
const uint32_t maxBytes = maxBits /8 ; // compare against bytes out during loop
944
934
945
935
if (nUniquesIn > 32 || nUniquesIn < MIN_STRING_MODE_UNIQUES )
@@ -956,7 +946,6 @@ int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, co
956
946
nextOutIx = nUniquesIn + 1 ;
957
947
outVals [0 ] = 1 | (unsigned char )((nUniquesIn - 17 )<<4 ); // indicate string mode in first 3 bits, 0 for uniques uncompressed, then number uniques - 17 (excess 16 as always 17+ values) in next 4 bits
958
948
}
959
- outVals [nextOutIx ] = 0 ; // init for esmOutputBits
960
949
// output two initial values
961
950
// first unique assumed
962
951
const unsigned char inVal0 = inVals [0 ];
@@ -965,15 +954,15 @@ int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, co
965
954
// first two values are the same
966
955
nUniques = 1 ;
967
956
// output 1 to indicate first unique value repeated
968
- outVals [ nextOutIx ] = 1 ; // 1=repeat for second value
957
+ outBits = 1 ; // 1=repeat for second value
969
958
twoValsPos [inVal0 ] = 1 ;
970
959
}
971
960
else
972
961
{
973
962
// second val is a new unique
974
963
nUniques = 2 ;
975
964
// set up position of 2nd unique
976
- outVals [ nextOutIx ] = 0 ; // 0=uniques in first two values
965
+ outBits = 0 ; // 0=uniques in first two values
977
966
twoValsPos [inVal0 ] = 1 ;
978
967
twoValsPos [inVals [1 ]] = 2 ;
979
968
}
@@ -991,10 +980,11 @@ int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, co
991
980
{
992
981
// first occurrence of this unique
993
982
// output a 0 to indicate new unique
994
- if (++ nextOutBit == 8 )
983
+ if (++ nextOutBit == 64 )
995
984
{
996
- // update out index and next out bit
997
- outVals [++ nextOutIx ] = 0 ;
985
+ // output outBits and init for next output
986
+ esmOutputOutBits (outVals , & nextOutIx , & outBits );
987
+ outBits = 0 ;
998
988
nextOutBit = 0 ;
999
989
}
1000
990
twoValsPos [inVal ] = inPos ;
@@ -1008,7 +998,7 @@ int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, co
1008
998
{
1009
999
// pos of unique plus one and next input value match
1010
1000
// output repeated value: 01 plus unique
1011
- esmOutputBits (outVals , 2 + encodingBits [nUniques - 1 ], 1 |(uoInVal <<2 ), & nextOutIx , & nextOutBit );
1001
+ thisOutIx2 (outVals , 2 + encodingBits [nUniques - 1 ], 1 |(uoInVal <<2 ), & nextOutIx , & nextOutBit , & outBits );
1012
1002
continue ;
1013
1003
}
1014
1004
// look for continuation of matching characters
@@ -1025,22 +1015,21 @@ int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, co
1025
1015
tvPos ++ ;
1026
1016
}
1027
1017
// output 11 plus 3 more bits for string length 2 to 9
1028
- esmOutputBits (outVals , 5 , 3 | ((strCount - 2 )<<2 ), & nextOutIx , & nextOutBit );
1018
+ thisOutIx2 (outVals , 5 , 3 | ((strCount - 2 )<<2 ), & nextOutIx , & nextOutBit , & outBits );
1029
1019
// output the unique that started this string, which gives its position
1030
- esmOutputBits (outVals , encodingBits [nUniques - 1 ], uoInVal , & nextOutIx , & nextOutBit );
1020
+ thisOutIx2 (outVals , encodingBits [nUniques - 1 ], uoInVal , & nextOutIx , & nextOutBit , & outBits );
1031
1021
inPos += strCount - 1 ;
1032
1022
nextInVal = inVals [inPos ]; // new next val after string
1033
1023
}
1034
1024
else
1035
1025
{
1036
1026
// this pair doesn't match the one for first occurrence of this unique
1037
1027
// repeated value: 01
1038
- esmOutputBits (outVals , 2 + encodingBits [nUniques - 1 ], 1 |(uoInVal <<2 ), & nextOutIx , & nextOutBit );
1028
+ thisOutIx2 (outVals , 2 + encodingBits [nUniques - 1 ], 1 |(uoInVal <<2 ), & nextOutIx , & nextOutBit , & outBits );
1039
1029
}
1040
1030
}
1031
+ esmOutputRemainder (outVals , & nextOutIx , & nextOutBit , & outBits );
1041
1032
// output final bits
1042
- if (nextOutBit > 0 )
1043
- nextOutIx ++ ; // index past final bits
1044
1033
if (inPos < nValues )
1045
1034
{
1046
1035
outVals [nextOutIx ++ ] = inVals [lastPos ]; // output last input byte
@@ -1524,19 +1513,19 @@ int32_t decodeAdaptiveTextMode(const unsigned char *inVals, unsigned char *outVa
1524
1513
{
1525
1514
// peak at the next 7 bits to decide what to do
1526
1515
dtbmPeekBits (7 , bitPos , & theBits , & dtbmThisInVal );
1527
- if ((theBits & 7 ) == 5 )
1516
+ if ((theBits & 7 ) != 5 )
1517
+ {
1518
+ // output the corresponding text char and skip corresponding number of bits
1519
+ outVals [nextOutVal ++ ] = (unsigned char )pTextChars [textDecodePos [theBits ]];
1520
+ dtbmSkipBits (inVals , textDecodeBits [theBits ], & thisInValIx , & bitPos , & dtbmThisInVal );
1521
+ }
1522
+ else
1528
1523
{
1529
1524
// skip three bits, get 7 or 8 more bits and output original value
1530
1525
dtbmSkipBits (inVals , 3 , & thisInValIx , & bitPos , & dtbmThisInVal );
1531
1526
dtbmGetBits (inVals , input7or8 , & thisInValIx , & bitPos , & theBits , & dtbmThisInVal );
1532
1527
outVals [nextOutVal ++ ] = (unsigned char )theBits ;
1533
1528
}
1534
- else
1535
- {
1536
- // output the corresponding text char and skip corresponding number of bits
1537
- outVals [nextOutVal ++ ] = (unsigned char )pTextChars [textDecodePos [theBits ]];
1538
- dtbmSkipBits (inVals , textDecodeBits [theBits ], & thisInValIx , & bitPos , & dtbmThisInVal );
1539
- }
1540
1529
}
1541
1530
// Process the last three values: requires at least 2 bytes (3*3) and up to 4 bytes (3*7)
1542
1531
uint32_t lastBits = dtbmThisInVal >>bitPos ; // next value already read
0 commit comments