Project 3.04 Section 3 ⚡ Embedded Relevance: Critical switch Jump Tables TBB / TBH Branch Performance O(1) Dispatch

3.04 Switch Statements vs If-Else Ladders: Compiler Jump Tables (TBB / TBH)

Executive Summary: Exploring multi-case selection via switch statements vs if-else chains. We analyze compiler jump table generation (ARM TBB - Table Branch Byte / TBH - Table Branch Halfword), demonstrating why switch statements achieve deterministic O(1) execution time for dense integer cases.

💻 1. Annotated Source Code

#include <iostream>
using namespace std;

/*
	int x = 5;   
	if(x < 10)
*/



int main() {
	char grade;

	cout << "Enter your letter grade (A-F): ";
	cin >> grade;

	switch (grade) {
	case 'A':
	case 'a':
		cout << "Great job!" << endl;
		break;
	case 'B':
	case 'b':
		cout << "Good job!" << endl;
		break;
	case 'C':
	case 'c':
		cout << "You can do better!" << endl;
		break;
	case 'D':
	case 'd':
		cout << "You're getting pretty close to failing" << endl;
		break;
	case 'F':
	case 'f':
		cout << "You are failing the course!" << endl;
		break;
	default:
		cout << "You have entered an invalid grade.  Try again." << endl;

	}//end switch

	return 0;
}

📐 2. Architecture & UML Class Model

📐 Switch Jump Table & Score Classifier Model
+ Public - Private # Protected
<<compilation-unit>> GradeClassifier Jump Table (TBB/TBH)
-letterGrade : char
+classifyScore(score: int) : char
+printGradeFeedback(grade: char) : void

📚 3. Core C++ Concepts Deep-Dive

1. switch Statement Syntax

switch evaluates an integral or enum expression and transfers control to matching case labels. Missing break; statements cause intentional or accidental fallthrough.

2. [[fallthrough]] Attribute (C++17)

Marking intentional case fallthrough with [[fallthrough]]; silences compiler warnings (-Wimplicit-fallthrough).

⚡ 4. Embedded Systems & Hardware Reality

1. Jump Tables in ARM Assembly (TBB / TBH)

When cases are contiguous integers (e.g. 0 to 7), the compiler does NOT emit a series of comparisons. Instead, it emits a Jump Table (TBB [PC, R0]) in Flash. The CPU indexes directly into the table in $O(1)$ constant time, regardless of case count!

💡 5. Production-Ready Embedded Refactoring

Deterministic command dispatcher using jump table switch:

💡 Production-Ready Refactor
#include <cstdint>

enum class PacketCmd : uint8_t {
    Ping = 0,
    GetTelemetry,
    SetRelayOn,
    SetRelayOff,
    Reboot
};

// Compiles to a single ARM TBB jump table instruction (O(1) execution!)
void dispatchPacketCommand(PacketCmd cmd) noexcept {
    switch (cmd) {
        case PacketCmd::Ping:         sendAck(); break;
        case PacketCmd::GetTelemetry: streamSensors(); break;
        case PacketCmd::SetRelayOn:   relay_set(true); break;
        case PacketCmd::SetRelayOff:  relay_set(false); break;
        case PacketCmd::Reboot:       system_reset(); break;
        default:                      sendErrorNack(); break;
    }
}

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. How does an optimizing compiler execute a dense 'switch' statement with 10 sequential integer cases on ARM Cortex-M?
A It generates a Jump Table (using TBB/TBH instructions), indexing directly to target case addresses in deterministic O(1) time
B It compiles 10 sequential 'if-else' comparison instructions taking O(N) time
C It creates 10 separate threads
D It sends commands over the I2C bus
Detailed Explanation: For dense integer cases, compilers generate a jump table containing branch offsets, achieving $O(1)$ dispatch in 2-3 clock cycles.
Q2. What happens if a developer forgets a 'break;' statement at the end of a switch case in C++?
A Execution falls through into the next case statement, executing subsequent code unintentionally
B The compiler throws a syntax error
C The switch statement terminates immediately
D The microcontroller restarts
Detailed Explanation: Without break;, execution continues into the subsequent case block (fallthrough), often causing serious logic bugs.
Q3. Which C++17 attribute explicitly marks that a switch case fallthrough is intentional?
A [[fallthrough]];
B [[continue]];
C [[ignore]];
D [[next]];
Detailed Explanation: [[fallthrough]]; (C++17) informs the compiler that fallthrough is deliberate, silencing -Wimplicit-fallthrough warnings.
Q4. Can floating-point variables (e.g. float x = 3.14f) be used as the condition in a switch statement?
A No, switch statements in C++ only accept integral or enumeration types
B Yes, in C++20 and later
C Yes, if cast to void*
D Yes, but only on 64-bit platforms
Detailed Explanation: C++ strictly requires switch expressions to be integral (integers, characters) or enumeration types; floating-point values are not allowed.