Constant calculations not computed

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was very surprised to discover the following about avr-gcc:

when I put something like:

y = x * (1600 / 100);

I expected the compiler to reduce it to:

y = x * 16;

But it seems like it actually keeps both the multiplication and the division, when it is clearly not needed.
In the case where 1600 is replaced by a #defined variable, this becomes a serious pain in the ass since performance suffers a lot.
Is there some options I can put to tell the compiler to precompute all values?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do you know that it does not pre-compute? Can you show the assembler listing for the area where it happens?

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well after checking the disassembly, I realized I messed up my define. At one point in the code, I've set a define as 0.64 * 100, and it has stored it as floating point, thus taking many cycles for the conversion. I blamed the division for the increase in cycles, without investigating to the depth of the problem. My bad... thanks Jim for putting me on the right track