function y = sum(x,varargin)
%MATLAB Code Generation Library Function

%   Copyright 2002-2019 The MathWorks, Inc.
%#codegen

coder.internal.allowHalfInputs;
coder.internal.prefer_const(varargin);
narginchk(1,4);
if islogical(x) && ~issparse(x)
    % Optimize the logical --> double case.
    ytype = coder.const(process_sumprod_inputs('sum',x,varargin{:}));
    if isa(ytype,'double')
        coder.inline('always');
        nz = sumprod('nnz',x,varargin{:});
        y = double(nz);
        return
    end
end
y = sumprod('sum',x,varargin{:});
