function c = bitxorreduce(a,lidx,ridx)
% MATLAB Code Generation Library function.
%
% BITXORREDUCE Perform bitwise-xor reduction on a range of bits in a fixed point word.
%
%    C = BITXORREDUCE(A,LIDX, RIDX) returns a bit after perform bitwise xor
%        on all the indvidual bits in the range LIDX and RIDX
%
%    The return type is ufix1
%
%    A must be a fi object matrix, array, or a scalar fi object
%    (signed or unsigned)
%
%    lidx must be an integer corresponding to a bit position in A
%
%    ridx must be an integer corresponding to a bit position in A
%
%    left index (lidx) must be greater than equal to right index (ridx)
%    and less than equal to word length of input operand a
%

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

if eml_ambiguous_types
    c = coder.ignoreConst(reshape(zeros(size(a)),size(a)));
    return;
end

if (nargin > 1)
    coder.internal.prefer_const(lidx);
    eml_invariant(~isempty(lidx), 'fixed:fi:emptyNotAllowed','BITXORREDUCE');
end

if (nargin > 2)
    coder.internal.prefer_const(ridx);
    eml_invariant(~isempty(ridx), 'fixed:fi:emptyNotAllowed','BITXORREDUCE');
end

% Only FI fixed-point types supported
eml_invariant(isfi(a) && isfixed(a), ...
        'fixed:coder:fiFcnDTypeErrorFixPtOnly','BITXORREDUCE');

if nargin == 1
    % Neither lidx or ridx were provided
    lidx = a.WordLength;
    ridx = 1;
elseif nargin == 2
    % Only lidx was provided
    ridx = 1;
end

eml_bitop_index_checks('bitandreduce', a, lidx, ridx)

coder.inline('always');

ufix1 = numerictype(0, 1, 0);

% If a has an attached fimath, then c will also have one
% Otherwise c is a fimath-less fi
if coder.const(eml_fimathislocal(a))
    c = fi(zeros(size(a)), 'numerictype', ufix1, 'fimath', eml_fimath(a));
else
    c = fi(zeros(size(a)), 'numerictype', ufix1);
end

% lidx and ridx must be constant for eml_bitxorreduce.
% We can simply use double() to get real world index
% values, regardless of the data type of lidx and ridx.
lidx_const = coder.const(double(lidx) - 1);
ridx_const = coder.const(double(ridx) - 1);

for k = 1:numel(c)
    c(k) = eml_bitxorreduce(a(k), lidx_const, ridx_const);
end
