All Topics  
Lookup table

 

   Email Print
   Bookmark   Link






 

Lookup table



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a lookup table is a data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
, usually an array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
 or associative array
Associative array

An associative array is an abstract data type composed of a Collection of unique keys and a collection of values, where each key is associated with one value ....
, often used to replace a runtime computation with a simpler array indexing operation. The savings in terms of processing time can be significant, since retrieving a value from memory is often faster than undergoing an 'expensive' computation. Lookup tables are also used extensively to validate input values by matching against a list of valid (or invalid) items in an array and, in some programming languages, may include pointer functions (or offsets to labels) to process the matching input.






Discussion
Ask a question about 'Lookup table'
Start a new discussion about 'Lookup table'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a lookup table is a data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
, usually an array
Array

In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory....
 or associative array
Associative array

An associative array is an abstract data type composed of a Collection of unique keys and a collection of values, where each key is associated with one value ....
, often used to replace a runtime computation with a simpler array indexing operation. The savings in terms of processing time can be significant, since retrieving a value from memory is often faster than undergoing an 'expensive' computation. Lookup tables are also used extensively to validate input values by matching against a list of valid (or invalid) items in an array and, in some programming languages, may include pointer functions (or offsets to labels) to process the matching input. In data analysis applications, such as image processing
Image processing

In electrical engineering and computer science, image processing is any form of signal processing for which the input is an , such as photographs or video frame; the output of image processing can be either an image or a set of characteristics or parameters related to the image....
, a lookup table is used to transform the input data into a more desirable output format. For example, a grayscale picture of the planet Saturn will be transformed into a color image to emphasize the differences in its rings.

A classic example of reducing run-time computations using lookup tables is to obtain the result of a trigonometry
Trigonometry

Trigonometry is a branch of mathematics that deals with triangle s, particularly those plane triangles in which one angle has 90 degrees . Trigonometry deals with relationships between the sides and the angles of triangles and with the trigonometric functions, which describe those relationships....
 calculation, such as the sine
Siné

Maurice Sinet, known as Sin? is a France cartoonist.As a young man he studied drawing and graphic arts, earning his life as a cabaret singer....
 of a value. Calculating trigonometric functions can substantially slow a computing application. The same application can finish much sooner when it first precalculates the sine of a number of values, for example for each whole number of degrees. Then, when the program requires the sine of a value, it can use the lookup table to retrieve the closest sine value from a memory address, and may also take the step of interpolating to the sine of the desired value, rather than work out the unique value every time using the mathematical formula. Lookup tables are thus used by mathematics co-processors in computer systems. An error in a lookup table was responsible for Intel's infamous floating-point divide bug
Pentium FDIV bug

The Pentium FDIV bug was a computer bug in Intel's original Pentium floating point unit. Certain floating point division operations performed with these processors would produce incorrect results....
.

Before the advent of computers, printed lookup tables of values were used by people to speed up hand calculations of complex functions, such as in trigonometry, logarithms, and statistical density functions.

Functions of a single variable (such as sine and cosine) may be implemented by a simple array. Functions involving two or more variables require multidimensional array indexing techniques. The latter case may thus employ a two-dimensional array of power[x][y] to replace a function to calculate xy for a limited range of x and y values. Functions that have more than one result may be implemented with lookup tables that are arrays of structures.

As mentioned, there are intermediate solutions that use tables in combination with a small amount of computation, often using interpolation
Interpolation

In the mathematics subfield of numerical analysis, interpolation is a method of constructing new data points within the range of a discrete set of known data points....
. Pre-calculation combined with interpolation can produce higher accuracy for values that fall between two precomputed values. This technique requires slightly more time to be performed but can greatly enhance accuracy in applications that require the higher accuracy. Depending on the values being precomputed, pre-computation with interpolation can also be used to shrink the lookup table size while maintaining accuracy.

In image processing
Image processing

In electrical engineering and computer science, image processing is any form of signal processing for which the input is an , such as photographs or video frame; the output of image processing can be either an image or a set of characteristics or parameters related to the image....
, lookup tables are often called LUT
3D LUT

In the film industry, 3D LUTs are used to calculate preview colors for a monitor or digital projector of how an image will be reproduced on the final film print....
s and give an output value for each of a range of index values. One common LUT, called the colormap or palette
Palette (computing)

In computer graphics, a palette is either a given, finite set of colors for the management of digital images , or a small on-screen graphical element for choosing from a limited set of choices, not necessarily colors ....
, is used to determine the colors and intensity values with which a particular image will be displayed. Windowing
Computed tomography

