Skip to content

Commit 4aca6a6

Browse files
committed
Cleanup initialization code
1 parent 5ab14d0 commit 4aca6a6

9 files changed

Lines changed: 428 additions & 91 deletions

src/Memory.hpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
#ifndef knz_Memory
1818
#define knz_Memory
1919

20+
#if __cplusplus >= 202002L
21+
#include <bit>
22+
#endif
2023
#include <cstring>
2124
#include "types.hpp"
2225

@@ -102,11 +105,17 @@ static KANZI_ALWAYS_INLINE uint64 knz_bswap64(uint64 x) {
102105

103106
// Detect host endianness
104107

105-
#ifndef HOST_IS_LITTLE
106-
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || defined(__BIG_ENDIAN__)
107-
#define HOST_IS_LITTLE 0
108-
#else
109-
#define HOST_IS_LITTLE 1
108+
#if __cplusplus >= 202002L
109+
static_assert(std::endian::native == std::endian::little ||
110+
std::endian::native == std::endian::big,
111+
"Kanzi supports only little- and big-endian hosts");
112+
#else
113+
#ifndef HOST_IS_LITTLE
114+
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || defined(__BIG_ENDIAN__)
115+
#define HOST_IS_LITTLE 0
116+
#else
117+
#define HOST_IS_LITTLE 1
118+
#endif
110119
#endif
111120
#endif
112121

@@ -122,7 +131,10 @@ static KANZI_ALWAYS_INLINE T readEndian(const byte* p) {
122131
#endif
123132

124133
// Swap if host and source endianness differ
125-
#if HOST_IS_LITTLE
134+
#if __cplusplus >= 202002L
135+
if constexpr (SourceIsBigEndian !=
136+
(std::endian::native == std::endian::big)) {
137+
#elif HOST_IS_LITTLE
126138
if (SourceIsBigEndian) {
127139
#else
128140
if (!SourceIsBigEndian) {
@@ -141,7 +153,10 @@ static KANZI_ALWAYS_INLINE T readEndian(const byte* p) {
141153
template <typename T, bool TargetIsBigEndian>
142154
static KANZI_ALWAYS_INLINE void writeEndian(byte* p, T val) {
143155

144-
#if HOST_IS_LITTLE
156+
#if __cplusplus >= 202002L
157+
if constexpr (TargetIsBigEndian !=
158+
(std::endian::native == std::endian::big)) {
159+
#elif HOST_IS_LITTLE
145160
if (TargetIsBigEndian) {
146161
#else
147162
if (!TargetIsBigEndian) {

src/entropy/AdaptiveProbMap.hpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ namespace kanzi {
4646
_index = 0;
4747

4848
for (int j = 0; j <= 64; j++) {
49-
_data[j] = uint16(j << 6) << 4;
49+
// 65536 does not fit in uint16. Keep the endpoint aligned
50+
// with the maximum target used by the adaptive update.
51+
_data[j] = (j == 64) ? uint16(65528) : uint16(j << 10);
5052
}
5153

5254
for (int i = 1; i < n; i++) {
@@ -93,18 +95,14 @@ namespace kanzi {
9395
const int mult = (FAST == false) ? 33 : 32;
9496
_index = 0;
9597

96-
if (n == 0) {
97-
_data = new uint16[mult];
98-
}
99-
else {
100-
_data = new uint16[n * mult];
98+
const int size = (n == 0) ? mult : n * mult;
99+
_data = new uint16[size];
101100

102-
for (int j = 0; j < mult; j++)
103-
_data[j] = uint16(Global::squash((j - 16) * 128) << 4);
101+
for (int j = 0; j < mult; j++)
102+
_data[j] = uint16(Global::squash((j - 16) * 128) << 4);
104103

105-
for (int i = 1; i < n; i++)
106-
memcpy(&_data[i * mult], &_data[0], mult * sizeof(uint16));
107-
}
104+
for (int i = 1; i < n; i++)
105+
memcpy(&_data[i * mult], &_data[0], mult * sizeof(uint16));
108106
}
109107

110108
// Return improved prediction given current bit, prediction and context
@@ -131,4 +129,3 @@ namespace kanzi {
131129

132130
}
133131
#endif
134-

src/entropy/TPAQPredictor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ namespace kanzi
341341
bsVersion = ctx->getInt("bsVersion", bsVersion);
342342
}
343343

344+
// The ring buffer and hash table use bit masks for indexing.
345+
// Normalize their sizes to powers of two before creating the masks.
346+
bufferSize = 1u << Global::_log2(bufferSize);
347+
hashSize = 1u << Global::_log2(hashSize);
348+
344349
mixersSize <<= (2 * extraMem);
345350
statesSize <<= (2 * extraMem);
346351
hashSize <<= (2 * extraMem);

src/io/CompressedInputStream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using namespace std;
2626

2727

2828
const int CompressedInputStream::BITSTREAM_TYPE = 0x4B414E5A; // "KANZ"
29-
const int CompressedInputStream::BITSTREAM_FORMAT_VERSION = 6;
29+
const int CompressedInputStream::BITSTREAM_FORMAT_VERSION = 7;
3030
const int CompressedInputStream::DEFAULT_BUFFER_SIZE = 256 * 1024;
3131
const int CompressedInputStream::EXTRA_BUFFER_SIZE = 512;
3232
const kanzi::byte CompressedInputStream::COPY_BLOCK_MASK = kanzi::byte(0x80);

src/io/CompressedOutputStream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using namespace kanzi;
2929
using namespace std;
3030

3131
const int CompressedOutputStream::BITSTREAM_TYPE = 0x4B414E5A; // "KANZ"
32-
const int CompressedOutputStream::BITSTREAM_FORMAT_VERSION = 6;
32+
const int CompressedOutputStream::BITSTREAM_FORMAT_VERSION = 7;
3333
const int CompressedOutputStream::DEFAULT_BUFFER_SIZE = 256 * 1024;
3434
const kanzi::byte CompressedOutputStream::COPY_BLOCK_MASK = kanzi::byte(0x80);
3535
const kanzi::byte CompressedOutputStream::TRANSFORMS_MASK = kanzi::byte(0x10);

src/test/TestMalformedStream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ int main()
196196
}
197197

198198
if (expectHeaderFailure("unsupported version",
199-
buildHeader(type, version + 1, 0, entropy, transform, blockSize, 0, 0, true),
199+
buildHeader(type, version + 2, 0, entropy, transform, blockSize, 0, 0, true),
200200
Error::ERR_STREAM_VERSION, "cannot read this version") != 0) {
201201
return 1;
202202
}

src/test/TestTransforms.cpp

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ limitations under the License.
3737
using namespace std;
3838
using namespace kanzi;
3939

40-
static const int BS_VERSION = 6;
40+
static const int BS_VERSION = 7;
4141

4242
static void writeInt16LE(kanzi::byte buf[], int value)
4343
{
@@ -632,40 +632,73 @@ static int testTransformCapacityValidation()
632632
{
633633
LZXCodec<false> tf;
634634
kanzi::byte lzSrc[128];
635-
kanzi::byte lzEncoded[256];
636-
kanzi::byte lzDecoded[128];
635+
kanzi::byte lzDst[256];
637636

638637
for (int i = 0; i < 128; i++)
639-
lzSrc[i] = kanzi::byte(i & 3);
638+
lzSrc[i] = kanzi::byte(i);
640639

641-
memset(lzEncoded, 0, sizeof(lzEncoded));
642-
memset(lzDecoded, 0x7E, sizeof(lzDecoded));
643-
SliceArray<kanzi::byte> input(lzSrc, 128, 0);
644-
SliceArray<kanzi::byte> encoded(lzEncoded, int(sizeof(lzEncoded)), 0);
640+
SliceArray<kanzi::byte> input(lzSrc, 128, 1);
641+
SliceArray<kanzi::byte> output(lzDst, 256, 0);
642+
const int savedIIdx = input._index;
643+
const int savedOIdx = output._index;
645644

646-
if (tf.forward(input, encoded, 128) == false) {
647-
cout << "LZX setup encoding failed" << endl;
645+
if (tf.forward(input, output, 128) != false) {
646+
cout << "LZX forward should reject oversized remaining input count" << endl;
648647
return 1;
649648
}
650649

651-
const int encodedSize = encoded._index;
652-
SliceArray<kanzi::byte> exactInput(lzEncoded, encodedSize, 0);
653-
SliceArray<kanzi::byte> output(lzDecoded, 128, 0);
650+
if ((input._index != savedIIdx) || (output._index != savedOIdx)) {
651+
cout << "LZX forward input capacity failure moved indexes" << endl;
652+
return 1;
653+
}
654+
}
654655

655-
if (tf.inverse(exactInput, output, encodedSize) != false) {
656-
cout << "LZX should reject input without the read-length guard" << endl;
656+
{
657+
LZXCodec<false> tf;
658+
kanzi::byte lzSrc[128];
659+
kanzi::byte lzDst[176];
660+
661+
for (int i = 0; i < 128; i++)
662+
lzSrc[i] = kanzi::byte(i);
663+
664+
memset(lzDst, 0x7E, sizeof(lzDst));
665+
SliceArray<kanzi::byte> input(lzSrc, 128, 0);
666+
SliceArray<kanzi::byte> output(lzDst, int(sizeof(lzDst)), 32);
667+
668+
if (tf.forward(input, output, 128) != false) {
669+
cout << "LZX forward should reject incompressible input" << endl;
657670
return 1;
658671
}
659672

660-
if ((exactInput._index != 0) || (output._index != 0)) {
661-
cout << "LZX guard failure moved slice indexes" << endl;
673+
if ((output._index != 32) || (lzDst[32] != kanzi::byte(0x7E))) {
674+
cout << "LZX final-size check wrote output on failure" << endl;
662675
return 1;
663676
}
677+
}
664678

665-
SliceArray<kanzi::byte> paddedInput(lzEncoded, int(sizeof(lzEncoded)), 0);
679+
{
680+
Context v7ctx;
681+
v7ctx.putInt("bsVersion", 7);
682+
LZXCodec<false> encoder(v7ctx);
683+
LZXCodec<false> decoder(v7ctx);
684+
kanzi::byte lzSrc[256];
685+
vector<kanzi::byte> lzEncoded(encoder.getMaxEncodedLength(256));
686+
kanzi::byte lzDecoded[512];
687+
688+
for (int i = 0; i < 256; i++)
689+
lzSrc[i] = kanzi::byte(i & 3);
690+
691+
SliceArray<kanzi::byte> input(lzSrc, 256, 0);
692+
SliceArray<kanzi::byte> encoded(&lzEncoded[0], int(lzEncoded.size()), 0);
693+
SliceArray<kanzi::byte> output(lzDecoded, 512, 0);
694+
695+
const bool encodedOk = encoder.forward(input, encoded, 256);
696+
const int encodedSize = encoded._index;
697+
SliceArray<kanzi::byte> encodedInput(&lzEncoded[0], encodedSize, 0);
698+
const bool decodedOk = decoder.inverse(encodedInput, output, encodedSize);
666699

667-
if (tf.inverse(paddedInput, output, encodedSize) == false) {
668-
cout << "LZX should accept input with the read-length guard" << endl;
700+
if (!encodedOk || !decodedOk || (output._index != 256) || (memcmp(lzSrc, lzDecoded, 256) != 0)) {
701+
cout << "LZX v7 round trip failed" << endl;
669702
return 1;
670703
}
671704
}

0 commit comments

Comments
 (0)