cycleburn - How to switch off gcc Optimization for a C Source Code Region
 
 

cycleburn - How to switch off gcc Optimization for a C Source Code Region

August 3, 2025
development, HOWTO
C, RPCL

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).

Here’s the RPCL implementation of cycleburn:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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_options

This 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