Main Content

Recommendations for Arithmetic and Scaling

Arithmetic Operations and Fixed-Point Scaling

The sections that follow describe the relationship between arithmetic operations and fixed-point scaling, and offer some basic recommendations that may be appropriate for your fixed-point design. For each arithmetic operation,

  • The general [Slope Bias] encoding scheme described in Scaling is used.

  • The scaling of the result is automatically selected based on the scaling of the two inputs. In other words, the scaling is inherited.

  • Scaling choices are based on

    • Minimizing the number of arithmetic operations of the result

    • Maximizing the precision of the result

    Additionally, binary-point-only scaling is presented as a special case of the general encoding scheme.

In embedded systems, the scaling of variables at the hardware interface (the ADC or DAC) is fixed. However for most other variables, the scaling is something you can choose to give the best design. When scaling fixed-point variables, it is important to remember that

  • Your scaling choices depend on the particular design you are simulating.

  • There is no best scaling approach. All choices have associated advantages and disadvantages. It is the goal of this section to expose these advantages and disadvantages to you.

Addition

Consider the addition of two real-world values:

Va=Vb+Vc.

These values are represented by the general [Slope Bias] encoding scheme described in Scaling:

Vi=Fi2EiQi+Bi.

In a fixed-point system, the addition of values results in finding the variable Qa:

Qa=FbFa2EbEaQb+FcFa2EcEaQc+Bb+BcBaFa2Ea.

This formula shows

  • In general, Qa is not computed through a simple addition of Qb and Qc.

  • In general, there are two multiplications of a constant and a variable, two additions, and some additional bit shifting.

Inherited Scaling for Speed

In the process of finding the scaling of the sum, one reasonable goal is to simplify the calculations. Simplifying the calculations should reduce the number of operations, thereby increasing execution speed. The following choices can help to minimize the number of arithmetic operations:

  • Set Ba = Bb + Bc. This eliminates one addition.

  • Set Fa = Fb or Fa = Fc. Either choice eliminates one of the two constant times variable multiplications.

The resulting formula is

Qa=2EbEaQb+FcFa2EcEaQc

or

Qa=FbFa2EbEaQb+2EcEaQc.

These equations appear to be equivalent. However, your choice of rounding and precision may make one choice stand out over the other. To further simplify matters, you could choose Ea = Ec or Ea = Eb. This will eliminate some bit shifting.

Inherited Scaling for Maximum Precision

In the process of finding the scaling of the sum, one reasonable goal is maximum precision. You can determine the maximum-precision scaling if the range of the variable is known. Maximize Precision shows that you can determine the range of a fixed-point operation from max(Va) and min(Va). For a summation, you can determine the range from

min(V˜a)=min(V˜b)+min(V˜c),max(V˜a)=max(V˜b)+max(V˜c).

You can now derive the maximum-precision slope:

Fa2Ea=max(V˜a)min(V˜a)2wsa1=Fa2Eb(2wsb1)+Fc2Ec(2wsc1)2wsa1.

In most cases the input and output word sizes are much greater than one, and the slope becomes

Fa2EaFb2Eb+wsbwsa+Fc2Ec+wscwsa,

which depends only on the size of the input and output words. The corresponding bias is

Ba=min(V˜a)Fa2Eamin(Qa).

The value of the bias depends on whether the inputs and output are signed or unsigned numbers.

If the inputs and output are all unsigned, then the minimum values for these variables are all zero and the bias reduces to a particularly simple form:

Ba=Bb+Bc.

If the inputs and the output are all signed, then the bias becomes

BaBb+Bc+Fb2Eb(2wsb1+2wsb1)+Fc2Ec(2wsc1+2wsc1),BaBb+Bc.

Binary-Point-Only Scaling

For binary-point-only scaling, finding Qa results in this simple expression:

Qa=2EbEaQb+2EcEaQc.

This scaling choice results in only one addition and some bit shifting. The avoidance of any multiplications is a big advantage of binary-point-only scaling.

Note

The subtraction of values produces results that are analogous to those produced by the addition of values.

Accumulation

The accumulation of values is closely associated with addition:

Va_new=Va_old+Vb.

Finding Qa_new involves one multiplication of a constant and a variable, two additions, and some bit shifting:

Qa_new=Qa_old+FbFa2EbEaQb+BbFa2Ea.

The important difference for fixed-point implementations is that the scaling of the output is identical to the scaling of the first input.

Binary-Point-Only Scaling

For binary-point-only scaling, finding Qa_new results in this simple expression:

Qa_new=Qa_old+2EbEaQb.

This scaling option only involves one addition and some bit shifting.

Note

The negative accumulation of values produces results that are analogous to those produced by the accumulation of values.

Multiplication

Consider the multiplication of two real-world values:

Va=VbVc.

These values are represented by the general [Slope Bias] encoding scheme described in Scaling:

Vi=Fi2EiQi+Bi.

In a fixed-point system, the multiplication of values results in finding the variable Qa:

Qa=FbFcFa2Eb+EcEaQbQc+FbBcFa2EbEaQb+FcBbFa2EcEaQc+BbBcBaFa2Ea.

This formula shows

  • In general, Qa is not computed through a simple multiplication of Qb and Qc.

  • In general, there is one multiplication of a constant and two variables, two multiplications of a constant and a variable, three additions, and some additional bit shifting.

