Math Functions in VHDL

Michael Vandegriend, Gabriel Ricardo, Scott Medynski

 

Overview

The VHDL language contains only simple math operators such as addition, multiplication, and division. Although these operations may suffice for control logic, other activities, such as digital signal processing (DSP), require more complex mathematical functions, such as trigonometric functions and exponentials. None of these complex operations are built into VHDL, so the user must develop them from the basic arithmetic operations. These functions can be achieved by methods for approximating roots of equations and for representing functions as power series. Some of these methods are used in this VHDL math library.

The VHDL math library contains many different functions involving integer, real, and complex numbers. Real and integer functions include trigonometric functions (sin, acos, tanh, etc), exponential and logarithmic functions (exp, ln, log, square root, yx), random number generators, and other miscellaneous functions. The complex number functions handle both rectangular and polar forms and include the basic arithmetic operators (+, -, *, /), the square root exponential (exp(x)) functions, the complex conjugate function, conversion functions (polar to rect, rect to polar), and other miscellaneous functions.

 

Taylor Series

Taylor series' are used to represent functions as infinite polynomials. Given a function f(x), where f(x) is infinitely differentiable on an open interval I containing 0, the Taylor series for this function exists and is given by

f(x) = Sum [(fk(0)xk/(k!))]

for all real x, where fk(0) is the k-th derivative evaluated at zero, xk is the k-th power of x, and the sum is from k = 0 to infinity. Expanding this formula results in f(x) = f(0) + f'(0)x + f''(0)x2/(2!) + f'''(0)x3/(3!) + .....

Using this method, functions such as exp(x), cos(x), and sin(x) can be represented as the following power series.

exp(x) = Sum [xk/(k!)] = 1 + x + x2/(2!) + x3/(3!) + ......

cos(x) = Sum [(-1)kx2k/((2k)!)] = 1 - x2/(2!) + x4/(4!) - x6/(6!) + ....

sin(x) = Sum [(-1)kx2k+1/((2k+1)!)] = x - x3/(3!) + x5/(5!) - x7/(7!) + ....

 

There are two feasible methods for using a Taylor series to approximate a function. The first method involves truncating the series at a point where the desired accuracy can be obtained. Note that this method is more efficient the closer x is to 0. The second method involves using a recursive formula in which each recursion adds another element of the series. A recursive formula for exp(x) is given as

xn+1 = xn(x)/n + xn

where xn is the present value, xn+1 is the next value, and x1, the first value, is 1. The process is repeated until the difference between the next value and the current value is below a specified error threshold.

 

Newton-Raphson Method

The Newton-Raphson method is used to find a root of the equation f(x) = 0, where f must be differentiable on some interval containing the root, and f'(x) ¹ 0 in the interval. This method will only find one root of the function, so for a function with multiple roots, the interval must be chosen so that it contains the desired root only. The formula for the Newton-Raphson method is

xn+1 = xn - f(xn)/f'(xn)

where xn is the current estimate of x, and xn+1 is the next estimate of x. The process is finished when the difference between the next value and current value of x is below a desired error threshold. Note that the initial estimate, x1, must be chosen, and its value reflects the interval in which the desired root must be found.

This method can be used for calculating the square root of a value. The resulting formula for a square root is

xn+1 = xn/2 - x/(2xn).

 

Hardware Implementation

The above functions, and those provided in the library, utilize basic arithmetic operations involving real operands. Real numbers require floating point calculations, which Altera's Max+Plus II synthesis tools do not support. Code for a floating point adder and a floating point multiplier can be found at www.ics.uci.edu/pub/hlsynth/HLSynth95/HLSynth95/complete/.

For more information on designing floating point adders, multipliers, and such, check out the EE480 course notes, or "Digital System Design Using VHDL" by Charles Roth (1998), which has code for a floating point multiplier.

 

References

Etgen, Garret. Salas and Hille's Calculus: One Variable Early Transcendentals. Toronto: Wiley, 1996.