代码阅读笔记

来源:互联网 发布:淘宝客机器人免费 编辑:程序博客网 时间:2024/05/10 02:19

1. 强制类型转换 不提示warning方法

/* Casts 'pointer' to 'type' and issues a compiler warning if the cast changes

 * anything other than an outermost "const" or "volatile" qualifier.
 *
 * The cast to int is present only to suppress an "expression using sizeof
 * bool" warning from "sparse" (see
 * http://permalink.gmane.org/gmane.comp.parsers.sparse/2967). */
#define CONST_CAST(TYPE, POINTER)                               \
    ((void) sizeof ((int) ((POINTER) == (TYPE) (POINTER))),     \

     (TYPE) (POINTER))


2. 找出整型数中最低位为“1”的位置,等同于(x-x&(x-1))/2,那个效率高呢?

static int
raw_ctz(uint32_t n)
{
    unsigned int k;
    int count = 31;


#define CTZ_STEP(X)                             \
    k = n << (X);                               \
    if (k) {                                    \
        count -= X;                             \
        n = k;                                  \
    }
    CTZ_STEP(16);
    CTZ_STEP(8);
    CTZ_STEP(4);
    CTZ_STEP(2);
    CTZ_STEP(1);
#undef CTZ_STEP


    return count;
}

原创粉丝点击