C语言编程常用数值计算的高性能实现

来源:互联网 发布:mysql中check约束 编辑:程序博客网 时间:2024/04/28 06:41

本篇介绍一组非常简单却又很常用的数值计算的宏实现。本篇所提到的数值计算问题,相信C语言初学者都能做得出来,但是本篇中给出的例子实现却更注重效率。这些例子实现的最大特点是,消除了逻辑跳转。这样做的好处是避免了分支预测的风险,或者换句话说,可以更好地发挥处理器流水线的性能。由于本篇的问题都很简单,笔者就废话少说,直接看例子了。

/* 高位全0,低N位全1 */#define Low_N_Bits_One(N)     ((1<<N) -1)/* 高位全1,低N位全0 */#define Low_N_Bits_Zero(N)    (~((1<<N) -1))/* 低位第N位置1 */#define Set_Bit_N(NUM, N)     (NUM | (1 << (N - 1)))/* 低位第N位置0 */#define Clear_Bit_N(NUM, N)   (NUM & (~(1 << (N - 1))))/* 低位第N位反转 */#define Reverse_Bit_N(NUM, N) ((NUM) ^ (1 << (N - 1)))/* 上取整 */#define UpRoun8(Num)  (((Num) + 0x7)  & (~0x7))#define UpRoun16(Num) (((Num) + 0xf)  & (~0xf))#define UpRoun32(Num) (((Num) + 0x1f) & (~0x1f))/* 下取整 */#define LowRoun8(Num)  ((Num) & (~0x7))#define LowRoun16(Num) ((Num) & (~0xf))#define LowRoun32(Num) ((Num) & (~0x1f))/* 求商 */#define Div8(Num)         ((Num)>>3)#define Div16(Num)        ((Num)>>4)#define Div32(Num)        ((Num)>>5)/* 余数进位求商 */#define UpDiv8(Num)        (((Num)>>3) + !!((Num) & 0x7))#define UpDiv16(Num)        (((Num)>>4) + !!((Num) & 0xf))#define UpDiv32(Num)        (((Num)>>5) + !!((Num) & 0x1f))/* 求余 */#define M8(Num)        ((Num) & 0x7)#define M16(Num)       ((Num) & 0xf)#define M32(Num)       ((Num) & 0x1f)/* 求反余即求最小的x,满足(Num + x) % 模数 == 0例如 RM16(Num) 等价于 (16 - Num%16) % 16 */#define RM8(Num)        ((~(Num) + 1) & 0x7)#define RM16(Num)        ((~(Num) + 1) & 0xf)#define RM32(Num)        ((~(Num) + 1) & 0x1f)

最后,再列出gcc提供的位操作相关的部分built-in函数

int __builtin_ffs (unsigned int x)
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.


int __builtin_clz (unsigned int x)
Returns the number of leading 0-bits in x, starting at the most significant bit position.
If x is 0, the result is undefined.

int __builtin_ctz (unsigned int x)
Returns the number of trailing 0-bits in x, starting at the least significant bit position.
If x is 0, the result is undefined.


int __builtin_clrsb (int x)
Returns the number of leading redundant sign bits in x, i.e. the number of bits
following the most significant bit that are identical to it. There are no special cases
for 0 or other values.


int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.


int __builtin_parity (unsigned int x)
Returns the parity of x, i.e. the number of 1-bits in x modulo 2.


上述函数的操作数宽度是int。

如果在上述函数名后面加上l或ll,则操作数宽度就分别对应long或long long。

0 0
原创粉丝点击