Sie sind auf Seite 1von 8

2015 IEEE 22nd Symposium on Computer Arithmetic

A general-purpose method for faithfully rounded


oating-point function approximation in FPGAs

David B. Thomas
Dept. of Electrical and Electronic Engineering
Imperial College London
dt10@imperial.com

AbstractA barrier to wide-spread use of Field Pro- segmentation and a xed-point polynomial evaluator
grammable Gate Arrays (FPGAs) has been the complexity of to provide faithful evaluation.
programming, but recent advances in High-Level Synthesis (HLS)
have made it possible for non-experts to easily create oating- A segmentation algorithm which reliably and auto-
point numerical accelerators from C-like code. However, HLS matically partitions the oating-point input domain of
users are limited to the set of numerical primitives provided by a function into segments which can be approximated
HLS vendors and designers of oating-point IP cores, and cannot with xed-point polynomials of low degree.
easily implement new fast or accurate numerical primitives.
This paper presents a method for automatically creating high- An evaluation of the architecture and segmentation
performance pipelined oating-point function approximations, algorithm over ten target functions, comparing post-
which can be integrated as IP cores into numerical accelerators, place and route area and performance in Virtex-6
whether derived from HLS or traditional design methods. Both against reference implementations.
input and output are oating-point, but internally the function
approximator uses xed-point polynomial segments, guaranteeing The C++ source code for the tool is freely available as part of
a faithfully rounded output. A robust and automated non- the FloPoCo [1] framework.
uniform segmentation scheme is used to segment any twice-
differentiable input function and produce platform-independent II. P ROBLEM S TATEMENT
VHDL. The approach is demonstrated across ten functions, which
are automatically generated then placed and routed in Xilinx We will work with the extended set of real numbers R,
devices. The method provides a 1.1x-3x improvement in area over which contains the positive and negative reals, as well as
composite numerical approximations, while providing similar signed zeros, signed innities, and NaN (not a number):
performance and signicantly better relative error.
R = {NaN, } {x : x R x < 0} { 0, +0}
Keywords. Function Approximation, Floating-Point, Faithful Round- {x : x R 0 < x} {+}
ing, FPGA.
The extended reals are given a total order, which denes
NaN < and 0 < +0.
I. I NTRODUCTION
A oating-point representation F R is dened using
FPGAs are now used to accelerate complex numerical three parameters: wf (F ), the number of explicit fractional bits;
algorithms, often using complex data-ow graphs of oating- we (F ), the number of exponent bits; and e (F ), the minimum
point operations. Existing work on oating-point function exponent. The number format F then consists of the special
approximations has looked for resource efcient methods of numbers (NaN, 0, ), plus regular numbers composed of
accurately evaluating common functions, such as the expo- sign, exponent, and signicand:
nential [7] and logarithm [8], but each method is specic to
a particular function. When FPGA designers are faced with F = {0, , NaN} {s 2e (1 + f )} where
a function which is not available as an optimised primitive, s {1, +1}
they are forced to use numerical approximations, which re- e Z e (F ) e e+ (F )
quire signicant area, have high latencies, and unknown error
properties. 2wf (F ) f Z 0 f < 1}

This paper presents a method for approximating any twice- Here the normalised signicand will be considered as (1+f )
differentiable function, including those with local minima and [1, 2) with f representing the explicit bits from [0, 1). This
maxima. The resulting tool is completely automated, and denition follows the FloPoCo numbers format [1] which is
produces faithfully rounded pipelined oating-point operators. often used in FPGAs, rather than IEEE; the main difference is
This allows designers to produce hardware operators for func- the lack of denormal numbers.
tions which have not yet been hand optimised, and to replace We also separate a oating-point representation into bi-
composite uni-variate functions containing multiple rounding- nades, where each binade contains all consecutive numbers
errors with a single faithfully rounded operator. The main with the same exponent, with one binade for each of the special
contributions of this paper are: numbers, and binades associated with each distinct exponent:

A generic hardware architecture for pipelined oating- x if x {NaN, , 0, +0, +}
bin(x) =
point function approximations, which uses non-linear (sign(x), expnt(x)) otherwise

1063-6889/15 $31.00 2015 IEEE 42


DOI 10.1109/ARITH.2015.27
        for segment i, mapping the input signicand to the
   
output signicand.
3) Attach an exponent and sign or special number ag,
 !" !"
producing y = ai (yf ).


 The resulting hardware architecture is shown in Figure 1.

 
 &''&'
Note that Step 2 only depends on i and xf , while Step 3


()

) only depends on i and yf , meaning that only the segment


 and the signicand are needed after Step 1. To achieve this
!"%!
"
we need to choose the segments such that the only thing that
varies within a given segment is the signicand, and that the
   
 
 

mapping yf = p(xf ) can be approximated with a polynomial
   

 ###
of a chosen degree. The process for dening these segments

will rst be described, then the approximation architecture



