134 lines
3.5 KiB
C++
134 lines
3.5 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Crc32Generator.h
|
|
*
|
|
* @brief Header file for crc32 generator class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_CRC32_GENERATOR_H)
|
|
#define ACTIV_BASE_CRC32_GENERATOR_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
|
|
#if (defined ACTIV_PROCESSOR_X86_64)
|
|
#if (defined ACTIV_COMPILER_GCC) || (defined ACTIV_COMPILER_VISUAL_C)
|
|
#define ACTIV_CRC32_INTRINSICS
|
|
#endif
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/*
|
|
* @brief Crc32Generator
|
|
*
|
|
* @tparam If true use the standard (ethernet) polynomial else use Castognoli. Only Castognoli is compatible with SSE4.2.
|
|
*/
|
|
template<bool USE_STANDARD_LOOKUP_TABLE = false>
|
|
class Crc32Generator
|
|
{
|
|
public:
|
|
//! Default constructor
|
|
ACTIV_BASE_API Crc32Generator();
|
|
|
|
//! Is SSE 4.2 supported.
|
|
ACTIV_BASE_API static bool IsSse42Supported();
|
|
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param string
|
|
*
|
|
* @return The calculated crc32.
|
|
*/
|
|
uint32_t operator()(const String &string) const;
|
|
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
* @param initialCrc
|
|
*
|
|
* @return The calculated crc32.
|
|
*/
|
|
uint32_t operator()(const void *pData, const size_t dataLength, const uint32_t initialCrc) const;
|
|
|
|
/**
|
|
* @brief Calculate the crc32 of the provided data using the standard algorithm.
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
* @param initialCrc
|
|
*
|
|
* @return The calculated crc32.
|
|
*/
|
|
ACTIV_BASE_API static uint32_t CalculateUsingStandardAlgorithm(const void *pData, const size_t dataLength,
|
|
const uint32_t initialCrc);
|
|
|
|
/**
|
|
* @brief Calculate the crc32 of the provided data using the splice by 8 algorithm.
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
* @param initialCrc
|
|
*
|
|
* @return The calculated crc32.
|
|
*/
|
|
ACTIV_BASE_API static uint32_t CalculateUsingSpliceBy8Algorithm(const void *pData, const size_t dataLength,
|
|
const uint32_t initialCrc);
|
|
|
|
#if (defined ACTIV_CRC32_INTRINSICS)
|
|
|
|
/**
|
|
* @brief Calculate the crc32 of the provided data using the SSE4.2 crc instruction (Intel platforms only).
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
* @param initialCrc
|
|
*
|
|
* @return The calculated crc32.
|
|
*/
|
|
ACTIV_BASE_API static uint32_t CalculateUsingSseInstruction(const void *pData, const size_t dataLength,
|
|
const uint32_t initialCrc);
|
|
|
|
#endif
|
|
|
|
/**
|
|
* @brief Calculate the crc32 of the provided data using a simple endian safe algorithm.
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
* @param initialCrc
|
|
*
|
|
* @return The calculated crc32.
|
|
*/
|
|
ACTIV_BASE_API static uint32_t CalculateUsingSimpleAlgorithm(const void *pData, const size_t dataLength,
|
|
const uint32_t initialCrc);
|
|
|
|
private:
|
|
typedef uint32_t (*CrcFunction)(const void *, const size_t, const uint32_t);
|
|
const CrcFunction crcFunction;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool USE_STANDARD_LOOKUP_TABLE>
|
|
ACTIV_FORCE_INLINE uint32_t Crc32Generator<USE_STANDARD_LOOKUP_TABLE>::operator()(const String &string) const
|
|
{
|
|
return operator() (string.data(), string.length(), 0);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool USE_STANDARD_LOOKUP_TABLE>
|
|
ACTIV_FORCE_INLINE uint32_t Crc32Generator<USE_STANDARD_LOOKUP_TABLE>::operator()(const void * pData, const size_t dataLength,
|
|
const uint32_t initialCrc) const
|
|
{
|
|
return crcFunction(pData, dataLength, initialCrc);
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_CRC32_GENERATOR_H)
|