Frage

In the following code segment my intent is to force a few specific values of the input argument from a full range of possibilities. I am using g++ version 4.7.2 with all optimizations turned off, and my question is whether or not the compiler with honor the intended order of execution.

int
MP3Filter::checkBitrate(int nBitrate)
{
    // ##########################################
    // this assumes a specific order of execution
    // ?? will the compiler mess with this ??

    if      (nBitrate <= 32)    return 32;
    else if (nBitrate <= 40)    return 40;
    else if (nBitrate <= 48)    return 48;
    else if (nBitrate <= 56)    return 56;
    else if (nBitrate <= 64)    return 64;
    else if (nBitrate <= 80)    return 80;
    else if (nBitrate <= 96)    return 96;
    else if (nBitrate <= 112)   return 112;
    else if (nBitrate <= 128)   return 128;
    else if (nBitrate <= 160)   return 160;
    else if (nBitrate <= 192)   return 192;
    else if (nBitrate <= 224)   return 224;
    else if (nBitrate <= 256)   return 256;
    return 320;
}

Is there a way to tell g++ to NOT mess with a particular code section?

UPDATE: Thanks for your comments guys. Perhaps I was a bit too hasty to post. I am having some issues with my code and it seemed this routines was not working as I intended. So because of all of the constants I just suspected the compiler was optimizing something out. But you have clarified things for me.

Thanks again, -Andres

War es hilfreich?

Lösung

Optimizations should be irrelevant for this code. The compiler is required to generate code that behaves as the language standard requires it to behave, and in this case, there is no flexibility in the values that your function can return for any particular argument value.

The compiler can generate whatever code it likes, as long as the function returns the required result. If getting the correct result from the function is all you care about, the language standard already covers that. either g++ does this right, or g++ has a bug. (I'm not aware of any g++ bug in this area, and I'd be surprised if there were such a bug.)

If you care about something other than having the function returning the correct result (for example, the particular sequence of CPU instructions), then (a) I have to wonder why you'd care, and (b) C++ is not a language that lets you impose such constraints. If the sequence of instructions matters to you, you might consider using assembly language.

What exactly is your concern?

Andere Tipps

I think you can tell g++ to turn all optimizations off with the -O0 flag. Are you concerned with the optimizer. Nothing stands out about this code to make me think that it will not compile correctly though.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top