当将x设置为零(x = 0)时,我的csapp书指出了两种方法。
第一:
xorq %rcx, %rcx
Run Code Online (Sandbox Code Playgroud)
第二:
movq $0, %rcx
Run Code Online (Sandbox Code Playgroud)
它还表明第一个仅占用3个字节,而第二个仅占用7个字节。
两种方式如何运作?为什么第一个字节比第二个字节少?
给定constexpr size N,有什么方法可以在编译时将其uintN_t映射到相应的类型,而无需编写自己的详尽映射,例如:
template <size_t N>
struct size_to_type {};
template<>
struct size_to_type<8> { using type = uint8_t; };
template<>
struct size_to_type<16> { using type = uint16_t; };
// etc
Run Code Online (Sandbox Code Playgroud)
当对应的类型不存在时,我会报错。
我可以编写一个概念来识别任何标准std::chrono持续时间类型,而不必只明确列出它们,例如:
template <typename T>
concept IsStdDuration =
std::same_as<T, std::chrono::nanoseconds> || std::same_as<T, std::chrono::microseconds> || ...
Run Code Online (Sandbox Code Playgroud)
是的,我知道我可以使用“任何这些类型”辅助概念来减少样板文件,但我想知道是否可以避免首先明确地列出它们。
给定任意类型的随机数引擎 e,E我如何uint64_t在整个范围内生成随机数?
像这样的东西,其中needs_random()函数需要随机数uint64_t,并且blah()函数以现代<random>风格编写,可以接受任何类型的随机引擎E。
void needs_random(uint64_t r);
template <typename E>
void blah(E& engine) {
// ...
needs_random(???);
}
Run Code Online (Sandbox Code Playgroud)
具体来说,我正在尝试从任意引擎播种xorshift随机数生成器e,并且xorshift生成器需要两个uint64_t值作为种子。
c++ ×3
assembly ×1
c++-chrono ×1
c++-concepts ×1
c++14 ×1
c++17 ×1
c++20 ×1
code-size ×1
machine-code ×1
random ×1
uint8t ×1
x86-64 ×1