given a set of segments is shown.
 


 
$%
  IV. S EGMENTATION A LGORITHM
    We partition the input domain FD into n segments S1 ..Sn ,
        where each segment is a contiguous subset of FD and can be
identied by its lower-bound Si and upper-bound Si :
Fig. 1. Overview of the function approximation architecture.
Si = {x : x FD Si x Si }
with the segments forming a dis-joint covering of the input
There is a total order on the binades, following the order on
domain. We will use the notation {a...b} to represent a segment
the contained oating-point numbers.
with {a...b} = a and {a...b} = b.
The function approximation problem requires a target
Out approach uses uses the following steps, each of which
function ft : R  R dened by the user, and produces
ensures stricter properties on the segmentation.
an approximation fa : FD  FR over some oating-point
domain FD and range FR . Faithful rounding requires: 1) Make all segments at (contained within a single
x FD : fa (x) {upFR (ft (x)), downFR (ft (x))} binade) in their domain.
2) Make all segments monotonically increasing, de-
where upF (x) returns x or the next representable number in F creasing, or constant in the segment image.
less than x, and downF (x) returns x or the next representable 3) Make all segments at in their image.
number in F greater than x. At the boundaries of representable 4) Split segments until they can be approximated by a
numbers, one or both of the numbers may be a special value, polynomial of degree at most d.
such as or Nan. The function ft must be a total function 5) Split segments until they can be faithfully approxi-
over the input domain, and fa will also be a total function. If mated by a xed-point polynomial of degree at most
the target function is only dened or needed over a subset d.
of the domain, the user can maps other regions to NaN.
The function must be twice-differentiable, and singularities are Each stage may result in one or more of the current segments
allowed at special numbers (zeros and innities), as long as being split, so the total segment count will never decrease.
the function is well-behaved for regular inputs. Certain target functions will cause more growth in different
stages for example, periodic and multi-modal functions such
The rst goal of this work is that the approximation as sin(.) will experience growth in stage 2, many functions will
should be automated and reliable, so a non-expert user should grow in stage 4, while singularities such as 1/x may experience
have condence that an approximation will be produced in some growth in stage 5. The intent and execution of each stage
a reasonable amount of time. The second goal is that the will now be described.
approximation should be faithful and complete, over the entire
input domain, so users should not need to understand or A. Flatten segment domains
describe accuracy trade-offs.
The starting point is the single segment NaN ..+, which
III. OVERALL A PPROACH includes every binade in the domain. In this stage the single
segment is replaced with one segment for each binade:
The approach suggested here is to partition the input
domain x FD into n segments S1 ..Sn at compile time, then S1 = {{NaN}, {}, {0}, {+0}, {+}}
split the run-time calculation of y = fa (x) into three stages:     
2e 2 2wf (FD ) ... 2e : e e (FD )..e+ (FD )
1) Use a segmentation function s(.) to map the input    
x FD to an integer i = s(x), where i 1..n 2e ...2e 2 2wf (FD ) : e e (FD )..e+ (FD )
identies which of n segments contains the input.
2) Evaluate a xed-point degree-d polynomial yf = Ensuring that each segment only covers one binade of the
p(xf , ci ), where c1 ..cn each contain d+1 coefcients domain means that once an input has been assigned to a

