cycleburn — How to switch off gcc Optimization for a C Source Code Region
cycleburn
gcc allows to save the current optimization settings to a stack during compilation (#pragma GCC push_options), to change them to a desired value (#pragma GCC optimize ("O0")). #pragma GCC pop_options restores the previously pushed settings. The only thing to consider is that these changes need to be around a function (trying to change the optimization settings for a few lines within a function is not supported).
Get Thomas G. Obermair’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s the RPCL implementation of cycleburn :
#pragma GCC push_options
#pragma GCC optimize ("O0")
int RPCLInternalCycleburn(RPCL * rpcl) {
uint64_t cycles;
uint64_t i;
char a[16];
char b[16];
char c[16];
if (RPCLPopDuint64_t(rpcl, &cycles) != RPCL_OK) {
return (RPCL_ERROR);
}
for (i = 0; i < cycles; i++) {
memcpy(b, a, 16);
memcpy(c, b, 16);
memcpy(a, c, 16);
}
return (RPCL_OK);
}
#pragma GCC pop_optionsThis is how cycleburn is registered within RPCL:
RPCLRegister(rpcl, "cycleburn", "burn cycles ( iterations - )", RPCLInternalCycleburn);Burning Cycles
Testing this within an RPCL shell may look like this:
ok timer-start 10000000 cycleburn timer-show
35054 microseconds elapsed
ok ▂Resources
This post first appeared on blog.inlab.net .







