BBC BASIC for loop optimization?

Oh, nice catch! This is precisely the sort of thing I’m curious about.

The things I noticed in RetroBASIC are not exactly earth-shattering… FOR/NEXT is 95% a step 1, most loops are small, most branches are backward. 1 and 0 are the most common numbers, etc.

The particular trick I would like to explore is making an FP increment. I think this can be made easy if you’re willing to punt to the normal addition routine if anything is difficult about the number. “difficult” in this case is non-1 step, any negative signs, and/or large or small numbers.

If the number is “simple”, then all you have to do is increment the value in the right-most byte. That’s pointed to by the exponent. So basically you do EXP-$40 to get which byte is the right one, check that to see if its below 255 (or 99 in BCD) and then INC it. If any test fails, call ADD.

The question is whether this is faster or slowed than a simple ADD. You have to do a SBC $40 on the exponent and then branch if the value is negative, CMP the result to see if it’s <4 meaning the values are “complete” (nothing’s been trunced off the right side), and then test whether the INC will cause a carry. So all those tests might end up taking more time than calling the normal FP add.