Go:位域和位打包

发布于 2024-11-04 01:54:03 字数 953 浏览 3 评论 0原文

C 语言的位域提供了一种在结构中定义任意宽度字段的相当方便的方法(先不用担心可移植性问题。)例如,这是一个带有几个字段和一个“标志”的简单结构:

#pragma pack(push,1)
struct my_chunk{

    unsigned short fieldA: 16;
    unsigned short fieldB: 15;
    unsigned short fieldC:  1;
};
#pragma pop()

添加 #pragma语句将此结构打包为 32 位字(例如,确保 my_chunk 指针的指针操作对齐,同时节省空间)。

访问每个字段在语法上非常好:

struct my_chunk aChunk;
aChunk.fieldA = 3;
aChunk.fieldB = 2;
aChunk.fieldC = 1;

在没有语言帮助的情况下执行此操作的替代方法相当丑陋,并且几乎转变成汇编程序。例如,一种解决方案是为您想要访问的每个字段提供位移宏:

#define FIELD_A  0xFF00
#define FIELD_B  0x00FE
#define FIELD_C  0x0001

#define get_field(p, f) ((*p)&f)
#define set_field(p, f, v) (*p) = (v<<f) + (*p)&(~f)

...
set_field(&my_chunk, FIELD_A, 12345);

..或大致类似的东西(为了更正式,请查看 this)

所以问题是,如果我想在 go 中“执行”位字段,那么这样做的最佳实践是什么?

The C language's bitfields provide a fairly convenient method of defining arbitrary-width fields within a structure (nevermind the problems with portability for a minute.) For example, here's a simple structure with a couple fields and a 'flag':

#pragma pack(push,1)
struct my_chunk{

    unsigned short fieldA: 16;
    unsigned short fieldB: 15;
    unsigned short fieldC:  1;
};
#pragma pop()

Adding the #pragma statements packs this structure into a 32-bit word (ensuring that pointer manipulations of my_chunk pointers are aligned, for example, along with space savings).

Accessing each field is syntactically very nice:

struct my_chunk aChunk;
aChunk.fieldA = 3;
aChunk.fieldB = 2;
aChunk.fieldC = 1;

The alternate way of doing this without the language's help is rather ugly and pretty much devolves into assembler. e.g. one solution is to have bitshift macros for each field you want to access:

#define FIELD_A  0xFF00
#define FIELD_B  0x00FE
#define FIELD_C  0x0001

#define get_field(p, f) ((*p)&f)
#define set_field(p, f, v) (*p) = (v<<f) + (*p)&(~f)

...
set_field(&my_chunk, FIELD_A, 12345);

.. or something roughly like that (for more formality, take a look at this)

So the question is, if I want to "do" bitfields in go, what is the best practice for doing so?

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

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

发布评论

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

评论(3

っ〆星空下的拥抱 2024-11-11 01:54:03

“Go 目前没有针对结构体位字段的计划。”

您可以编写一个 Go 包来执行此操作;不需要汇编程序。

"There are no current plans for struct bitfields in Go."

You could write a Go package to do this; no assembler is required.

暮倦 2024-11-11 01:54:03

如果目标只是拥有一个非常小的结构,您可能会这样做:

package main

import "fmt"

type my_chunk uint32

func (c my_chunk) A() uint16 {
  return uint16((c & 0xffff0000) >> 16) 
}

func (c *my_chunk) SetA(a uint16) {
  v := uint32(*c)
  *c = my_chunk((v & 0xffff) | (uint32(a) << 16))
}

func main() {
  x := my_chunk(123)
  x.SetA(12)
  fmt.Println(x.A())
}

在当前的 6g/8g 中,您正在查看一个带有 ~6 个 getter 指令的函数调用,随着时间的推移,这样的调用可能会是内联。

If the goal is just to have a very small struct, you'd probably just do:

package main

import "fmt"

type my_chunk uint32

func (c my_chunk) A() uint16 {
  return uint16((c & 0xffff0000) >> 16) 
}

func (c *my_chunk) SetA(a uint16) {
  v := uint32(*c)
  *c = my_chunk((v & 0xffff) | (uint32(a) << 16))
}

func main() {
  x := my_chunk(123)
  x.SetA(12)
  fmt.Println(x.A())
}

With the current 6g/8g, you're looking at a function call with ~6 instructions for the getter, and with time such calls will probably be inlined.

亚希 2024-11-11 01:54:03

我创建了一个 Go 工具,可以生成模拟位字段的代码。基本上,您使用指示每个字段的位宽度的结构标签来注释 Go 结构。

然后,该工具使用它作为输入来生成带有 getter 和 setter 的 Go 类型,以便轻松操作这些位。

github.com/arl/bitfield

I've created a Go tool that generates code to emulate bit fields. Basically you annotate a Go struct with struct tags indicating the width in bits of each field.

The tool then uses that as input to generate a Go type with getters and setters allowing to easily manipulate the bits.

github.com/arl/bitfield

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