43
segment, only the signicand of the input is needed for C. Flatten Segment Images
further processing. In principle, this means that there will be
5 + 2we (FD )+1 segments after this stage. The segments are now known to be contained within one
binade, and the segment images are monotonic. This allows
For practical purposes, this stage is also used to reect the segments to be safely split by Algorithm 2, which simply
explicit directions from the user about what input interval is walks across each segment domain, and splits it at any points
interesting to them. For example, if a user species that only where the segment image crosses a binade boundary in the
non-negative inputs will be used, the target function is modied range.
to return NaN for all negative segments. These can all then be
optimised into one large segment that returns NaN. Algorithm 2 FlattenImages
1: S3
B. Ensure Monotonicity 2: for S S2 do
3: while bin( S ) = bin( S) do
Flattening the domain ensures the input doesnt cross 4: r max{x : x S bin( S ) = bin(ft (x))}
binade boundaries, and if the output also doesnt cross bi- 5: S3 S3 {{ S ...r}}
nade boundaries then a signicand-to-signicand xed-point 6: S {x : x S r < x}
polynomial can be used to approximate the segment function. 7: end while
However, as arbitrary (twice-differentiable) input functions are 8: S3 S3 {S}
allowed, the segment output image must rst be restricted to 9: end for
monotonically increasing or decreasing ranges. This will allow
the next stage to reliably determine where the segment image
intersects the range binade boundaries. This algorithm doesnt depend on whether the segment
image is increasing or decreasing, nor whether there are
Segments covering special (NaN etc.) inputs will have discontinuities in the initial segment images. Even if there
constant outputs, so only segments covering regular inputs are no discontinuities in ft , the discreteness of FD and FR
need to be considered. The algorithm which transforms the may mean that the output jumps by more than one output
at segments S1 to the monotonic segments S2 is shown in binade, so there is no requirement that adjacent segments have
Algorithm 1. This assumes the existence of a function: contiguous range binades. The maximum on line 4 can be
implemented using bisection search over the segment input,
roots : (R  R) R R  P(R) taking O(wf (FD )) time, so is fast even for large signicand
widths.
which returns a set of the locations of all real roots within a
given interval.
D. Split to real-coefcient polynomials
Algorithm 1 EnsureMonotonicity
The segments in S3 are now at in both the domain and
1: S2
range, so all inputs for a segment are in the same domain
2: for S S1 do
binade, and all outputs are in the same range binade. This
3: if special(S) then allows the problem to be recast from a oating-point ap-
4: S2 S2 {S} proximation problem to a xed-point approximation problem.
5: else Special segments in the domain map to constants, and any
6: Z roots(ft , S , S) special segments in the range are special (constant) for the
7: for z Z do entire segment, so polynomial approximations are only needed
8: zd downFD (z) {Round down to zd FD } for regular input and output binades.
9: if zd S then
10: S2 S2 {{ S ...zd }} Given a segment S, we can dene a function fS which
11: S {x : x S x > zd } {May be empty} directly maps an input signicand to an output signicand.
12: end if
13: end for fS : [0, 1)  [0, 1)
14: if S = then {No roots, or left-overs} fS (x) = sr 2er ft (sd 2ed (1 + x)) 1 where
15: S2 S2 {S}
(sd , ed ) = bin( S ) (sr , er ) = bin(ft ( S ))
16: end if
17: end if The transform function is not necessarily unique to a given
18: end for
segment, and will be shared by any segments with the same
input and output binades.
This approach is overly conservative, as it will split
Given the transformed function, the segments will be split
segments which contain a minima or maxima, but are still
as necessary until all segments can be faithfully approximated
completely contained within one output binade. For example,
via a polynomial of degree d or less. This relies on a polyno-
if we choose ft (x) = 10 sin(x)/9, it will correctly split the
mial approximation function:
segment {0.5...1} at /4, as the image crosses between two
binades in the range. However, with ft (x) = 9 sin(x)/10
approx : (R  R) R R N  R R
the output maximum at /4 occurs entirely within one range
binade, so splitting the segment is not strictly necessary. approx(f, a, b, d) (c, )

44
The input consists of: a function f , approximation interval This ensures the same properties on  and c as approx, but
[a, b], and maximum degree d; and returns a tuple of coef- also ensures that coefcient ci is represented as a xed-point
cients c, plus the maximum error . In an abuse of notation, number with li fractional bits:
the coefcients are an innite set from c0 , c1 , ..., c , and the
i 0 : 2li ci Z
function ensures that:
 

  Choosing the LSBs for each coefcient requires some sort
 i 
 f (x) c i x  i > d : ci = 0 (1) of heuristic, and this work appeals to that of [2]. They propose
 
i=0 ,x[a,b] that for an input interval [0, 1) split into k equal ranges, and
a target output LSB p, the coefcients should have LSBs li =
Exactly what approximation is performed is left unspecied
2p+ik . In our case some segments will extend over the entire
a minimax approximation will guarantee the smallest , but
[0, 1) range, so k = 1. Another empirical heuristic is used to
for practical purposes it is sometimes useful to fall back on
encourage speed and reliability of approximation, so an extra
a more robust method which converges. The approximation
bit is added, resulting in the approximation:
algorithm (both theoretical and the implementation described
later) only requires that the bound is correct, and is entirely li = 2w(FR )+i1 (2)
content if approx(.) only returns linear approximations.
The nal stage uses the segments S4 from the previous
The approx(.) function is used by Algorithm 3 to adap-
stage, and produces a nal set of domain segments, but now
tively split the segments until all segments can be faithfully
augmented with the xed-point polynomial and the error. This
rounded. Currently a recursive binary splitting approach is
set of augmented segments contains all the information needed
used, which is not guaranteed to provide an optimal split, but
to build the concrete hardware architecture. The process is
which is simple and reliable.
shown in Algorithm 4.
Algorithm 3 SplitPolynomials Algorithm 4 SplitConcrete
1: T S3
1: l (2w(FR )1 , 2w(FR ) , 2w(FR )+1 , ...)
2: S4
2: T S4
3: for S T do
3: S5
4: T T/S
4: for S T do {While segments left to process}
5: (poly, err) approx(fS , S , S, d)
5: T T/S
6: if err 2w(FR )3 then 6: i0
7: S4 S4 {S} 7: while S = i d do {Find lowest degree poly}
8: else 8: (poly, err) xapprox(fS , S , S, d, l)
9: m downFD (( S + S)/2)
9: if err 2w(FR )2 then
10: T T {{ S ...m}}
10: S5 S5 {(S, poly, err)}
11: T T {{upFD (m)... S}}
11: S
12: end if
12: else
13: end for
13: ii+1
14: end if
The temporary set T can be viewed as a list or queue, 15: end while
and in practise the splitting at line 9 is well handled using 16: if S = then {Segment needs further splitting}
recursion. There can be at most wf (FD ) levels of recursion, 17: m downFD (( S + S)/2)
so stack overow is not a danger. 18: T T {{ S ..m}}
The splitting condition on line 6 is deliberately conserva- 19: T T {{upFD (m).. S}}
tive, and requires a polynomial that is more than faithful. This 20: end if
is an empirical tradeoff between the time for (potentially sub- 21: end for
optimal) approximation over the reals, and the more expensive
approximation over xed-point numbers in the next stage. It The while loop starting at line 7 is used to ensure that
results in more segments than are strictly necessary, but results the lowest degree feasible polynomial is chosen, which has
in a better (faster) user experience. two purposes. First, it is used to decrease complexity for the
xapprox function, by not asking it to approximate very sim-
ple segments with high-degree polynomials. Highly linear seg-
E. Split to concrete polynomials ments are very common, for example in sin(x) close to zero,
The nal stage in the approximation algorithm is to en- which can cause stability problems in minimax algorithms.
sure that each segment can be faithfully approximated by Second, it avoids the situation where xapprox can produce
a polynomial with xed-point coefcients. This requires a a high-degree approximation to a low-degree segment, but
more complex function xapprox which provides the same does so by producing exquisitely balanced polynomials. Such
behaviour as approx, but takes an extra parameter l dening polynomials may result in very large high-order coefcients,
the least-signicant-bit of each coefcient: drastically increasing the cost of the polynomial evaluator and