Computed tomography is a medical imaging method employing tomography. Geometry Processing is used to generate a stereoscopy of the inside of an object from a large series of two-dimensional X-ray images taken around a single axis of rotation....
 in computed tomography
Computed tomography

Computed tomography is a medical imaging method employing tomography. Geometry Processing is used to generate a stereoscopy of the inside of an object from a large series of two-dimensional X-ray images taken around a single axis of rotation....
 refers to a related concept.

While often effective, employing a lookup table may nevertheless result in a severe penalty if the computation that the LUT replaces is relatively simple. Memory retrieval time and the complexity of memory requirements can increase application operation time and system complexity relative to what would be required by straight formula computation. The possibility of polluting the cache
Cache pollution

Cache pollution describes situations where an executing computer program loads data into CPU cache unnecessarily, thus causing other needed data to be evicted from the cache into lower levels of the memory hierarchy, potentially all the way down to main memory, thus causing a performance hit....
 may also become a problem. Table accesses for large tables will almost certainly cause a cache miss. This phenomenon is increasingly becoming an issue as processors outpace memory. A similar issue appears in rematerialization
Rematerialization

Rematerialization or remat is a compiler optimization which saves time by recomputing a value instead of loading it from memory. It is typically tightly integrated with register allocation, where it is used as an alternative to spilling registers to memory....
, a compiler optimization
Compiler optimization

Compiler optimization is the process of tuning the output of a compiler to minimize or maximize some attribute of an executable computer program....
. In some environments, such as the Java programming language
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, table lookups can be even more expensive due to mandatory bounds-checking involving an additional comparison and branch for each lookup.

There are two fundamental limitations on when it is possible to construct a lookup table for a required operation. One is the amount of memory that is available: one cannot construct a lookup table larger than the space available for the table, although it is possible to construct disk-based lookup tables at the expense of lookup time. The other is the time required to compute the table values in the first instance; although this usually needs to be done only once, if it takes a prohibitively long time, it may make the use of a lookup table an inappropriate solution. Tables can however be statically defined in many cases, avoiding any additional processing once compiled.

Examples


Computing sines


Most computers, which only perform basic arithmetic operations, cannot directly calculate the sine
Siné

Maurice Sinet, known as Sin? is a France cartoonist.As a young man he studied drawing and graphic arts, earning his life as a cabaret singer....
 of a given value. Instead, they use the CORDIC
CORDIC

CORDIC is a simple and efficient algorithm to calculate hyperbolic function and trigonometric functions. It is commonly used when no hardware multiplier is available as the only operations it requires are addition, subtraction, bitshift and lookup table....
 algorithm or a complex formula such as the following Taylor series
Taylor series

In mathematics, the Taylor series is a representation of a function as an Series of terms calculated from the values of its derivatives at a single point....
 to compute the value of sine to a high degree of precision:

(for x close to 0)

However, this can be expensive to compute, especially on slow processors, and there are many applications, particularly in traditional computer graphics
Computer graphics

Computer graphics are graphics created by computers and, more generally, the representation and manipulation of pictorial data by a computer....
, that need to compute many thousands of sine values every second. A common solution is to initially compute the sine of many evenly distributed values, and then to find the sine of x we choose the sine of the value closest to x. This will be close to the correct value because sine is a continuous function
Continuous function

In mathematics, a continuous function is a function for which, intuitively, small changes in the input result in small changes in the output. Otherwise, a function is said to be discontinuous....
 with a bounded rate of change. For example:

real array sine_table[-1000..1000] for x from -1000 to 1000 sine_table[x] := sine(pi * x / 1000)

function lookup_sine(x) return sine_table[round(1000 * x / pi)]

Unfortunately, the table requires quite a bit of space: if IEEE double-precision floating-point numbers are used, over 16,000 bytes would be required. We can use fewer samples, but then our precision will significantly worsen. One good solution is linear interpolation
Linear interpolation

Linear interpolation is a method of curve fitting using linear polynomials. It is heavily employed in mathematics , and numerous applications including computer graphics....
, which draws a line between the two points in the table on either side of the value and locates the answer on that line. This is still quick to compute, and much more accurate for smooth function
Smooth function

In mathematical analysis, a differentiability class is a classification of function according to the properties of their derivatives. Higher order differentiability classes correspond to the existence of more derivatives....
s such as the sine function. Here is our example using linear interpolation:

function lookup_sine(x) x1 := floor(x*1000/pi) y1 := sine_table[x1] y2 := sine_table[x1+1] return y1 + (y2-y1)*(x*1000/pi-x1)

When using interpolation, it is often beneficial to use non-uniform sampling, which means that where the function is close to straight, we use few sample points, while where it changes value quickly we use more sample points to keep the approximation close to the real curve. For more information, see interpolation
Interpolation

