Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/21/10 17:18
Read: times


 
#179253 - wait call + assumptions about two-complement, ...
Responding to: ???'s previous message
The reason they do a call to the empty wait() function is to throw off the optimizer. Only if the compiler supports global optimization would it be able to notice that wait() doesn't have any side effect, which means that the for loop doesn't have any side effect and can be removed.

Another common trick to make a sw-only busy-loop more optimizer-surviving is to define the loop variable as volatile.

Both the above options have much better chance to survive the optimizer than just writing:
for (i = a = 0; i < 10000; i++) {
    a += 2*i;
}

At least if the result of variable a doesn't get used somewhere. Some compilers can even notice that the above loop must always give the same result in a, so they can rewrite the above into:
a = <precomputed_value>;

where the value will depend on the data types of a and i.

But back to the earlier discussion.

The operators >> and << are defined as shift operators, not rotate operators. Any compiler who manages to rotate instead of shift are seriously broken. So seriously broken that it really doesn't matter what is "recommended" coding styles.

Note that there are things that are open to implementation with the shift operators - for example how they handle signed numbers, or if the target hardware doesn't keep integers in two-complements form but maybe instead have a sign bit.

If you look at the source code for a multiprecision math library, you might see loops using the shift operator for walking through all bits in a very large integer.

I recommend everyone to spend time reading the C language standard. It does tell what is guaranteed behaviour and what is implementation-specific or maybe undefined.

The next thing is to decide what are "allowed" assumptions when writing a program. Should all programs always assume that we do not know if the target hardware uses two-complmenent format for negative integers? Are we willing to take the cost of writing code that don't rely on this?

Or are we willing to write code under the assumption that the target hardware might possibly store all integers in BCD format, i.e. not binary but consuming 4 bits for every decimal digit? How is that applicable for an embeded program that at the same time has to set specific bits in specific registers in a specific processor to configure or control a specific pin of the processor?

We may not know what happens in the future, but at least as long as we work with binary computers, no computer manufacturer will dare to throw out the support for two-complement integers just because it would break a huge percentage of existing software implemented in most languages. Who would like a processor that has both +0 and -0?

The shift look assumes that the loop variable can hold 2^n states - but that is a very safe assumption for a program that assumes that the loop variable will perfectly map to the 256 combinations possible from the 8 LED you can directly fit to an 8-bit wide port.

List of 49 messages in thread
TopicAuthorDate
p89lpc936 keil programming help required.            01/01/70 00:00      
   magic code?            01/01/70 00:00      
      mov 0A5H,#0FFH            01/01/70 00:00      
   a glaring difference            01/01/70 00:00      
      a glaring difference            01/01/70 00:00      
         what happens if            01/01/70 00:00      
            what happens if            01/01/70 00:00      
               BiDir or PushPull            01/01/70 00:00      
               I leave it to you            01/01/70 00:00      
                  magic code?            01/01/70 00:00      
                  I leave it to you            01/01/70 00:00      
                     1) formatted, 2)commented, 3) correct            01/01/70 00:00      
                        1) formatted, 2)commented, 3) correct            01/01/70 00:00      
                           it is STILL two different things            01/01/70 00:00      
                              Apples and Orange Juice            01/01/70 00:00      
                                 Please focus on the problem i have            01/01/70 00:00      
                                    Software delay loop in C is a no-no            01/01/70 00:00      
                                       Software delay loop in C is a no-no            01/01/70 00:00      
                                          Reduce problem into smaller problems            01/01/70 00:00      
                                             Reduce problem into smaller problems            01/01/70 00:00      
                                             Simulator vs real hardware            01/01/70 00:00      
                                             Current limits            01/01/70 00:00      
                                             Per Westermark's previous message            01/01/70 00:00      
                                                Delay speed            01/01/70 00:00      
                                                   Elaborate            01/01/70 00:00      
                                                      tried 5 for the '51 and Keil won            01/01/70 00:00      
                                    the problem you have is ...            01/01/70 00:00      
                                       the problem you have is ...            01/01/70 00:00      
                                          do you have LEDs connected to the simulator?            01/01/70 00:00      
                                          not executing on actual hardware while simulator is fine            01/01/70 00:00      
                                    Oh So Focused            01/01/70 00:00      
   Double-post            01/01/70 00:00      
   Are you using the limited version of KEIL? the for loop....            01/01/70 00:00      
      valid C            01/01/70 00:00      
         Thats the point            01/01/70 00:00      
            nope            01/01/70 00:00      
               I wouldnt trust it            01/01/70 00:00      
                  if you do not trust it ....            01/01/70 00:00      
               And if you trust it so much why doesnt it work?            01/01/70 00:00      
                  And if you trust it so much why doesnt it work?            01/01/70 00:00      
                     trust, yes, but knowledge also required            01/01/70 00:00      
                  wait call + assumptions about two-complement, ...            01/01/70 00:00      
                     The C Standard            01/01/70 00:00      
                        who is "you"?            01/01/70 00:00      
            Yes, you can!            01/01/70 00:00      
      Quite common loop design for bit operations            01/01/70 00:00      
      for( expression-1; expression-2; expression-3 ) [ed]            01/01/70 00:00      
         erratum            01/01/70 00:00      
            Corrigendum            01/01/70 00:00      

Back to Subject List