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
Type Format Description
short _Fract s0.7 1 sign bit, no integer bits, 7 fractional bits
unsigned short _Fract 0.8 8 fractional bits only
_Fract s0.15 1 sign bit, 15 fractional bits
unsigned _Fract 0.16 16 fractional bits only
long _Fract s0.31 1 sign bit, no integer bits, 31 fractional bits
unsigned long _Fract 0.32 32 fractional bits only
long long _Fract s0.63 1 sign bit, no integer bits, 63 fractional bits
unsigned long long _Fract 0.64 64 fractional bits only
short _Accum s8.7 1 sign bit, 8 integer bits, 7 fractional bits
unsigned short _Accum 8.8 8 integer bits, 8 fractional bits
_Accum s16.15 1 sign bit, 16 integer bits, 15 fractional bits
unsigned _Accum 16.16 16 integer bits, 16 fractional bits
long _Accum s32.31 1 sign bit, 32 integer bits, 31 fractional bits
unsigned long _Accum 32.32 32 integer bits, 32 fractional bits
long long _Accum s32.31 1 sign bit, 32 integer bits, 31 fractional bits
unsigned long long _Accum 32.32 32 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> alias Type
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
Type Suffixes
short _Fract hr, HR
unsigned short _Fract uhr, UHR
_Fract r, R
unsigned _Fract ur, UR
long _Fract lr, LR
unsigned long _Fract ulr, ULR
long long _Fract llr, LLR
unsigned long long _Fract ullr, ULLR
short _Accum hk, HK
unsigned short _Accum uhk, UHK
_Accum k, K
unsigned _Accum uk, UK
long _Accum lk, LK
unsigned long _Accum ulk, ULK
long long _Accum llk, LLK
unsigned long long _Accum ullk, ULLK