开发者

C++ - constant and optimization

开发者 https://www.devze.com 2023-04-12 23:24 出处:网络
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:

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) ? purew开发者_如何学Pythonhite : 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号