function c = bitconcat(varargin)
% MATLAB Code Generation Library function.
%
% CONCAT Perform concatenation of fixpt input operands.

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

% Error if any input argument is variable sized
for idx = 1:nargin
    eml_invariant(eml_is_const(size(varargin{idx})), ...
        'Coder:toolbox:NoVarSizeInputs', sprintf('%d',(idx)));
end

if (nargin == 1)
    c = bitconcat_unary(varargin{1});
else
    c = bitconcat_nary(varargin{:});
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function c = bitconcat_nary(varargin)
if nargin == 1
    c = varargin{1}; % unreachable: if reachable check length(a) >= 2
else
    c = bitconcat_binary(varargin{1}, bitconcat_nary(varargin{2:nargin}));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function c = bitconcat_unary(a)
if numel(a)<2
    if eml_ambiguous_types
        c = eml_guessdouble(coder.ignoreConst(zeros(size(a))));
    else
        eml_invariant(false, ...
                      'fixed:coder:functionNotDefinedFor1stArgType', ...
                                  'BITCONCAT', class(a));
        c = 0;
    end
else
    c = bitconcat_unary2(a,1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function c = bitconcat_unary2(a,i)

if i == numel(a) 
   c = a(i);
else
   c = bitconcat_binary(a(i),bitconcat_unary2(a,i+1));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function w = bitconcat_binary(u, v)

if eml_ambiguous_types
    if isscalar(u)
        w = coder.ignoreConst(reshape(zeros(size(v)),size(v)));
    else
        w = coder.ignoreConst(reshape(zeros(size(u)),size(u)));
    end
    w = eml_guessdouble(w);

    return;
end

eml_invariant(false, ...
    'fixed:coder:functionNotDefinedFor1stArgType', 'BITCONCAT', class(u));

w = 0;
