Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- TMS320C2x DSP - floating-point numbers are often represented on microprocessors in http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
      Floating-point numbers are often represented on microprocessors in
      a two-word format of mantissa and exponent. The mantissa is stored
      in one word. The exponent, the second word, indicates the number of
      bit positions from the left the decimal point is loacated. If the
      mantissa is 16 bits, a 4-bit exponent is sufficient to express the
      location of the decimal point. Because of its 16-bit word size, a
      16/4-bit floating-point format functions efficiently on the
      TMS320C2x. The theory and implementatation of floating-point
      arithmetic has been presented in an application report in the book,
      "Digital Signal Processing Applications with the TMS320 Family".

      Operations in the TMS320C2x central ALU are performed in two's-
      complement fixed-point notation. To implement floating-point
      arithmetic, operands must be converted to fixed-point for arithmetic
      operations, and then converted back to floating-point.

      Conversion to floating-point notation is performed by normalizing
      the input data (i.e., shifting the MSB of the data word into the MSB
      of the internal memory word). The exponent word then indicates how
      many shifts are required. To multiply two floating-point numbers,
      the mantissas are multiplied and the exponents added. The resulting
      mantissa must be renormalized. Since the input operands are
      normalized, no more than one left shift is required to normalize the
      result.

      Floating-point addition or subtraction requires shifting the
      mantissa so that the exponents of the two operands are equal. The
      difference between the exponents is used to left-shift the lower
      power operand before adding; then, the output of the add must be
      normalized.

      TMS320C2x instructions useful in floating-point operations are the
      NORM, LACT, ADDT, and SUBT instructions. NORM may be used to convert
      fixed-point numbers to floating-point. LACT may be used to convert
      back to fixed-point numbers. Addition and subtraction can be
      computed in floating-point using ADDT or SUBT.

      In the following examples, the mantissas are assumed to be in the
      Q15 format. Q15, one of the various types of Q format, is a number
      representation commonly used when performing operations on non-
      integer numbers. In Q format, Q<N>, <N> denotes how many digits are
      located to the right of the binary point. A 16-bit number in Q15
      format, therefore, has an assumed binary point immediately to the
      right of the most significant bit. Since the most significant bit
      constitutes the sign of the number, then numbers represented in Q15
      may take on values from +1 (represented by +0.99997...) to -1.

      NORM for Floating-point Multiply:

*  THIS SUBROUTINE PERFORMS A FLOATING-POINT MULTIPLY USING
*  THE NORM INSTRUCTION. THE INPUTS AND OUTPUTS ARE OF THE
*  FORM:
*           C = MC * 2**EC
*
*  SINCE THE MANTISSAS, MA AND MB, ARE NORMALIZED, MC CAN BE
*  NORMALIZED WITH A LEFT SHIFT OF EITHER 0 OR 1 IN THE
*  ACCUMULATOR. THE EXPONENT OF THE RESULT IS ADJUSTED
*  APPROPRIATELY. FOR EXAMPLE, MULTIPLICATION OF THE TWO
*  NUMBERS A AND B, WHERE A = 0.1 * 2**2 AND B = 0.1 * 2**4,
*  PROCEEDS AS FOLLOWS:
*
*     1)    A * B = 0.01 * 2**6
*     2)    A * B = 0.1 * 2**5      (NORMALIZED RESULT)
*
MULT  LAC   EA
      ADD   EB          ; EC = EXPONENT OF RESULT BEFORE
      SACL  EC          ; NORMALIZTION.
      LT    MA
      MPY   MB
      PAC               ; (ACC) = MA * MB

TMS32020:
*
      SFL               ; TAKES CARE OF THE REDUNDANT SIGN BIT.
      LARP  AR0
      LAR   AR0,0       ; AR0 IS INITIALIZED TO 0.
*
      NORM              ; FINDS MSB AND MODIFIES AR0.
*
      SACH  MC          ; MC = MA * MB (NORMALIZED)
      SAR   AR0,TMP
      LAC   EC
      SUB   TMP
      SACL  EC
      RET               ; RETURN TO MAIN PROGRAM.

TMS320C25:
*
      SFL               ; TAKES CARE OF THE REDUNDANT SIGN BIT.
      LARP  AR5
      LAR   AR5,EC      ; AR5 IS INITIALIZED WITH EC.
*
      NORM  *-          ; FINDS MSB AND MODIFIES AR5.
*
      SACH  MC          ; MC = MA * MB (NORMALIZED)
      SAR   AR5,EC
      RET               ; RETURN TO MAIN PROGRAM.


      Floating-point implemntation programs often require denormalization
      as well as normalization to return results in a 16-bit format. The
      following examples assume the mantissa is in the accumulator and the
      exponent is in the auxiliary register, which is the format of the
      NORM instruction after execution:

TMS32020:
*  THIS ROUTINE DENORMALIZES NUMBERS NORMALIZED BY THE NORM
*  INSTRUCTION. THE DENORMALIZED NUMBER WILL BE IN THE
*  ACCUMULATOR.
*
DENORM      LARP  1           ; USE AR1 TO POINT TO BLOCK B0.
            LRLK  AR1,>200
            SAR   AR0,*+      ; STORE EXPONENT AT >200.
            SACH  *-          ; STORE MANTISSA AT >201.
*
*  SUBTRACT EXPONENT FROM 16 TO DETERMINE THE NUMBER OF SHIFTS
*  REQUIRED TO DENORMALIZE.
*
            LAC   *           ; LOAD ACCUMULATOR WITH EXPONENT.
            BZ    OUT         ; CHECK FOR ZERO EXPONENT.
            LACK  >10
            SUB   *
            SACL  *
            LT    *+
            LACT  *           ; DENORMALIZE NUMBER.
            RET               ; RETURN TO MAIN PROGRAM.
OUT         MAR   *+          ; POINT TO MANTISSA.
            ZALH  *           ; LOAD ACCUMULATOR WITH RESULT.
            RET               ; RETURN TO MAIN PROGRAM.

TMS320C25:
*  THIS ROUTINE DENORMALIZES NUMBERS NORMALIZED BY THE NORM
*  INSTRUCTION (NORM *-). THE DENORMALIZED NUMBER WILL BE IN
*  THE ACCUMULATOR.
*
DENORM      LARP  1           ; USE AR1 TO POINT TO BLOCK B0.
            LRLK  AR1,>200
            SAR   AR4,*+      ; STORE EXPONENT AT >200.
            SACH  *-          ; STORE MANTISSA AT >201.
*
            LAC   *           ; LOAD ACCUMULATOR WITH EXPONENT.
            BZ    OUT         ; CHECK FOR ZERO EXPONENT.
            LT    *+
            LACT  *           ; DENORMALIZE NUMBER.
            RET               ; RETURN TO MAIN PROGRAM.
OUT         MAR   *+          ; POINT TO MANTISSA.
            ZALH  *           ; LOAD ACCUMULATOR WITH RESULT.
            RET               ; RETURN TO MAIN PROGRAM.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson