在扩展汇编器中划分浮点数

发布于 2025-01-09 07:43:10 字数 790 浏览 0 评论 0原文

在我的 C 程序中,我想在扩展汇编程序中执行以下操作: 将两个浮点数(变量)“分子”除以“分母”,并将结果放入另一个变量“结果”中。这是我对它的外观的猜测,但它似乎无法编译。

float result, numerator, denominator;

asm volatile(" fdiv %2, %1 "
           : "=r" (result)
           : "1"  (numerator), "2"  (denominator));

编译错误:

example_program.c:66:16: error: matching constraint references invalid operand number
                : "1"  (numerator), "2"  (denominator));

example_program:16: error: matching constraint references invalid operand number

example_program.c:64:3: error: matching constraint references invalid operand number
   asm volatile(" fdiv %2, %1 "
   ^
example_program.c:64:3: error: matching constraint references invalid operand number
make: *** [<builtin>: example_program] Error 1

In my C program, I would like to do the following in extended assembler:
Divide two floating point numbers (variables), 'numerator' by 'denominator', and put the result in another variable, 'result'. This is my guess of how it would look like, but it does not seem to compile.

float result, numerator, denominator;

asm volatile(" fdiv %2, %1 "
           : "=r" (result)
           : "1"  (numerator), "2"  (denominator));

Compiling errors:

example_program.c:66:16: error: matching constraint references invalid operand number
                : "1"  (numerator), "2"  (denominator));

example_program:16: error: matching constraint references invalid operand number

example_program.c:64:3: error: matching constraint references invalid operand number
   asm volatile(" fdiv %2, %1 "
   ^
example_program.c:64:3: error: matching constraint references invalid operand number
make: *** [<builtin>: example_program] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2025-01-16 07:43:10

在进行任何内联汇编编程之前,请仔细阅读文档。 Gcc风格的内联汇编比较复杂并且容易出错。如果你弄错了,通常不会有错误。相反,错误的代码会默默地产生。因此,请确保您准确理解自己在做什么。就您而言,您应该特别关注 §6.47.2.9

阅读本文档,以下代码似乎是正确的:

float test(float numerator, float denominator)
{
    float result;

    asm ("fdiv%Z2 %2" : "=t"(result) : "0"(numerator), "fm"(denominator));

    return result;
}

这允许源操作数是内存操作数。如果不需要,则语句可以更简单:

asm ("fdiv %2" : "=t"(result) : "0"(numerator), "f"(denominator));

如果不将结果写入不同的变量,而是覆盖分子,则代码可以更简单。

asm ("fdiv %1" : "+t"(result) : "f"(denominator));

Before doing any inline assembly programming, read the documentation carefully. Gcc-style inline assembly is complicated and easy to get wrong. If you get it wrong, there is often no error. Instead, wrong code will silently be produced. So make sure you understand exactly what you are doing. In your case, you should especially focus on §6.47.2.9.

Reading this documentation, the following code seems correct:

float test(float numerator, float denominator)
{
    float result;

    asm ("fdiv%Z2 %2" : "=t"(result) : "0"(numerator), "fm"(denominator));

    return result;
}

This permits the source operand to be a memory operand. If that is not required, the statement can be simpler:

asm ("fdiv %2" : "=t"(result) : "0"(numerator), "f"(denominator));

If instead of writing the result into a different variable, the numerator is to be overwritten, the code can be even simpler.

asm ("fdiv %1" : "+t"(result) : "f"(denominator));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文