Main Content

Mapping of Different Rounding and Overflow Methods from MATLAB to HLS

The specified fixed-point data types in MATLAB® code are converted to High-Level Synthesis (HLS) fixed-point data types during code generation.

The tables show the mapping of different MATLAB fixed-point rounding and overflow methods to their equivalent HLS methods.

Rounding Methods

MATLAB Fixed-Point Rounding MethodsDescriptionEquivalent HLS Fixed-Point Rounding Methods

Nearest

(default for MATLAB)

Rounds to the nearest representable number.

SC_RND

Zero

Rounds to the nearest representable number in the direction of zero.

SC_TRN_ZERO

Floor

Rounds to the nearest representable number in the direction of negative infinity.

Equivalent to two's complement truncation.

SC_TRN

(default for HLS)

Round

Rounds to the nearest representable number.

SC_RND_INF

Convergent

Rounds to the nearest representable number.

SC_RND_CONV

Note

The ceiling rounding method is not supported for HLS code generation.

Overflow Methods

MATLAB Fixed-Point Overflow MethodsDescriptionEquivalent HLS Fixed-Point Overflow Methods

Saturate

(default for MATLAB)

Saturate to maximum or minimum value of the fixed-point range on overflow.

SC_SAT

Wrap

Wrap on overflow. This mode is also known as two's complement overflow.

SC_WRAP

(default for HLS)

The rounding and overflow methods are represented as typecasts on expressions in the generated HLS code.

For example, consider the MATLAB function exampleFun and the generated HLS code to understand the conversion of rounding and overflow methods.

MATLAB Code

function y = exampleFun(a, b)
    y = fi(a + b, 1, 5, 2, fimath('RoundingMethod','Nearest','OverflowAction','Saturate'));
end

Generated HLS Code

class exampleFunClass
{
    public:
    sc_fixed<5,3> exampleFun(sc_fixed<7,3> a, sc_fixed<7,3> b)
    {
        return (sc_fixed<5,3,SC_RND,SC_SAT>)((sc_fixed<8,4>)a + (sc_fixed<8,4>)b);
    }
};