常用宏定义
数值相关的宏定义
闰年的判断 ,年份可以整除4并且不能整除100,或者可以整除400,则为闰年;
#define IS_LEAP_YEAR(y) (((((y) % 4) == 0) && (((y) % 100) != 0)) \
|| (((y) % 400) == 0))
**MAX 与 MIN ** ;
#define MAX(x, y) (((x) < (y)) ? (y) : (x))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
BCD码 ;
#define BCD2HEX(x) (((x) >> 4) * 10 + ((x) & 0x0F))
#define HEX2BCD(x) (((x) % 10) + ((((x) / 10) % 10) << 4))
字符相关的宏定义
字符范围判断
#define in_range(c, lo, up) ((uint8)c >= lo && (uint8)c <= up)
#define isprint(c) in_range(c, 0x20, 0x7f)
#define isdigit(c) in_range(c, '0', '9')
#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
#define islower(c) in_range(c, 'a', 'z')
#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
#define isascii(c) ((unsigned) (c) <= 0177)
byte相关的宏定义
#define MSB(x) (((x) >> 8) & 0xff)
#define LSB(x) ((x) & 0xff)
#define MSW(x) (((x) >> 16) & 0xffff)
#define LSW(x) ((x) & 0xffff)
#define WORDSWAP(x) (MSW(x) | (LSW(x) << 16))
#define LLSB(x) ((x) & 0xff)
#define LNLSB(x) (((x) >> 8) & 0xff)
#define LNMSB(x) (((x) >> 16) & 0xff)
#define LMSB(x) (((x) >> 24) & 0xff)
#define LONGSWAP(x) ((LLSB(x) << 24) \
| ( LNLSB ( x) << 16 ) \
| ( LNMSB ( x) << 8 ) \
| ( LMSB ( x) ) )
bit相关的宏定义
#define BIT_IS_1(x,y) (((x)>>(y))&0x01u)
#define SETBITS(x,y,n) (x) = (n) ? ((x)|(1 << (y))) : ((x) &(~(1 << (y))));
#define BIT_INVERSE(x,y) ((x)=(x)^(1<<(y)))
#define BIT_OF_BYTES(x, y) (BITS(x[(y)/8], (y)%8))
#define SETBITSTO0_OF_BYTES(x, y) (x[(y)/8]) &= (~(1 << ((y)%8)))
#define SETBITSTO1_OF_BYTES(x, y) (x[(y)/8]) |= (1 << ((y)%8))
数组与结构体相关的宏定义
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
#define MOFFSET(structure, member) ((int) &(((structure *) 0) -> member))
#define MEMBER_SIZE(structure, member) (sizeof (((structure *) 0) -> member))
对齐的宏定义
#define ALIGN_UP(x, align) (((int) (x) + (align - 1)) & ~(align - 1))
#define ALIGN_DOWN(x, align) ((int)(x) & ~(align - 1))
#define ALIGNED(x, align) (((int)(x) & (align - 1)) == 0)
#define PAGE_SIZE 4096
#define PAGE_MASK (~(PAGE_SIZE-1))
#define PAGE_ALIGN(addr) -(((addr)+PAGE_SIZE-1) & PAGE_MASK)