如果给定角度,以下代码用于计算sin
和cos
值。为了尽可能快地计算这些函数,采用了汇编代码实现。
#include <stdio.h>
float sinx( float degree ) {
float result, two_right_angles = 180.0f ;
__asm__ __volatile__ ( "fld %1;"
"fld %2;"
"fldpi;"
"fmul;"
"fdiv;"
"fsin;"
"fstp %0;"
: "=g" (result)
: "g"(two_right_angles), "g" (degree)
) ;
return result ;
}
float cosx( float degree ) {
float result, two_right_angles = 180.0f, radians ;
__asm__ __volatile__ ( "fld %1;"
"fld %2;"
"fldpi;"
"fmul;"
"fdiv;"
"fstp %0;"
: "=g" (radians)
: "g"(two_right_angles), "g" (degree)
) ;
__asm__ __volatile__ ( "fld %1;"
"fcos;"
"fstp %0;" : "=g" (result) : "g" (radians)
) ;
return result ;
}
float square_root( float val ) {
float result ;
__asm__ __volatile__ ( "fld %1;"
"fsqrt;"
"fstp %0;"
: "=g" (result)
: "g" (val)
) ;
return result ;
}
int main() {
float theta ;
printf( "Enter theta in degrees : " ) ;
scanf( "%f", &theta ) ;
printf( "sinx(%f) = %f\n", theta, sinx( theta ) );
printf( "cosx(%f) = %f\n", theta, cosx( theta ) );
printf( "square_root(%f) = %f\n", theta, square_root( theta ) ) ;
return 0 ;
}
上面的代码来自这里,我正在尝试用 gcc 编译上面的代码:
g++ -Wall -fexceptions -g -c /filename.cpp
但是,它失败了,并给出了以下错误消息:
Error: operand type mismatch for `fstp'|
Error: operand type mismatch for `fstp'|
我想知道为什么编译会失败以及如何成功编译它们。谢谢!