coefcient tables.
xapprox : (R  R) R R N Z  R R
The extra splitting step at line 16 is another step for reliabil-
xapprox(f, a, b, d, l) (c, ) ity and usage. In principle the polynomials are already accurate

45
$0 $ Algorithm 5 SearchSegments
-  *## . i1 lessp(x, b1 [0])
i2 2i1 + lessp(x, b2 [i1 ])
. i3 2i2 + lessp(x, b3 [i2 ])
 )
...

+, ik 2ik1 + lessp(x, bk [ik1 ])

+, 
*+ ,
*+ ,
of 2j1 boundary points from a table bj . The boundary points
-+, -+, are extracted from the segment boundaries as:
bj [i] = S2kj (2i+1) , 1 j k, 0 i 2j1
 
 
*## .

$##/0 $ 1/0 $ The total number of entries in the boundary tables grows
linearly with the number of segments, while the number of
boundary tables grows logarithmically.
Fig. 2. Hardware implementation of the segmenter for increasing numbers
of index bits, using LUTs for small j and block-RAMs for larger j. In hardware each stage is implemented using a table look-
up and comparison, with the area and delay cost increasing for
later stages. The comparison function lessp(., .) in Algorithm 5
enough, but in practise xapprox may not be able to nd a is derived from the ordering over the input domain FD , and
solution, either directly reporting an error, or taking excessively provides the new bit at each stage:
long. In such cases it is better to split the segment again, 
resulting in an extra segment and sub-optimal polynomials, 1, if x y
lessp(x, y) =
but guaranteeing completion. 0, otherwise
In a hardware this total order can be implemented using a case
V. H ARDWARE A RCHITECTURE statement to order the two numbers according to the segment
type, then an integer comparison of the exponent and fraction
The output of the segmentation algorithm is a set of tuples elds to resolve ordering within segment types.
S5 = {S1 , S2 , ..., Sn }, where n depends on the approximation
range, the input and output types, and the properties of the Figure 2 shows the three types of stage used in the current
target function. Each segment is a tuple (S, p, ), containing: implementation. For j = 1 the boundary constant can be
combined with the comparator, requiring a single level of
S: the segment input domain, which is guaranteed not LUTs, and for 1 < j 7 it is feasible to store the boundaries
to overlap any other segment input domain. in a LUT-based ROM in contemporary architectures, allowing
a delay of one cycle per stage. For j > 7 the tables become
p: polynomial coefcients of (at most) degree d,
large enough to that they must be stored in block RAM.
rounded to the LSBs given in Equation 2;
: the maximum absolute error between the polyno- B. Polynomial Evaluation
mial output and the target function, expressed in terms The concrete segments describe n polynomials with xed-
of the output signicand (i.e.  2wf (FR ) means it point coefcients, all of which map from [0, 1) with wf (FD )
is within one ULP). fractional bits to [0, 1) with wf (FR ) fractional bits. All poly-
These segments can now be used to build the hardware nomials share the same fractional precision for the coefcients,
architecture described earlier in Section III. There are only but both the range of the coefcients and the approximation
three components: segmenter; table-lookup; and polynomial error will vary for each segment.
evaluator. Table-lookup is straightforward, but design decisions The polynomial evaluator must guarantee faithful evalua-
still need to be made in the segmentation and polynomial tion across all polynomial segments, while trying to minimimse
evaluation stage. pipeline depth and DSP+logic consumption. The approach
used here follows a much simplied form of [2], which
A. Segmentation separates evaluation error into approximation, evaluation, and
rounding error:
Given an input x FD , the segmenter needs to nd the
unique i 1..n such that Si i Si . Because the 2wf (FR ) approx + eval + round
segments form a complete partition of FD , such a segment The maximum approximation error across all polynomials is:
is guaranteed to exist, even if the segment just returns NaN.
There is no special structure to the partition apart from the total approx = max (1 ..n ) 2wf (FR )2 (3)
ordering over the segments, so it takes k = log2 n steps to
The upper bound of 2wF (FR )2 on approx is guaranteed
locate the correct segment using binary search.
by line 9 of Algorithm 4, while the nal rounding error is
As the target is a pipelined hardware architecture, the 2wF (FR )1 , which gives a bound on evaluation error of:
binary search is unrolled into k stages, each of which recovers eval 2wf (FR ) 2 2wf (FR )2 = 2wf (FR )1
one bit of i, shown in Algorithm 5. At the start of each stage
j there are j 1 known bits of i, which are used to select one

