diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-11-27 22:59:50 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-11-27 22:59:50 +0300 |
commit | 00a040eee75ef0076180958ed736e3dfe171b49e (patch) | |
tree | 80172eee5ba972721be28bbebc918f057aba299c /src | |
parent | fa1541bc5eb0339e0752bda6afd67f6669e32232 (diff) | |
download | atracdenc-00a040eee75ef0076180958ed736e3dfe171b49e.tar.gz |
Support of big endian platforms
Diffstat (limited to 'src')
-rw-r--r-- | src/bitstream/bitstream.cpp | 78 | ||||
-rw-r--r-- | src/bitstream/bitstream.h | 4 |
2 files changed, 39 insertions, 43 deletions
diff --git a/src/bitstream/bitstream.cpp b/src/bitstream/bitstream.cpp index e8f1857..afcad8e 100644 --- a/src/bitstream/bitstream.cpp +++ b/src/bitstream/bitstream.cpp @@ -1,41 +1,24 @@ #include "bitstream.h" + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define NBITSTREAM__LITTLE_ENDIAN_CPU +#endif + namespace NBitStream { -union TMix { - unsigned long long ull = 0; - uint8_t bytes[8]; +union UBytes { + uint32_t ui = 0; + uint8_t bytes[4]; }; TBitStream::TBitStream(const char* buf, int size) : Buf(buf, buf+size) {} + TBitStream::TBitStream() {} -void TBitStream::Write(unsigned long long val, int n) { - if (n > 23 || n < 0) - abort(); - const int bitsLeft = Buf.size() * 8 - BitsUsed; - const int bitsReq = n - bitsLeft; - const int bytesPos = BitsUsed / 8; - const int overlap = BitsUsed % 8; - - if (overlap || bitsReq >= 0) { - Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0); - } - TMix t; - t.ull = val; - t.ull = (t.ull << (64 - n) >> overlap); - for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) { - Buf[bytesPos+i] |= t.bytes[7-i]; - - // std::cout << "bufPos: "<< bytesPos+i << " buf: " << (int)Buf[bytesPos+i] << std::endl; - } - - BitsUsed += n; -} -/* -void TBitStream::Write(unsigned long long val, int n) { +void TBitStream::Write(uint32_t val, int n) { if (n > 23 || n < 0) abort(); const int bitsLeft = Buf.size() * 8 - BitsUsed; @@ -46,25 +29,38 @@ void TBitStream::Write(unsigned long long val, int n) { if (overlap || bitsReq >= 0) { Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0); } - TMix t; - t.ull = (val << (64 - n)) >> overlap; - *(unsigned long long*)&Buf[bytesPos-8] |= t.ull; + UBytes t; + t.ui = (val << (32 - n) >> overlap); + + for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) { +#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU + Buf[bytesPos+i] |= t.bytes[3-i]; +#else + Buf[bytesPos + i] |= t.bytes[i]; +#endif + } + BitsUsed += n; } -*/ -unsigned long long TBitStream::Read(int n) { - if (n >23 || n < 0) - abort(); + +uint32_t TBitStream::Read(int n) { + if (n >23 || n < 0) + abort(); const int bytesPos = ReadPos / 8; const int overlap = ReadPos % 8; - TMix t; - for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) { - t.bytes[7-i] = (uint8_t)Buf[bytesPos+i]; - } + + UBytes t; + for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) { +#ifdef NBITSTREAM__LITTLE_ENDIAN_CPU + t.bytes[3-i] = (uint8_t)Buf[bytesPos+i]; +#else + t.bytes[i] = (uint8_t)Buf[bytesPos+i]; +#endif + } - t.ull = (t.ull << overlap >> (64 - n)); - ReadPos += n; - return t.ull; + t.ui = (t.ui << overlap >> (32 - n)); + ReadPos += n; + return t.ui; } unsigned long long TBitStream::GetSizeInBits() const { diff --git a/src/bitstream/bitstream.h b/src/bitstream/bitstream.h index eb813cc..b3a9b72 100644 --- a/src/bitstream/bitstream.h +++ b/src/bitstream/bitstream.h @@ -18,8 +18,8 @@ class TBitStream { public: TBitStream(const char* buf, int size); TBitStream(); - void Write(unsigned long long val, int n); - unsigned long long Read(int n); + void Write(uint32_t val, int n); + uint32_t Read(int n); unsigned long long GetSizeInBits() const; uint32_t GetBufSize() const { return Buf.size(); }; const std::vector<char>& GetBytes() const { |