Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lookup table
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Array that replaces runtime computation with a simpler array indexing operation}} {{Use dmy dates|date=November 2019}} In [[computer science]], a '''lookup table''' ('''LUT''') is an [[array data structure|array]] that replaces [[runtime (program lifecycle phase)|runtime]] computation of a mathematical [[function (mathematics)|function]] with a simpler array indexing operation, in a process termed as '''''direct addressing'''''. The savings in processing time can be significant, because retrieving a value from memory is often faster than carrying out an "expensive" computation or [[input/output]] operation.<ref>{{cite web |url=http://pmcnamee.net/c++-memoization.html |title=Automated Memoization in C++ |first=Paul |last=McNamee |date=21 August 1998 |url-status=unfit |archive-url=https://web.archive.org/web/20190416012134/http://pmcnamee.net/c++-memoization.html |archive-date=2019-04-16}}</ref> The tables may be [[precomputation|precalculated]] and stored in [[static memory allocation|static]] program storage, calculated (or [[prefetcher|"pre-fetched"]]) as part of a program's initialization phase (''[[memoization]]''<!--note: not the same as "memorization"-->), or even stored in hardware in application-specific platforms. 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. [[Field-programmable gate array|FPGAs]] also make extensive use of reconfigurable, hardware-implemented, lookup tables to provide programmable hardware functionality. LUTs differ from [[hash tables]] in a way that, to retrieve a value <math>v</math> with key <math>k</math>, a hash table would store the value <math>v</math> in the slot <math>h(k)</math> where <math>h</math> is a [[hash function]] i.e. <math>k</math> is used to compute the slot, while in the case of LUT, the value <math>v</math> is stored in slot <math>k</math>, thus directly addressable.<ref name="Kwok95">{{cite journal|journal=Communications in Numerical Methods in Engineering|volume=11|issue=5|first1=W.|last1=Kwok|first2=K.|last2=Haghighi|first3=E.|last3=Kang|year=1995|doi=10.1002/cnm.1640110511|title=An efficient data structure for the advancing-front triangular mesh generation technique|pages=465–473|url=https://onlinelibrary.wiley.com/doi/10.1002/cnm.1640110511|publisher=Wiley & Sons}}</ref>{{rp|p=466}} == History == [[File:Abramowitz&Stegun.page97.agr.jpg|thumb|Part of a 20th-century table of [[common logarithm]]s in the reference book [[Abramowitz and Stegun]].]] Before the advent of computers, lookup tables of values were used to speed up hand calculations of complex functions, such as in [[trigonometry]], [[common logarithm|logarithms]], and statistical density functions.<ref>{{cite book |editor1-last= Campbell-Kelly |editor1-first= Martin|editor1-link=Martin Campbell-Kelly |editor2-last= Croarken |editor2-first= Mary|editor2-link=Mary Croarken |editor3-last= Robson |editor3-first= Eleanor|editor3-link=Eleanor Robson |title= The History of Mathematical Tables: From Sumer to Spreadsheets |title-link= The History of Mathematical Tables |year= 2003 |publisher=Oxford University Press }} </ref> In ancient (499 AD) India, [[Aryabhata]] created one of the first [[Āryabhaṭa's sine table|sine tables]], which he encoded in a Sanskrit-letter-based number system. In 493 AD, [[Victorius of Aquitaine]] wrote a 98-column multiplication table which gave (in [[Roman numerals]]) the product of every number from 2 to 50 times and the rows were "a list of numbers starting with one thousand, descending by hundreds to one hundred, then descending by tens to ten, then by ones to one, and then the fractions down to 1/144"<ref>Maher, David. W. J. and John F. Makowski. "[https://ecommons.luc.edu/cgi/viewcontent.cgi?article=1024&context=classicalstudies_facpubs Literary Evidence for Roman Arithmetic With Fractions]", 'Classical Philology' (2001) Vol. 96 No. 4 (2001) pp. 376–399. (See page p.383.)</ref> Modern school children are often taught to memorize "[[multiplication table|times tables]]" to avoid calculations of the most commonly used numbers (up to 9 x 9 or 12 x 12). Early in the history of computers, [[input/output]] operations were particularly slow – even in comparison to processor speeds of the time. It made sense to reduce expensive read operations by a form of manual [[cache (computing)|caching]] by creating either static lookup tables (embedded in the program) or dynamic prefetched arrays to contain only the most commonly occurring data items. Despite the introduction of systemwide caching that now automates this process, application level lookup tables can still improve performance for data items that rarely, if ever, change. Lookup tables were one of the earliest functionalities implemented in computer [[spreadsheet]]s, with the initial version of [[VisiCalc]] (1979) including a <code>LOOKUP</code> function among its original 20 functions.<ref>[https://vlookupweek.wordpress.com/2012/03/31/bill-jelen-from-1979-visicalc-and-lookup/ Bill Jelen: "From 1979 – VisiCalc and LOOKUP"!], by MrExcel East, 31 March 2012</ref> This has been followed by subsequent spreadsheets, such as [[Microsoft Excel]], and complemented by specialized <code>VLOOKUP</code> and <code>HLOOKUP</code> functions to simplify lookup in a vertical or horizontal table. In Microsoft Excel the <code>XLOOKUP</code> function has been rolled out starting 28 August 2019. == Limitations == Although the performance of a LUT is a guaranteed <math>O(1)</math> for a lookup operation, no two entities or values can have the same key <math>k</math>. When the size of [[Universe (mathematics)|universe]] <math>\cup</math>—where the keys are drawn—is large, it might be impractical or impossible to be stored in [[computer memory|memory]]. Hence, in this case, a [[hash table]] would be a preferable alternative.{{r|Kwok95|p=468}} == Examples == === Trivial hash function === For a [[trivial hash function]] lookup, the unsigned [[raw data]] value is used ''directly'' as an index to a one-dimensional table to extract a result. For small ranges, this can be amongst the fastest lookup, even exceeding binary search speed with zero branches and executing in [[constant time]].<ref>{{cite book|last1=Cormen|first1=Thomas H.|title=Introduction to algorithms|date=2009|publisher=MIT Press|location=Cambridge, Mass.|isbn=9780262033848|pages=253–255|edition=3rd|url=https://mitpress.mit.edu/books/introduction-algorithms|accessdate=26 November 2015}}</ref> ====Counting bits in a series of bytes==== One discrete problem that is expensive to solve on many computers is that of counting the number of bits that are set to 1 in a (binary) number, sometimes called the ''[[Hamming weight|population function]]''. For example, the decimal number "37" is "00100101" in binary, so it contains three bits that are set to binary "1".<ref name="apress11">{{cite book|doi=10.1007/978-1-4302-4159-1_26|publisher=Apress|url=https://link.springer.com/chapter/10.1007/978-1-4302-4159-1_26|title= Developing for Performance. In: packetC Programming |author1=Jungck P.|author2=Dencan R.|author3=Mulcahy D.|year=2011|isbn=978-1-4302-4159-1}}</ref>{{rp|p=282}} A simple example of [[C (programming language)|C]] code, designed to count the 1 bits in a ''int'', might look like this:{{r|apress11|p=283}} <syntaxhighlight lang="c"> int count_ones(unsigned int x) { int result = 0; while (x != 0) { x = x & (x - 1); result++; } return result; }</syntaxhighlight> The above implementation requires 32 operations for an evaluation of a 32-bit value, which can potentially take several [[Cycles per instruction|clock cycles]] due to [[Control flow|branching]]. It can be "[[loop unrolling|unrolled]]" into a lookup table which in turn uses [[trivial hash function]] for better performance.{{r|apress11|p=282-283}} The bits array, ''bits_set'' with 256 entries is constructed by giving the number of one bits set in each possible byte value (e.g. 0x00 = 0, 0x01 = 1, 0x02 = 1, and so on). Although a [[Runtime (program lifecycle phase)|runtime]] algorithm can be used to generate the ''bits_set'' array, it's an inefficient usage of clock cycles when the size is taken into consideration, hence a precomputed table is used—although a [[compile time]] script could be used to dynamically generate and append the table to the [[source file]]. Sum of ones in each byte of the [[Integer (computer science)|integer]] can be calculated through [[trivial hash function]] lookup on each byte; thus, effectively avoiding branches resulting in considerable improvement in performance.{{r|apress11|p=284}} <syntaxhighlight lang="c"> int count_ones(int input_value) { union four_bytes { int big_int; char each_byte[4]; } operand = input_value; const int bits_set[256] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}; return (bits_set[operand.each_byte[0]] + bits_set[operand.each_byte[1]] + bits_set[operand.each_byte[2]] + bits_set[operand.each_byte[3]]); }} </syntaxhighlight> === Lookup tables in image processing === {{original research section|date=October 2021}} [[File:Red Green Blue 16 bit Look up Table Sample.svg|thumb|right|Red (A), Green (B), Blue (C) 16-bit lookup table file sample. (Lines 14 to 65524 not shown)]] {{blockquote|"Lookup tables (LUTs) are an excellent technique for optimizing the evaluation of functions that are expensive to compute and inexpensive to cache. ... For data requests that fall between the table's samples, an interpolation algorithm can generate reasonable approximations by averaging nearby samples."<ref>[https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-quality-rendering/chapter-24-using-lookup-tables-accelerate-color nvidia gpu gems2 : using-lookup-tables-accelerate-color]</ref>}} In data analysis applications, such as [[image processing]], a lookup table (LUT) can be used to transform the input data into a more desirable output format. For example, a grayscale picture of the planet Saturn could be transformed into a color image to emphasize the differences in its rings. In image processing, lookup tables are often called '''[[3D LUT|LUT]]'''s (or 3DLUT), and give an output value for each of a range of index values. One common LUT, called the ''colormap'' or ''[[palette (computing)|palette]]'', is used to determine the colors and intensity values with which a particular image will be displayed. In [[computed tomography]], "windowing" refers to a related concept for determining how to display the intensity of measured radiation. ===Discussion=== {{original research section|date=October 2021}} A classic example of reducing run-time computations using lookup tables is to obtain the result of a [[trigonometry]] calculation, such as the [[sine]] of a value.<ref>{{cite web |author1=Sasao, T.; Butler, J. T.; Riedel, M. D. |title=Application of LUT Cascades to Numerical Function Generators |url=https://apps.dtic.mil/sti/citations/ADA596280 |website=Defence Technical Information Center |publisher=NAVAL POSTGRADUATE SCHOOL MONTEREY CA DEPT OF ELECTRICAL AND COMPUTER ENGINEERING |access-date=17 May 2024}}</ref> 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 (The table can be defined as static variables at compile time, reducing repeated run time costs). 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 interpolate to the sine of the desired value, instead of calculating by mathematical formula. Lookup tables can thus be used by mathematics [[coprocessor]]s in computer systems. An error in a lookup table was responsible for Intel's infamous [[Pentium FDIV bug|floating-point divide bug]]. 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 '''x<sup>y</sup>''' 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]]. 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 it. Depending on the values being precomputed, [[precomputation]] with interpolation can also be used to shrink the lookup table size while maintaining accuracy. 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 [[cache pollution|polluting the cache]] 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]], a [[compiler optimization]]. In some environments, such as the [[Java (programming language)|Java programming language]], 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. As previously stated however, tables can be statically defined in many cases. === Computing sines === Most computers only perform basic arithmetic operations and cannot directly calculate the [[sine]] of a given value. Instead, they use the [[CORDIC]] algorithm or a complex formula such as the following [[Taylor series]] to compute the value of sine to a high degree of precision:<ref name="sharif14">{{cite journal|journal= Journal of Circuits, Systems and Computers|volume=23|number=4|year=2014|first=Haidar|last=Sharif|doi=10.1142/S0218126614500510|url=https://www.worldscientific.com/doi/abs/10.1142/S0218126614500510|title=High-performance mathematical functions for single-core architectures|publisher=World Scientific}}</ref>{{rp|p=5}} :<math>\operatorname{sin}(x) \approx x - \frac{x^3}{6} + \frac{x^5}{120} - \frac{x^7}{5040}</math> (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]], 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'' through array indexing operation. This will be close to the correct value because sine is a [[continuous function]] with a bounded rate of change.{{r|sharif14|p=6}} For example:<ref>{{cite book|title= The Art of Assembly Language, 2nd Edition |author=Randall Hyde|date=1 March 2010|publisher=No Starch Press|isbn=978-1593272074|url=https://www.ic.unicamp.br/~pannain/mc404/aulas/pdfs/Art%20Of%20Intel%20x86%20Assembly.pdf|via=University of Campinas Institute of Computing}}</ref>{{rp|p=545–548}} <syntaxhighlight lang="abap"> 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)] </syntaxhighlight> [[File:Interpolation example linear.svg|thumb|Linear interpolation on a portion of the sine function|right]] 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]], 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]]s such as the sine function. Here is an example using linear interpolation: <syntaxhighlight lang="abap"> 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) </syntaxhighlight> Linear interpolation provides for an interpolated function that is continuous, but will not, in general, have continuous [[derivative]]s. For smoother interpolation of table lookup that is continuous '''and''' has continuous [[first derivative]], one should use the [[cubic Hermite spline#Interpolation on the unit interval with matched derivatives at endpoints|cubic Hermite spline]]. When using interpolation, the size of the lookup table can be reduced by using ''[[nonuniform 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]]. == Other usages of lookup tables == === Caches === {{main|Cache (computing)}} Storage caches (including disk caches for files, or processor caches for either 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 sub-range 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 address). === Hardware LUTs === In [[digital logic]], a lookup table can be implemented with a [[multiplexer]] whose select lines are driven by the address signal and whose inputs are the values of the elements contained in the array. These values can either be hard-wired, as in an [[ASIC]] whose purpose is specific to a function, or provided by [[D latch]]es which allow for configurable values. ([[ROM]], [[EPROM]], [[EEPROM]], or [[random-access memory|RAM]].) An ''n''-bit LUT can encode any ''n''-input [[Boolean function]] by storing the [[truth table]] of the function in the LUT. This is an efficient way of encoding [[Boolean logic]] functions, and LUTs with 4-6 bits of input are in fact the key component of modern [[field-programmable gate array]]s (FPGAs) which provide reconfigurable hardware logic capabilities. === Data acquisition and control systems === In [[data acquisition]] and [[control system]]s, lookup tables are commonly used to undertake the following operations in: * The application of [[calibration]] data, so as to apply corrections to uncalibrated measurement or [[setpoint (control system)|setpoint]] values; and * Undertaking [[measurement unit]] conversion; and * Performing generic user-defined computations. In some systems, [[polynomial]]s may also be defined in place of lookup tables for these calculations. ==See also== *[[Associative array]] *[[Branch table]] *[[Gal's accurate tables]] *[[Memoization]] *[[Memory-bound function]] *[[Nearest-neighbor interpolation]] *[[Shift register lookup table]] *[[Palette (computing)|Palette]], a.k.a. color lookup table or CLUT – for the usage in computer graphics *[[3D lookup table]] – usage in film industry ==References== {{Reflist}} ==External links== * [[Wikibooks:360 Assembly/Branch Instructions|Fast table lookup using input character as index for branch table]] * [https://web.archive.org/web/20120414190621/http://webster.cs.ucr.edu/AoA/Windows/HTML/TableLookups.html Art of Assembly: Calculation via Table Lookups] * [http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable "Bit Twiddling Hacks" (includes lookup tables)] By Sean Eron Anderson of [[Stanford University]] * [https://web.archive.org/web/20120831094028/http://apl.jhu.edu/~paulmac/c%2B%2B-memoization.html Memoization in C++] by Paul McNamee, [[Johns Hopkins University]] showing savings * [https://books.google.com/books?id=gJrmszNHQV4C&dq=beautiful+code+%22population+count%22&pg=PT169 "The Quest for an Accelerated Population Count"] by Henry S. Warren Jr. [[Category:Arrays]] [[Category:Associative arrays]] [[Category:Computer performance]] [[Category:Software optimization]] [[Category:Articles with example C code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Blockquote
(
edit
)
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Main
(
edit
)
Template:Original research section
(
edit
)
Template:R
(
edit
)
Template:Reflist
(
edit
)
Template:Rp
(
edit
)
Template:Short description
(
edit
)
Template:Use dmy dates
(
edit
)