Skip to content

Commit 00a040e

Browse files
committed
Support of big endian platforms
1 parent fa1541b commit 00a040e

File tree

3 files changed

+45
-44
lines changed

3 files changed

+45
-44
lines changed

src/bitstream/bitstream.cpp

+37-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
#include "bitstream.h"
2+
3+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
4+
#define NBITSTREAM__LITTLE_ENDIAN_CPU
5+
#endif
6+
27
namespace NBitStream {
38

4-
union TMix {
5-
unsigned long long ull = 0;
6-
uint8_t bytes[8];
9+
union UBytes {
10+
uint32_t ui = 0;
11+
uint8_t bytes[4];
712
};
813

914
TBitStream::TBitStream(const char* buf, int size)
1015
: Buf(buf, buf+size)
1116
{}
17+
1218
TBitStream::TBitStream()
1319
{}
14-
void TBitStream::Write(unsigned long long val, int n) {
15-
if (n > 23 || n < 0)
16-
abort();
17-
const int bitsLeft = Buf.size() * 8 - BitsUsed;
18-
const int bitsReq = n - bitsLeft;
19-
const int bytesPos = BitsUsed / 8;
20-
const int overlap = BitsUsed % 8;
21-
22-
if (overlap || bitsReq >= 0) {
23-
Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0);
24-
}
25-
TMix t;
26-
t.ull = val;
27-
t.ull = (t.ull << (64 - n) >> overlap);
2820

29-
for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
30-
Buf[bytesPos+i] |= t.bytes[7-i];
31-
32-
// std::cout << "bufPos: "<< bytesPos+i << " buf: " << (int)Buf[bytesPos+i] << std::endl;
33-
}
34-
35-
BitsUsed += n;
36-
}
37-
/*
38-
void TBitStream::Write(unsigned long long val, int n) {
21+
void TBitStream::Write(uint32_t val, int n) {
3922
if (n > 23 || n < 0)
4023
abort();
4124
const int bitsLeft = Buf.size() * 8 - BitsUsed;
@@ -46,25 +29,38 @@ void TBitStream::Write(unsigned long long val, int n) {
4629
if (overlap || bitsReq >= 0) {
4730
Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0);
4831
}
49-
TMix t;
50-
t.ull = (val << (64 - n)) >> overlap;
51-
*(unsigned long long*)&Buf[bytesPos-8] |= t.ull;
32+
UBytes t;
33+
t.ui = (val << (32 - n) >> overlap);
34+
35+
for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
36+
#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU
37+
Buf[bytesPos+i] |= t.bytes[3-i];
38+
#else
39+
Buf[bytesPos + i] |= t.bytes[i];
40+
#endif
41+
}
42+
5243
BitsUsed += n;
5344
}
54-
*/
55-
unsigned long long TBitStream::Read(int n) {
56-
if (n >23 || n < 0)
57-
abort();
45+
46+
uint32_t TBitStream::Read(int n) {
47+
if (n >23 || n < 0)
48+
abort();
5849
const int bytesPos = ReadPos / 8;
5950
const int overlap = ReadPos % 8;
60-
TMix t;
61-
for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
62-
t.bytes[7-i] = (uint8_t)Buf[bytesPos+i];
63-
}
51+
52+
UBytes t;
53+
for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) {
54+
#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU
55+
t.bytes[3-i] = (uint8_t)Buf[bytesPos+i];
56+
#else
57+
t.bytes[i] = (uint8_t)Buf[bytesPos+i];
58+
#endif
59+
}
6460

65-
t.ull = (t.ull << overlap >> (64 - n));
66-
ReadPos += n;
67-
return t.ull;
61+
t.ui = (t.ui << overlap >> (32 - n));
62+
ReadPos += n;
63+
return t.ui;
6864
}
6965

7066
unsigned long long TBitStream::GetSizeInBits() const {

src/bitstream/bitstream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class TBitStream {
1818
public:
1919
TBitStream(const char* buf, int size);
2020
TBitStream();
21-
void Write(unsigned long long val, int n);
22-
unsigned long long Read(int n);
21+
void Write(uint32_t val, int n);
22+
uint32_t Read(int n);
2323
unsigned long long GetSizeInBits() const;
2424
uint32_t GetBufSize() const { return Buf.size(); };
2525
const std::vector<char>& GetBytes() const {

test/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ endmacro(use_11)
1414

1515
use_11()
1616

17-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")
17+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer")
19+
else ()
20+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-omit-frame-pointer")
21+
endif ()
22+
1823

1924
include_directories(${gtest_SOURCE_DIR}/include)
2025

0 commit comments

Comments
 (0)