2

I am optimizing some hotspots in my application and compilation is done using gcc-arm.

Now, is there any chance that the following statements result in different assembler code:

static const pixel_t roundedwhite = 4294572537U;
return (packed >= roundedwhite) ? purewhite : packed;

// OR
const pixel_t roundedwhite = 4294572537U;
return (packed >= roundedwhite) ? purewhite : packed;

// OR
return (packed >= 4294572537U) ? purewhite : packed;

Is there any chance that my ARM compiler might produce the unwanted code for the first case or should this get optimized anyway?

I assume that it's pretty the same, but, unfortunately, I am not that sure in what gcc-arm does compared to ordinary gcc and I can't access the disassembly listing.

Thank you very much.

Rob
  • 5,223
  • 5
  • 41
  • 62
M. Williams
  • 4,945
  • 2
  • 26
  • 27

2 Answers2

4

Call gcc with the -S flag and take a look at the assembly:

-S

Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified.

I would it out try myself to include in the answer, but I don't have an ARM compiler handy.

Rob
  • 5,223
  • 5
  • 41
  • 62
1

One difference is surely that the first version, with static will use up some memory, even if it the value will get inlined in the expression. This would make sense if you want to calculate a more complex expression once and then store the result, but for this simple constant the static is unnecessary. That said, the compiler will very likely inline the value, as this is a very simple optimization and there is no reason for it not to.

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
  • That's what it would do outside of function scope, but I assumed because the definition was before a return that it was inside a function scope. According to http://www.cprogramming.com/tutorial/statickeyword.html: The use of static inside a function is the simplest. It simply means that once the variable has been initialized, it remains in memory until the end of the program. – Janick Bernet Oct 13 '11 at 13:36
  • @LokiAstari could you please give some detailed explanation here? – M. Williams Oct 13 '11 at 13:37
  • @Kotti: What he meant is, depending on where you define the `static const`, it can have different meaning. If you define it inside the function, like I assumed, it will allocate static memory. If you define it outside the function, in the cpp, it will mean static linkage, i.e., the const is not visible to other files/classes. – Janick Bernet Oct 13 '11 at 13:41
  • @inflagranti: Point taken, I agree with that. But that still does not require space. A variable is only required to take up space if its address is taken, thus it is just as easily optimized in both statement.s – Martin York Oct 13 '11 at 13:43
  • @inflagranti: Makes no difference if it is static storage duration or automatic storage duration it does not require a memory location. It is just an object and can be held totally in registers (or as in the case inlined). The only time it requires a location is when its address is taken. The semantics of static storage duration objects just means the compiler must preserve its state beyond the context of the current function call. See `1.8 The C++ object model` in the standard. An object has a type/storage duration (and optionally a name) it does not require a memory location. – Martin York Oct 13 '11 at 13:55
  • @LokiAstari: I created a new question about this http://stackoverflow.com/questions/7755257/will-a-static-variable-always-use-up-space , as I think it warrants one on its own. Feel free to post your answer there, too. – Janick Bernet Oct 13 '11 at 14:00