Intel® High Level Synthesis Compiler Pro Edition: Reference Manual

ID 683349
Date 6/20/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

8.2.1. Arbitrary Precision Fixed-Point Literals in Operations

When you use the ac_fixed datatype to declare literals in your component, the compiler expands the literal, regardless of the size you set for the ac_fixed literal. To keep the compiler from expanding the literal, cast the literal to an ac_fixed data type.

For example, the literal in the following code snippet is treated as a double data type:
Using Fixed12 = ac_fixed<12, 6, true>;

component Fixed12 threshold_vanilla(Fixed12 a){
    if (a < 12.7) {
        return a;
    } else {
        return 12.7;
    }
}

In this code snippet, the 12-bit ac_fixed value is compared with 12.7. The literal 12.7 is treated as a double, with value 12.699999…. This literal is converted to a fixed-point to be compared with a, but because the literal is unconstrained, the compiler chooses a large fixed-point value and this can result in a large comparison:

You can force a smaller comparison by casting the literal 12.7 to an appropriately-sized ac_fixed data type:
Using Fixed12 = ac_fixed<12, 6, true>;

component Fixed12 threshold_cast(Fixed12 a){
    if (a < (Fixed12)12.7) {
        return a;
    } else {
        return 12.7;
    }
}

For more details about the impact of choosing single precision literals and functions over their double precision counterparts, review the following tutorial:
<quartus_installdir>//hls/examples/tutorials/best_practices/single_vs_double_precision_math