日期:2014-05-16  浏览次数:20791 次

linux驱动编程里,整形和浮点数相乘,报错?
int temp1,temp2;
int result;
.....
.....
result=(temp2<<8 | temp1)* 0.0625; //问题语句
==============================
整形和浮点相称,最后还是转换成整形,我在VC6.0里证实了我的想法,可是在这里为什么这句话会报错?去掉浮点数,就可以通过编译

执行make 编译,报如下错误
  ....省略部分
ERROR: "__aeabi_d2iz" [drivers/mytestdriver/upstar2440_ds18b20.ko] undefined!
ERROR: "__aeabi_dmul" [drivers/mytestdriver/upstar2440_ds18b20.ko] undefined!
ERROR: "__aeabi_i2d" [drivers/mytestdriver/upstar2440_ds18b20.ko] undefined!
make[1]: *** [__modpost] Error 1
make: *** [modules] Error 2

=====================
如何解决?


------解决方案--------------------
ernel code can use floating point if this use is surrounded by kernel_fpu_begin()/kernel_fpu_end(). These function handle saving and restoring the fpu context. Also, they call preempt_disable()/preempt_enable(), which means no sleeping, page faults etc. in the code between those functions. Google the function names for more information.

内核没有提供浮点运算支持。需要额外加一些code,如下:
C/C++ code

kernel_fpu_begin();
//浮点运算
kernel_fpu_end();

------解决方案--------------------

补充一下,linux内核不会链接libc,所以有很多用户态的写法都不适用于内核。
如同linux创始人说的: 不应该在内核里使用浮点数……
http://lkml.indiana.edu/hypermail/linux/kernel/0405.3/1620.html
------解决方案--------------------
补充一下,linux内核不会链接libc,所以有很多用户态的写法都不适用于内核。

+1
------解决方案--------------------
result=(temp2<<8 | temp1)* 0.0625
--》
result=(temp2<<8 | temp1) * 5 / 80;