compiler-optimization Questions
7
Solved
Okay, I'm aware that the standard dictates that a C++ implementation may choose in which order arguments of a function are evaluated, but are there any implementations that actually 'take advantage...
Simonton asked 7/3, 2009 at 8:41
3
Solved
If a C program has undefined behavior, anything can happen. Therefore compilers may assume that any given program does not contain UB. So, suppose our program contains the following:
x += 5;
/* Do ...
Scuppernong asked 18/9, 2023 at 22:45
0
I have already searched for some answers on Google and Stack Overflow, and I am aware that compilers cannot assume that functions won't modify arguments passed through const references, as these fu...
Wotton asked 27/8, 2023 at 14:59
3
Solved
In C++ loops such as
for(;;) {}
used to be undefined behavior prior to P2809R3. Trivial infinite loops are not Undefined Behavior, whereas they are not in C(?).
In the aforementioned proposal, it ...
Demars asked 23/8, 2023 at 10:22
3
Solved
I have a bunch of code like the following:
int sign(MyEnum e)
{
switch(e)
{
case A:
case B:
return 1;
case C:
case D:
return -1;
default:
throw std::runtime_error("Invalid enum value");
...
Agamogenesis asked 16/7, 2019 at 15:52
2
Solved
Let's say I have code like:
if (someEnumerable.Count() < 2) {
// Do something
}
Would this result in someEnumerable being iterated over fully, or would evaluation complete if Count() reaches a...
Moriahmoriarty asked 25/8, 2023 at 11:51
1
Solved
Consider the following valarray-like class:
#include <stdlib.h>
struct va
{
void add1(const va& other);
void add2(const va& other);
size_t* data;
size_t size;
};
void va::add1(...
Darbies asked 17/8, 2023 at 10:58
2
We came across a JaxB class loading issue as highlighted by Jaxb classCastException.
To fix that I added com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true and that actually FIXED the issue....
Assertive asked 23/1, 2018 at 1:58
1
Some software (often performance-oriented, e.g. Linux kernel, DPDK) has C helpers for influencing branch prediction.
I have an absolutely simple code snippet (suppose I know the percantage of a >...
Reasonable asked 6/7, 2023 at 11:47
2
Solved
int foo(void *restrict ptr1, void *restrict ptr2)
{
if (ptr1 == ptr2) {
return 1234;
} else {
return 4321;
}
}
restrict implies the the memory pointed to by a pointer is not aliased by any ot...
Sublett asked 20/7, 2023 at 13:54
2
Solved
I'd like to make a very small compiled exe, which was written in C. But the smallest I can managed to get is 67KB. I'm using MinGW.
I've tried not to use any header file, and this compiles with no ...
Shuttle asked 8/11, 2015 at 18:34
3
Solved
Consider following float loop, compiled using -O3 -mavx2 -mfma
for (auto i = 0; i < a.size(); ++i) {
a[i] = (b[i] > c[i]) ? (b[i] * c[i]) : 0;
}
Clang done perfect job at vectorizing it. It...
Desiccant asked 13/7, 2023 at 23:17
2
I'm writing some codes for numerical computation using c++. I need to write code very carefully to help compiler to generate good instructions. Then, I find some things strange for g++ 9.2 with -O3...
Quickfreeze asked 26/12, 2019 at 9:48
4
Solved
I have a macro used all over my code that in debug mode does:
#define contract(condition) \
if (!(condition)) \
throw exception("a contract has been violated");
... but in release mode...
Havoc asked 18/5, 2017 at 17:12
1
Solved
Generally, the default constructor should be the fastest way of making an empty container.
That's why I was surprised to see that it's worse than initializing to an empty string literal:
#include &...
Impearl asked 25/6, 2023 at 23:40
4
Solved
The problem:
I'm trying to figure out how to write a code (C preffered, ASM only if there is no other solution) that would make the branch prediction miss in 50% of the cases.
So it has to be a p...
Smyth asked 10/3, 2015 at 10:34
1
struct base {
virtual void vcall() = 0;
};
struct foo final : base {
void vcall() final;
};
void call_base(base& b) {
b.vcall();
}
void call_foo(foo& f) {
call_base(f);
}
void call_...
Rateable asked 19/6, 2023 at 21:4
1
Solved
I read that string literals are always stored in read only memory and it makes sense as to why.
However if I initialize a character array using a string literal, it still stores the string literal ...
Tenorrhaphy asked 12/6, 2023 at 12:1
2
Solved
I would like to understand how debugging symbols can still be useful when compiling with -g -O2 in gcc or clang.
Because I understand, the compiler first adds the debugging symbols, and then apply ...
Brutish asked 6/6, 2023 at 11:30
1
Solved
I have a loop that loads two float* arrays into __m256 vectors and processes them. Following this loop, I have code that loads the balance of values into the vectors and then processes them. So the...
Gass asked 2/6, 2023 at 12:48
1
Solved
For context, suppose that I'm writing a bytecode interpreter, where the instructions follow one of two formats:
+---+--------+------+------+----------------+
| 1 | opcode | treg | areg | unused |
+...
Asare asked 18/5, 2023 at 10:36
0
I am currently looking for answers to why gcc generates strange instructions like "rep ret" in the generated assembly code. I came across a question on Stack Overflow where someone raised...
Clermontferrand asked 19/5, 2023 at 9:0
4
Solved
I stumbled across this Reddit post which is a joke on the following code snippet,
void f(int& x) {
if (x != 1) {
x = 1;
}
}
void g(int& x) {
x = 1;
}
saying that the two functions are ...
Marrowbone asked 16/5, 2023 at 9:40
1
Take this simple example:
struct has_destruct_t {
int a;
~has_destruct_t() {}
};
struct no_destruct_t {
int a;
};
int bar_no_destruct(no_destruct_t);
int foo_no_destruct(void) {
no_destruct_...
Arroba asked 15/5, 2023 at 18:35
1
Solved
Consider the following functions:
static inline float Eps(const float x) {
const float eps = std::numeric_limits<float>::epsilon();
return (1.0f + eps) * x - x;
}
float Eps1() {
return Ep...
Jab asked 10/5, 2023 at 3:49
© 2022 - 2025 — McMap. All rights reserved.