Quadrature encoder - Most efficient software implementation
Here goes my own implementation. 

Using a LUT, one can store the increment that must go into a counter, which determines the position of the rotor.

The index of the LUT is a combination of the current state, the last known state, and the last known direction:

Bits 0 and 1 (the 2 LSBs) are the current state (A and B values, respectively), while  bits 3 and 2 are the previous state. 

Bit 4 is the previous direction. If a state is missed, the motor is assumed to be going in the same direction as in the last state, thus recovering from a missed step.

Pros: No need for signal debouncing. Small code footprint. Will recover from missed steps.

Cons: CPU time wasted when motor is stopped or rotating slowly. Polling frequency needs to be higher than 4x the maximum encoder frequency (2x per edge, 2 channels).

Pseudo-code here:

int32_t counter_lut[32] = {
        //Direction = 1
        0, // 00 to 00
       -1, // 00 to 01
       +1, // 00 to 10
       +2, // 00 to 11

       +1, // 01 to 00
        0, // 01 to 01
       +2, // 01 to 10
       -1, // 01 to 11

       -1, // 10 to 00
       +2, // 10 to 01
        0, // 10 to 10
       +1, // 10 to 11

       +2, // 11 to 00
       +1, // 11 to 01
       -1, // 11 to 10
        0, // 11 to 11

        //Direction = 0
        0, // 00 to 00
       -1, // 00 to 01
       +1, // 00 to 10
       -2, // 00 to 11

       +1, // 01 to 00
        0, // 01 to 01
       -2, // 01 to 10
       -1, // 01 to 11

       -1, // 10 to 00
       -2, // 10 to 01
        0, // 10 to 10
       +1, // 10 to 11

       -2, // 11 to 00
       +1, // 11 to 01
       -1, // 11 to 10
        0, // 11 to 11
};

uint32_t counter = 0,  direction = 0, lut_index = 0;

void encoderInterrupt(void){

    lut_index |= getInput(encoder_A)&lt;&lt;1 | getInput(encoder_B);
    counter += counter_lut[lut_index];

    if (counter_lut[lut_index] != 0){
        direction = (counter_lut[lut_index] &gt; 0) ? 1 : 0;
    }

    //Prepare for next iteration by shifting current state
    //bits to old state bits and also the direction bit
    lut_index = ((lut_index &lt;&lt; 2) &amp; 0b1100) | (direction&lt;&lt;4);
}


Tested to be working perfectly on a 100 counts per revolution encoder, with a gearbox of 12.5:1 and maximum 260RPM (after gearbox), making it 5000 state transitions per revolution (100 counts * 12.5 * 4 encoder signal edges), for a maximum of ~22000 state transitions per second.

The sample frequency is set to 100kHz, and the interrupt priority is above all other interrupts, which is not a huge deal considering how little code the CPU actually has to execute. This was tested on a 144MHz Cortex-M4 microcontroller.