The invention relates generally to methods and systems for performing arithmetic calculations in digital processing systems.
Because calculations with floating-point numbers can require significant computing power, some digital processing systems include special hardware for performing floating-point arithmetic called floating point processors (FPP), math coprocessors, etc. However, low-cost digital signal processors, microprocessors and microcontrollers such as those used in disk drives do not have floating-point processors.
Some fixed-point processors use a modified form of integers for calculations. Numbers entered as real values are scaled by dividing by larger numbers and then rounded or truncated to an integer. The processor considers the scale value n (from number *2n) and uses this to determine the location of the fixed radix point. For example, the number 1.75 could be represented as a 4-bit integer 7 (i.e. ‘0111’) with a scale of 2. The scale value of 2 means that the first two bits are for the value (and sign for 2's complement numbers) to the left of the radix point, the third bit represents “0.5” and the fourth bit to represents “0 . . . 25”. The scale value is a shift of the radix point. A 4-bit number where the first 2 bits represent the integer portion and the second two represent the fraction is commonly referred to as a 2.2 format.
Other standard ways to represent numbers include representing floating point numbers as an “exponent”, “significand”, and “sign bit”. The encoding of a floating point number into a binary number can be done by normalizing the number by shifting the bits either left or right until the shifted result lies between 1 and 0.5 if the exponent is a power of 2. (If the exponent is a power of 16, the shifted result lies between 1 and 0.0625 ( 1/16).) A left-shift by one bit corresponds to multiplying by 2, and a right-shift corresponds to dividing by 2. The number of bit-positions shifted to normalize the number can be recorded as a signed integer. The negative of this integer (i.e., the number of bit-shifts required to recover the original number) can be defined as the base-2 exponent. Whether the right or left shift is assigned to the positive value is not significant. The normalized number between ½ and 1 is typically called the significand, because it contains the significant bits of the number. This floating point encoding is analogous to scientific notation for decimal numbers. The word mantissa is often used as a synonym for significand.
An IEEE standard defines “Fp32” as a single precision floating-point format in which a floating point number is represented by a sign bit, eight exponent bits, and 23 significand bits. The exponent is biased upward by 127 so that exponents in the range 2−126 to 2127 are represented using integers from 1 to 254. For “normal” numbers, the 23 significand bits are interpreted as the fractional portion of a 24-bit mantissa with an implied 1 as the integer portion.
Single chip digital signal processors (DSPs) are specialized microprocessors designed for fast, real-time computations. One common feature of DSPs is the “multiply and/or accumulate” instruction, or MAC. This instruction multiplies two values and stores the result in the accumulator.
U.S. Pat. No. 7,225,216 to Wyland (issued May 29, 2007) describes a floating point multiply-accumulator that uses “mantissa logic” for combining a mantissa portion of floating point inputs and “exponent logic” coupled to the “mantissa logic.” The exponent logic adjusts the combination of an exponent portion of the floating point inputs by a predetermined value to produce a shift amount and allows pipeline stages in the mantissa logic, wherein an unnormalized floating point result is produced from the mantissa logic on each clock cycle.
Published application 2006/0195497 by Dobbek, et al. (Aug. 31, 2006) describes a shift process for a digital signal processor for shifting an operand to either maximum or the minimum value depending on the bit of data input when saturation occurs. A saturation detection circuit is combined with an arithmetic shifter and a final decision multiplexor. The final decision multiplexor receives the output from the arithmetic shifter and the saturated value from the saturation circuit. When saturation is detected by the saturation detection circuit, the final decision multiplexor selects the saturate minimum or the saturate maximum depending on whether the most significant bit of the data in equals one or zero, respectively.
In published application 20060294175 Koob, et al. (Dec. 28, 2006) describe a method of counting leading zeros or ones in a data word in a digital signal processor. During operation, the execution unit can receive a data word that has a width of N bits. The execution unit can sign extend the data word to a wider temporary data word. The temporary data word can be input to a counter to count the leading zeros within the temporary data word to get a result.
In published application 0060200732 Dobbek, et al. (Sep. 7, 2006) describe a processor based nested form polynomial engine. An instruction causes a processor to set coefficient and data address pointers for evaluating a polynomial, to load a coefficient and data operand into a coefficient register and a data register, respectively, to multiply the contents of the coefficient register and data register to produce a product, to add a next coefficient operand to the product to produce a sum, to provide the sum to an accumulator and to repeat the loading, multiplying, adding and providing until evaluation of the polynomial is complete.
The invention uses the fact that leading sign bits in the 2's compliment number system are sometimes redundant, i.e., more than one bit is used to represent the sign. These redundant sign bits reduce the dynamic range of the number. The invention extends the dynamic range by removing redundant sign bits and saving the count of bits removed as an exponent. An embodiment of the invention encodes a fixed point number into a mantissa by removing redundant sign bits by shifting the significant bits to the left. The number of bits shifted is recorded as the exponent. In one embodiment the mantissa and exponent are combined into a single word of memory for the system which allows efficient loading of the value from memory in a single fetch cycle. The mantissa and exponent can be used in multiplication calculations, for example, with fixed point numbers to achieve increased dynamic range. When the mantissa is multiplied by a fixed point number, the initial result is larger by a factor of 2exponent, and a bit-shift to the right by the number of bits represented by the exponent removes this factor.
One embodiment of the invention provides a mantissa/exponent generator a microprocessor or digital signal processor that executes an instruction for encoding a fixed point number in mantissa-exponent form. Another embodiment of the invention provides an instruction implemented in a microprocessor or digital signal processor for multiplying a fixed point number by a second fixed point number encoded into the mantissa-exponent form.
All of the operations used in the invention as described below at run-time are fixed point operations. This allows the use of lower cost fixed point processors rather than more expensive floating point processors. Converting selected fixed point numbers into a “CTFP number” form as described herein according to the invention facilitates the run-time calculations.
In the 2's compliment number system, the sign bit is the highest order bit of the number. When the subsequent lower order bits are the same as the sign bit then there is no added information, i.e. these bits are redundant. These redundant sign bits detract from the dynamic range of the number since fixed point numbers have a fixed size in bits. The invention includes a method for counting and removing the redundant sign bits of a fixed point number in a single microprocessor instruction. The result of counting the redundant sign bits (Count) allows the shifting of the original data to the left by the number of bits in the Count (i.e. left justifying) to create a mantissa and storing the Count as a base-2 exponent in a mantissa-exponent pair. Determining in one instruction how many highest order bits are just “copies” of the sign bit allows efficient run-time construction of a new number form (mantissa-exponent) which can extend the dynamic range by the number of redundant sign bits.
The multiply and accumulation process in real-time control systems typically uses accumulators that have more bits than data words stored in memory. For example, a typical processor might use 32-bit data words and a 48-bit accumulator. When a CTFP number is formed out of a fixed point number there may only be a few bits of data left in a 32-bit number. That is, the number is small with respect to the 32-bit data. However, there may be data in the lower 16 bits of the accumulator that adds detail to the number when shifted up in the top of the accumulator. For example, suppose 0x000000018000 is in the accumulator and the fullscale of the variable represented is 64.0. (“Fullscale” is used to refer the maximum value for a variable.) The accumulator value actually represents 0x18000/2̂47*64==>44.703×10̂-9. Given data variables of 32 bits, the stored result using the uppermost 32 bits would be only 0x00000001 with an error of 50%. An embodiment of the invention allows the number to be represented with little loss of detail by the encoded 32-bit word 0x6000001E with the upper 16 bits 0x6000 being the mantissa and the lower 16 bits 0x001E being the exponent. The number in this form is (0x6000/2̂15)/2̂0x001E*64.0==>44.703×10̂-9. In this case the number is represented perfectly. The number of bits and the position of the bits for the mantissa and exponent in the encoded word can be different in other embodiments.
An overview of a second embodiment of the process of converting a 2's complement fixed point number into a mantissa and exponent according to the invention is shown in the flowchart of
As an example, consider the 32 bit positive 2's complement number in hexadecimal form of 0x1312 4557. The upper byte is 0x13 (“0001 0011” in binary representation). The most significant bit (MSB) is a sign bit, and it is “0”. To eliminate redundant sign bits and to maintain the same sign, the two leading zeros in this example will be removed. The leading sign detector will return the value of 2. The 0x1312 4577 number will be shifted left by 2 bit positions to form the mantissa and the value “2” will be saved as the Count.
For an example of a negative number consider 0xF800 1234 as the input value. The upper byte is 0xF8 (1111 1000 in binary representation). The MSB is a negative sign bit of “1”. To eliminate redundant sign bits and to maintain the same sign, four leading ones in this example need to be removed. The leading sign detector will return the value of 4. The 0xF800 1234 value will be shifted left by 4 bit positions and the value “4” will be saved as the exponent.
The embodiments of the LSC and MAC_CTFP instructions described above are just one example of ways that the invention can be implemented in specific instructions. In another alternative embodiment, for example, a single instruction that performed the LSC and MAC_CTFP could be designed. The instructions can be architected to use the mantissa and exponent values combined into a single word of memory as described in reference to
The foregoing description of the exemplary embodiment of the invention has been presented for the purposes of illustration and description. It is not intended to be exhaustive or to limit the invention to the precise form disclosed. Many modifications and variations are possible within the scope of the invention.