小编pla*_*cel的帖子

可靠的浮点/整数类型转换溢出检测

有没有一种安全的方法来可靠地确定整数类型是否T可以存储浮点整数值f( so f == floor(f))而不会发生任何溢出?

请记住,不能保证浮点类型F与 IEC 559 (IEEE 754) 兼容,并且有符号整数溢出是C++ 中的未定义行为。我对根据当前 C++(编写时为 C++17)标准正确并避免未定义行为的解决方案感兴趣。

以下幼稚的方法不可靠,因为由于浮点舍入,不能保证类型F可以表示std::numeric_limits<I>::max()

#include <cmath>
#include <limits>
#include <type_traits>

template <typename I, typename F>
bool is_safe_conversion(F x)
{
    static_assert(std::is_floating_point_v<F>);
    static_assert(std::is_integral_v<I>);

    // 'fmax' may have a different value than expected
    static constexpr F fmax = static_cast<F>(std::numeric_limits<I>::max());

    return std::abs(x) <= fmax; // this test may gives incorrect results
}
Run Code Online (Sandbox Code Playgroud)

任何的想法?

c++ floating-point integer integer-overflow type-conversion

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

在C ++中用std :: allocator :: allocate(0)分配零个对象

new int[0]在C ++中是允许的,但是std::allocator<int>().allocate(0)定义明确?更笼统地说,所有分配器都必须接受0作为分配参数吗?

编辑:阅读我测试了Visual Studio的答案后std::allocatorallocate(0)nullptr

deallocate(nullptr, anything) 是点头。

因此,使用nullptr是一个很好的建议,但是标准不要求deallocate(nullptr, 0)是nop,请参见是否允许使用C ++ allocator :: deallocate(NULL,1)?

c++ memory-management allocator dynamic-memory-allocation language-lawyer

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

C++ 中的欧几里德整数模

在哪里可以找到计算整数欧几里得除法余数的实现或库0 <= r < |n|

c++ math integer division modulo

4
推荐指数
2
解决办法
8395
查看次数

如何使用SSE2添加数组中的所有元素?

假设我有一个非常简单的代码,例如:

double array[SIZE_OF_ARRAY];
double sum = 0.0;

for (int i = 0; i < SIZE_OF_ARRAY; ++i)
{
    sum += array[i];
}
Run Code Online (Sandbox Code Playgroud)

我基本上想要使用SSE2进行相同的操作.我怎样才能做到这一点?

c x86 sse simd sse2

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

比std :: nth_element更快的东西

我正在研究一个kd-tree实现,我现在正在使用std :: nth_element来按照中位数对元素的向量进行分区.但是std :: nth_element占用树构造的90%的时间.有谁能建议更有效的替代方案?

提前致谢

c++ sorting algorithm kdtree c++11

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

编译器宏以检测BMI2指令集

我在网上搜索以找到合适的解决方案,但没有成功。因此,我希望你们中的一些人对此有所了解:是否有任何方法可以检测“ Intel Bit Manipulation Instruction Sets 2 ”(BMI2)的编译时间?我想根据其可用性做出一些有条件的事情。

c++ x86 intel instruction-set bmi

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

转发引用作为仿函数参数的优点

有很多关于新C++转发引用的讨论.然而,有时在特定情况下,我仍然不清楚它们是否提供任何优势.

很明显,按值传递重态函子(就像随机数生成器)并不是一个好主意.所以让我们使用引用.好的,但......

...有没有使用任何好处转发引用

template <class T, class Functor>
T func(T x, Functor &&f)
{
    T y;
    // do some computations involving f(x) and store it in y
    return y;
}
Run Code Online (Sandbox Code Playgroud)

而不是const引用

template <class T, class Functor>
T func(T x, const Functor &f)
{
    T y;
    // do some computations involving f(x) and store it in y
    return y;
}
Run Code Online (Sandbox Code Playgroud)

在没有转发functor对象的函数中?

问题的主要方面是绩效考虑.

c++ c++11

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

表达模板化负数字文字的首选方式

虽然关于模板化数字文字的使用存在一个很好的问题,但它没有提到数字文字是负值时的情况.

应该首选哪种变体?为什么?

一个

template <typename T>
T expr(T x)
{
    constexpr T scale = T(-9.0);
    return x * scale;
}
Run Code Online (Sandbox Code Playgroud)

template <typename T>
T expr(T x)
{
    constexpr T scale = -T(9.0);
    return x * scale;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

_mm256_shuffle_ps如何工作?

我明白了_mm_shuffle_ps工作.例如,在下面.

__m128 r = _mm_shuffle_ps(x,y, _MM_SHUFFLE(2,0,2,0)); 
Run Code Online (Sandbox Code Playgroud)

r将有内容x[0],x[2],y[0],y[2].

但是我看到它_MM_SHUFFLE也需要4个参数_mm256_shuffle_ps,而矢量每个都有8个元素.所以,逻辑上_MM_SHUFFLE应该采取8个参数.有人可以解释这是如何工作的吗?

c x86 simd intrinsics avx

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

为什么 rand() 比 arc4random() 快得多?

我正在编写一个需要快速整数随机数生成的游戏人工智能。该游戏适用于Mac OS,因此有两种选择rand()(纯C)和arc4random()(BSD)。我没有发现这两个函数在速度上有任何比较,所以我写了一个小程序来测试:

long i;

// Record timestamp here.
srand((unsigned int)time(NULL));
for (i = 0; i < 999999999; ++i) {
    rand();
}
// Record timestamp here.
for (i = 0; i < 999999999; ++i) {
    arc4random();
}
// Record timestamp here and print all three.
Run Code Online (Sandbox Code Playgroud)

我测试了好几次。结果相当稳定:srand()999999999次迭代rand()大约需要6秒,而arc4random()需要更长的时间(大约30秒)。

有什么原因arc4random()需要更长的时间吗?或者我的测试有什么缺陷。谢谢你!

c random bsd arc4random

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