gpt4 book ai didi

c++ - 对同一数据有 8 位和 16 位 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:23 25 4
gpt4 key购买 nike

问题

我正在使用 C++ 开发视频游戏模拟器。

内存由 uint8_t 值数组表示。通常,我还需要以 16 位值(两个连续的 8 位值)的形式访问此内存的内容。

我有一些看起来像这样的东西:

struct Memory {

uint8_t map[0xFFFF];

// Let's say that [] is for accessing 8-bit values
uint8_t& operator[](uint16_t address) {
return map[address];
}

// () is for 16-bit values
uint16_t& operator()(uint16_t address) {
return ???; // Looking for a good implementation here
}
};

例如,如果内存包含 [0xC0, 0xFF, 0x66, 0xAA...] 那么 [] 将返回:

内存[0] -> 0xC0内存[1] -> 0xFF内存[2] -> 0x66

() 将返回(取决于系统字节顺序):

mem(0) -> 0xFFC0内存(1) -> 0x66FF内存(2) -> 0xAA66

这些访问方法将会被很多次调用。我想利用 () 中的指针进行快速访问。我不想通过移动和 |'ing 8 位对来计算 16 位值(我不能,因为该方法必须返回一个引用)。

我的问题:在 C++ 中有没有办法在同一个缓冲区上有不同的 View ?指向相同数据的 8 位 View 和 16 位 View 将是理想的。

(例如,JS ArrayBuffer API 提供了 DataView,它可以做到这一点。也许有一个聪明的技巧可以用 C++ 中的指针干净地实现它?)

尝试 #1

我的第一个猜测是使用某种 union :

union mem_t {
uint8_t bytes[0xFFFF];
uint16_t words[0x8000]
}

但是只能访问偶数字节的 16 位值:

mem.words[0] -> 0xFFC0

mem.words[1] -> 0xAA66//应该是 0x66FF :(

尝试 #2

一个可行的解决方案是为偶地址和奇地址添加两个额外的 16 位指针,以处理重叠:

uint16_t* even16 = (uint16_t*) map;
uint16_t* odd16 = (uint16_t*) (map + 1);

uint16_t& Memory::operator()(uint16_t address) {
return address % 2 ? odd16[address / 2] : even16[address / 2];
}

它工作正常,但看起来很复杂,我相信还有更优雅的解决方案。

谢谢。

最佳答案

类似这样,但您可能会违反主机系统上的内存对齐规则:

struct Memory {

uint8_t map[0xFFFF];

// Let's say that [] is for accessing 8-bit values
uint8_t& operator[](uint16_t address) {
return map[address];
}

// () is for 16-bit values
uint16_t& operator()(uint16_t address) {
return *reinterpret_cast<uint16_t*>(&map[address]);
}
};

关于c++ - 对同一数据有 8 位和 16 位 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com