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。
- C语言编程常用数值计算的高性能实现
- 几道数值计算题目的c语言实现
- 高性能的贪吃蛇C语言实现
- atitit.常用编程语言的性能比较 c c++ java
- 数值作业:龙贝格算法计算积分C语言实现
- 利用c语言编译器做复杂的数值计算
- C语言计算一个数每个位的数值
- C#(Csharp)+OpenCL+CLBLAS库,实现GPU高性能计算。
- libPhenom:Facebook开源的高性能C语言并发编程框架
- libPhenom:Facebook开源的高性能C语言并发编程框架
- 数值计算--全选主元--c语言
- C语言之数值计算--级数算法
- .Net高性能数值计算库—ILNumerics.Net
- 编程常用算法 --- C/C++ 语言实现
- 高性能c语言编码
- C语言常用数值类型转换函数
- 数值作业:变步长梯形求积算法计算积分C语言实现
- c语言的数值运算
- 客户端与服务器新建套接字连接的一种方法
- Leetcode no. 214
- Java线程安全策略
- for循环的实质及运用
- Android mvp的简单使用和实现
- C语言编程常用数值计算的高性能实现
- start_armboot函数简介
- 设计模式
- 设计模式一之观察者模式
- JAVA——自定义异常
- 字符串-全排序
- LINUX驱动编写技巧(1)
- ANDROID中vnd.android.cursor的解释
- Leetcode no. 226