46
Knowing the error budget and the range of the coefcients, in- TABLE I. S ELECTED TEST FUNCTIONS .
termediate rounding can be selected. A rather simple approach Class Name Function Interval
is chosen, which is to choose a number of guard-bits g, and to log log(x) [0, ]
round each polynomial stage except the nal to wf FR ig P
exp exp(x) [, ]
LSBs. So given the polynomial coefcients c0 ..cd , and an input normpdf exp(x2 /2)/ 2 [16, +16]
x [0, 1), the approximation is: sigmoid 1/(1 + exp(x)) [, +]
C
log1p log(x + 1) [1, +]
a d = cd (4) exp(x) 1 [, +]
  expm1
ai = round ci + x aa+1 , 2wF (FR )ig (5) sin sin(x) [, ]
  cos cos(x) [, ]
a0 = round c0 + x a1 , 2wF (FR ) (6) A erf erf(x) [32, +32]
sintan sin(tan(x)) [2, 2]
where round (x, r) = r x/r + 1/2 (7)
cos
1.E+00 erf
The guard bits are progressively increased from g = 1 up, and
1.E-01 expm1
the rst g giving faithful rounding according to the approach log1p

Max Rel. Error


in Section II.D of [2] is chosen. All sources of rounding error 1.E-02 normpdf
are considered in this evaluation process, so if approx can be 1.E-03
accurately evaluated (using a tool such as Sollya), then the 1.E-04
xed-point segment will be faithfully rounded. 1.E-05
1.E-06
For segments which cover the rst or last value within a 1.E-07
binade there is a small chance that the faithfully rounded output 1.E-08
will underow or overow, so we expect a rounded value a0 1.E-09
[0, 1), but the xed-point evaluator returns a0 < 0 or a0 1. 2^-28 2^-24 2^-20 2^-16 2^-12 2^-8 2^-4 2^0 2^4
Due to the image attening, we know that a correctly rounded Binade
output must exist within the target binade, so simply saturating
the polynomial to the range [0, 1) is sufcient to ensure correct Fig. 3. Maximum relative error of composite references within positive
rounding. These corner cases can automatically be tested by binades for composite functions (generic approximations not shown, as they
ensuring that the endpoints of domain segments are included are all faithful).
in any test input sequences.
based on the authors experience building such systems. For
VI. E VALUATION
comparison purposes, the composite functions were imple-
The segmentation algorithm and hardware architecture mented by chaining together existing FloPoCo operators, using
have been implemented as a single combined C++ program the built-in critical path management to maintain performance.
within the FloPoCo [1] framework, which takes an approxi-
For the approximate functions, a comparison point was pro-
mation problem as input, and produces pipelined VHDL and
vided by using existing published numerical approximations.
a test-bench. The whole process is automated, and requires no
This follows the process typically used by FPGA designers
manual assistance. The Sollya [3] library is used for expression
when faced with a function with no available hardware prim-
parsing throughout, and is also used to provide approx(.) using
itive search the literature for an approximation which: a)
the remez and uncertifiedInfNorm functions, plus
contains no loops; and b) only relies on functions for which
xapprox(.) using fpminimax and infNorm. MPFR [4]
they do have hardware primitives. This paper uses approxima-
is used to provide all high-precision correctly rounded calcu-
tions given in Abramowitz and Stegun [5], which is a well-
lations, both indirectly via Sollya, and directly in many places.
known source of approximations.1 The formulas and operation
To evaluate the performance of the function approximators, counts are given in Table II, which were implemented using
three types of functions are considered: FloPoCo, again using critical path management, and also
taking advantage of constant multipliers and squarers where
1) Primitive functions with existing hand-optimised im- possible. To be clear, we do not claim the A&S approximations
plementations. are the most accurate or most efcient, but they are typical of
2) Composite functions which can be implemented in the solutions used by hardware engineers, who do not know
terms of existing hand-optimised primitives. about minimax tools or range-reduction methods.
3) Approximated functions which cannot be expressed
in terms of existing primitives, and must be approx- One motivation for this work is that faithful rounding is
imated using polynomials or other techniques. important, as it ensures relative error bounds. To demonstrate
how bad the relative error of the composite reference imple-
The reference library of primitive functions is taken to be mentations can get, Figure 3 shows the maximum relative
those provided by FloPoCo, as a well-respected source of high- error of ve of the functions within each positive input binade.
performance oating-point cores for FPGAs. As expected, the relative error of expm1 and log1p degrades
The list of functions tested is given in Table I, along 1 The target audience of this paper knows that there are much better ways of
with the function denitions, and an approximation interval. generating approximations, as does the author. However, the target audience
The restricted approximation intervals for certain operators are of the tool just want to get things done; in the absence of an off-the-shelf IP
intended to reect typical usage in a hardware application, core, an A&S approximation is a solution, while a book or paper is not.

