DSPs operate in either fixed-point arithmetic or floating point arithmetic. In general, most of the high performance DSPs operate with fixed-point arithmetic because of different reasons that have to do with number of arithmetic instructions that can be executed per second (in many cases by parallelizing over several ALUs, power dissipation, memory use and access bandwidth, etc.). Because of this, a basic but important skill for anybody working with DSPs is to know how fixed-point arithmetic works.
One of the tricky points found at the beginning of working with fixed point arithmetic is that, although the DSP architecture determines the number of bits of the registers used for arithmetic operations, the placement of the decimal point within those bits is entirely up to the programmer’s choice. Even more, the programmer can change the placement of the decimal point from one operation to the next with no problem as long as it keeps track of where the decimal point is.
The placement of the decimal point is defined through the Q formats. The Q formats specify the number of decimal bits (i.e. binary digits) being used. For example, a Q15 format means that there are 15 decimal bits in the Q15 number. If a signed Q15 number is stored in a 16-bit register, the number is being represented with the most significant bit being the sign bit and the rest of the digits representing negative (decimal) powers of 2, from 2^-1 for the bit next to the most significant bit to 2^-15 for the least significant bit. Note that there is an implicit decimal point between the most significant bit and the one next to it.
As I mentioned before, the placement of the decimal point, or in other words the Q format being used, can be changed at any point of the algorithm. This is useful so as to maximize for each case the dynamic range provided by the limited number of bits in the ALU registers. Adding and subtracting numbers in any Q format has no especial difficulty, provided the programmer is careful that all the terms have the decimal point correctly align by all being represented in the same Q format. Multiplication requires a little bit of more care because the factors can be of different Q format but the result is of yet another Q format. To understand this, recall that when multiplying two 16-bit numbers, the result is a 32-bit number. In general, if we are multiplying a number of the format I.D (meaning a number with I integer digits and D decimal digits) with a number of the format J.E, the result is a number of the format (I+J).(D+E). Now, if the two numbers beings multiplied are in Q15 format (1.15 format using the notation I.D), the result is in the Q30 format 2.30. This means that 32 bits are necessary to represent the result, but in fact, the two integer digits are sign bits, with one being redundant (or a sign extension). The solution to the appearance of the redundant sign bit is simply to shift the number to the left one bit, transforming the result into Q31 format (1.31). In real world DSPs it is typical to find different multiplication instructions. For example, if the ALU works on 32-bits wide words, it is possible to find a multiplication instruction that takes the 16 most significant bits of each input argument as if they were in Q15 format and results in a Q30 format result as explained above. Also, it is possible to find a multiplication instruction that takes 2 32-bit arguments and results in a 32-bit result in Q31 format, where the 32 least significant bits from the results are lost in a rounding operation.
For the programmer, all this means that when debugging it is necessary to constantly be converting hexadecimal (or binary) numbers of a myriad of Q formats into decimal numbers. Because of this, I have become over time best friend with a number of matlab routines that help me in doing these conversions. If interested, you can follow this link to see the routine that takes a 16-bit hexadecimal number in Q15 format and converts it into a decimal number. Also, this link converts a 32-bit hexadecimal number in Q30 number into a decimal number. The routines are also instructive in helping understand how the different Q formats work.






