function x = eml_ifftshift(x,dim)
%MATLAB Code Generation Private Function

%   IFFTSHIFT of X along dimension DIM.  DIM is not validated.

%   Copyright 2006-2020 The MathWorks, Inc.
%#codegen

coder.internal.allowHalfInputs;
eml_invariant(nargin == 2, ...
    'MATLAB:minrhs');
coder.internal.prefer_const(dim);
if dim > ndims(x)
    return
end
vlen = size(x,dim);
if vlen <= 1
    return
end
vlend2 = coder.internal.indexDivide(vlen,2);
if coder.internal.indexTimes(vlend2,2) == vlen
    % Even length dimensions are handled with eml_fftshift.
    x = eml_fftshift(x,dim);
    return
end
vstride = coder.internal.prodsize(x,'below',dim);
npages = coder.internal.prodsize(x,'above',dim);
vspread = coder.internal.indexTimes(coder.internal.indexMinus(vlen,1),vstride);
midoffset = coder.internal.indexTimes(vlend2,vstride);
i2 = zeros(coder.internal.indexIntClass);
for i = 1:npages
    i1 = i2;
    i2 = coder.internal.indexPlus(i2,vspread);
    for j = 1:vstride
        i1 = coder.internal.indexPlus(i1,1);
        i2 = coder.internal.indexPlus(i2,1);
        % Unshift x(i1:vstride:i2))
        ia = coder.internal.indexPlus(i1,midoffset);
        ib = i2;
        xtmp = x(ib);
        for k = 1:vlend2
            ia = coder.internal.indexMinus(ia,vstride);
            ic = ib;
            ib = coder.internal.indexMinus(ic,vstride);
            x(ic) = x(ia);
            x(ia) = x(ib);
        end
        x(ib) = xtmp;
    end
end

% LocalWords:  Unshift vstride