In the mathematics subfield of numerical analysis, interpolation is a method of constructing new data points within the range of a discrete set of known data points....
.

Sine table example
// C 8-bit Sine Table const unsigned char sinetable[256] = ;

Counting bits

Another, more discrete problem that is expensive to solve on many computers is that of counting the number of bits which are set to 1 in a number, sometimes called the population function. For example, the number 37 is 100101 in binary, so it contains three set bits. A simple piece of C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 code designed to count the 1 bits in a int might look like this:

int count_ones(unsigned int x)

Unfortunately, this simple algorithm can take potentially hundreds of cycles on a modern architecture, because it makes many branches (loops) and branching is slow. This can be ameliorated using loop unrolling and some other more clever tricks, but there is a simple and fast solution using table lookup: simply construct a table bits_set with 256 entries giving the number of one bits set in each possible byte value. We then use this table to find the number of ones in each byte of the integer and add up the results. With no branches, four memory accesses, and almost no arithmetic, this can be dramatically faster than the algorithm above (this code assumes that int is 32-bit wide):

int count_ones(unsigned int x)

Note that even this simple algorithm can be too slow now, because the code runs faster from the cache of modern processors, but lookup tables do not fit well in caches and can cause a slower access to memory (in addition it requires computing addresses within a table, to perform the four lookups needed). On a 64-bit platform, the lookup table, if used, cannot be appropriately increased in size as it would exhaust processor caches, and if the lookup table is used to count bits by group of 8, then eight successive lookups are needed and this slows down the performance.

Caches

Storage caches (including disk caches for files, or processor caches for either for code or data) work also like a lookup table: the table is built with very fast memory instead of being stored on slower external memory, and maintains two pieces of data for a subrange of bits composing an external memory (or disk) address (notably the lowest bits of any possible external address):
  • one piece (the tag) contains the value of the remaining bits of the address; if these bits match with those from the memory address to read or write, then the other piece contains the cached value for this address.
  • the other piece maintains the data associated to that address.
A single (fast) lookup is performed to read the tag in the lookup table at the index specified by the lowest bits of the desired external storage address, and to determine if the memory address is hit by the cache. When a hit is found, no access to external memory is needed (except for write operations, where the cached value may need to be updated asynchronously to the slower memory after some time, or if the position in the cache must be replaced to cache another addresss).

Hardware LUTs


In digital logic, an n-bit lookup table can be implemented with a multiplexer
Multiplexer

In electronics, a multiplexer or mux is a device that performs multiplexing; it selects one of many analog or digital input signals and outputs that into a single line....
 whose select lines are the inputs of the LUT and whose inputs are constants. An n-bit LUT can encode any n-input Boolean function
Boolean function

In mathematics, a Boolean function is a function of the form f : Bk ? B, where B =  is a Boolean domain and k is a nonnegative integer called the arity of the function....
 by modeling such functions as truth table
Truth table

A truth table is a mathematical table used in logic?specifically in connection with Boolean algebra , boolean functions, and propositional calculus?to compute the functional values of logical expression s on each of their functional arguments, that is, on each combination of values taken by their logical variables....
s. This is an efficient way of encoding Boolean logic
Boolean logic

Boolean algebra is a logical calculus of logical values, developed by George Boole in the late 1830s. It resembles the algebra of real numbers as taught in high school, but with the numeric operations of multiplication xy, addition x + y, and negation −x replaced by the respective logical operations of conjun...
 functions, and LUTs with 4-6 bits of input are in fact the key component of modern FPGAs
Field-programmable gate array

A field-programmable gate array is a semiconductor device that can be configured by the customer or designer after manufacturing—hence the name "field-programmable"....
.

See also

  • Branch table
    Branch table

    In computer programming, a branch table is a term used to describe an efficient method of transferring program control to another part of a program using a table of branch Instruction s....
  • Memoization
    Memoization

    In computing, memoization is an Optimization technique used primarily to speed up computer programs by having Subroutine avoid repeating the calculation of results for previously-processed inputs....
  • Memory bound function
    Memory bound function

    Memory bound refers to a situation in which the time to complete a given computational problem is decided primarily by the amount of available Memory to hold data....
  • Palette
    Palette (computing)

    In computer graphics, a palette is either a given, finite set of colors for the management of digital images , or a small on-screen graphical element for choosing from a limited set of choices, not necessarily colors ....
     and Colour Look-Up Table
    CLUT

    A colour look-up table is a mechanism used to transform a range of input colors into another range of colors. It can be a hardware device built into an imaging system or a software function built into an application....
     - for the usage in computer graphics


External links