Inherited Scaling for Speed

The number of arithmetic operations can be reduced with these choices:

  • Set Ba = BbBc. This eliminates one addition operation.

  • Set Fa = FbFc. This simplifies the triple multiplication—certainly the most difficult part of the equation to implement.

  • Set Ea = Eb + Ec. This eliminates some of the bit shifting.

The resulting formula is

Qa=QbQc+BcFc2EcQb+BbFb2EbQc.

Inherited Scaling for Maximum Precision

You can determine the maximum-precision scaling if the range of the variable is known. Maximize Precision shows that you can determine the range of a fixed-point operation from

max(V˜a)

and

min(V˜a).

For multiplication, you can determine the range from

min(V˜a)=min(VLL,VLH,VHL,VHH),max(V˜a)=max(VLL,VLH,VHL,VHH),

where

VLL=min(V˜b)min(V˜c),VLH=min(V˜b)max(V˜c),VHL=max(V˜b)min(V˜c),VHH=max(V˜b)max(V˜c).

Binary-Point-Only Scaling

For binary-point-only scaling, finding Qa results in this simple expression:

Qa=2Eb+EcEaQbQc.

Gain

Consider the multiplication of a constant and a variable

Va=KVb,

where K is a constant called the gain. Since Va results from the multiplication of a constant and a variable, finding Qa is a simplified version of the general fixed-point multiplication formula:

Qa=(KFb2EbFa2Ea)Qb+(KBbBaFa2Ea).

Note that the terms in the parentheses can be calculated offline. Therefore, there is only one multiplication of a constant and a variable and one addition.

To implement the above equation without changing it to a more complicated form, the constants need to be encoded using a binary-point-only format. For each of these constants, the range is the trivial case of only one value. Despite the trivial range, the binary point formulas for maximum precision are still valid. The maximum-precision representations are the most useful choices unless there is an overriding need to avoid any shifting. The encoding of the constants is

(KFb2EbFa2Ea)=2EXQX(KBbBaFa2Ea)=2EYQY

resulting in the formula

Qa=2EXQXQB+2EYQY.

Inherited Scaling for Speed

The number of arithmetic operations can be reduced with these choices:

  • Set Ba = KBb. This eliminates one constant term.

  • Set Fa = KFb and Ea = Eb. This sets the other constant term to unity.

    The resulting formula is simply

    Qa=Qb.

If the number of bits is different, then either handling potential overflows or performing sign extensions is the only possible operation involved.

Inherited Scaling for Maximum Precision

The scaling for maximum precision does not need to be different from the scaling for speed unless the output has fewer bits than the input. If this is the case, then saturation should be avoided by dividing the slope by 2 for each lost bit. This prevents saturation but causes rounding to occur.

Division

Division of values is an operation that should be avoided in fixed-point embedded systems, but it can occur in places. Therefore, consider the division of two real-world values:

Va=Vb/Vc.

These values are represented by the general [Slope Bias] encoding scheme described in Scaling:

Vi=Fi2EiQi+Bi.

In a fixed-point system, the division of values results in finding the variable Qa:

Qa=Fb2EbQb+BbFcFa2Ec+EaQc+BcFa2EaBaFa2Ea.

This formula shows

  • In general, Qa is not computed through a simple division of Qb by Qc.

  • In general, there are two multiplications of a constant and a variable, two additions, one division of a variable by a variable, one division of a constant by a variable, and some additional bit shifting.

Inherited Scaling for Speed

The number of arithmetic operations can be reduced with these choices:

  • Set Ba = 0. This eliminates one addition operation.

  • If Bc = 0, then set the fractional slope Fa = Fb/Fc. This eliminates one constant times variable multiplication.

The resulting formula is

Qa=QbQc2EbEcEa+(Bb/Fb)Qc2EcEa.

If Bc ≠ 0, then no clear recommendation can be made.

Inherited Scaling for Maximum Precision

You can determine the maximum-precision scaling if the range of the variable is known. Maximize Precision shows that you can determine the range of a fixed-point operation from

max(V˜a)

and

min(V˜a).

For division, you can determine the range from

min(V˜a)=min(VLL,VLH,VHL,VHH),max(V˜a)=max(VLL,VLH,VHL,VHH),

where for nonzero denominators

VLL=min(V˜b)/min(V˜c),VLH=min(V˜b)/max(V˜c),VHL=max(V˜b)/min(V˜c),VHH=max(V˜b)/max(V˜c).

Binary-Point-Only Scaling

For binary-point-only scaling, finding Qa results in this simple expression:

Qa=QbQc2EbEcEa.

Note

For the last two formulas involving Qa, a divide by zero and zero divided by zero are possible. In these cases, the hardware will give some default behavior but you must make sure that these default responses give meaningful results for the embedded system.

Summary

From the previous analysis of fixed-point variables scaled within the general [Slope Bias] encoding scheme, you can conclude

  • Addition, subtraction, multiplication, and division can be very involved unless certain choices are made for the biases and slopes.

  • Binary-point-only scaling guarantees simpler math, but generally sacrifices some precision.

Note that the previous formulas don't show the following:

  • Constants and variables are represented with a finite number of bits.

  • Variables are either signed or unsigned.

  • Rounding and overflow handling schemes. You must make these decisions before an actual fixed-point realization is achieved.

See Also