12.2 Data Types

All 12 of the primary fixed-point types and their aliases, described in Section 4.1 “Overview and principles of the fixed-point data types” of ISO/IEC TR 18037:2008, are supported. Fixed-point data values contain fractional and optional integral parts. The format of fixed-point data in XC32 are as specified in the table below.

In the formats shown, s is the sign bit for a signed type (there is no sign bit for an unsigned type). The period character (.) is the specifier that separates the integral part and the fractional part. The numeric digits represent the number of bits in the integral part or in the fractional part.

Table 12-1. Fixed Point Formats
TypeFormatDescription
short _Fracts0.71 sign bit, no integer bits, 7 fractional bits
unsigned short _Fract0.88 fractional bits only
_Fracts0.151 sign bit, 15 fractional bits
unsigned _Fract0.1616 fractional bits only
long _Fracts0.311 sign bit, no integer bits, 31 fractional bits
unsigned long _Fract0.3232 fractional bits only
long long _Fracts0.631 sign bit, no integer bits, 63 fractional bits
unsigned long long _Fract0.6464 fractional bits only
short _Accums8.71 sign bit, 8 integer bits, 7 fractional bits
unsigned short _Accum8.88 integer bits, 8 fractional bits
_Accums16.151 sign bit, 16 integer bits, 15 fractional bits
unsigned _Accum16.1616 integer bits, 16 fractional bits
long _Accums32.311 sign bit, 32 integer bits, 31 fractional bits
unsigned long _Accum32.3232 integer bits, 32 fractional bits
long long _Accums32.311 sign bit, 32 integer bits, 31 fractional bits
unsigned long long _Accum32.3232 integer bits, 32 fractional bits

The _Sat type modifier may be used with any type in the above table to indicate that values are saturated, as described in ISO/IEC TR 18037:2008. For example, _Sat short _Fract is the saturating form of short _Fract. Signed types saturate at the most negative and positive numbers representable in the corresponding format. Unsigned types saturate at zero and the most positive number representable in the format.

The MPLAB XC32 C compiler provides an include file, stdfix.h, which provides various pre-processor macros related to fixed-point support. These include those show in the following table.
<stdfix.h> aliasType
fract_Fract
accum_Accum
sat_Sat
Usage example:
  #include <stdfix.h>
  void main () {
    int i;
    fract a[5] = {0.5,0.4,0.2,0.0,-0.1};
    fract b[5] = {0.1,0.8,0.6,0.5,-0.1};
    accum dp = 0;
    /* Calculate dot product of a[] and b[] */
    for (i=0; i<5; i++) {
        dp += a[i] * b[i];
    }
  }

The default behavior of overflow on signed or unsigned types is saturation. The pragmas described in Section 4.1.3 "Rounding and Overflow" of ISO/IEC TR 18037:2008 to control the rounding and overflow behavior are not supported.

The following table describes the fixed-point literal suffixes supported to form fixed-point literals of each type.

Table 12-2. Fixed-Point Literal Suffixes
TypeSuffixes
short _Fracthr, HR
unsigned short _Fractuhr, UHR
_Fractr, R
unsigned _Fractur, UR
long _Fractlr, LR
unsigned long _Fractulr, ULR
long long _Fractllr, LLR
unsigned long long _Fractullr, ULLR
short _Accumhk, HK
unsigned short _Accumuhk, UHK
_Accumk, K
unsigned _Accumuk, UK
long _Accumlk, LK
unsigned long _Accumulk, ULK
long long _Accumllk, LLK
unsigned long long _Accumullk, ULLK