mirror of
https://github.com/libretro/scummvm.git
synced 2025-02-25 22:07:34 +00:00
COMMON: Split slow and fast CRC implementation
They have almost nothing in common. And we probably want to kill slow implementation anyway now that we have tests
This commit is contained in:
parent
05f1a0df51
commit
357249fb5a
166
common/crc.h
166
common/crc.h
@ -23,7 +23,7 @@
|
||||
*
|
||||
* Filename: crc.c
|
||||
*
|
||||
* Description: Slow and fast implementations of the CRC standards.
|
||||
* Description: Fast implementation of the CRC standards.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
@ -41,139 +41,44 @@
|
||||
|
||||
namespace Common {
|
||||
|
||||
template <typename T, bool need_reflect>
|
||||
class CRCCommon {
|
||||
public:
|
||||
CRCCommon(T poly, T init_remainder, T final_xor);
|
||||
T finalize(T remainder) const;
|
||||
T crcSlow(byte const message[], int nBytes) const;
|
||||
T getInitRemainder() const { return reflectRemainder(_init_remainder); }
|
||||
|
||||
protected:
|
||||
const T _poly;
|
||||
const T _init_remainder;
|
||||
const T _final_xor;
|
||||
|
||||
const int _width;
|
||||
const int _topbit;
|
||||
|
||||
template<typename R>
|
||||
static R reflect(R data);
|
||||
|
||||
byte reflectData(byte x) const { return need_reflect ? reflect<byte>(x) : x; }
|
||||
T reflectRemainder(T x) const { return need_reflect ? reflect<T>(x) : x; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class CRCNormal : public CRCCommon<T, false> {
|
||||
class CRCNormal {
|
||||
public:
|
||||
CRCNormal(T poly, T init_remainder, T final_xor);
|
||||
|
||||
T crcFast(byte const message[], int nBytes) const;
|
||||
T processByte(byte byteVal, T remainder) const;
|
||||
T getInitRemainder() const { return _init_remainder; }
|
||||
T finalize(T remainder) const { return remainder ^ _final_xor; }
|
||||
|
||||
private:
|
||||
const T _init_remainder;
|
||||
const T _final_xor;
|
||||
|
||||
T _crcTable[256];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class CRCReflected : public CRCCommon<T, true> {
|
||||
class CRCReflected {
|
||||
public:
|
||||
CRCReflected(T poly, T init_remainder, T final_xor);
|
||||
|
||||
T crcFast(byte const message[], int nBytes) const;
|
||||
T processByte(byte byteVal, T remainder) const;
|
||||
T getInitRemainder() const { return _reflected_init_remainder; }
|
||||
T finalize(T remainder) const { return remainder ^ _final_xor; }
|
||||
|
||||
private:
|
||||
T _crcTable[256];
|
||||
const T _reflected_init_remainder;
|
||||
const T _final_xor;
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: reflect()
|
||||
*
|
||||
* Description: Reorder the bits of a binary sequence, by reflecting
|
||||
* them about the middle position.
|
||||
*
|
||||
* Notes: No checking is done that nBits <= 32.
|
||||
*
|
||||
* Returns: The reflection of the original data.
|
||||
*
|
||||
*********************************************************************/
|
||||
template<typename T, bool need_reflect> template<typename R>
|
||||
R CRCCommon<T, need_reflect>::reflect(R data) {
|
||||
R reflection = 0;
|
||||
|
||||
/*
|
||||
* Reflect the data about the center bit.
|
||||
*/
|
||||
for (byte bit = 0; bit < sizeof(R) * 8; ++bit) {
|
||||
/*
|
||||
* If the LSB bit is set, set the reflection of it.
|
||||
*/
|
||||
if (data & 0x01) {
|
||||
reflection |= (1 << ((sizeof(R) * 8 - 1) - bit));
|
||||
}
|
||||
|
||||
data = (data >> 1);
|
||||
}
|
||||
|
||||
return reflection;
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: crcSlow()
|
||||
*
|
||||
* Description: Compute the CRC of a given message.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
* Returns: The CRC of the message.
|
||||
*
|
||||
*********************************************************************/
|
||||
template<typename T, bool need_reflect>
|
||||
T CRCCommon<T, need_reflect>::crcSlow(byte const message[], int nBytes) const {
|
||||
T remainder = _init_remainder;
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a byte at a time.
|
||||
*/
|
||||
for (int b = 0; b < nBytes; ++b) {
|
||||
/*
|
||||
* Bring the next byte into the remainder.
|
||||
*/
|
||||
remainder ^= reflectData(message[b]) << (_width - 8);
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a bit at a time.
|
||||
*/
|
||||
for (byte bit = 8; bit > 0; --bit) {
|
||||
/*
|
||||
* Try to divide the current data bit.
|
||||
*/
|
||||
if (remainder & _topbit) {
|
||||
remainder = (remainder << 1) ^ _poly;
|
||||
} else {
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The final remainder is the CRC result.
|
||||
*/
|
||||
return reflectRemainder(remainder) ^ _final_xor;
|
||||
}
|
||||
|
||||
|
||||
template<typename T, bool need_reflect>
|
||||
CRCCommon<T, need_reflect>::CRCCommon(T poly, T init_remainder, T final_xor) :
|
||||
_poly(poly), _init_remainder(init_remainder), _final_xor(final_xor), _width(8 * sizeof(T)), _topbit(1 << (8 * sizeof(T) - 1)) {}
|
||||
|
||||
template <typename T>
|
||||
CRCNormal<T>::CRCNormal(T poly, T init_remainder, T final_xor) : CRCCommon<T, false>(poly, init_remainder, final_xor) {
|
||||
CRCNormal<T>::CRCNormal(T poly, T init_remainder, T final_xor) : _init_remainder(init_remainder), _final_xor(final_xor) {
|
||||
const T topbit = 1ULL << (8 * sizeof(T) - 1);
|
||||
|
||||
/*
|
||||
* Compute the remainder of each possible dividend.
|
||||
*/
|
||||
@ -181,7 +86,7 @@ CRCNormal<T>::CRCNormal(T poly, T init_remainder, T final_xor) : CRCCommon<T, fa
|
||||
/*
|
||||
* Start with the dividend followed by zeros.
|
||||
*/
|
||||
T remainder = dividend << (this->_width - 8);
|
||||
T remainder = dividend << (8 * sizeof(T) - 8);
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a bit at a time.
|
||||
@ -190,8 +95,8 @@ CRCNormal<T>::CRCNormal(T poly, T init_remainder, T final_xor) : CRCCommon<T, fa
|
||||
/*
|
||||
* Try to divide the current data bit.
|
||||
*/
|
||||
if (remainder & this->_topbit) {
|
||||
remainder = (remainder << 1) ^ this->_poly;
|
||||
if (remainder & topbit) {
|
||||
remainder = (remainder << 1) ^ poly;
|
||||
} else {
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
@ -200,14 +105,12 @@ CRCNormal<T>::CRCNormal(T poly, T init_remainder, T final_xor) : CRCCommon<T, fa
|
||||
/*
|
||||
* Store the result into the table.
|
||||
*/
|
||||
this->_crcTable[dividend] = remainder;
|
||||
_crcTable[dividend] = remainder;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
CRCReflected<T>::CRCReflected(T poly, T init_remainder, T final_xor) : CRCCommon<T, true>(poly, init_remainder, final_xor) {
|
||||
T reflected_poly = this->reflectRemainder(poly);
|
||||
|
||||
CRCReflected<T>::CRCReflected(T reflected_poly, T reflected_init_remainder, T final_xor) : _reflected_init_remainder(reflected_init_remainder), _final_xor(final_xor) {
|
||||
/*
|
||||
* Compute the remainder of each possible dividend.
|
||||
*/
|
||||
@ -251,20 +154,20 @@ CRCReflected<T>::CRCReflected(T poly, T init_remainder, T final_xor) : CRCCommon
|
||||
*********************************************************************/
|
||||
template<typename T>
|
||||
T CRCNormal<T>::crcFast(byte const message[], int nBytes) const {
|
||||
T remainder = this->_init_remainder;
|
||||
T remainder = _init_remainder;
|
||||
|
||||
/*
|
||||
* Divide the message by the polynomial, a byte at a time.
|
||||
*/
|
||||
for (int b = 0; b < nBytes; ++b) {
|
||||
byte data = message[b] ^ (remainder >> (this->_width - 8));
|
||||
remainder = this->_crcTable[data] ^ (remainder << 8);
|
||||
byte data = message[b] ^ (remainder >> (8 * sizeof(T) - 8));
|
||||
remainder = _crcTable[data] ^ (remainder << 8);
|
||||
}
|
||||
|
||||
/*
|
||||
* The final remainder is the CRC.
|
||||
*/
|
||||
return remainder ^ this->_final_xor;
|
||||
return remainder ^ _final_xor;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
@ -280,39 +183,34 @@ T CRCNormal<T>::crcFast(byte const message[], int nBytes) const {
|
||||
*********************************************************************/
|
||||
template<typename T>
|
||||
T CRCReflected<T>::crcFast(byte const message[], int nBytes) const {
|
||||
T remainder = this->reflectRemainder(this->_init_remainder);
|
||||
T remainder = _reflected_init_remainder;
|
||||
|
||||
/*
|
||||
* Divide the message by the polynomial, a byte at a time.
|
||||
*/
|
||||
for (int b = 0; b < nBytes; ++b) {
|
||||
byte data = message[b] ^ remainder;
|
||||
remainder = this->_crcTable[data] ^ (remainder >> 8);
|
||||
remainder = _crcTable[data] ^ (remainder >> 8);
|
||||
}
|
||||
|
||||
/*
|
||||
* The final remainder is the CRC.
|
||||
*/
|
||||
return remainder ^ this->_final_xor;
|
||||
return remainder ^ _final_xor;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T CRCNormal<T>::processByte(byte byteVal, T remainder) const {
|
||||
byte data = byteVal ^ (remainder >> (this->_width - 8));
|
||||
byte data = byteVal ^ (remainder >> (8 * sizeof(T) - 8));
|
||||
|
||||
return this->_crcTable[data] ^ (remainder << 8);
|
||||
return _crcTable[data] ^ (remainder << 8);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T CRCReflected<T>::processByte(byte byteVal, T remainder) const {
|
||||
byte data = byteVal ^ remainder;
|
||||
|
||||
return this->_crcTable[data] ^ (remainder >> 8);
|
||||
}
|
||||
|
||||
template<typename T, bool need_reflect>
|
||||
T CRCCommon<T, need_reflect>::finalize(T remainder) const {
|
||||
return remainder ^ _final_xor;
|
||||
return _crcTable[data] ^ (remainder >> 8);
|
||||
}
|
||||
|
||||
class CRC_CCITT : public CRCNormal<uint16> {
|
||||
@ -327,12 +225,12 @@ public:
|
||||
|
||||
class CRC16 : public CRCReflected<uint16> {
|
||||
public:
|
||||
CRC16() : CRCReflected<uint16>(0x8005, 0x0000, 0x0000) {}
|
||||
CRC16() : CRCReflected<uint16>(0xa001, 0x0000, 0x0000) {}
|
||||
};
|
||||
|
||||
class CRC32 : public CRCReflected<uint32> {
|
||||
public:
|
||||
CRC32() : CRCReflected<uint32>(0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF) {}
|
||||
CRC32() : CRCReflected<uint32>(0xEDB88320, 0xFFFFFFFF, 0xFFFFFFFF) {}
|
||||
};
|
||||
|
||||
} // End of namespace Common
|
||||
|
173
common/crc_slow.h
Normal file
173
common/crc_slow.h
Normal file
@ -0,0 +1,173 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* Filename: crc.c
|
||||
*
|
||||
* Description: Slow implementation of the CRC standards.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2000 by Michael Barr. This software is placed into
|
||||
* the public domain and may be used for any purpose. However, this
|
||||
* notice must not be changed or removed and no warranty is either
|
||||
* expressed or implied by its publication or distribution.
|
||||
**********************************************************************/
|
||||
|
||||
#ifndef COMMON_CRC_SLOW_H
|
||||
#define COMMON_CRC_SLOW_H
|
||||
|
||||
#include "common/system.h" // For types.
|
||||
|
||||
namespace Common {
|
||||
|
||||
template <typename T>
|
||||
class CRCSlow {
|
||||
public:
|
||||
CRCSlow(T poly, T init_remainder, T final_xor, bool need_reflect);
|
||||
T crcSlow(byte const message[], int nBytes) const;
|
||||
|
||||
protected:
|
||||
const T _poly;
|
||||
const T _init_remainder;
|
||||
const T _final_xor;
|
||||
|
||||
const int _width;
|
||||
const int _topbit;
|
||||
|
||||
const bool _reflect;
|
||||
|
||||
template<typename R>
|
||||
static R reflect(R data);
|
||||
|
||||
byte reflectData(byte x) const { return _reflect ? reflect<byte>(x) : x; }
|
||||
T reflectRemainder(T x) const { return _reflect ? reflect<T>(x) : x; }
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: reflect()
|
||||
*
|
||||
* Description: Reorder the bits of a binary sequence, by reflecting
|
||||
* them about the middle position.
|
||||
*
|
||||
* Notes: No checking is done that nBits <= 32.
|
||||
*
|
||||
* Returns: The reflection of the original data.
|
||||
*
|
||||
*********************************************************************/
|
||||
template<typename T> template<typename R>
|
||||
R CRCSlow<T>::reflect(R data) {
|
||||
R reflection = 0;
|
||||
|
||||
/*
|
||||
* Reflect the data about the center bit.
|
||||
*/
|
||||
for (byte bit = 0; bit < sizeof(R) * 8; ++bit) {
|
||||
/*
|
||||
* If the LSB bit is set, set the reflection of it.
|
||||
*/
|
||||
if (data & 0x01) {
|
||||
reflection |= (1 << ((sizeof(R) * 8 - 1) - bit));
|
||||
}
|
||||
|
||||
data = (data >> 1);
|
||||
}
|
||||
|
||||
return reflection;
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: crcSlow()
|
||||
*
|
||||
* Description: Compute the CRC of a given message.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
* Returns: The CRC of the message.
|
||||
*
|
||||
*********************************************************************/
|
||||
template<typename T>
|
||||
T CRCSlow<T>::crcSlow(byte const message[], int nBytes) const {
|
||||
T remainder = _init_remainder;
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a byte at a time.
|
||||
*/
|
||||
for (int b = 0; b < nBytes; ++b) {
|
||||
/*
|
||||
* Bring the next byte into the remainder.
|
||||
*/
|
||||
remainder ^= reflectData(message[b]) << (_width - 8);
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a bit at a time.
|
||||
*/
|
||||
for (byte bit = 8; bit > 0; --bit) {
|
||||
/*
|
||||
* Try to divide the current data bit.
|
||||
*/
|
||||
if (remainder & _topbit) {
|
||||
remainder = (remainder << 1) ^ _poly;
|
||||
} else {
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The final remainder is the CRC result.
|
||||
*/
|
||||
return reflectRemainder(remainder) ^ _final_xor;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
CRCSlow<T>::CRCSlow(T poly, T init_remainder, T final_xor, bool need_reflect) :
|
||||
_poly(poly), _init_remainder(init_remainder), _final_xor(final_xor), _width(8 * sizeof(T)), _topbit(1 << (8 * sizeof(T) - 1)), _reflect(need_reflect) {}
|
||||
|
||||
class CRC_CCITT_Slow : public CRCSlow<uint16> {
|
||||
public:
|
||||
CRC_CCITT_Slow() : CRCSlow<uint16>(0x1021, 0xFFFF, 0x0000, false) {}
|
||||
};
|
||||
|
||||
class CRC_BINHEX_Slow : public CRCSlow<uint16> {
|
||||
public:
|
||||
CRC_BINHEX_Slow() : CRCSlow<uint16>(0x1021, 0x0000, 0x0000, false) {}
|
||||
};
|
||||
|
||||
class CRC16_Slow : public CRCSlow<uint16> {
|
||||
public:
|
||||
CRC16_Slow() : CRCSlow<uint16>(0x8005, 0x0000, 0x0000, true) {}
|
||||
};
|
||||
|
||||
class CRC32_Slow : public CRCSlow<uint32> {
|
||||
public:
|
||||
CRC32_Slow() : CRCSlow<uint32>(0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true) {}
|
||||
};
|
||||
|
||||
} // End of namespace Common
|
||||
|
||||
#endif
|
@ -1,6 +1,7 @@
|
||||
#include <cxxtest/TestSuite.h>
|
||||
|
||||
#include "common/crc.h"
|
||||
#include "common/crc_slow.h"
|
||||
|
||||
namespace {
|
||||
const byte *testString = (const byte *)"The quick brown fox jumps over the lazy dog";
|
||||
@ -13,7 +14,6 @@ public:
|
||||
void test_crc32() {
|
||||
Common::CRC32 crc;
|
||||
TS_ASSERT_EQUALS(crc.crcFast(testString, testLen), 0x414fa339U);
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0x414fa339U);
|
||||
uint32 running = crc.getInitRemainder();
|
||||
for (const byte *ptr = testString; *ptr; ptr++)
|
||||
running = crc.processByte(*ptr, running);
|
||||
@ -23,7 +23,6 @@ public:
|
||||
void test_crc16() {
|
||||
Common::CRC16 crc;
|
||||
TS_ASSERT_EQUALS(crc.crcFast(testString, testLen), 0xfcdfU);
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0xfcdfU);
|
||||
uint16 running = crc.getInitRemainder();
|
||||
for (const byte *ptr = testString; *ptr; ptr++)
|
||||
running = crc.processByte(*ptr, running);
|
||||
@ -33,7 +32,6 @@ public:
|
||||
void test_crc_ccitt() {
|
||||
Common::CRC_CCITT crc; // aka ccitt-false
|
||||
TS_ASSERT_EQUALS(crc.crcFast(testString, testLen), 0x8fddU);
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0x8fddU);
|
||||
uint16 running = crc.getInitRemainder();
|
||||
for (const byte *ptr = testString; *ptr; ptr++)
|
||||
running = crc.processByte(*ptr, running);
|
||||
@ -43,10 +41,29 @@ public:
|
||||
void test_crc_binhex() {
|
||||
Common::CRC_BINHEX crc; // Aka xmodem
|
||||
TS_ASSERT_EQUALS(crc.crcFast(testString, testLen), 0xf0c8U);
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0xf0c8U);
|
||||
uint16 running = crc.getInitRemainder();
|
||||
for (const byte *ptr = testString; *ptr; ptr++)
|
||||
running = crc.processByte(*ptr, running);
|
||||
TS_ASSERT_EQUALS(crc.finalize(running), 0xf0c8U);
|
||||
}
|
||||
|
||||
void test_crc32_slow() {
|
||||
Common::CRC32_Slow crc;
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0x414fa339U);
|
||||
}
|
||||
|
||||
void test_crc16_slow() {
|
||||
Common::CRC16_Slow crc;
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0xfcdfU);
|
||||
}
|
||||
|
||||
void test_crc_ccitt_slow() {
|
||||
Common::CRC_CCITT_Slow crc; // aka ccitt-false
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0x8fddU);
|
||||
}
|
||||
|
||||
void test_crc_binhex_slow() {
|
||||
Common::CRC_BINHEX_Slow crc; // Aka xmodem
|
||||
TS_ASSERT_EQUALS(crc.crcSlow(testString, testLen), 0xf0c8U);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user