function cout = bitrol(ain,kin)
% MATLAB Code Generation Library function.
%
% BITROL bitwise rotate left

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

% To make sure the number of the input arguments is right
eml_invariant(nargin == 2, ...
    'fixed:coder:fiInvalidMethodSignature', 'BITROL');

if eml_ambiguous_types
    if isscalar(ain)
        cout = coder.ignoreConst(reshape(zeros(size(kin)),size(kin)));
    else
        cout = coder.ignoreConst(reshape(zeros(size(ain)),size(ain)));
    end
    return;
end

eml_invariant(~isempty(ain), 'fixed:fi:emptyNotAllowed','BITROL');
eml_invariant(isfi(ain),     'fixed:fi:firstInputNotFi');
eml_invariant(isfixed(ain),  'fixed:coder:fiFcnDTypeErrorFixPtOnly','BITROL');
eml_invariant(~eml_isslopebiasscaled(ain), ...
    'fixed:fi:unsupportedSlopeBias', 'BITROL');

aNT = numerictype(ain);
eml_invariant(aNT.WordLength > 1, ...
    'fixed:fi:notDefinedForOneBitFi','BITROL');

eml_invariant(~isempty(kin), 'fixed:fi:emptyNotAllowed','BITROL');
eml_invariant(isreal(kin),   'fixed:fi:bitRotateIndexMustBeReal','BITROL');
eml_invariant(isscalar(kin), 'fixed:bitsxx:invalidShiftVal','BITROL');
coder.internal.prefer_const(kin);
if eml_is_const(kin)
    eml_invariant((kin >= 0) && (floor(double(kin)) == double(kin)), ...
        'fixed:bitsxx:invalidShiftVal','BITROL');
end

aFM = fimath(ain);   

% set rnd,sat modes to trivial modes
a = fi(ain, 'OverflowMode', 'wrap', 'RoundMode', 'floor');
% If a is not fixed-point, then DTO is active, do not support this for now
eml_invariant(isfixed(a),  'Coder:FE:FixedPointDTOEnabled','BITROL');

% First decide the output size & complexity
ctemp = fi(zeros(size(a)),aNT,fimath(a));
if ~isreal(a)
    c = complex(ctemp,ctemp);
else
    c = ctemp;
end

% Inline the generated code if a is a scalar
if isscalar(a)
    coder.inline('always');
end

% Do the bitshift
if eml_is_const(kin)
    k = coder.const(eml_get_int_shift_or_bit_index(kin));
else
    k = eml_get_int_shift_or_bit_index(kin);
end
for m = 1:numel(a)
    if isreal(a)
        c(m) = eml_bit_rol(a(m),k);
    else
        cr = eml_bit_rol(real(a(m)),k);
        ci = eml_bit_rol(imag(a(m)),k);
        c(m) = complex(cr,ci);
    end
end

% restore rnd,sat modes or return a fimath-less fi
if coder.const(eml_fimathislocal(ain))
    cout = fi(c, 'OverflowMode', aFM.OverflowMode, 'RoundMode', aFM.RoundMode);
else
    cout = eml_fimathislocal(c,false);
end
