3 ms·
You know, it's been about fifteen years. I remember I had to convert this to C in a hurry because new features had to be added. Doing it in assembler was just
by robomartin 14y ago
You know, it's been about fifteen years.
I remember I had to convert this to C in a hurry because new features had to be added. Doing it in assembler was just about unthinkable. The entire project consists of nearly 100 assembler files and resulted in about 90 files once translated into C.
This was a marathon run and once the entire project was translated and working the focus was narrowly shifted in the direction of adding functionality rather than optimizing the code.
This code was running on an 8051-derivative. Performance was important. You are always counting clock cycles on these systems. I think that if you look at the resulting machine code with the relevant variables mapped to the data memory space the modulo statement doesn't really do any better than a spelled-out conditional statement. In fact, it could be a couple of clock cycles slower (just guessing).
Again, it's been a long time. I can totally see looking at the generated assembly and saying something like "That's as fast as it it going to get. Move on!".
We all make less-than-elegant code at one point or another. Hopefully not too many times. :-)
http://www.keil.com/support/man/docs/is51/ http://www.keil.com/support/man/docs/is51/
https://www.silabs.com/Support%20Documents/TechnicalDocs/C8051F02x.pdf https://www.silabs.com/Support%20Documents/TechnicalDocs/C80...
- winter_blue 14y ago> the modulo statement doesn't really do any better than a spelled-out conditional statement Actually, on modern processors the modulo would probably be more efficient than the conditional due to cool stuff they do like pipelining. Pipelining is basically where they execute instructions ahead of time (sort of in pseudo-parallel); and so when there's a branch, there's a fair chance the results from future processing might have to be thrown out. One of the techniques that's used to go around this is branch prediction[1], where they initially guess whether a branch will be taken or not and then depending on whether the branch was actually taken, they change their guess. This works particularly well for for loops and the kind. Another development on the ARM side (and MIPS too, I think) are instructions that are embedded with conditionals in them.[2] [1] http://en.wikipedia.org/wiki/Branch_predictor http://en.wikipedia.org/wiki/Branch_predictor [2] http://en.wikipedia.org/wiki/ARM_architecture#Conditional_execution http://en.wikipedia.org/wiki/ARM_architecture#Conditional_ex...
- robomartin 14y agoAbsolutely true. I was talking about the 8051, which is the context of the code I posted.