Files
unity/crnlib/crn_traits.h
T
Alexander Suvorov 3e12aff909 Fix miscellaneous compiler warnings
DXT Testing:

The modified algorithm has been tested on the Kodak test set using 64-bit build with default settings (running on Windows 10, i7-4790, 3.6GHz). All the decompressed test images are identical to the images being compressed and decompressed using original version of Crunch (revision ea9b8d8).

[Compressing Kodak set without mipmaps using DXT1 encoding]
Original: 1582222 bytes / 28.866 sec
Modified: 1468204 bytes / 11.858 sec
Improvement: 7.21% (compression ratio) / 58.92% (compression time)

[Compressing Kodak set with mipmaps using DXT1 encoding]
Original: 2065243 bytes / 36.878 sec
Modified: 1914805 bytes / 15.625 sec
Improvement: 7.28% (compression ratio) / 57.63% (compression time)

ETC Testing:

The modified algorithm has been tested on the Kodak test set using 64-bit build with default settings (running on Windows 10, i7-4790, 3.6GHz). The ETC1 quantization parameters have been selected in such a way, so that ETC1 compression gives approximately the same average Luma PSNR as the corresponding DXT1 compression (which is equal to 34.044 dB for the Kodak test set compressed without mipmaps using DXT1 encoding and default quality settings).

[Compressing Kodak set without mipmaps using ETC1 encoding]
Total size: 1607858 bytes
Total time: 17.181 sec
Average bitrate: 1.363 bpp
Average Luma PSNR: 34.050 dB
2017-09-11 13:52:21 +02:00

174 lines
5.6 KiB
C++

