【发布时间】:2019-05-07 19:29:51
【问题描述】:
在我解决了this question 的问题后,我继续扩展我的代码的这个版本,将我以前的模板版本中的数据字段的并集与这个版本合并,到目前为止我有这个:
main.cpp
#include <iostream>
#include <type_traits>
#include "Register.h"
int main() {
using namespace vpc;
std::cout << std::boolalpha;
std::cout << "std::bitset<64> is trivially copyable "
<< std::is_trivially_copyable<std::bitset<64>>::value << '\n'
<< "QWord is trivially copyable "
<< std::is_trivially_copyable<QWord>::value << '\n'
<< "DWord is trivially copyable "
<< std::is_trivially_copyable<DWord>::value << '\n'
<< "Word is trivially copyable "
<< std::is_trivially_copyable<Word>::value << '\n'
<< "Byte is trivially copyable "
<< std::is_trivially_copyable<Byte>::value << '\n'
// << "Bits is trivially copyable "
//<< std::is_trivially_copyable<Bits>::value << '\n'
<< "My Register is trivially copyable "
<< std::is_trivially_copyable<Register>::value << "\n\n";
std::cout << "sizeof(std::bitset<Byte>) = " << sizeof(Byte) << " bytes\n";
std::cout << "sizeof(std::bitset<Word>) = " << sizeof(Word) << " bytes\n";
std::cout << "sizeof(std::bitset<DWord>) = " << sizeof(DWord) << " bytes\n";
std::cout << "sizeof(std::bitset<QWord>) = " << sizeof(QWord) << " bytes\n";
std::cout << "sizeof(Register) = " << sizeof(Register) << " bytes\n\n";
Register r;
std::cout << "sizeof(Register::byte) = " << sizeof(r.byte) << " bytes\n";
std::cout << "sizeof(Register::Byte) = " << sizeof(r.byte) / sizeof(r.byte[0]) << " bytes\n";
std::cout << "sizeof(Register::word) = " << sizeof(r.word) << " bytes\n";
std::cout << "sizeof(Register::Word) = " << sizeof(r.word) / sizeof(r.word[0]) << " bytes\n";
std::cout << "sizeof(Register::dword) = " << sizeof(r.dword) << " bytes\n";
std::cout << "sizeof(Register::DWord) = " << sizeof(r.dword) / sizeof(r.dword[0]) << " bytes\n";
std::cout << "sizeof(Register::value) = " << sizeof(r.value) << " bytes\n";
std::cout << "sizeof(Register) = " << sizeof(r) << " bytes\n\n";
r.value = 0xFFFFFFFFFFFFFFFF;
std::cout << "value = " << r.value.to_ullong() << '\n' << r.value << '\n';
for (std::uint16_t i = 0; i < 8; i++) {
std::cout << "byte_" << i << " : " << r.byte[i] << '\n';
}
return EXIT_SUCCESS;
}
Register.h
#pragma once
#include <algorithm>
#include <bitset>
#include <string>
#include <vector> // include for typedefs below.
namespace vpc {
typedef std::int8_t i8;
typedef std::int16_t i16;
typedef std::int32_t i32;
typedef std::int64_t i64;
const std::uint16_t BYTE = 0x08;
const std::uint16_t WORD = 0x10;
const std::uint16_t DWORD = 0x20;
const std::uint16_t QWORD = 0x40;
typedef std::bitset<BYTE> Byte;
typedef std::bitset<WORD> Word;
typedef std::bitset<DWORD> DWord;
typedef std::bitset<QWORD> QWord;
struct Register {
union {
QWord value{ 0 };
union {
DWord dword[2];
struct {
DWord dword0;
DWord dword1;
};
};
union {
Word word[4];
struct {
Word word0;
Word word1;
Word word2;
Word word3;
};
};
union {
Byte byte[8];
struct {
Byte byte0;
Byte byte1;
Byte byte2;
Byte byte3;
Byte byte4;
Byte byte5;
Byte byte6;
Byte byte7;
};
};
};
Register() : value{ 0 } {}
};
Register reverseBitOrder(Register& reg, bool copy = false);
} // namespace vpc
Register.cpp
#include "Register.h"
namespace vpc {
Register reverseBitOrder(Register& reg, bool copy) {
auto str = reg.value.to_string();
std::reverse(str.begin(), str.end());
if (copy) { // return a copy
Register cpy;
cpy.value = QWord(str);
return cpy;
}
else {
reg.value = QWord(str);
return {};
}
}
} // namespace vpc
输出
std::bitset<64> is trivially copyable true
QWord is trivially copyable true
DWord is trivially copyable true
Word is trivially copyable true
Byte is trivially copyable true
My Register is trivially copyable true
sizeof(std::bitset<Byte>) = 4 bytes
sizeof(std::bitset<Word>) = 4 bytes
sizeof(std::bitset<DWord>) = 4 bytes
sizeof(std::bitset<QWord>) = 8 bytes
sizeof(Register) = 32 bytes
sizeof(Register::byte) = 16 bytes
sizeof(Register::Byte) = 4 bytes
sizeof(Register::word) = 16 bytes
sizeof(Register::Word) = 4 bytes
sizeof(Register::dword) = 8 bytes
sizeof(Register::DWord) = 2 bytes
sizeof(Register::value) = 8 bytes
sizeof(Register) = 32 bytes
value = 18446744073709551615
1111111111111111111111111111111111111111111111111111111111111111
byte_0 : 11111111
byte_1 : 11111111
byte_2 : 11001100
byte_3 : 11001100
byte_4 : 11001100
byte_5 : 11001100
byte_6 : 11001100
byte_7 : 11001100
查看打印出的bitset 类型大小的数据后,将它们与它们作为联合中结构成员的实际大小进行比较。我试图弄清楚这里发生了什么。
我不确定我是否正确执行 sizeof 计算是否与 bitset 的内部存储有关基础类型是std::bitset 类型的结构。从标题中您可以看到它们有 4 个变体:bitset<8> = Byte、bitset<16> = Word、bitset<32> = DWord & bitset<64> = QWord
本质上应该有这些的可分映射:
// each [] = 1 byte or 8 bits for simplicity
bitset<64> = [] [] [] [] [] [] [] []
bitset<32> = [] [] [] []
bitset<16> = [] []
bitset<8> = []
所以当我尝试在联合中使用它们时:
union {
QWord q;
union {
DWord d[2];
struct {
DWord d_0;
DWord d_1;
};
};
union {
Word w[4];
struct {
Word w_0;
Word w_1;
Word w_2;
Word w_3;
};
};
union {
Byte b[8];
struct {
Byte b_0;
Byte b_1;
Byte b_2;
Byte b_3;
Byte b_4;
Byte b_5;
Byte b_6;
Byte b_7;
};
};
};
我认为通过使用我在此联合上方显示的模式,我可以将数据打包成字节大小对齐:
// each inner [] = 1 byte or 8 bits
// and each outer [] = index into array
0 1 2 3 4 5 6 7
value = [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0 1
dword[2] = [[] [] [] []], [[] [] [] []]
0 1 2 3
word[4] = [[] []], [[] []], [[] []], [[] []]
0 1 2 3 4 5 6 7
byte[8] = [[ ]] [[ ]] [[ ]] [[ ]] [[ ]] [[ ]] [[ ]] [[ ]]
然而,这似乎并没有像我预期的那样发生。
我的总体目标是模拟上面我表达的模式,以便寄存器的基本大小为 64 位或 8 字节宽,并且通过使用联合我将能够访问子字节、字或双字完整的qword。
您能否详细说明我在这里遗漏了什么?我不确定它是否与std::bitset 在内存中的存储方式有关,是否与结构对齐有关,或者是否与联合本身有关。
【问题讨论】:
-
提醒您,使用联合的类型双关语在 C++ 中是未定义的行为。
Byte byte[4];是一个错字,应该是[8]吗?您正在使用哪些编译器和编译器选项? I can't compile this. -
@KamilCuk 是的,这是一个错字,谢谢您指出!
-
@KamilCuk 我明白,但几乎你在 C 语言中所做的所有关于指针和其他类似操作、类型转换等的操作都可以定义为类型双关语和 UB。
-
@KamilCuck 是否有另一种方式来表示我提出的模型?
-
这整个问题只是...为什么是
sizeof(bitset<8>) != 1?
标签: c++ c++17 unions std-bitset struct-member-alignment