%#codegen
function norm_x = normalize_angle(x)
    if isfloat(x)
        % floating-point type
        norm_x = x;
    else
        if x.WordLength <= 16
            % 16-bit fi: leave as it is
            norm_x = x;
        else
            % convert to 16 bit type
            nt = numerictype(1, 16, 12);

            fm = fimath(x);
            x = setfimath(x, hdlfimath);

            twopi = fi(2*pi, 1, 32, 28, hdlfimath);
            q = divide( numerictype(1, 32, 0),x, twopi);
            norm_x = fi(x - q*twopi, nt, fm);
        end
    end
end
