fi Objects and C Integer Data Types
Note
The sections in this topic compare the fi
object with fixed-point
data types and operations in C. In these sections, the information on ANSI® C is adapted from Samuel P. Harbison and Guy L. Steele Jr., C: A
Reference Manual, 3rd ed., Prentice Hall, 1991.
Integer Data Types
This section compares the numerical range of fi
integer data types to
the minimum numerical range of C integer data types, assuming a Two's Complement representation.
C Integer Data Types
Many C compilers support a two's complement representation of signed integer data
types. The following table shows the minimum ranges of C integer data types using a two's
complement representation. The integer ranges can be larger than or equal to the ranges
shown, but cannot be smaller. The range of a long
must be larger than
or equal to the range of an int
, which must be larger than or equal to
the range of a short
.
In the two's complement representation, a signed integer with n bits has a range from to , inclusive. An unsigned integer with n bits has a range from 0 to , inclusive. The negative side of the range has one more value than the positive side, and zero is represented uniquely.
Integer Type | Minimum | Maximum |
---|---|---|
| –128 | 127 |
| 0 | 255 |
| –32,768 | 32,767 |
| 0 | 65,535 |
| –32,768 | 32,767 |
| 0 | 65,535 |
| –2,147,483,648 | 2,147,483,647 |
| 0 | 4,294,967,295 |
fi Integer Data Types
The following table lists the numerical ranges of the integer data types of the
fi
object, in particular those equivalent to the C integer data
types. The ranges are large enough to accommodate the two's complement representation,
which is the only signed binary encoding technique supported by Fixed-Point Designer™ software.
Constructor | Signed | Word Length | Fraction Length | Minimum | Maximum | Closest ANSI C Equivalent |
---|---|---|---|---|---|---|
| Yes | n | 0 | Not applicable | ||
| No | n | 0 | 0 | Not applicable | |
| Yes | 8 | 0 | –128 | 127 |
|
| No | 8 | 0 | 0 | 255 |
|
| Yes | 16 | 0 | –32,768 | 32,767 |
|
| No | 16 | 0 | 0 | 65,535 |
|
| Yes | 32 | 0 | –2,147,483,648 | 2,147,483,647 |
|
| No | 32 | 0 | 0 | 4,294,967,295 |
|
Unary Conversions
Unary conversions dictate whether and how a single operand is converted before an
operation is performed. This section discusses unary conversions in ANSI C and of fi
objects.
ANSI C Usual Unary Conversions
Unary conversions in ANSI C are automatically applied to the operands of the unary
!
, –, ~, and *
operators, and of the binary
<<
and >>
operators, according to the
following table:
Original Operand Type | ANSI C Conversion |
---|---|
|
|
|
|
|
|
Array of T | Pointer to T |
Function returning T | Pointer to function returning T |
1If type int
cannot represent all the
values of the original data type without overflow, the converted type is unsigned
int
.
fi Usual Unary Conversions
The following table shows the fi
unary conversions:
C Operator | fi Equivalent | fi Conversion |
---|---|---|
| ~ | Result is |
~ |
| Result is same numeric type as operand. |
| No equivalent | Not applicable |
|
| Result is same numeric type as operand. Round mode is always
|
|
| Result is same numeric type as operand. Round mode is always
|
|
| Result is same numeric type as operand. |
|
| Result is same numeric type as operand. Overflow mode is obeyed. For
example, overflow might occur when you negate an unsigned
|
Binary Conversions
This section describes the conversions that occur when the operands of a binary operator are different data types.
ANSI C Usual Binary Conversions
In ANSI C, operands of a binary operator must be of the same type. If they are different, one is converted to the type of the other according to the first applicable conversion in the following table:
Type of One Operand | Type of Other Operand | ANSI C Conversion |
---|---|---|
| Any |
|
| Any |
|
| Any |
|
| Any |
|
|
|
|
|
|
|
|
|
|
|
|
|
1Type long
is only used if it can
represent all values of type unsigned
.
fi Usual Binary Conversions
When one of the operands of a binary operator (+
, –,
*
, .*
) is a fi
object and the
other is a MATLAB® built-in numeric type, then the non-fi
operand is
converted to a fi
object before the operation is performed, according
to the following table:
Type of One Operand | Type of Other Operand | Properties of Other Operand After Conversion to a fi Object |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Overflow Handling
The following sections compare how ANSI C and Fixed-Point Designer software handle overflows.
ANSI C Overflow Handling
In ANSI C, the result of signed integer operations is whatever value is produced by the machine instruction used to implement the operation. Therefore, ANSI C has no rules for handling signed integer overflow.
The results of unsigned integer overflows wrap in ANSI C.
fi Overflow Handling
Addition and multiplication with fi
objects yield results that can
be exactly represented by a fi
object, up to word lengths of 65,535
bits or the available memory on your machine. This is not true of division, however,
because many ratios result in infinite binary expressions. You can perform division with
fi
objects using the divide
function, which requires you to explicitly specify the numeric type
of the result.
The conditions under which a fi
object overflows and the results
then produced are determined by the associated fimath
object. You can
specify certain overflow characteristics separately for sums (including differences) and
products. Refer to the following table:
fimath Object Properties Related to Overflow Handling | Property Value | Description |
---|---|---|
|
| Overflows are saturated to the maximum or minimum value in the range. |
| Overflows wrap using modulo arithmetic if unsigned, two's complement wrap if signed. | |
|
| Full-precision results are kept. Overflow does not occur. An error is
thrown if the resulting word length is greater than
The rules for computing the resulting product word and fraction lengths are given in fimath Object Properties in the Property Reference. |
| The least significant bits of the product are kept. Full precision is kept, but overflow is possible. This behavior models the C language integer operations. The The rule for computing the resulting product fraction length is given in fimath Object Properties in the Property Reference. | |
| The most significant bits of the product are kept. Overflow is prevented, but precision may be lost. The
The rule for computing the resulting product fraction length is given in fimath Object Properties in the Property Reference. | |
| You can specify both the word length and the fraction length of the resulting product. | |
| Positive integer | The word length of product results when
|
| Positive integer | The maximum product word length allowed when
|
| Integer | The fraction length of product results when
|
|
| Full-precision results are kept. Overflow does not occur. An error is
thrown if the resulting word length is greater than
The rules for computing the resulting sum word and fraction lengths are given in fimath Object Properties in the Property Reference. |
| The least significant bits of the sum are kept. Full precision is kept, but overflow is possible. This behavior models the C language integer operations. The The rule for computing the resulting sum fraction length is given in fimath Object Properties in the Property Reference. | |
| The most significant bits of the sum are kept. Overflow is prevented, but precision may be lost. The The rule for computing the resulting sum fraction length is given in fimath Object Properties in the Property Reference. | |
| You can specify both the word length and the fraction length of the resulting sum. | |
| Positive integer | The word length of sum results when |
| Positive integer | The maximum sum word length allowed when |
| Integer | The fraction length of sum results when |