Tag: strict aliasing

GCC别名检查瓦特/限制指针

考虑以下两个片段: #define ALIGN_BYTES 32 #define ASSUME_ALIGNED(x) x = __builtin_assume_aligned(x, ALIGN_BYTES) void fn0(const float *restrict a0, const float *restrict a1, float *restrict b, int n) { ASSUME_ALIGNED(a0); ASSUME_ALIGNED(a1); ASSUME_ALIGNED(b); for (int i = 0; i < n; ++i) b[i] = a0[i] + a1[i]; } void fn1(const float *restrict *restrict a, float *restrict b, int n) { […]

使用这个指针会导致热循环中出现奇怪的去最佳化

我最近遇到了一个奇怪的去优化(或者说错过了优化的机会)。 考虑将3位整数数组有效解包为8位整数的函数。 它在每个循环迭代中解包16个整数: void unpack3bit(uint8_t* target, char* source, int size) { while(size > 0){ uint64_t t = *reinterpret_cast<uint64_t*>(source); target[0] = t & 0x7; target[1] = (t >> 3) & 0x7; target[2] = (t >> 6) & 0x7; target[3] = (t >> 9) & 0x7; target[4] = (t >> 12) & 0x7; target[5] = (t >> […]

在C ++中,我应该费心去cachingvariables,还是让编译器进行优化? (混叠)

考虑下面的代码( p是unsigned char*的types,而bitmap->width是一些整数types,它是未知的,取决于我们使用的外部库的版本): for (unsigned x = 0; x < static_cast<unsigned>(bitmap->width); ++x) { *p++ = 0xAA; *p++ = 0xBB; *p++ = 0xCC; } 它是值得优化它[..] 有没有这样的情况下,可以写出更有效的结果: unsigned width(static_cast<unsigned>(bitmap->width)); for (unsigned x = 0; x < width; ++x) { *p++ = 0xAA; *p++ = 0xBB; *p++ = 0xCC; } …或者这是编译器优化这个微不足道的? 你认为什么是“更好”的代码? 编者注(艾克):对于那些想知道三文治文本的人来说,原来的这个问题,如同所说的那样,离危险的地方很近,尽pipe得到了正面的反馈,但却非常接近封闭。 这些已经被打破了。 但是,请不要惩罚处理这些问题的答复者。