Right I’ve had a look at the assembly and I’m thinking that this might actually be a compiler error. Having said that I’m still getting a feel for this, and my assumptions might be wrong. I’m creating a simple 32 bit unsigned integer as an loop counter. The unsigned specification is the important piece. If the loop termination is 0x40000000 then the compiler generates three lines of assembly:
sext.w a4,a5
li a5,1073741824
bltu a4,a5,.L3
The only difference between the assembly when the loop termination is 0x80000000 is that those three lines are replaced with the single line:
bgez a5,.L3
That feels wrong since in the first three lines the value 1073741824 is the loop termination value 0x40000000 whereas in the second version of the assembly the termination value 0x80000000 never appears. I’m guessing that the compiler is treating the 0x80000000 as a negative number as the MSB is 1? Then it just does a branch greater then zero? But that variable is unsigned?
I guess I’ll be trying this with a termination of 0x7fffffff when I get a chance. I hope I’ve not made a stupid assumption in this, but I’d expect a variable uint32_t delay = 0x80000000; to be interpreted differently by the compiler.