Template the number of buckets during error calc so the compiler can unroll...

This commit is contained in:
Pavel Krajcevski 2014-03-25 16:24:08 -04:00
parent 6012b53cdb
commit 4128bcf073
2 changed files with 35 additions and 5 deletions

View file

@ -217,16 +217,16 @@ uint32 RGBAVector::ToPixel(const uint32 channelMask, const int pBit) const {
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template<const uint8 nBuckets>
double RGBACluster::QuantizedError( double RGBACluster::QuantizedError(
const RGBAVector &p1, const RGBAVector &p2, const RGBAVector &p1, const RGBAVector &p2,
uint8 nBuckets, uint32 bitMask, const RGBAVector &errorMetricVec, uint32 bitMask, const RGBAVector &errorMetricVec,
const int pbits[2], uint8 *indices const int pbits[2], uint8 *indices
) const { ) const {
// nBuckets should be a power of two. // nBuckets should be a power of two.
assert(nBuckets == 3 || !(nBuckets & (nBuckets - 1))); assert(!(nBuckets & (nBuckets - 1)));
const uint8 indexPrec = 8-CountBitsInMask(~(nBuckets - 1));
const uint8 indexPrec = (nBuckets == 3)? 3 : 8-CountBitsInMask(~(nBuckets - 1));
typedef uint32 tInterpPair[2]; typedef uint32 tInterpPair[2];
typedef tInterpPair tInterpLevel[16]; typedef tInterpPair tInterpLevel[16];
@ -295,6 +295,21 @@ double RGBACluster::QuantizedError(
return totalError; return totalError;
} }
template double RGBACluster::QuantizedError<4>(
const RGBAVector &p1, const RGBAVector &p2,
uint32 bitMask, const RGBAVector &errorMetricVec,
const int pbits[2], uint8 *indices) const;
template double RGBACluster::QuantizedError<8>(
const RGBAVector &p1, const RGBAVector &p2,
uint32 bitMask, const RGBAVector &errorMetricVec,
const int pbits[2], uint8 *indices) const;
template double RGBACluster::QuantizedError<16>(
const RGBAVector &p1, const RGBAVector &p2,
uint32 bitMask, const RGBAVector &errorMetricVec,
const int pbits[2], uint8 *indices) const;
uint32 RGBACluster::GetPrincipalAxis(RGBADir &axis, float *eigOne, float *eigTwo) const { uint32 RGBACluster::GetPrincipalAxis(RGBADir &axis, float *eigOne, float *eigTwo) const {
// We use these vectors for calculating the covariance matrix... // We use these vectors for calculating the covariance matrix...

View file

@ -71,6 +71,7 @@
#include "Matrix4x4.h" #include "Matrix4x4.h"
#include <algorithm> #include <algorithm>
#include <cassert>
#include <cmath> #include <cmath>
#include <cfloat> #include <cfloat>
#include <cstring> #include <cstring>
@ -164,11 +165,25 @@ public:
// Returns the error if we were to quantize the colors right now with the // Returns the error if we were to quantize the colors right now with the
// given number of buckets and bit mask. // given number of buckets and bit mask.
template<const uint8 nBuckets>
double QuantizedError( double QuantizedError(
const RGBAVector &p1, const RGBAVector &p2, const RGBAVector &p1, const RGBAVector &p2,
uint8 nBuckets, uint32 bitMask, const RGBAVector &errorMetricVec, uint32 bitMask, const RGBAVector &errorMetricVec,
const int pbits[2] = NULL, uint8 *indices = NULL) const; const int pbits[2] = NULL, uint8 *indices = NULL) const;
double QuantizedError(
const RGBAVector &p1, const RGBAVector &p2,
uint32 nBuckets, uint32 bitMask, const RGBAVector &errorMetricVec,
const int pbits[2] = NULL, uint8 *indices = NULL) const {
switch(nBuckets) {
case 4: return QuantizedError<4>(p1, p2, bitMask, errorMetricVec, pbits, indices);
case 8: return QuantizedError<8>(p1, p2, bitMask, errorMetricVec, pbits, indices);
case 16: return QuantizedError<16>(p1, p2, bitMask, errorMetricVec, pbits, indices);
}
assert(!"Unsupported num buckets");
return std::numeric_limits<double>::max();
}
bool AllSamePoint() const { return m_Max == m_Min; } bool AllSamePoint() const { return m_Max == m_Min; }
// Returns the principal axis for this point cluster. // Returns the principal axis for this point cluster.