parent
4b94f1fa83
commit
2b010811d8
@ -25,34 +25,54 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef ITERATORBASEDCRC32_H_
|
#ifndef ITERATOR_BASED_CRC32_H
|
||||||
#define ITERATORBASEDCRC32_H_
|
#define ITERATOR_BASED_CRC32_H
|
||||||
|
|
||||||
#include "../Util/SimpleLogger.h"
|
#include "../Util/SimpleLogger.h"
|
||||||
|
|
||||||
#include <boost/crc.hpp> // for boost::crc_32_type
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#if defined(__x86_64__)
|
||||||
|
#include <cpuid.h>
|
||||||
|
#else
|
||||||
|
#include <boost/crc.hpp> // for boost::crc_32_type
|
||||||
|
|
||||||
|
inline void __get_cpuid(
|
||||||
|
int param,
|
||||||
|
unsigned *eax,
|
||||||
|
unsigned *ebx,
|
||||||
|
unsigned *ecx,
|
||||||
|
unsigned *edx
|
||||||
|
) { *ecx = 0; }
|
||||||
|
#endif
|
||||||
|
|
||||||
template<class ContainerT>
|
template<class ContainerT>
|
||||||
class IteratorbasedCRC32 {
|
class IteratorbasedCRC32 {
|
||||||
private:
|
private:
|
||||||
typedef typename ContainerT::iterator IteratorType;
|
typedef typename ContainerT::iterator IteratorType;
|
||||||
unsigned crc;
|
unsigned crc;
|
||||||
|
|
||||||
typedef boost::crc_optimal<32, 0x1EDC6F41, 0x0, 0x0, true, true> my_crc_32_type;
|
|
||||||
typedef unsigned (IteratorbasedCRC32::*CRC32CFunctionPtr)(char *str, unsigned len, unsigned crc);
|
typedef unsigned (IteratorbasedCRC32::*CRC32CFunctionPtr)(char *str, unsigned len, unsigned crc);
|
||||||
|
CRC32CFunctionPtr crc_function;
|
||||||
|
|
||||||
unsigned SoftwareBasedCRC32(char *str, unsigned len, unsigned ){
|
#if !defined(__x86_64__)
|
||||||
boost::crc_optimal<32, 0x1EDC6F41, 0x0, 0x0, true, true> CRC32_processor;
|
boost::crc_optimal<32, 0x1EDC6F41, 0x0, 0x0, true, true> CRC32_processor;
|
||||||
|
#endif
|
||||||
|
unsigned SoftwareBasedCRC32(char *str, unsigned len, unsigned ){
|
||||||
|
#if !defined(__x86_64__)
|
||||||
CRC32_processor.process_bytes( str, len);
|
CRC32_processor.process_bytes( str, len);
|
||||||
return CRC32_processor.checksum();
|
return CRC32_processor.checksum();
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned SSE42BasedCRC32( char *str, unsigned len, unsigned crc){
|
// adapted from http://byteworm.com/2010/10/13/crc32/
|
||||||
|
unsigned SSE42BasedCRC32( char *str, unsigned len, unsigned){
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
unsigned q=len/sizeof(unsigned),
|
unsigned q = len/sizeof(unsigned);
|
||||||
r=len%sizeof(unsigned),
|
unsigned r = len%sizeof(unsigned);
|
||||||
*p=(unsigned*)str/*, crc*/;
|
unsigned *p = (unsigned*)str;
|
||||||
|
|
||||||
//crc=0;
|
//crc=0;
|
||||||
while (q--) {
|
while (q--) {
|
||||||
@ -77,25 +97,17 @@ private:
|
|||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
inline unsigned cpuid() const {
|
||||||
unsigned cpuid(unsigned functionInput){
|
unsigned eax, ebx, ecx, edx;
|
||||||
unsigned eax;
|
// on X64 this calls hardware cpuid(.) instr. otherwise a dummy impl.
|
||||||
unsigned ebx;
|
__get_cpuid( 1, &eax, &ebx, &ecx, &edx );
|
||||||
unsigned ecx;
|
|
||||||
unsigned edx;
|
|
||||||
asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (functionInput));
|
|
||||||
return ecx;
|
return ecx;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
CRC32CFunctionPtr DetectNativeCRC32Support(){
|
CRC32CFunctionPtr DetectNativeCRC32Support(){
|
||||||
#if defined(__x86_64__)
|
static const int SSE42_BIT = 0x00100000;
|
||||||
static const int SSE42_BIT = 20;
|
const unsigned ecx = cpuid();
|
||||||
unsigned ecx = cpuid(1);
|
const bool has_SSE42 = ecx & SSE42_BIT;
|
||||||
bool has_SSE42 = ecx & (1 << SSE42_BIT);
|
|
||||||
#else
|
|
||||||
bool has_SSE42 = false;
|
|
||||||
#endif
|
|
||||||
if (has_SSE42) {
|
if (has_SSE42) {
|
||||||
SimpleLogger().Write() << "using hardware based CRC32 computation";
|
SimpleLogger().Write() << "using hardware based CRC32 computation";
|
||||||
return &IteratorbasedCRC32::SSE42BasedCRC32; //crc32 hardware accelarated;
|
return &IteratorbasedCRC32::SSE42BasedCRC32; //crc32 hardware accelarated;
|
||||||
@ -104,23 +116,21 @@ private:
|
|||||||
return &IteratorbasedCRC32::SoftwareBasedCRC32; //crc32cSlicingBy8;
|
return &IteratorbasedCRC32::SoftwareBasedCRC32; //crc32cSlicingBy8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CRC32CFunctionPtr crc_function;
|
|
||||||
public:
|
public:
|
||||||
IteratorbasedCRC32(): crc(0) {
|
IteratorbasedCRC32(): crc(0) {
|
||||||
crc_function = DetectNativeCRC32Support();
|
crc_function = DetectNativeCRC32Support();
|
||||||
}
|
}
|
||||||
|
|
||||||
// virtual ~IteratorbasedCRC32() { }
|
|
||||||
|
|
||||||
unsigned operator()( IteratorType iter, const IteratorType end) {
|
unsigned operator()( IteratorType iter, const IteratorType end) {
|
||||||
unsigned crc = 0;
|
unsigned crc = 0;
|
||||||
while(iter != end) {
|
while(iter != end) {
|
||||||
char * data = reinterpret_cast<char*>(&(*iter) );
|
char * data = reinterpret_cast<char*>(&(*iter) );
|
||||||
crc =((*this).*(crc_function))(data, sizeof(typename ContainerT::value_type*), crc);
|
crc =((*this).*(crc_function))(data, sizeof(typename ContainerT::value_type), crc);
|
||||||
++iter;
|
++iter;
|
||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* ITERATORBASEDCRC32_H_ */
|
#endif /* ITERATOR_BASED_CRC32_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user