function c = bitcmp(a,assumedtype)
%MATLAB Code Generation Library Function

%   Copyright 1984-2023 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.assert(nargin >= 1,'MATLAB:minrhs');
if nargin == 2
    coder.internal.prefer_const(assumedtype);
    coder.internal.assert(coder.internal.isConst(assumedtype), ...
        'Coder:toolbox:TrailingStringInputMustBeConstant');
end

% Handle Simulink size propagation.
if coder.internal.isAmbiguousTypes
    c = zeros(size(a));
    return
end

% Constant-fold with an extrinsic call.
if ~coder.target('MATLAB') && coder.internal.isConst(a)
    if nargin == 2
        c = coder.const(feval(mfilename,a,assumedtype));
    else
        c = coder.const(feval(mfilename,a));
    end
    return
end

% Validate input data.
coder.internal.assert(isinteger(a) || isa(a,'double'), ...
    'Coder:toolbox:unsupportedClass','bitcmp',class(a));
coder.internal.assert(isreal(a), ...
    'MATLAB:bitcmp:complexOperands');

% Determine the assumed and output classes.
outputcls = coder.internal.builtinClass(a);
if nargin == 2
    coder.internal.assert(coder.internal.isTextRow(assumedtype), ...
        'MATLAB:bitcmp:NDiscontinueSupport');
    assumedcls = char(assumedtype);
    coder.internal.assert(coder.internal.isIntegerClass(assumedcls), ...
        'MATLAB:bitcmp:unknownTypeString');
    coder.internal.assert(isa(a,'double') || isa(a,assumedcls), ...
        'MATLAB:bitcmp:invalidTypeString');
elseif isinteger(a)
    assumedcls = outputcls;
else
    assumedcls = 'uint64';
end

% Validate double values, if applicable.
if ~isa(a,assumedcls)
    imin = intmin(assumedcls);
    imax = intmax(assumedcls);
    coder.internal.assert(all_in_integer_range(a,imin,imax), ...
        'MATLAB:bitcmp:outOfRange');
end

% Perform the operation.
if coder.target('hdl')
    coder.inline('always');
    c = bitnot(a,assumedcls,outputcls);
elseif coder.internal.isConstTrue(isscalar(a))
    coder.inline('always');
    c = bitnot(a(1),assumedcls,outputcls);
else
    f = @(x)bitnot(x,assumedcls,outputcls);
    c = coder.internal.sxfun('bitcmp',f,a);
end

%--------------------------------------------------------------------------

function c = bitnot(a,assumedcls,outputcls)
coder.inline('always');

coder.internal.prefer_const(assumedcls,outputcls);
atmp = cast(a,coder.internal.builtinClass(a));
a1 = eml_cast(atmp,assumedcls,'to zero','spill');
c = cast(eml_bitnot(a1),outputcls);

%--------------------------------------------------------------------------
