function r = mod(x,y)
%MATLAB Code Generation Library Function
%  Limitations:
%
%  1. The mixed double/integer case is not supported if the integer is an
%     int64 or a uint64.
%
%  Copyright 2002-2019 The MathWorks, Inc.
%#codegen

coder.internal.allowHalfInputs;
eml_invariant(nargin > 1,'MATLAB:minrhs');
if ischar(x) || islogical(x)
    r = mod(double(x),y);
    return
elseif ischar(y) || islogical(y)
    r = mod(x,double(y));
    return
end
eml_invariant(isreal(x) && isreal(y),'MATLAB:mod:complexArgument');
eml_invariant(coder.internal.isBuiltInNumeric(x) || isa(x,'half'), ...
    'Coder:toolbox:unsupportedClass',mfilename,class(x));
eml_invariant(coder.internal.isBuiltInNumeric(y) || isa(y,'half'), ...
    'Coder:toolbox:unsupportedClass',mfilename,class(y));
mixedid = isinteger(x) && isa(y,'double') && isscalar(y);
mixeddi = isa(x,'double') && isinteger(y) && isscalar(x);
eml_invariant(isa(x,class(y)) || ...
    (~isinteger(x) && ~isinteger(y)) || mixedid || mixeddi, ...
    'MATLAB:mixedClasses', ...
    'IfNotConst','Fail');
if mixedid
    eml_invariant(~(isa(x,'int64') || isa(x,'uint64')), ...
        'Coder:toolbox:mod_int64MixedDoubleNotSupported', ...
        mfilename,class(x),class(y),mfilename, ...
        'IfNotConst','Fail');
elseif mixeddi
    eml_invariant(~(isa(y,'int64') || isa(y,'uint64')), ...
        'Coder:toolbox:mod_int64MixedDoubleNotSupported', ...
        mfilename,class(x),class(y),mfilename, ...
        'IfNotConst','Fail');
end
r = coder.internal.scalexpAlloc(coder.internal.scalarEg(x,y),x,y);
if mixedid
    coder.internal.prefer_const(y);
    yi = cast(y(1),'like',x);
    if yi == y(1)
        r = coder.internal.applyBinaryScalarFunction(mfilename, ...
            zeros('like',r),@coder.internal.scalar.mod,x,yi);
    else
        % Don't bother to parallelize the edge case where an integer-valued
        % MOD is computed with a double input that is either not an integer
        % or is out-of-range for the data type.
        for k = 1:numel(r)
            r(k) = coder.internal.scalar.mod(double(x(k)),y(1));
        end
    end
elseif mixeddi
    coder.internal.prefer_const(x);
    xi = cast(x(1),'like',y);
    if xi == x(1)
        r = coder.internal.applyBinaryScalarFunction(mfilename, ...
            zeros('like',r),@coder.internal.scalar.mod,xi,y);
    else
        % Don't bother to parallelize the edge case where an integer-valued
        % MOD is computed with a double input that is either not an integer
        % or is out-of-range for the data type.
        for k = 1:numel(r)
            r(k) = coder.internal.scalar.mod(x(1),double(y(k)));
        end
    end
else
    r = coder.internal.applyBinaryScalarFunction(mfilename, ...
        zeros('like',r),@coder.internal.scalar.mod,x,y);
end