47
TABLE II. N UMERICAL APPROXIMATIONS USED FOR FUNCTIONS . plementations, both in terms of resources, and performance.
Function A&S xC x 2
xy x+C 1/x exp(x) Numbers in brackets measure the generic approximations
sin 4.3.97 1 1 5 5 relative to the reference, and bolded numbers indicate the best
cos 4.3.99 1 1 4 5 implementation for each category. For the rst two primitive
erf 7.1.26 2 5 7 1 1
comparisons (log and exp) the generic approximations are
clearly worse on every metric, but this is expected as the
reference primitives take advantage of range-reductions, and
dramatically as the input approaches zero, with erf following
have been heavily optimised. However, the approximation for
a similar degradation. In contrast, cos shows poor error for
log with d = 3 is within a factor of 3 of the primitive in all
large inputs, due to naive range reduction for inputs in the
metrics except for DSPs.
range [/2, ], while normpdf loses accuracy due to rounding
when squaring the input. The generic methods proposed here For the next four comparisons (expm1, log1p, sigmoid,
are not shown, as they are simply staight lines showing faithful normpdf) the generic approximations are a little closer to
rounding. the composite references, and for the sigmoid actually beat
the composite in terms of LUT and FF resources due to the
VHDL implementations were generated for each target,
logic-based divider. However, the generic approximation is
using the generic approximation method for d = 3 and
producing faithfully rounded results, while all of the composite
d = 4, and the reference method (primitive, composite, or
functions have known regions of relative inaccuracy.
approximate). The target platform was set as Virtex-6 with
a target of 400MHz for all generated functions. The generic The last group of comparisons (erf, cos, sin, tansin) are
implementations were then all tested to ensure they were all the main target for this work: functions which have no direct
faithful. Exhaustive testing has been applied to all operators composite implementation, and currently rely on an engineer to
for half-precision we (FD ) = we (FR ) = 5, wf (FD ) = nd some numerical approximation. Now the approximations
wf (FR ) = 10, with no errors observed. Single precision are better on every metric except for block-RAMs the
operators have been tested using: 1000 points evenly spaced numerical approximations rely heavily on polynomials, and
within each input binade; 100000 points evenly spaced over the so use little RAM. erf in particular shows substantial savings,
input interval; 100 points evenly spaced within each domain requiring very few resources; apart from the latency, it requires
segment, including S and S. resources similar to the hand-optimised implementation of log.
This testing is not intended to check whether the con-
struction process is correct, it is only to check for coding VII. R ELATED W ORK
or synthesis errors in the concrete tools. The process as
given should be faithful by construction, as all oating-point Function approximation has been an active topic of re-
calculations during construction are performed using either search for many years, both for software and hardware. The
MPFR [4] with correct rounding, or Sollya [3], which uses central concepts of range-reduction, approximation, and recon-
interval arithmetic to control evaluation error. This includes struction are well established [6], especially for oating-point
all calculations related to segmentation, including root nding inputs in both software and hardware. This section will focus
and function minimisation, and the bounding of the maxi- on work specically related to pipelined implementations of
mum approximation across all polynomials. This results in a function approximation for use in FPGAs.
conservative upper bound on the maximum polynomial error Most work has focussed on approximating known common
approx in Equation 3, which is then taken into account while oating-point functions, such as the exponential [7] and log-
determining the number of guard bits needed in Equation 7 arithm [8]. These use signicant function-specic knowledge,
for faithful rounding. both in the range-reduction and approximation stages, and are
important building blocks both for manual FPGA design, and
A number of heuristics are introduced in the process
for HLS derived designs.
desribed in this paper, such as the deliberate over-segmentation
in Section IV-D and Section IV-E, but these are only aimed at General purpose function approximations for hardware are
achieving good compile-time performance and reliability - the less commonly used in FPGAs, and is mostly used in the
result should always be faithful. As a tool for FPGA users, the xed-point domain. One example is the approach used in [9],
emphasis is taking zero designer effort and a small amount of which uses a similar approach to that used here, using a non-
tool/compiler time, rather than an extremely optimal process uniform segmentation of the xed-point input to select faith-
which may take many hours and need manual tweaking. The fully rounded polynomial segments. The segmentation is more
worst-case would be failing with an error message that remez restricted than the approach used here, allowing for a much
failed to converge, or function does not oscillate, as the user more efcient mapping of input to segment, but restricting the
would not be able to take any meaningful action. ability to place polynomials exactly where needed.
All implementations were synthesised, placed, and routed, Another general purpose xed-point function evaluator is
using Xilinx ISE 14.3. The target device was a Virtex-6 provided by FloPoCo [2], which uses a uniform segmentation
xc6vcx130t-ff1156-2 part. Re-timing was enabled during syn- of the input to make segment selection a simple table-lookup.
thesis, and gobal optimisation during mapping, but apart from This approach is unsuitable as a general-purpose function
that the defaults were used, and the same toolchain settings approximation, but is ideal for the relatively smooth functions
were used for both generic approximations and reference found within a range-reduced approximation problem.
components.
Altera have produced a large number of oating-point func-
Table III summarises the resulting single-precision im- tions for their OpenCL HLS tool, covering all the functions