// File: crn_traits.h
// See Copyright Notice and license at the end of inc/crnlib.h
#pragma once
namespace crnlib {
template <typename T>
struct int_traits {
enum { cMin = crnlib::cINT32_MIN,
cMax = crnlib::cINT32_MAX,
cSigned = true };
};
template <>
struct int_traits<int8> {
enum { cMin = crnlib::cINT8_MIN,
cMax = crnlib::cINT8_MAX,
cSigned = true };
};
template <>
struct int_traits<int16> {
enum { cMin = crnlib::cINT16_MIN,
cMax = crnlib::cINT16_MAX,
cSigned = true };
};
template <>
struct int_traits<int32> {
enum { cMin = crnlib::cINT32_MIN,
cMax = crnlib::cINT32_MAX,
cSigned = true };
};
template <>
struct int_traits<uint8> {
enum { cMin = 0,
cMax = crnlib::cUINT8_MAX,
cSigned = false };
};
template <>
struct int_traits<uint16> {
enum { cMin = 0,
cMax = crnlib::cUINT16_MAX,
cSigned = false };
};
template <>
struct int_traits<uint32> {
enum { cMin = 0,
cMax = crnlib::cUINT32_MAX,
cSigned = false };
};
template <typename T>
struct scalar_type {
enum { cFlag = false };
static inline void construct(T* p) { helpers::construct(p); }
static inline void construct(T* p, const T& init) { helpers::construct(p, init); }
static inline void construct_array(T* p, uint n) { helpers::construct_array(p, n); }
static inline void destruct(T* p) { helpers::destruct(p); }
static inline void destruct_array(T* p, uint n) { helpers::destruct_array(p, n); }
};
template <typename T>
struct scalar_type<T*> {
enum { cFlag = true };
static inline void construct(T** p) { memset(p, 0, sizeof(T*)); }
static inline void construct(T** p, T* init) { *p = init; }
static inline void construct_array(T** p, uint n) { memset(p, 0, sizeof(T*) * n); }
static inline void destruct(T**) {}
static inline void destruct_array(T**, uint) {}
};
#define CRNLIB_DEFINE_BUILT_IN_TYPE(X) \
template <> \
struct scalar_type<X> { \
enum { cFlag = true }; \
static inline void construct(X* p) { memset(p, 0, sizeof(X)); } \
static inline void construct(X* p, const X& init) { memcpy(p, &init, sizeof(X)); } \
static inline void construct_array(X* p, uint n) { memset(p, 0, sizeof(X) * n); } \
static inline void destruct(X*) {} \
static inline void destruct_array(X*, uint) {} \
};
CRNLIB_DEFINE_BUILT_IN_TYPE(bool)
CRNLIB_DEFINE_BUILT_IN_TYPE(char)
CRNLIB_DEFINE_BUILT_IN_TYPE(unsigned char)
CRNLIB_DEFINE_BUILT_IN_TYPE(short)
CRNLIB_DEFINE_BUILT_IN_TYPE(unsigned short)
CRNLIB_DEFINE_BUILT_IN_TYPE(int)
CRNLIB_DEFINE_BUILT_IN_TYPE(unsigned int)
CRNLIB_DEFINE_BUILT_IN_TYPE(long)
CRNLIB_DEFINE_BUILT_IN_TYPE(unsigned long)
#ifdef __GNUC__
CRNLIB_DEFINE_BUILT_IN_TYPE(long long)
CRNLIB_DEFINE_BUILT_IN_TYPE(unsigned long long)
#else
CRNLIB_DEFINE_BUILT_IN_TYPE(__int64)
CRNLIB_DEFINE_BUILT_IN_TYPE(unsigned __int64)
#endif
CRNLIB_DEFINE_BUILT_IN_TYPE(float)
CRNLIB_DEFINE_BUILT_IN_TYPE(double)
CRNLIB_DEFINE_BUILT_IN_TYPE(long double)
#undef CRNLIB_DEFINE_BUILT_IN_TYPE
// See: http://erdani.org/publications/cuj-2004-06.pdf
template <typename T>
struct bitwise_movable {
enum { cFlag = false };
};
// Defines type Q as bitwise movable.
// Bitwise movable: type T may be safely moved to a new location via memcpy, without requiring the old copy to be destructed.
// However, the final version of the object (wherever it winds up in memory) must be eventually destructed (a single time, of course).
// Bitwise movable is a superset of bitwise copyable (all bitwise copyable types are also bitwise movable).
#define CRNLIB_DEFINE_BITWISE_MOVABLE(Q) \
template <> \
struct bitwise_movable<Q> { \
enum { cFlag = true }; \
};
template <typename T>
struct bitwise_copyable {
enum { cFlag = false };
};
// Defines type Q as bitwise copyable.
// Bitwise copyable: type T may be safely and freely copied (duplicated) via memcpy, and *does not* require destruction.
#define CRNLIB_DEFINE_BITWISE_COPYABLE(Q) \
template <> \
struct bitwise_copyable<Q> { \
enum { cFlag = true }; \
};
#define CRNLIB_IS_POD(T) __is_pod(T)
#define CRNLIB_IS_SCALAR_TYPE(T) (scalar_type<T>::cFlag)
#define CRNLIB_IS_BITWISE_COPYABLE(T) (CRNLIB_IS_SCALAR_TYPE(T) || CRNLIB_IS_POD(T) || (bitwise_copyable<T>::cFlag))
#define CRNLIB_IS_BITWISE_COPYABLE_OR_MOVABLE(T) (CRNLIB_IS_BITWISE_COPYABLE(T) || (bitwise_movable<T>::cFlag))
#define CRNLIB_HAS_DESTRUCTOR(T) ((!scalar_type<T>::cFlag) && (!__is_pod(T)))
// From yasli_traits.h:
// Credit goes to Boost;
// also found in the C++ Templates book by Vandevoorde and Josuttis
typedef char (&yes_t)[1];
typedef char (&no_t)[2];
template <class U>
yes_t class_test(int U::*);
template <class U>
no_t class_test(...);
template <class T>
struct is_class {
enum { value = (sizeof(class_test<T>(0)) == sizeof(yes_t)) };
};
template <typename T>
struct is_pointer {
enum { value = false };
};
template <typename T>
struct is_pointer<T*> {
enum { value = true };
};
CRNLIB_DEFINE_BITWISE_COPYABLE(empty_type);
CRNLIB_DEFINE_BITWISE_MOVABLE(empty_type);
} // namespace crnlib