Lookup Table Optimization
A function lookup table is a method by which you can approximate a function using a table with a finite number of points (X, Y). The X values of the lookup table are called the breakpoints. You approximate the value of the ideal function at a point by interpolating between the two breakpoints closest to the point. Because table lookups and simple estimations can be faster than mathematical function evaluations, using lookup table blocks often result in speed gains when simulating a model.
To optimize lookup tables in your model:
Limit uneven lookup tables.
Unevenly spaced breakpoints require a general-purpose algorithm such as a binary search to determine where the input lies in relation to the breakpoints. This additional computation increases ROM and execution time.
Prevent evenly spaced lookup tables from being treated as unevenly spaced.
The position search in evenly spaced lookup tables is much faster. In addition, the interpolation requires a simple division.
Sometimes, when a lookup table is converted to fixed-point, a quantization error results. A lookup table that is evenly spaced in floating-point, could be unevenly spaced in the generated fixed-point code. Use the
fixpt_evenspace_cleanup
function to convert the data into an evenly spaced lookup table again.Use power of two spaced breakpoints in lookup tables.
In power of two spaced lookup tables, a bit shift replaces the position search, and a bit mask replaces the interpolation making this construct the most efficient regardless of your target language and hardware.
The following table summarizes the effects of lookup table breakpoint spacing.
Parameter | Even Power of 2 Spaced Data | Evenly Spaced Data | Unevenly Spaced Data |
---|---|---|---|
Execution speed | The execution speed is the fastest. The position search and interpolation are the same as for evenly spaced data. However, to increase the speed more, a bit shift replaces the position search, and a bit mask replaces the interpolation. | The execution speed is faster than that for unevenly spaced data, because the position search is faster and the interpolation requires a simple division. | The execution speed is the slowest of the different spacings because the position search is slower, and the interpolation requires more operations. |
Error | The error can be larger than that for unevenly spaced data because approximating a function with nonuniform curvature requires more points to achieve the same accuracy. | The error can be larger than that for unevenly spaced data because approximating a function with nonuniform curvature requires more points to achieve the same accuracy. | The error can be smaller because approximating a function with nonuniform curvature requires fewer points to achieve the same accuracy. |
ROM usage | Uses less command ROM, but more data ROM. | Uses less command ROM, but more data ROM. | Uses more command ROM, but less data ROM. |
RAM usage | Not significant. | Not significant. | Not significant. |
Use the Model Advisor Identify Questionable Fixed-Point Operations check to identify lookup table blocks where there is potential for efficiency improvements.