GCC __attribute__((mode(XX)) 实际上做了什么?

发布于 2024-10-09 08:37:16 字数 489 浏览 5 评论 0原文

这是由今天早些时候关于 bignum 库和 gcc 对 C 语言的特定 hack 主题的问题引起的。具体来说,使用了这两个声明:

typedef unsigned int dword_t __attribute__((mode(DI)));

在 32 位系统上和

typedef unsigned int dword_t __attribute__((mode(TI)));

在 64 位系统上。

我认为鉴于这是对 C 语言的扩展,因此没有办法实现它在当前(C99)标准中实现的任何功能。

所以我的问题很简单:这个假设正确吗?这些语句对底层内存做了什么?我认为结果是我在 32 位系统中为 dword 提供了 2*sizeof(uint32_t) ,为 2*sizeof(uint64_t) 为64位系统,我说得对吗?

This arose from a question earlier today on the subject of bignum libraries and gcc specific hacks to the C language. Specifically, these two declarations were used:

typedef unsigned int dword_t __attribute__((mode(DI)));

On 32 bit systems and

typedef unsigned int dword_t __attribute__((mode(TI)));

On 64-bit systems.

I assume given this is an extension to the C language that there exists no way to achieve whatever it achieves in current (C99) standards.

So my questions are simple: is that assumption correct? And what do these statements do to the underlying memory? I think the result is I have 2*sizeof(uint32_t) for a dword in 32-bit systems and 2*sizeof(uint64_t) for 64-bit systems, am I correct?

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

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

发布评论

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

评论(2

情绪少女 2024-10-16 08:37:16

这些允许您显式指定类型的大小,而不依赖于编译器或机器语义,例如“long”或“int”的大小。

此页面对它们进行了相当详细的描述。

我引用该页面的内容:

QI:一个整数,其宽度与
最小可寻址单元,通常为 8
位。

HI:一个整数,宽度是
QI 模式整数,通常为 16 位。

SI:整数,宽度是 QI 的四倍
模式整数,通常为32位。

DI:一个
整数,宽度是 QI 的八倍
模式整数,通常为 64 位。

SF:A
浮点值,与 SI 一样宽
模式整数,通常为32位。

DF:A
浮点值,与 DI 一样宽
模式整数,通常为 64 位。

所以DI本质上是sizeof(char) * 8

可以在此处找到更多说明,包括 TI 模式 (可能比第一个链接更好,但两者都仅供参考)。

因此,TI 本质上是 sizeof(char) * 16(128 位)。


旧链接现已失效,因此这里是官方 GCC 文档< /a>.

These allow you to explicitly specify a size for a type without depending on compiler or machine semantics, such as the size of 'long' or 'int'.

They are described fairly well on this page.

I quote from that page:

QI: An integer that is as wide as the
smallest addressable unit, usually 8
bits.

HI: An integer, twice as wide as
a QI mode integer, usually 16 bits.

SI: An integer, four times as wide as a QI
mode integer, usually 32 bits.

DI: An
integer, eight times as wide as a QI
mode integer, usually 64 bits.

SF: A
floating point value, as wide as a SI
mode integer, usually 32 bits.

DF: A
floating point value, as wide as a DI
mode integer, usually 64 bits.

So DI is essentially sizeof(char) * 8.

Further explanation, including TI mode, can be found here (possibly better than the first link, but both provided for reference).

So TI is essentially sizeof(char) * 16 (128 bits).


The old links are now dead, so here's the official GCC documentation.

挽袖吟 2024-10-16 08:37:16

@haelix刚刚读了这个问题,我也试图理解这个事情。根据我的阅读:你可以在GCC源代码树的[gcc/gcc/machmode.def]中找到定义。对于“SD”,它应该是:

    /* Decimal floating point modes.  */ 
DECIMAL_FLOAT_MODE (SD, 4, decimal_single_format);

“DECIMAL_FLOAT_MODE”说:

     DECIMAL_FLOAT_MODE (MODE, BYTESIZE, FORMAT);
declares MODE to be of class DECIMAL_FLOAT and BYTESIZE bytes
wide.  All of the bits of its representation are significant.

@haelix Just read this question and I also tried to understand this thing. By my reading: you can find the definitions in the [gcc/gcc/machmode.def] in GCC source tree. For 'SD' it should be:

    /* Decimal floating point modes.  */ 
DECIMAL_FLOAT_MODE (SD, 4, decimal_single_format);

and 'DECIMAL_FLOAT_MODE' says:

     DECIMAL_FLOAT_MODE (MODE, BYTESIZE, FORMAT);
declares MODE to be of class DECIMAL_FLOAT and BYTESIZE bytes
wide.  All of the bits of its representation are significant.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文