结构位域最大大小(C99、C++)

发布于 2024-08-29 05:07:47 字数 315 浏览 2 评论 0原文

位结构字段的最大位宽度是多少?

struct i { long long i:127;}

我可以在结构体内部定义一个位字段,位字段的大小可达 128 位、256 位或更大吗?有一些超宽向量类型,例如 sse2(128 位)、avx1/avx2(256 位)、avx-512(下一个 Xeon Phis 为 512 位)寄存器;还有 gcc 中的 __int128 等扩展。

What is maximal bit width for bit struct field?

struct i { long long i:127;}

Can I define a bit field inside struct, with size of bitfield up to 128 bit, or 256 bit, or larger? There are some extra-wide vector types, like sse2 (128-bit), avx1/avx2 (256-bit), avx-512 (512-bit for next Xeon Phis) registers; and also extensions like __int128 in gcc.

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

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

发布评论

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

评论(4

别在捏我脸啦 2024-09-05 05:07:47

C99 §6.7.2.1,第 3 段:

指定的表达式
位域的宽度应为
整数常量表达式具有
不应出现的非负值
超过对象中的位数
指定的类型,如果
省略冒号和表达式
。如果
值为零,声明
不得有声明符。

C++0xa §9.6,第 1 段:

...常量表达式应为
积分常量表达式
值大于或等于零。
积分常数的值
表达式可能大于
对象中的位数
的表示(3.9)
位域的类型; 在这种情况下
额外的位用作填充位
并且不参与价值
位域的表示(3.9)

所以在 C 中你根本不能这样做,在 C++ 中它也不会做你想做的事。

C99 §6.7.2.1, paragraph 3:

The expression that specifies the
width of a bit-field shall be an
integer constant expression that has
nonnegative value that shall not
exceed the number of bits in an object
of the type that is specified if the
colon and expression are omitted
. If
the value is zero, the declaration
shall have no declarator.

C++0xa §9.6, paragraph 1:

... The constant-expression shall be an
integral constant expression with a
value greater than or equal to zero.
The value of the integral constant
expression may be larger than the
number of bits in the object
representation (3.9) of the
bit-field’s type; in such cases the
extra bits are used as padding bits
and do not participate in the value
representation (3.9) of the bit-field
.

So in C you can't do that at all, and in C++ it won't do what you want it to.

梦回梦里 2024-09-05 05:07:47

C++ 标准对位字段的大小没有设置任何限制,除了它必须大于或等于 0 - 第 9.6/1 节。它还说:

位域被打包成一些
可寻址分配单元。 [笔记:
位域跨分配单元
在某些机器上而不是在其他机器上。
位域从右到左分配
在某些机器上,从左到右
其他的。 ]

我想这可以用来表示某种最大尺寸。

当然,这并不意味着您的特定编译器实现支持任意大小的位字段。

The C++ Standard sets no limits on the size of a bit-field, other than that it must be greater or equal to zero - section 9.6/1. It also says:

Bit-fields are packed into some
addressable allocation unit. [Note:
bit-fields straddle allocation units
on some machines and not on others.
Bit-fields are assigned right-to-left
on some machines, left-to-right on
others. ]

Which I suppose could be taken to indicate some sort of maximum size.

This does not mean that your specific compiler implementation supports arbitrarily sized bit-fields, of course.

深海蓝天 2024-09-05 05:07:47

通常,您分配的位不能多于基础类型的位。如果 long long 是 64 位,那么您的位字段可能仅限于 :64。

Typically, you cannot allocate more bits than the underlying type has. If long long is 64 bits, then your bitfield is probably limited to :64.

残花月 2024-09-05 05:07:47

由于位域的值被分配给整数,我假设您可以使用的最大位域值是 intmax_t 的大小。

编辑:

来自 C99 规范:

6.7.2.1 项目符号 9:

位域被解释为有符号的
或无符号整数类型组成
指定的位数。如果
值 0 或 1 被存储到
类型的非零宽度位域
_Bool,位域的值应等于该值
已存储。

6.7.2.1 第 10 条:

实现可以分配任何
足够大的可寻址存储单元
保存一个位字段。如果空间足够
仍然是一个位字段,立即
跟随另一个位字段
结构应打包成
同一单元的相邻位。如果
剩余空间不足,是否
不适合的位域被放入
下一个单元或与相邻单元重叠
单位是实现定义的。这
位域的分配顺序
一个单元内(从高阶到低阶
或从低阶到高阶)是
实现定义的。对齐方式
可寻址存储单元的数量为
未指定。

Since the values of bit-fields are assigned to integers, I'd assume that the largest bit-field value you can use is that of the size of intmax_t.

Edit:

From the C99 Spec:

6.7.2.1 Bullet 9:

A bit-field is interpreted as a signed
or unsigned integer type consisting of
the specified number of bits. If
the value 0 or 1 is stored into a
nonzero-width bit-field of type
_Bool, the value of the bit-field shall compare equal to the value
stored.

6.7.2.1 Bullet 10:

An implementation may allocate any
addressable storage unit large enough
to hold a bit- field. If enough space
remains, a bit-field that immediately
follows another bit-field in a
structure shall be packed into
adjacent bits of the same unit. If
insufficient space remains, whether a
bit-field that does not fit is put into
the next unit or overlaps adjacent
units is implementation-defined. The
order of allocation of bit-fields
within a unit (high-order to low-order
or low-order to high-order) is
implementation-defined. The alignment
of the addressable storage unit is
unspecified.

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