小编Bee*_*ope的帖子

为什么使用xor而不是mov需要更少的字节?

当将x设置为零(x = 0)时,我的csapp书指出了两种方法。

第一:

xorq %rcx, %rcx
Run Code Online (Sandbox Code Playgroud)

第二:

movq $0, %rcx
Run Code Online (Sandbox Code Playgroud)

它还表明第一个仅占用3个字节,而第二个仅占用7个字节。

两种方式如何运作?为什么第一个字节比第二个字节少?

assembly x86-64 machine-code code-size

1
推荐指数
1
解决办法
96
查看次数

从大小到对应的uintN_t类型的映射

给定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)

当对应的类型不存在时,我会报错。

c++ template-meta-programming uint8t c++17

1
推荐指数
1
解决办法
79
查看次数

仅对 std::chrono 持续时间类型进行建模的概念

我可以编写一个概念来识别任何标准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)

是的,我知道我可以使用“任何这些类型”辅助概念来减少样板文件,但我想知道是否可以避免首先明确地列出它们。

c++ c++-chrono c++-concepts c++20

1
推荐指数
1
解决办法
412
查看次数

生成随机uint64_t

给定任意类型的随机数引擎 eE我如何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++ random c++14

0
推荐指数
1
解决办法
195
查看次数