48
Function Method LUTs FFs DSPs BRAMs Cycles MHz Lat. (ns)
d=3 1372 (3.37) 1754 (4.34) 9 (9) 24 (24) 36 (3.6) 166 (0.63) 216 (5.68)
exp d=4 1882 (4.62) 2456 (6.08) 15 (15) 15 (15) 42 (4.2) 188 (0.71) 224 (5.89)
Prim 407 - 404 - 1 - 1 - 10 - 264 - 38 -
d=3 1376 (1.72) 1762 (2.95) 10 (2) 12 (12) 34 (2.62) 188 (1.22) 181 (2.15)
log d=4 1618 (2.03) 2068 (3.46) 13 (2.6) 7 (7) 40 (3.08) 200 (1.3) 200 (2.38)
Prim 798 - 597 - 5 - 1 - 13 - 154 - 84 -
d=3 1304 (1.79) 1718 (2.23) 9 (9) 12 (12) 35 (1.94) 202 (0.78) 173 (2.47)
expm1 d=4 1876 (2.57) 2419 (3.14) 15 (15) 7 (7) 41 (2.28) 134 (0.52) 306 (4.37)
Comp 730 - 770 - 1 - 1 - 18 - 259 - 70 -
d=3 2203 (1.92) 3047 (3.14) 17 (3.4) 10 (10) 36 (1.89) 168 (1.1) 214 (1.73)
log1p d=4 4020 (3.5) 4974 (5.12) 28 (5.6) 15 (15) 48 (2.53) 207 (1.36) 232 (1.87)
Comp 1148 - 971 - 5 - 1 - 19 - 153 - 124 -
d=3 1259 (0.81) 1607 (0.89) 9 (9) 5 (5) 34 (1) 206 (0.9) 165 (1.11)
sigmoid d=4 1559 (1.01) 1984 (1.09) 12 (12) 6 (6) 40 (1.18) 198 (0.87) 202 (1.36)
Comp 1548 - 1814 - 1 - 1 - 34 - 228 - 149 -
d=3 1498 (1.77) 1966 (3.12) 11 (2.75) 25 (25) 36 (2) 190 (0.9) 190 (2.21)
normpdf d=4 1968 (2.33) 2513 (3.98) 15 (3.75) 16 (16) 42 (2.33) 178 (0.85) 236 (2.74)
Comp 846 - 631 - 4 - 1 - 18 - 210 - 86 -
d=3 881 (0.19) 1064 (0.26) 6 (0.55) 4 (4) 31 (0.44) 208 (1.1) 149 (0.4)
erf d=4 1001 (0.22) 1289 (0.32) 8 (0.73) 5 (5) 37 (0.53) 213 (1.12) 174 (0.47)
Approx 4627 - 4062 - 11 - 1 - 70 - 189 - 370 -
d=3 1220 (0.47) 1561 (0.75) 9 (0.82) 5 () 32 (0.68) 160 (0.87) 200 (0.78)
cos d=4 1498 (0.57) 1889 (0.91) 12 (1.09) 5 () 39 (0.83) 188 (1.03) 207 (0.81)
Approx 2608 - 2087 - 11 - 0 - 47 - 184 - 256 -
d=3 1366 (0.52) 1690 (0.79) 10 (0.77) 5 () 33 (0.69) 174 (0.9) 189 (0.76)
sin d=4 1545 (0.58) 1914 (0.89) 12 (0.92) 6 () 39 (0.81) 203 (1.05) 192 (0.77)
Approx 2648 - 2142 - 13 - 0 - 48 - 193 - 248 -
d=3 855 - 1016 - 6 - 3 - 30 - 178 - 169 -
tansin
d=4 991 - 1266 - 8 - 4 - 36 - 186 - 193 -
TABLE III. P OST- PLACE AND ROUTE RESULTS FOR SINGLE - PRECISION OPERATORS IN V IRTEX -6 .

