How to Use Lookup Table Instead of Calculation in Embedded C
In Embedded C, you can replace runtime calculations with a
lookup table by precomputing values and storing them in an array. Access the array using an index instead of performing the calculation each time, which speeds up execution and reduces CPU load.Syntax
A lookup table in Embedded C is typically an array that holds precomputed values. You declare it as a constant array to save memory and prevent modification. Access values using an index, like table[index].
- const type name[size]: Declares the lookup table.
- index: The input used to find the precomputed value.
- value = table[index]: Retrieves the value from the table.
c
const int lookup_table[5] = {10, 20, 30, 40, 50}; int value = lookup_table[2]; // value will be 30
Example
This example shows how to replace a simple calculation (square of a number) with a lookup table. Instead of calculating n * n each time, the program uses a precomputed table for numbers 0 to 9.
c
#include <stdio.h> // Precomputed squares for numbers 0 to 9 const int square_table[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; int main() { for (int i = 0; i < 10; i++) { // Use lookup table instead of calculation int square = square_table[i]; printf("Square of %d is %d\n", i, square); } return 0; }
Output
Square of 0 is 0
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Common Pitfalls
Common mistakes when using lookup tables include:
- Accessing indexes outside the table range, causing undefined behavior.
- Using non-constant tables that waste RAM instead of storing in flash memory.
- Not updating the table when input range changes, leading to incorrect results.
Always check index bounds and declare tables as const to store them in read-only memory.
c
/* Wrong: No bounds check, may access invalid index */ int get_value(int index) { const int table[3] = {100, 200, 300}; return table[index]; // Unsafe if index >= 3 or index < 0 } /* Right: Check bounds before access */ int get_value_safe(int index) { const int table[3] = {100, 200, 300}; if (index < 0 || index >= 3) { return -1; // Error value } return table[index]; }
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Lookup Table Declaration | Use const array to store precomputed values | const int table[5] = {1,2,3,4,5}; |
| Access Value | Use index to get value instead of calculation | value = table[index]; |
| Bounds Checking | Prevent invalid index access | if(index >= 0 && index < size) {...} |
| Memory Usage | Declare as const to store in flash memory | const int table[] = {...}; |
| Use Case | Replace expensive calculations with fast lookup | e.g., trigonometric values, squares |
Key Takeaways
Use const arrays as lookup tables to store precomputed values in Embedded C.
Access lookup tables with valid indexes to avoid undefined behavior.
Lookup tables speed up code by replacing runtime calculations with fast memory access.
Always check index bounds before accessing the lookup table.
Declare lookup tables as const to save RAM and use flash memory efficiently.