📄 types.h
字号:
/**
* Typename for 32 bits signed integer
*/
typedef signed int s32;
/**
* Typename for 32 bits floatting point number
*/
typedef float f32;
/**
* Typename for 64 bits floatting point number
*/
typedef double f64;
# if defined(HASINT64)
/**
* Typename for 64 bits unsigned integer
*/
typedef unsigned int long u64;
/**
* Typename for 64 bits signed integer
*/
typedef signed long long s64;
# endif
#endif
/**
* Fixed-point types
* hi-Siiiiiiiiiiiiiiiiiiiii-lo | hi-ffffffffff-lo
*/
typedef s32 sfp22_10;
/**
* Fixed-point types
* hi-iiiiiiiiiiiiiiiiiiiiii-lo | hi-ffffffffff-lo
*/
typedef u32 ufp22_10;
/**
* Fixed point types
* hi-Siiiiiiiiiiiiiii-lo | hi-ffffffffffffffff-lo
*/
typedef s32 sfp16_16;
/**
* Fixed point types
* hi-iiiiiiiiiiiiiiii-lo | hi-ffffffffffffffff-lo
*/
typedef u32 ufp16_16;
/*
COMmy macros
*/
#define COM_RELEASE(ref) if (ref) { (ref)->Release(); (ref) = 0; }
#define SINGLE_RELEASE(ref) if (ref) { delete (ref); (ref) = 0; }
#define ARRAY_RELEASE(ref) if (ref) { delete [](ref); (ref) = 0; }
template <class T>
class rect
{
public:
rect() : x( 0 ), y( 0 ), w( 0 ), h( 0 )
{}
rect( T xx, T yy, T ww, T hh ) : x( xx ), y( yy ), w( ww ), h( hh )
{}
T x, y, w, h;
};
template <class T>
class AutoArray
{
T *ptr;
public:
AutoArray( T *c_ptr )
{
ptr = c_ptr;
}
~AutoArray()
{
ARRAY_RELEASE( ptr );
}
INLINE void cancel()
{
ptr = 0;
}
};
template <class T>
class Automatic
{
T *ptr;
public:
Automatic( T *c_ptr )
{
ptr = c_ptr;
}
~Automatic()
{
SINGLE_RELEASE( ptr );
}
INLINE void cancel()
{
ptr = 0;
}
};
// Derive from NoCopies to disallow copies of derived class
class NoCopies
{
protected:
NoCopies()
{}
~NoCopies()
{}
private:
NoCopies( const NoCopies &cp );
NoCopies &operator=( const NoCopies &cp );
};
// Byte-order swapping
#define BOSWAP32(n) ( ((n) << 24) | (((n) & 0x00ff0000) >> 8) | (((n) & 0x0000ff00) << 8) | ((n) >> 24) ) /* only works for u32 */
#define BOSWAP16(n) ( ((n) << 8) | ((n) >> 8) ) /* only works for u16 */
#ifdef LITTLE_ENDIAN
# define swapLE(n)
# define getLE(n) (n)
INLINE void swapBE( u32 &n )
{
n = BOSWAP32( n );
}
INLINE void swapBE( u16 &n )
{
n = BOSWAP16( n );
}
INLINE u32 getBE( u32 n )
{
return BOSWAP32( n );
}
INLINE u16 getBE( u16 n )
{
return BOSWAP16( n );
}
INLINE void swapBE( s32 &n )
{
n = BOSWAP32( ( u32 ) n );
}
INLINE void swapBE( s16 &n )
{
n = BOSWAP16( ( u16 ) n );
}
INLINE s32 getBE( s32 n )
{
return BOSWAP32( ( u32 ) n );
}
INLINE s16 getBE( s16 n )
{
return BOSWAP16( ( u16 ) n );
}
#else
# define swapBE(n)
# define getBE(n) (n)
INLINE void swapLE( u32 &n )
{
n = BOSWAP32( n );
}
INLINE void swapLE( u16 &n )
{
n = BOSWAP16( n );
}
INLINE u32 getLE( u32 n )
{
return BOSWAP32( n );
}
INLINE u16 getLE( u16 n )
{
return BOSWAP16( n );
}
INLINE void swapLE( s32 &n )
{
n = BOSWAP32( ( u32 ) n );
}
INLINE void swapLE( s16 &n )
{
n = BOSWAP16( ( u16 ) n );
}
INLINE s32 getLE( s32 n )
{
return BOSWAP32( ( u32 ) n );
}
INLINE s16 getLE( s16 n )
{
return BOSWAP16( ( u16 ) n );
}
#endif
} // namespace cat
// Rotation
#define ROL8(n, r) ( ((n) << (r)) | ((n) >> ( 8 - (r))) ) /* only works for u8 */
#define ROR8(n, r) ( ((n) >> (r)) | ((n) << ( 8 - (r))) ) /* only works for u8 */
#define ROL16(n, r) ( ((n) << (r)) | ((n) >> (16 - (r))) ) /* only works for u16 */
#define ROR16(n, r) ( ((n) >> (r)) | ((n) << (16 - (r))) ) /* only works for u16 */
#define ROL32(n, r) ( ((n) << (r)) | ((n) >> (32 - (r))) ) /* only works for u32 */
#define ROR32(n, r) ( ((n) >> (r)) | ((n) << (32 - (r))) ) /* only works for u32 */
/*
Add memory that is allocated on a 32-bit boundary
and has at least one block.
*/
#define MEMADD32(ptr, len, val) { \
register u32 *__data = (u32*)(ptr); /* pointer to data to clear */ \
register s32 __length = (len); /* number of 32-bit blocks */ \
\
switch (__length % 8) \
{ \
case 0: do { *__data++ += (val); \
case 7: *__data++ += (val); \
case 6: *__data++ += (val); \
case 5: *__data++ += (val); \
case 4: *__data++ += (val); \
case 3: *__data++ += (val); \
case 2: *__data++ += (val); \
case 1: *__data++ += (val); \
__length -= 8; \
} while(__length > 0); \
} \
}
/**
* Safe null-terminated string -> char buffer copy
* \param dest the resulting string
* \param src the string to copy
* \param size the number of char to copy
*/
#define STRNCPY(dest, src, size) { \
strncpy(dest, src, size); \
dest[size-1] = 0; \
}
/*
Because memory clearing is a frequent operation
*/
#define MEMCLR(dest, size) memset(dest, 0, size)
// Works for arrays, also
#define OBJCLR(object) memset(&(object), 0, sizeof(object))
/*
Fast binary method of counting bits
MIT Hackmem from X11
Only works for 32-bit integers
*/
#define _C1B_INTERMED(n) ( (n) - (((n) >> 1) & 033333333333) - (((n) >> 2) & 011111111111) )
#define COUNT1BITS32(n) ( ((_C1B_INTERMED(n) + (_C1B_INTERMED(n) >> 3)) & 030707070707) % 63 )
/*
Consider N an ordered set of 1/0 values.
LESSTHAN2BITS(n) := there are less than 2 bits in set N
Proof:
(N - 1) will clear the least significant of the bits in N.
Three cases exist concerning (N-1):
N contains 0 bits
An intersection with N would be trivially null.
N contains 1 bit
The least only existing bit was cleared,
thus an intersection with the original
set WOULD be null.
N contains more than 1 bit
A more significant bit remains in the set,
thus an intersection with the original
set WOULD NOT be null.
*/
#define AT_LEAST_2_BITS(n) ( (n) & ((n) - 1) )
#define LEAST_SIGNIFICANT_BIT(n) ( (n) & -(n) )
#define IS_POWER_OF_2(n) ( n && !AT_LEAST_2_BITS(n) )
INLINE cat::u32 next_highest_power_of_2( cat::u32 n )
{
if ( IS_POWER_OF_2( n ) )
return n;
cat::u16 b = 2;
while ( n >>= 1 )
b <<= 1;
return b;
}
/*
CEIL*
Bump 'n' to the next unit of 'width'.
*/
#define CEIL_UNIT(n, width) ( ( (n) + (width) - 1 ) / (width) )
#define CEIL(n, width) ( CEIL_UNIT(n, width) * (width) )
/*
Quick character manipulation
*/
//#define IS_ALPHA(ch) ( (((u8)(ch) & 0xc0) == 0x40) && ((((u8)(ch) - 1) & 0x1f) <= 0x19) )
//#define IS_NUM(ch) ( ((u8)(ch) - 0x30) < 10 )
//#define IS_ALPHANUM(ch) ( IS_ALPHA(ch) || IS_NUM(ch) )
//#define TO_LOWER(ch) (char)( (ch) | 0x20 ) /* must be upper/lower-case alpha */
//#define TO_UPPER(ch) (char)( (ch) & (~0x20) ) /* must be upper/lower-case alpha */
#endif // TYPES_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -