我现在正在运行以下汇编代码来获取cos
函数值:
float cosx( float radians ) {
float result;
__asm__ __volatile__ ( "fld %1;"
"fcos;"
"fstp %0;"
: "=m" (result)
: "m" (radians)
) ;
return result ;
}
但是,上面的函数可以接受float参数,得到的函数值也是float类型。因此,我正在编写一个可以接受双参数的类似函数,并且返回的函数值也应该达到双精度:
double cosx( double radians ) {
double result;
__asm__ __volatile__ ( "fld %1;"
"fcos;"
"fstp %0;"
: "=m" (result)
: "m" (radians)
) ;
return result ;
}
然而,第二个功能已被证明不能正常工作。因此,我想知道我应该怎么做才能使第二个功能正常工作。谢谢!我正在使用 gcc 来编译上述代码。
编辑: 在这里,我将说明为什么第二个功能不能正常工作:
int main() {
float theta ;
printf( "Enter theta : " ) ;
scanf( "%f", &theta ) ;
printf( "cosx(%f) = %lf\n", theta, cosx(theta) ); // line 5
printf( "cosx(%f) = %lf\n", theta, cosx(double(theta)) ); // line 6
return 0 ;
}
As we can see line5 and line6 invoke the first and the second function respectively. We expect they can have the same value. However, the output of the demonstration program is as follows:
Enter theta in : 0.5236 // pi/6
cos(0.5236) = 0.8660;
cos(0.5236) = 0.0000;