If versus switch

Hi all,

When browsing through the implementation of the libraries, i stumbled across the definition of digitalWrite and digitalRead (in wiring.c) and the code looks a bit like this

void digitalWrite(int pin, int val)
      if (digitalPinToPort(pin) != NOT_A_PIN) {
            // If the pin that support PWM output, we need to turn it off
            // before doing a digital write.

            if (analogOutPinToTimer(pin) == TIMER1A)
                  cbi(TCCR1A, COM1A1);

            if (analogOutPinToTimer(pin) == TIMER1B)
                  cbi(TCCR1A, COM1B1);
#if defined(__AVR_ATmega168__)
            if (analogOutPinToTimer(pin) == TIMER0A)
                  cbi(TCCR0A, COM0A1);
            if (analogOutPinToTimer(pin) == TIMER0B)
                  cbi(TCCR0A, COM0B1);

            if (analogOutPinToTimer(pin) == TIMER2A)
                  cbi(TCCR2A, COM2A1);
            if (analogOutPinToTimer(pin) == TIMER2B)
                  cbi(TCCR2A, COM2B1);                  

These are all if statements comparing one value to different constant values, so why is it not one switch statement? I thought that would be much more efficient?

I am completely new to arduino and microcrontrollers in general :-)


No good reason. It started out with only a few possibilities, so I used if-statements. As it grew, I just never bothered to convert it to a switch-statement.

Also, since wiring.c is compiled every time you compile your sketch, you can modify it and see whether your sketch size decreases. As a bonus, you can find out whether or not you broke everything when you upload the sketch and run it! :D

Seriously though, be sure to make a backup copy of wiring.c before modifying it.

I'm still waiting for the ordered boards to arrive :-) but I will certainly try!

Is there a particular reason that wiring.c get recompiled every time? Why is it not a library?

Actually, you can have the environment compile all the runtime library stuff into a library using the patch mentioned in the following article:


The files wiring.c, HardwareSerial.cpp, etc, will be recompiled if modified, but because they are made into a library instead of object files, your sketch will be considerably smaller. I hope this patch makes its way into the official release, because it will allow everyone to squeeze in a lot more code.

I have noticed that every time you compile your sketch, it seems to destroy the "applet" directory and re-create it from scratch, so I'm guessing that even though a makefile is involved, everything gets recompiled every time. One of the reasons for this is that it probably makes things easier for the Arduino developers, who are constantly trying new tweaks to the runtime source. Also, it doesn't seem to cost much time, so it's probably a good-enough solution for everyone.

I don’t know too much about the Assembler code that gets uploaded to the Ard but commonly switch statements are much more efficient when compiled down to Asm code because switch statements get put together in a jump table whereas a list of if statements (or worse, if elseif … statements) get converted into assembly if statements. I don’t remember the logistics and specifics of it all, but I do know that a jump table does a much better job at these kind of processing hurdles. If someone was really interested I can probably find my Assembler book and put up an example…