required by the OpenCL spec. Their approach is to build R EFERENCES


a exible set of tools and architectures, such as optimised [1] F. de Dinechin and B. Pasca, Designing custom arithmetic data paths
oating-point polynomial evaluators [10]. This allows them with opoco, Design Test of Computers, IEEE, vol. 28, no. 4, pp.
to quickly construct robust oating-point approximations, but 1827, July 2011.
appears to be, at least partially, a manual process. [2] F. de Dinechin, M. Joldes, and B. Pasca, Automatic generation
of polynomial-based hardware architectures for function evaluation,
The closest approach to this work in terms of goals in Application-specic Systems Architectures and Processors (ASAP),
appears to be [11], which takes a similar approach to non- 2010 21st IEEE International Conference on, July 2010, pp. 216222.
linear segmentation, followed by xed-point approximation. [3] S. Chevillard, M. Joldes, and C. Lauter, Sollya: An environment for
They also target automated implementation, and use a similar the development of numerical codes, in Mathematical Software - ICMS
approach to the binade approach used here to segment the input 2010, ser. Lecture Notes in Computer Science, K. Fukuda, J. van der
Hoeven, M. Joswig, and N. Takayama, Eds., vol. 6327. Heidelberg,
range. However, they use a segmentation scheme that is more Germany: Springer, September 2010, pp. 2831.
structured, limiting the ability to place segments, and cannot [4] L. Fousse, G. Hanrot, V. Lef`evre, P. Pelissier, and P. Zimmermann,
handle multi-modal functions. There is also an emphasis on MPFR: A multiple-precision binary oating-point library with correct
efciency over accuracy, with relatively low accuracies for rounding, ACM Trans. Math. Softw., vol. 33, no. 2, Jun. 2007.
single-precision approximations. [Online]. Available: http://doi.acm.org/10.1145/1236463.1236468
[5] M. Abramowitz and I. A. Stegun, Handbook of Mathematical Functions.
VIII. C ONCLUSION Dover Publications, 1965.
[6] J.-M. Muller, Elementary Functions: Algorithms and Implementation.
This paper presents an algorithm and architecture for Secaucus, NJ, USA: Birkhauser Boston, Inc., 1997.
approximation of oating-point functions in FPGAs. The al- [7] F. de Dinechin, P. Echeverra, M. Lopez-Vallejo, and
gorithm is designed to reliably produce a faithfully rounded B. Pasca, Floating-point exponentiation units for recongurable
approximation for any twice-differentiable function, while computing, ACM Trans. Recongurable Technol. Syst., vol. 6,
the architecture is designed to allow for high throughput no. 1, pp. 4:14:15, May 2013. [Online]. Available:
http://doi.acm.org/10.1145/2457443.2457447
pipelined implementations. A fully automated open-source
[8] N. Alachiotis and A. Stamatakis, Efcient oating-point logarithm
implementation of the approach has been created, suitable for unit for fpgas, in Parallel Distributed Processing, Workshops and Phd
use by hardware designers with little knowledge of function Forum (IPDPSW), 2010 IEEE International Symposium on, April 2010,
approximation. pp. 18.
Experiments in Virtex-6 using ten target functions show [9] D.-U. Lee, R. Cheung, W. Luk, and J. Villasenor, Hierarchical segmen-
tation for hardware function evaluation, Very Large Scale Integration
that while the approximations cannot compete with hand- (VLSI) Systems, IEEE Transactions on, vol. 17, no. 1, pp. 103116, Jan
optimised primitives (as expected), for functions composed of 2009.
multiple primitives it is able to provide guaranteed faithful [10] M. Langhammer and B. Pasca, Efcient oating-point polynomial
outputs with a 2x-3x cost in resources. However, the real evaluation on fpgas, in Field Programmable Logic and Applications
strength of the method is in approximating functions which (FPL), 2013 23rd International Conference on, Sept 2013, pp. 16.
cannot be decomposed into primitives, where it shows a 1.2x- [11] A synthesis method of general oating-point arithmetic units by
3x improvement in resource usage over numerical approxima- aligned partition, in Int. Cong. CSCC, 2008, pp. 11771180.
tions, while providing guaranteed accuracy.

49

Das könnte Ihnen auch gefallen