3,403 questions
Advice
2
votes
4
replies
69
views
determine cpu after c++ compilation with gcc?
Does anyone know if there is, in c++, any way to determine at runtime the cpu characteristics of the machine that compiled the code? For example, in gcc (which I'm using) the preprocessor variable ...
3
votes
2
answers
186
views
Why do GCC and Clang fail to auto-vectorize simple loop?
I have two functions counting the occurrences of a target char in the given input buffer. The functions vary only in how they communicate the result back to the caller; one returns the result and the ...
Best practices
2
votes
10
replies
274
views
How to tell the C compiler that data pointed to by a pointer won't be constantly modified by another thread after being passed to a function?
In C, when I pass a pointer to a function, the compiler always seems to assume that the data pointed to by that pointer might be continuously modified in another thread, even though in actual API ...
3
votes
5
answers
627
views
What is special about a ternary statement instead of a if-else statement in terms of optimization?
I'm talking from a language point of view in C or C++, where the compiler sees:
return condition ? a : b;
vs:
if (condition)
return a;
else
return b;
I've tried in my code, and both of them ...
25
votes
2
answers
4k
views
Does excessive use of [[likely]] and [[unlikely]] really degrade program performance in C++?
The C++ standard [dcl.attr.likelihood] says:
[Note 2: Excessive usage of either of these attributes is liable to result in performance degradation.
— end note]
I’m trying to understand what “...
0
votes
0
answers
43
views
How to build a gcc_tree_node from custom language Nodes
Nodes:
building a gcc_tree_node for a custom prograimming language
compile and base on C++26
the modules are avilable
the language using tab-block system
every keyword start with '/'
I want to ...
5
votes
3
answers
290
views
How to make the optimiser treat a local function as a black box and not optimise based on its implementation?
I thought that the noinline function attribute would force the compiler to treat a local function as a black box:
__attribute__((noinline)) void touch_noinline(int&) {}
void touch_external(int&...
2
votes
0
answers
61
views
Why is sequential indexing with fixed length stride slower in Estrin's method?
Preparing to make Estrin's method vectorisable I changed from normal linear indexing of the coefficients to bitreversed and restricted it to strictly powers of 2. Neither MSVC nor ICX can see how to ...
5
votes
1
answer
186
views
Why are [[no_unique_address]] members not transparently replaceable?
In the classic talk An (In-)Complete Guide to C++ Object Lifetimes by Jonathan Müller, there is a useful guideline as follows:
Q: When do I need to use std::launder?
A: When you want to re-use the ...
1
vote
2
answers
190
views
GCC switch statements do not simplify on identical handling
The switch statements in the following two functions
int foo(int value) {
switch (value) {
case 0:
return 0;
case 1:
return 0;
case 2:
return 1;
}
}
int ...
4
votes
1
answer
151
views
optimize computation of real part of complex product
I need (only) the real part of the product of two complex numbers. Naturally, I can code this as
real(x)*real(y) - imag(x)*imag(y);
or
real(x*y);
The latter, however, formally first computes the ...
5
votes
1
answer
267
views
Why does the C compiler save registers in a noreturn function?
I mainly use clang, but I have also explored other compilers during my experiments, such as MinGW GCC and MSVC, but they all have this problem.
E:\code\test>clang -v
clang version 20.1.7
Target: ...
1
vote
2
answers
161
views
Can I tell my compiler that a floating-point value is not NaN nor +/-infinity?
I'm writing a C function float foo(float x) which manipulates a floating-point value. It so happens, that I can guarantee the function will only ever be called with finite values - neither NaN's, nor +...
4
votes
0
answers
120
views
MSVC fixed short length polynomial evaluation curiosity - why does it not keep my coefficients array contiguous, and why subtract the absolute value?
MSVC seems to be taking the values from my array of coefficients and scattering them around in its .rdata section, not keeping them contiguous even though they're all used together. And it takes the ...
13
votes
1
answer
1k
views
Does C++23 guarantee std::launder can be omitted in placement new scenarios?
#include <iostream>
#include <new>
struct A {
int const n;
void f() {
new (this) A{2};
}
void g() {
std::cout << this->n;
}
void h() {
...
31
votes
2
answers
5k
views
Why do modern compilers assume malloc never fails?
In case of failure, malloc returns a null pointer.
In the following code the latest GCC and clang assume malloc never fails and simple remove the branch
#include <cstdlib>
int main() {
if (!...
2
votes
0
answers
96
views
FSharp inlining downcast optimization?
I am currently reading through the F# core library source code and stumbled upon a common pattern which made me wonder a little about the performance of it, and could not find anything about it by a ...
3
votes
2
answers
198
views
How to give C compiler freedom about return value
Say I have following C function:
i64 my_comparator1(i64 x, i64 y)
{
if (x > y) { return 1; }
if (x < y) { return -1; }
return 0;
}
If I happen to know something about arguments that ...
1
vote
0
answers
114
views
dlv 'Warning: debugging optimized function' despite 'go build -gcflags "-N -l"'
I compile my binary like this go build -gcflags '-N -l', but when I run them in dlv I get Warning: debugging optimized function. My guess it's that it's a different host than the build host and the ...
1
vote
4
answers
162
views
How to make a variable volatile in main loop but not in IR handler
I have a variable which is read from my main loop, and is both read and written from an interrupt handler. The interrupt can change the value at any time, so clearly it needs to be volatile in the ...
-2
votes
1
answer
174
views
Optimization of infinite loop which must return a value [closed]
I was really surprised that the code below types “res = 0” being compiled with optimization both on MSVC (the code) and Clang (the code) while it hangs (as expected) without optimization.
#include <...
-2
votes
1
answer
76
views
Is mulw faster than mul on riscv 64-bit platforms? [closed]
I wrote an unoptimized C source code for matrix multiplication, and I want to test the optimization capabilities of the Clang compiler.
void MatrixMul(unsigned int N, int *C, int *A, int *B) {
...
1
vote
0
answers
81
views
How to implement a Swift analogue of `benchmark::DoNotOptimize`?
I would like to do some (micro)benchmarking in Swift. I have been using package-benchmark for this. It comes with a blackHole helper function that forces the compiler to assume that a variable is read ...
0
votes
2
answers
632
views
Why does my Rust build takes so much time?
I LITERALLY tried everything, when I do cargo clean and then cargo build , it takes almost 10 minutes to build because of rocksdb , below is my Cargo.toml file, I have only 2 or 3 files of code which ...
7
votes
1
answer
185
views
Is [[likely]] redundant if [[unlikely]] exists in if-else branch in C++20?
Consider the following code:
void f1(bool ok) {
if (ok) [[likely]] {
// ...
} else [[unlikely]] {
// ...
}
}
void f2(bool ok) {
if (ok) [[likely]] {
// ...
...