The integer data is encoded as its two’s compliment to accommodate both positive and negative numbers in binary format. The two’s compliment can be represented using integer format or the fractional format.
Integer Format
The integer format data is represented as a signed two’s complement value, where the Most Significant bit is defined as a sign bit. The range of an N-bit two’s complement integer is -2N-1 to 2N-1-1 with a resolution of 1. For a 16-bit integer, the data range is -32768 (0x8000) to +32767 (0x7FFF) with a resolution of 1. For a 32-bit integer, the data range is -2,147,483,648 (0x8000 0000) to +2,147,483,647 (0x7FFF FFFF) with a resolution of 1.
Fractional Format
The fractional data format (Qn.m) has integral part (n) and fractional part (m) and the Most Significant bit represents the sign, thus consisting of (m+n+1) bits. It represents a signed two’s complement value. Qn.m format data has a range of [-2n, (2n-2-m)] with 2-m resolution.
The binary representation of an N-bit (m+n+1 bits) number in Qn.m is shown in Figure 1. The value is given by the equation shown in Figure 2.
Figure 1: Binary Representation
Figure 2: Equation Value
Q15 (1.15) Format
In Q15 format, the Most Significant bit is defined as a sign bit and the radix point is implied to lie just after the sign bit followed by the fractional value. This format is commonly referred to as 1.15 or Q15 format, where 1 is the number of bits used to represent the integer portion of the number, and 15 is the number of bits used to represent the fractional portion. The range of an N-bit two’s complement fraction with this implied radix point is -1.0 to (1 - 21-N). For a 16-bit fraction, the 1.15 data range is -1.0 (0x8000) to +0.999969482 (0x7FFF) with a precision of 3.05176x10-5.
Figure 3: Fractional Format (16 bits)
The following table shows the conversion of a two’s complement 16-bit integer +24576 to Q15 value +0.75.
Binary | Dec | Q15 | ||
---|---|---|---|---|
0 | 0 x (-215) | 0 | 0 x (-20) | 0 |
1 | 1 x 214 | 16384 | 1 x 2-1 | 0.5 |
1 | 1 x 213 | 8192 | 1 x 2-2 | 0.25 |
0 | 0 x 212 | 0 | 0 x 2-3 | 0 |
0 | 0 x 211 | 0 | 0 x 2-4 | 0 |
0 | 0 x 210 | 0 | 0 x 2-5 | 0 |
0 | 0 x 29 | 0 | 0 x 2-6 | 0 |
0 | 0 x 28 | 0 | 0 x 2-7 | 0 |
0 | 0 x 27 | 0 | 0 x 2-8 | 0 |
0 | 0 x 26 | 0 | 0 x 2-9 | 0 |
0 | 0 x 25 | 0 | 0 x 2-10 | 0 |
0 | 0 x 24 | 0 | 0 x 2-11 | 0 |
0 | 0 x 23 | 0 | 0 x 2-12 | 0 |
0 | 0 x 22 | 0 | 0 x 2-13 | 0 |
0 | 0 x 21 | 0 | 0 x 2-14 | 0 |
0 | 0 x 20 | 0 | 0 x 2-15 | 0 |
SUM | +24576 | SUM | +0.75 | |
= Radix Point |
Q15.16 Format
In the Q15.16 format, the Most Significant bit is defined as a sign bit followed by 16 bits of the integral part. The radix point is implied to lie just after the integral part, followed by 16 bits of the fractional value. This format is commonly referred to as Q15.16 format. The range of Q15.16 numbers is from -32768.0 (0x8000 0000) to +32767.9999847412109375 (0x7FFF FFFF) and has a precision of 2-16.
Figure 4: Fractional Format (32 bits)
The following table shows the conversion of a two’s complement 32-bit integer, -715827882 to Q15.16 value -10922.6666564941.
Binary | Dec | Q15.16 | ||
---|---|---|---|---|
1 | 1 x (-231) | -2147483648 | 1 x (-215) | -32768 |
1 | 1 x 230 | 1073741824 | 1 x 214 | 16384 |
0 | 0 x 229 | 0 | 0 x 213 | 0 |
1 | 1 x 228 | 268435456 | 1 x 212 | 4096 |
0 | 0 x 227 | 0 | 0 x 211 | 0 |
1 | 1 x 226 | 67108864 | 1 x 210 | 1024 |
0 | 0 x 225 | 0 | 0 x 29 | 0 |
1 | 1 x 224 | 16777216 | 1 x 28 | 256 |
0 | 0 x 223 | 0 | 0 x 27 | 0 |
1 | 1 x 222 | 4194304 | 1 x 26 | 64 |
0 | 0 x 221 | 0 | 0 x 25 | 0 |
1 | 1 x 220 | 1048576 | 1 x 24 | 16 |
0 | 0 x 219 | 0 | 0 x 23 | 0 |
1 | 1 x 218 | 262144 | 1 x 22 | 4 |
0 | 0 x 217 | 0 | 0 x 21 | 0 |
1 | 1 x 216 | 65536 | 1 x 20 | 1 |
0 | 0 x 215 | 0 | 0 x 2-1 | 0 |
1 | 1 x 214 | 16384 | 1 x 2-2 | 0.25 |
0 | 0 x 213 | 0 | 0 x 2-3 | 0 |
1 | 0 x 212 | 4096 | 1 x 2-4 | 0.0625 |
0 | 0 x 211 | 0 | 0 x 2-5 | 0 |
1 | 1 x 210 | 1024 | 1 x 2-6 | 0.015625 |
0 | 0 x 29 | 0 | 0 x 2-7 | 0 |
1 | 1 x 28 | 256 | 1 x 2-8 | 0.00390625 |
0 | 0 x 27 | 0 | 0 x 2-9 | 0 |
1 | 1 x 26 | 64 | 1 x 2-10 | 0.000976563 |
0 | 0 x 25 | 0 | 0 x 2-11 | 0 |
1 | 1 x 24 | 16 | 1 x 2-12 | 0.000244141 |
0 | 0 x 23 | 0 | 0 x 2-13 | 0 |
1 | 1 x 22 | 4 | 1 x 2-14 | 6.10352E-05 |
1 | 1 x 21 | 2 | 1 x 2-15 | 3.01576E-05 |
0 | 0 x 20 | 0 | 0 x 2-16 | 0 |
SUM | -715827882 | SUM | -10922.6666564941 | |
= Radix Point |
Integer - Fractional Format Mapping
The same twos complement N-bit word may represent an integer format value or a fractional format value. For example., the 16-bit integer range [-32768, +32767] format maps to Q15 range of [-1.0, +0.999969482]. Figure 5 shows the mapping between these formats.
Figure 5: Mapping between 16-bit integer format and Q15 fractional format
A similar relationship exists between the 32-bit integer format and the Q15.16 format, where the integer range [-2147483648, +2147483647] is mapped to the Q15.16 range [-32768.0, +32767.9999847412109375].
Figure 6: Mapping between 32-bit integer format and Q15.16 format
libq Library and Fixed-Point Data Format
The functions in the libq
library use the fixed-point data format. The
parameters passed and the results generated by the functions are fractional in nature.
There are two similar sets of math functions which perform the same math operations. One
set supports Q15 operands and the other supports Q15.16 operands. Q15.16 operand
functions, naturally, have better precision and range compared to Q15 operand
functions.