function y = quantize(x, varargin)
%QUANTIZE(x, ...) Quantize fixed-point numbers.

% Copyright 2011-2014 The MathWorks, Inc.

%#codegen

coder.internal.prefer_const(varargin);

if nargin < 1
    eml_invariant(false, 'MATLAB:narginchk:notEnoughInputs');
elseif eml_ambiguous_types
    y = coder.ignoreConst(zeros(size(x)));
    return;
elseif nargin > 6
    eml_invariant(false, 'MATLAB:narginchk:tooManyInputs');
elseif ~isinteger(x)
    eml_invariant(false, 'fixed:fi:unsupportedType',class(x));
end

if (nargin > 1)
    % At least one varargin is provided
    if isnumerictype(varargin{1})
        % Caller syntax: y = quantize(x,Ty,rm,om)
        if nargin > 4
            eml_invariant(false, 'MATLAB:narginchk:tooManyInputs');
        end
        
        Ty = varargin{1};
        
        if nargin > 2
            rm = varargin{2};
            if nargin > 3
                y = q(x,Ty,rm,varargin{3});
            else
                y = q(x,Ty,rm,'wrap');
            end
        else
            y = q(x,Ty,'floor','wrap');
        end
    elseif isscalar(varargin{1}) && ~isempty(varargin{1}) && isreal(varargin{1}) && (islogical(varargin{1}) || isnumeric(varargin{1}))
        % Caller syntax: y = quantize(x,s,wl,fl,rm,om)
        if nargin > 3
            eml_invariant(isnumeric(varargin{2}) && isscalar(varargin{2}), ...
                          'fixed:numerictype:invalidWLInputArg');
            eml_invariant(isnumeric(varargin{3}) && isscalar(varargin{3}), ...
                          'fixed:numerictype:invalidFLInputArg');
            Ty = numerictype(varargin{1}, ...
                double(varargin{2}), double(varargin{3}));
        elseif nargin > 2
            eml_invariant(isnumeric(varargin{2}) && isscalar(varargin{2}), ...
                          'fixed:numerictype:invalidWLInputArg');
            Ty = numerictype(varargin{1}, ...
                double(varargin{2}), (double(varargin{2}) - 1));
        else
            Ty = numerictype(varargin{1}, 16, 15);
        end
        
        if nargin > 4
            rm = varargin{4};
            if nargin > 5
                y = q(x,Ty,rm,varargin{5});
            else
                y = q(x,Ty,rm,'wrap');
            end
        else
            y = q(x,Ty,'floor','wrap');
        end
    else
        eml_invariant(false, ...
            'fixed:numerictype:invalidInputArg', varargin{1});
    end
else
    % Use all defaults (no varargin provided)
    y  = q(x, numerictype(1,16,15), 'floor', 'wrap');
end

function y = q(x,Ty,rm,om)
F    = fimath('OverflowMode',om,'RoundMode',rm);
temp = eml_cast(x,Ty,F);
y    = eml_fimathislocal(temp,false);
