Fork me on GitHub

  RISC-V的确是个好东西,可是,免费的东西往往需要付出代价才能得到了,最近遇到了一个算法中的问题,追了好久,最终追到了这个库函数中,没想到,这个库函数居然还隐藏着一些猫腻。值得记下来啊。

首先上一个在X86平台下的例子:

   1 #include <stdio.h>

 2 /* Position of the most significant bit of x */
 3 #define gap8_fl1(x)             (31 - __builtin_clz((x)))
 4 
 5 static int array[32];
 6 int main()
 7 {
 8     int test = 0xFFFFF00;
 9 
10     while(test > 0)
11     {   
12         test = test>>1;
13         printf("test:%x gap8:%d \n",test,gap8_fl1(test));
14     }   
21 
22     return 0;
23 }
24 ~ 

  这个是编译器的库函数,表示要获取你输入参数的位数。

测试结果:

test:7ffff80 gap8:26 
test:3ffffc0 gap8:25 
test:1ffffe0 gap8:24 
test:fffff0 gap8:23 
test:7ffff8 gap8:22 
test:3ffffc gap8:21 
test:1ffffe gap8:20 
test:fffff gap8:19 
test:7ffff gap8:18 
test:3ffff gap8:17 
test:1ffff gap8:16 
test:ffff gap8:15 
test:7fff gap8:14 
test:3fff gap8:13 
test:1fff gap8:12 
test:fff gap8:11 
test:7ff gap8:10 
test:3ff gap8:9 
test:1ff gap8:8 
test:ff gap8:7 
test:7f gap8:6 
test:3f gap8:5 
test:1f gap8:4 
test:f gap8:3 
test:7 gap8:2 
test:3 gap8:1 
test:1 gap8:0 
test:0 gap8:0 

  让我们再看一下在RISC-V平台下的打印:

test:7ffff80 gap8:26 
test:3ffffc0 gap8:25 
test:1ffffe0 gap8:24 
test:fffff0 gap8:23 
test:7ffff8 gap8:22 
test:3ffffc gap8:21 
test:1ffffe gap8:20 
test:fffff gap8:19 
test:7ffff gap8:18 
test:3ffff gap8:17 
test:1ffff gap8:16 
test:ffff gap8:15 
test:7fff gap8:14 
test:3fff gap8:13 
test:1fff gap8:12 
test:fff gap8:11 
test:7ff gap8:10 
test:3ff gap8:9 
test:1ff gap8:8 
test:ff gap8:7 
test:7f gap8:6 
test:3f gap8:5 
test:1f gap8:4 
test:f gap8:3 
test:7 gap8:2 
test:3 gap8:1 
test:1 gap8:0 
test:0 gap8:32

  这里就有一个差别,就是当x等于0的时候,__builtin_clz((x)的值在risc-v中居然是-1,这个坑真是的不小啊。算是学习了。

posted on 2018-11-09 19:32  虚生  阅读(3024)  评论(0编辑  收藏  举报