0% found this document useful (0 votes)
8 views4 pages

Qmath and IQmath Library Overview

Uploaded by

Getsuga Tenshou
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Qmath and IQmath Library Overview

Uploaded by

Getsuga Tenshou
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Using The Qmath and IQmath Libraries

2 Using The Qmath and IQmath Libraries


2.1 Qmath Data Types
The Qmath library uses a 16-bit fixed-point signed number (an “int16_t” in C99) as its basic data
type. The Q format of this fixed-point number can range from Q1 to Q15, where the Q format
number indicates the number of fractional bits. The Q format value is stored as an integer with an
implied scale based on the Q format and the number of fractional bits. Equation 2.1 shows how
a Q format decimal number xq is stored using an integer value xi with an implied scale, where n
represents the number of fractional bits.

Qn(xq ) = xi ∗ 2−n (2.1)

For example the Q12 value of 3.625 is stored as an integer value of 14848, shown in equation 2.2
below.

14848 ∗ 2−12 = Q12(3.625) (2.2)

C typedefs are provided for the various Q formats, and these Qmath data types should be used in
preference to the underlying “int16_t” data type to make it clear which variables are in Q format.
The following table provides the characteristics of the various Q formats (the C data type, the
number of integer bits, the number of fractional bits, the smallest negative value that can be repre-
sented, the largest positive value that can be represented, and the smallest difference that can be
represented):

Bits Range
Type Resolution
Integer Fractional Min Max
_q15 1 15 -1 0.999 970 0.000 030
_q14 2 14 -2 1.999 940 0.000 061
_q13 3 13 -4 3.999 830 0.000 122
_q12 4 12 -8 7.999 760 0.000 244
_q11 5 11 -16 15.999 510 0.000 488
_q10 6 10 -32 31.999 020 0.000 976
_q9 7 9 -64 63.998 050 0.001 953
_q8 8 8 -128 127.996 090 0.003 906
_q7 9 7 -256 255.992 190 0.007 812
_q6 10 6 -512 511.984 380 0.015 625
_q5 11 5 -1,024 1,023.968 750 0.031 250
_q4 12 4 -2,048 2047.937 500 0.062 500
_q3 13 3 -4,096 4,095.875 000 0.125 000
_q2 14 2 -8,192 8,191.750 000 0.250 000
_q1 15 1 -16,384 16,383.500 000 0.500 000

Table 2.1: Qmath Data Types

In addition to these specific Q format types, there is an additional type that corresponds to the
GLOBAL_Q format. This is _q, and it matches one of the above Q formats (based on the setting of

January 19, 2015 7


Using The Qmath and IQmath Libraries

GLOBAL_Q). The GLOBAL_Q format has no impact when using the specific _qN types and function
such as _q12.

8 January 19, 2015


Using The Qmath and IQmath Libraries

2.2 IQmath Data Types


The IQmath library uses a 32-bit fixed-point signed number (an “int32_t” in C99) as its basic data
type. The IQ format of this fixed-point number can range from IQ1 to IQ30, where the IQ format
number indicates the number of fractional bits. The IQ format value is stored as an integer with an
implied scale based on the IQ format and the number of fractional bits. Equation 2.3 shows how
a IQ format decimal number xiq is stored using an integer value xi with an implied scale, where n
represents the number of fractional bits.

IQn(xiq ) = xi ∗ 2−n (2.3)

For example the IQ24 value of 3.625 is stored as an integer value of 60817408, shown in equation
2.4 below.

60817408 ∗ 2−24 = IQ24(3.625) (2.4)

C typedefs are provided for the various IQ formats, and these IQmath data types should be used in
preference to the underlying “int32_t” data type to make it clear which variables are in IQ format.
The following table provides the characteristics of the various IQ formats (the C data type, the
number of integer bits, the number of fractional bits, the smallest negative value that can be repre-
sented, the largest positive value that can be represented, and the smallest difference that can be
represented):

January 19, 2015 9


Using The Qmath and IQmath Libraries

Bits Range
Type Resolution
Integer Fractional Min Max
_iq30 2 30 -2 1.999 999 999 0.000 000 001
_iq29 3 29 -4 3.999 999 998 0.000 000 002
_iq28 4 28 -8 7.999 999 996 0.000 000 004
_iq27 5 27 -16 15.999 999 993 0.000 000 007
_iq26 6 26 -32 31.999 999 985 0.000 000 015
_iq25 7 25 -64 63.999 999 970 0.000 000 030
_iq24 8 24 -128 127.999 999 940 0.000 000 060
_iq23 9 23 -256 255.999 999 881 0.000 000 119
_iq22 10 22 -512 511.999 999 762 0.000 000 238
_iq21 11 21 -1,024 1,023.999 999 523 0.000 000 477
_iq20 12 20 -2,048 2,047.999 999 046 0.000 000 954
_iq19 13 19 -4,096 4,095.999 998 093 0.000 001 907
_iq18 14 18 -8,192 8,191.999 996 185 0.000 003 815
_iq17 15 17 -16,384 16,383.999 992 371 0.000 007 629
_iq16 16 16 -32,768 32,767.999 984 741 0.000 015 259
_iq15 17 15 -65,536 65,535.999 969 483 0.000 030 518
_iq14 18 14 -131,072 131,071.999 938 965 0.000 061 035
_iq13 19 13 -262,144 262,143.999 877 930 0.000 122 070
_iq12 20 12 -524,288 524,287.999 755 859 0.000 244 141
_iq11 21 11 -1,048,576 1,048,575.999 511 720 0.000 488 281
_iq10 22 10 -2,097,152 2,097,151.999 023 440 0.000 976 563
_iq9 23 9 -4,194,304 4,194,303.998 046 880 0.001 953 125
_iq8 24 8 -8,388,608 8,388,607.996 093 750 0.003 906 250
_iq7 25 7 -16,777,216 16,777,215.992 187 500 0.007 812 500
_iq6 26 6 -33,554,432 33,554,431.984 375 000 0.015 625 000
_iq5 27 5 -67,108,864 67,108,863.968 750 000 0.031 250 000
_iq4 28 4 -134,217,728 134,217,727.937 500 000 0.062 500 000
_iq3 29 3 -268,435,456 268,435,455.875 000 000 0.125 000 000
_iq2 30 2 -536,870,912 536,870,911.750 000 000 0.250 000 000
_iq1 31 1 -1,073,741,824 1,073,741,823.500 000 000 0.500 000 000

Table 2.2: IQmath Data Types

In addition to these specific IQ format types, there is an additional type that corresponds to the
GLOBAL_IQ format. This is _iq, and it matches one of the above IQ formats (based on the setting
of GLOBAL_IQ).The GLOBAL_IQ format has no impact when using the specific _iqN types and
function such as _iq24.

10 January 19, 2015

You might also like