An ErrorCompensating Digitial Differential Analyzer (ECDDA)AbstractBresenham's line drawing algorithm is the defacto standard for efficient line drawing on rasterized output devices such as computer screens or plotters. The algorithm offers a number of benefits: it is solely based on simple integer operations, which are fast and cheap to implement, it minimizes the error that arises due to the discretization, and it is orderinvariant. In contrast, a naive digital differential analyzer (DDA) requires complex floatingpoint arithmetic, accumulates rounding errors due to the finite representation of possibly infinite rational numbers, and is not orderinvariant. However, on a modern desktop processor floatingpoint arithmetic is queued into the processor pipeline just as fast as integer operations, and a DDA requires a fewer number of instructions than Bresenham's algorithm. An idea I publically described in 2003 and implemented shortly afterwards allows one to upperbound the cumulative numeric error that may arise in the DDA and by that calculation adapt the differential such that the result is an "errorcompensating" DDA (ECDDA) with the property that all pixels along the line attain correct rounding provided that their cooridinate values are smaller than some predetermined maximum (e.g., 2^{21} for IEEE 64bit floating point numbers). The new algorithm has been shown to be significantly faster than traditional Bresenham line drawing on modern desktop processors. IntroductionA naive way of drawing a line from point (x_{1}, y_{1}) to point (x_{2}, y_{2}) is based on computing the slope (also called differential) between the two endpoints and then on iteratively adding the slope to one coordinate of the starting point while progressing along the axis of the other coordinate. For example, one might compute the slope as
slope := d_{y}/d_{x}
where d_{x} = x_{2}  x_{1} and d_{y} = y_{2}  y_{1}, and then evaluate the ycoordinate for each point with a given xcoordinate between x1 and x2 as
y := y_{1} + (x  x_{1})*slope.
Assuming that the two endpoints' coordinate values are integers one can consider x to be in the set of all integers between x1 and x2. Then each y will be some rational number, which must be discretized, that is, rounded to some integer, for display on the output device. Roundtonearest (simply denoted as round) minimizes the error the discretization introduces, and we may assume the convention that halfway values (e.g., 0.5, 1.5, etc.) are always rounded up to the nextbigger integer. Now, and one can draw the line using some pixel routine as
The algorithm above assumes that x_{2} > x_{1} and slope ≤ 1; adapting it to the other three cases is a simple matter and omitted here for clarity. What is more imporant is that the simple algorithm above has two notable problems that make it impractical. From a performance point of view, the algorithm is rather slow. This is because each iteration of the loop carries out two floatingpoint instructions, one multiplication and one rounding operation, both which are comparatively expensive operations. Floatingpoint instructions are often not available on resourceconstrained devices such as digital signal processors where they would add considerably to cost and energy consumption, and they are much slower than integer operations even on highend computing devices. The second and more concerning problem of the algorithm is that it is incorrect: intermediate pixels are not guaranteed to be assigned their "best" coordinate values on the output device, that is, those values that minimize the discretization error. This is because the slope value, a rational value, can only be represented with finite precision as a floatingpoint value and thus a minimal error in the slope value may result in a wrong rounding towards one or the other integer. At the same time, the appearance of the line may be dependent on whether the line is drawn from one endpoint to the other or in the reverse order. As an example, consider drawing a line from point (0, 0) to point (10, 1). The actual slope value is 0.1; however, in a "singleprecision" floatingpoint representation the value is 0.099999904632568359375. Evaluating y at x = 5 gives 0.499999523162841796875 as an intermediate result, which gets rounded to 0 rather than 1. Reversing the endpoints and thus drawing a line from (10, 1) to (0, 0) results in an intermediate value of y at x = 5 as 0.500000476837158203125, which gets rounded to 1. It seems that one may adopt a convention for drawing lines only in a certain direction (e.g., only with positive slope values); however, a consistent rounding result for halfway values cannot be guaranteed that way. The issue of rounding errors could be avoided by substituting the slope variable in the algorithm by its assignment and then carrying out multiplication before division. In detail, y could be computed as
y := y_{1} + round((x  x_{1})*(y_{2}  y_{1})/(x_{2}  x_{1})).
Unfortunately, this not only increases the number of floatingpoint instructions in the loop to three but also introduces a considerably slow operation, a floatingpoint division. A different solution is thus indicated. Bresenham's Line Drawing AlgorithmOne approach for avoiding the issue of rounding errors is to replace floatingpoint operations by integer operations. Looking at the simple algorithm above one can observe that the floatingpoint multiplication with the slope value can be easily avoided by replacing it with an iterative addition. That is, the algorithm can be rewritten as
Such an algorithm is called a digital differential analyzer (DDA). While this algorithm is still conceptually simple the problem of rounding errors is aggrevated. This is because the error by which the slope value is affected accumulates with each iteration of the loop. On the other hand, the algorithm is faster than the previous one because it replaces the floatingpoint multiplication by a simpler addition. One may now study the instances at which y results in round(y) being incremented by 1 compared to its previous value in the loop. (We may assume a positive slope < 1 without loss of generality, and y is thus a monotonically increasing value.) These instances occur precisely when y has crossed halfway between two integer values, that is, y  round(y) < 0 in the current iteration of the loop whereas y  round(y) ≥ 0 in the previous iteration of the loop. Let e := y  round(y); then e can be tracked by initializing it with 0, accumulating the slope values in the loop and decrementing it by 1 whenever e ≥ 0.5. This variable represents the error that is introduced by discretizing y to round(y). Using e, one may rewrite the algorithm as
Now, remember that e is defined as d_{y}/d_{x}. One can avoid the division in the computation of slope by multiplying e by d_{x}, and so the code becomes
Finally, one can multiply the inequality by 2 to obtain the following result:
This algorithm is truly notable because it not only requires no floatingpoint operations at all (and thus avoids the issue of rounding errors), it also does not rely on any integer multiplications or divisions (the multiplication by two can be carried out via a trivial bitshift operation). It is thus an ideal solution for resourceconstrained devices, and it has consequently become the defacto standard among linedrawing algorithms (or a basis for more complex algorithms). The algorithm was first presented in 1965 by Jack Bresenham and has since become known as Bresenham's line drawing algorithm. Even though this algorithm is fairly efficient in terms of both computing resources needed and processing speed, it is obviously more elaborate than the simple digital differential analyzer. While integer operations are fast, modern CPU architectures are aggressively optimized for floatingpoint operations and massively pipelined with multiple arithmetic processing units such that instructions can be executed (and finished) even when previous instructions haven't finished yet. It is therefore tempting to ask whether the simple DDA can be adapted in such a way as to somehow compensate the discretization error and thus to obtain an algorithm that is both correct and faster than Bresenham's algorithm. For this aim it is imperative to understand the cause of rounding errors in the DDA algorithm. Understanding FloatingPoint ErrorsComputers store numbers in the form of a binary (base 2) representation within some finite space of memory. This method is convenient for encoding integer values, but when it comes to encoding real numbers it turns out that the set of numbers that have a finite binary (or padic) representation is a diminishing set among all reals. Not only can no base exactly represent an irrational number, the number of rationals that do not end in a period of nonzero digits in a given base is diminishing as well. For example, consider the rational 1/10, which is 0.1 in base10 representation but results in the following infinite string of digits in binary representation:
Because of these limitations it is important to choose some encoding strategy that minimizes the discretization error. In a simple approach, reals are encoded by reserving a fixed number of digits past the decimal point within the available memory. For example, various decimal numbers can be represented (in base 10) as
Such an encoding is called a fixedpoint representation. While easy to implement, it is unsatisfactory. Small numbers cannot be represented accurately enough because the number of available digits after the decimal point is constant. For example, both 14/1000 and 6/1000 are encoded as 0.01 (with rounding), even though the former value is more than twice as large as the latter – and all digits on the left side of the decimal point are 0. Even smaller values (smaller than 0.005) can only be best approximated as 0. On the other hand, large numbers may not be known accurately enough. For example, the mass of Earth is roughly 5,972,190,000,000,000,000,000,000 kilograms, but what is the point of storing all the zeros when one is confident only about some number of digits at the beginning? Usually, what one is interested in is a certain number of significant digits at the beginning of a real number. Scientists and engineers long ago learned to cope with this by using scientific notation, in which a number is expressed as the product of a mantissa and some power of base ten. The mantissa is a signed number with an absolute value greater than or equal to one and less than ten to obtain a unique representation. So, for instance, the mass of Earth can be sufficiently represented as 5.9722×10^{24} kilograms. An encoding of scientific notation on computers generally uses a sign bit, a fixed number of bits (referred to as the precision) for a positive mantissa and a shorter number of bits for the exponent, denoting a power of base 2. Such a format is called a floatingpoint representation. IEEE 754 refers to an important (and pleasantly well designed) standard for floatingpoint representations, notably brought forward by Intel corporation. The standard includes specifications for the encodings of real numbers within various bit lengths, but also for the encoding of increasingly small (socalled "subnormal") numbers, infinities and special notanumbers (NaNs), and further specifies the details of floatingpoint operations, rounding rules, exception handling and format conversions. Among the most popular encodings the standard specifies are a 32bit and a 64bit representation, widely referred to as single and doubleprecision formats. The general representation of real numbers in these formats is of the following form:
Here, s denotes a sign bit, m refers to the bits of the mantissa after the decimal point (the leading bit of the mantissa is assumed to be 1), and e denotes the exponent. In singleprecision encoding, m takes 23 bits and e takes 8 bits, while a doubleprecision format reserves 52 bits for m and 11 bits for e. Floatingpoint representation is a flexible solution for storing real numbers within limited space, but it does not avoid the issue of rounding errors. In IEEE 754, the error depends on the precision of the selected floatingpoint format, the exponent chosen and the rounding mode selected. It can be expressed in terms of the absolute error, which is defined as
where x' denotes the floatingpoint representation of some real number x. For a given precision p and some exponent e, the absolute error can be upperbounded by 2^{p+e} for roundtonearest and by 2^{(p1)+e} for the floor or ceiling rounding operations. Can these upper bounds be used to calculate the worstcase discretization error that may arise in the DDA algorithm? As an example, consider drawing a line from point (0, 0) to point (1000, 100). The differential is 0.1, whose IEEE 64bit floatingpoint representation is affected by an approximate discretization error of 2.2×10^{17}, which is smaller than the upper bound 2^{53+2} ≈ 2.8×10^{17}. However, when adding up this differential 1000 times (and using roundtonearest) the error accumulates to 1.4×10^{12} and thus becomes larger by more than a factor of 1000 of the initial error. This is because less and less digits become available after the decimal point as the values are summed up. The result is slightly smaller than 100, just as the floatingpoint representation of 0.1 is slightly too small. On the other hand, when adding up 0.1 a 100 times the result is slightly larger than 10 – this seemingly undeterministic behavior is a result of the roundtonearest operation applied after each addition. A trick can help to upperbound the accumulation error that arises in the DDA... The ECDDA Algorithm
EnhancementsSubPixel PrecisionParallelizationPerformance Evaluation
Conclusion
Copyright 2001–2013, Michael Noisternig
