21 #include <type_traits>
36 #if __LDBL_MAX_EXP__ == 16384
39 typedef long double f80;
42 #if (__SIZEOF_FLOAT128__ == 16)
45 typedef __float128 f128;
61 #define RC_THREAD_LOCAL thread_local
64 #define RC_NOEXCEPT noexcept
66 #define decltype(x) typeof(x)
67 #define RC_THREAD_LOCAL
68 #define RC_NOEXCEPT throw()
81 return (1 == (T(1.1)));
86 template<
class T,
class Error,
bool>
struct TypeCheck {
87 static inline void Assert() { }
89 template<
class T,
class Error>
struct TypeCheck<T, Error, false> {
90 static inline void Assert() { T::__TypeCheckFailed__; }
98 #define RC_TYPE_TRUE_MACRO(StructName, Type) \
99 template<> struct StructName<Type> { static const bool value = true; };\
100 template<> struct StructName<const Type> { static const bool value = true; };\
101 template<> struct StructName<volatile Type> { static const bool value = true; };\
102 template<> struct StructName<const volatile Type> { static const bool value = true; };
105 template<
class T>
struct FloatType {
static const bool value =
false; };
115 template<
class T>
struct IntegerType {
static const bool value =
false; };
126 class ThisTypeIsNotAFloat { };
127 class ThisTypeIsNotAnInteger { };
132 TypeCheck<T, ThisTypeIsNotAFloat, FloatType<T>::value>
::Assert();
137 TypeCheck<T, ThisTypeIsNotAnInteger, IntegerType<T>::value>
::Assert();
142 template<
class T,
bool>
struct ForceFloatHelper {
145 template<
class T>
struct ForceFloatHelper<T, false> {
153 ->
typename ForceFloatHelper<T, FloatType<T>::value>::type {
160 template<
class T>
typename std::make_unsigned<T>::type
ForceUnsigned(T val) {
164 template<
class T>
typename std::make_signed<T>::type
ForceSigned(T val) {
168 #define RC_MAKE_FORCESIGNS(TS) \
169 u##TS ForceUnsigned(i##TS val) { return val; } \
170 i##TS ForceSigned(u##TS val) { return val; }
172 template <
class T> T
ForceSigned(T val) {
return val; }
181 = std::numeric_limits<T>::min()) {
return std::numeric_limits<T>::min();}
185 = std::numeric_limits<T>::max()) {
return std::numeric_limits<T>::max();}
190 = std::numeric_limits<T>::lowest()) {
191 return std::numeric_limits<T>::lowest();
194 template<
class T,
bool>
struct LOW_VAL_Helper {
195 static inline T F() {
return MIN_VAL<T>(); }
197 template<
class T>
struct LOW_VAL_Helper<T, true> {
199 static inline T F() {
return -MAX_VAL<T>(); }
202 template<
class T>
inline T
LOW_VAL(
const T &x
203 = LOW_VAL_Helper<T, FloatType<T>::value>::F()) {
204 return LOW_VAL_Helper<T, FloatType<T>::value>::F();
209 template<
class T,
bool>
struct MIN_POS_Helper {
210 static inline T F() {
return 1; }
212 template<
class T>
struct MIN_POS_Helper<T, true> {
213 static inline T F() {
return MIN_VAL<T>(); }
219 = MIN_POS_Helper<T, FloatType<T>::value>::F()) {
220 return MIN_POS_Helper<T, FloatType<T>::value>::F();
235 class RC_Float_Size_Checker {
241 static_assert(
sizeof(
f32)==4,
"f32 is not 4 bytes");
242 static_assert(
sizeof(
f64)==8,
"f64 is not 8 bytes");
244 static_assert(
sizeof(f128)==16,
"f128 is not 16 bytes");
247 char RC_f32_Is_Not_4_bytes[0-(
sizeof(
f32)!=4)];
248 char RC_f64_Is_Not_8_bytes[0-2*(
sizeof(
f64)!=8)];
250 char RC_f128_Is_Not_16_bytes[0-2*(
sizeof(f128)!=16)];
264 return ( (x >> 8) | (x << 8) );
270 | ((x >> 8) & 0x0000FF00)
271 | ((x << 8) & 0x00FF0000)
278 | ((x >> 40) & 0x000000000000FF00ull)
279 | ((x >> 24) & 0x0000000000FF0000ull)
280 | ((x >> 8) & 0x00000000FF000000ull)
281 | ((x << 8) & 0x000000FF00000000ull)
282 | ((x << 24) & 0x0000FF0000000000ull)
283 | ((x << 40) & 0x00FF000000000000ull)
337 return (*
reinterpret_cast<char *
>(&test) == 1);
Provides a set of convenience macros for metaprogramming and debugging.
#define RC_UNUSED_PARAM(v)
Use in function headers as void func(int RC_UNUSED_PARAM(x)) to suppress warnings about non-use.
Definition: Macros.h:175
#define RC_ARGS_EACH(Macro,...)
Does RC_ARGS_EACH(Macro,a,b,c) --> Macro(a)Macro(b)Macro(c), for any number of parameters up to 342 o...
Definition: Macros.h:87
void Assert() const
Throws RC::ErrorMsgNull if the pointer is null.
Definition: PtrCommon.h:12
The version information and configuration settings for RC Lib.
uint64_t u64
64-bit unsigned integer.
Definition: Types.h:31
int32_t i32
32-bit signed integer.
Definition: Types.h:28
float f32
32-bit float.
Definition: Types.h:33
f128 fBIGGEST
The biggest float type available.
Definition: Types.h:49
int64_t i64
64-bit signed integer.
Definition: Types.h:30
uint8_t u8
8-bit unsigned integer.
Definition: Types.h:25
double f64
64-bit float.
Definition: Types.h:34
long double f80
80-bit float. (Note: Usually takes 16 bytes.)
Definition: Types.h:39
#define RC_TYPE_TRUE_MACRO(StructName, Type)
For adding a new float or integral type, where StructName is FloatType or IntegerType.
Definition: Types.h:98
uint16_t u16
16-bit unsigned integer.
Definition: Types.h:27
int16_t i16
16-bit signed integer.
Definition: Types.h:26
int8_t i8
8-bit signed integer.
Definition: Types.h:24
uint32_t u32
32-bit unsigned integer.
Definition: Types.h:29
Auto-detects the endianness of the compilation target, and provides automatic endian conversion featu...
Definition: Types.h:259
static bool IsLittle()
True if this system is little-endian.
Definition: Types.h:335
static i16 Swap(i16 x)
Reverse the byte order.
Definition: Types.h:293
static i32 Swap(i32 x)
Reverse the byte order.
Definition: Types.h:295
static bool IsBig()
True if this system is big-endian.
Definition: Types.h:341
static u16 Swap(u16 x)
Reverse the byte order.
Definition: Types.h:263
static u8 Swap(u8 x)
Do nothing. (For consistency.)
Definition: Types.h:289
static i8 Swap(i8 x)
Do nothing. (For consistency.)
Definition: Types.h:291
static u32 Swap(u32 x)
Reverse the byte order.
Definition: Types.h:268
static T FromLittle(const T &x)
Converts x from little-endian.
Definition: Types.h:313
static T ToBig(const T &x)
Converts x to big-endian.
Definition: Types.h:319
static T FromBig(const T &x)
Converts x from big-endian.
Definition: Types.h:330
static i64 Swap(i64 x)
Reverse the byte order.
Definition: Types.h:297
static u64 Swap(u64 x)
Reverse the byte order.
Definition: Types.h:276
static T ToLittle(const T &x)
Converts x to little-endian.
Definition: Types.h:302
std::make_unsigned< T >::type ForceUnsigned(T val)
Returns the unsigned equivalent to the given type.
Definition: Types.h:160
T MIN_POS(const T &x __attribute__((unused))=MIN_POS_Helper< T, FloatType< T >::value >::F())
Provide the minimum positive value held by this type.
Definition: Types.h:218
FT * FuncPtr
Clean C-style function pointers: FuncPtr<void(int)> f = &func; f(4);.
Definition: Types.h:226
bool IsSignedType(const T &x __attribute__((unused))=0)
True if the type is signed.
Definition: Types.h:75
void AssertFloat()
For generating compilation errors if the type is not a float.
Definition: Types.h:131
bool IsIntegerType(const T &x __attribute__((unused))=1)
True if the type is an integer type.
Definition: Types.h:80
void AssertInteger()
For generating compilation errors if the type is not an integer.
Definition: Types.h:136
T MIN_VAL(const T &x __attribute__((unused))=std::numeric_limits< T >::min())
Provide the minimum value held by this type. For floats, the smallest positive value.
Definition: Types.h:180
T LOW_VAL(const T &x __attribute__((unused))=std::numeric_limits< T >::lowest())
Provide the lowest value held by this type, for which no value is less.
Definition: Types.h:189
FT(C::*) MemFuncPtr
Clean function pointer syntax for member function.
Definition: Types.h:230
std::make_signed< T >::type ForceSigned(T val)
Returns the signed equivalent to the given type.
Definition: Types.h:164
auto ForceFloat(T val) -> typename ForceFloatHelper< T, FloatType< T >::value >::type
If T is a float type, return the same type. Otherwise return it as an f64.
Definition: Types.h:152
T MAX_VAL(const T &x __attribute__((unused))=std::numeric_limits< T >::max())
Provide the maximum value held by this type.
Definition: Types.h:184