function y = linspace(d1,d2,n)
%MATLAB Code Generation Library Function

%   Limitations:
%     If supplied, the number of points N must be real and finite.

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

% This file is prepped for half support.
% coder.internal.allowHalfInputs;
coder.internal.assert(nargin > 1,'MATLAB:minrhs');
if nargin < 3
    n = cast(100,coder.internal.indexIntClass);
end
coder.internal.prefer_const(n);
coder.internal.assert(isa(d1,'float') && isscalar(d1), ...
    'Coder:toolbox:linspace_2', ...
    'IfNotConst','Fail');
coder.internal.assert(isa(d2,'float') && isscalar(d2), ...
    'Coder:toolbox:linspace_3', ...
    'IfNotConst','Fail');
coder.internal.assert(coder.internal.isConst(n) || ...
    eml_option('VariableSizing'), ...
    'Coder:toolbox:linspace_4', ...
    'IfNotConst','Fail');
coder.internal.assert(isreal(n) && isscalar(n) && ...
    coder.internal.isBuiltInNumeric(n), ...
    'Coder:toolbox:linspace_5', ...
    'IfNotConst','Fail');
outputEg = coder.internal.scalarEg(d1,d2);
if ~(n(1) >= 0)
    coder.internal.assert(~isnan(n),'Coder:toolbox:MustNotBeNaN','N');
    y = eml_expand(outputEg,[1,0]);
    return
end
if isa(d1,'half') || isa(d2,'half')
    arithClass = 'single';
else
    arithClass = class(outputEg);
end
y = Linspace(d1,d2,n,outputEg,arithClass);

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

function y = Linspace(d1,d2,n1,outputEg,arithClass)
coder.inline('always');
coder.internal.prefer_const(n1,outputEg,arithClass);
y = coder.nullcopy(eml_expand(outputEg,[1,floor(n1)]));
n = coder.internal.indexInt(numel(y));
if size(y,2) >= 1
    y(n) = d2;
    if size(y,2) >= 2
        y(1) = d1;
        if size(y,2) >= 3
            RMD2 = realmax(arithClass)/2;
            if d1 == -d2 && n > 2 % Make MISRA exception here.
                % Supplying d1 exactly equal to -d2 is a flag of sorts to
                % produce a perfectly symmetrical result. In other
                % circumstances, if d1 turns out only to be approximately
                % equal to -d2, then the execution will fall through to
                % produce an approximately symmetric result.
                for k = 2:n-1
                    y(k) = d2*(cast(2*k-n-1,arithClass)/cast(n-1,arithClass));
                end
                ONE = coder.internal.indexInt(1);
                if coder.internal.isSizeOdd(n)
                    y(eml_rshift(n,ONE) + ONE) = 0;
                end
            elseif ...
                    (((real(d1) < 0) ~= (real(d2) < 0)) && ...
                    (eml_fabs(real(d1)) > RMD2 || eml_fabs(real(d2)) > RMD2)) ...
                    || ...
                    (((imag(d1) < 0) ~= (imag(d2) < 0)) && ...
                    (eml_fabs(imag(d1)) > RMD2 || eml_fabs(imag(d2)) > RMD2))
                % Avoid computing d2 - d1 because it may overflow.
                delta1 = d1/cast(size(y,2)-1,arithClass);
                delta2 = d2/cast(size(y,2)-1,arithClass);
                for k = 1:size(y,2)-2
                    y(k+1) = d1 + ...
                        delta2*cast(k,arithClass) - ...
                        delta1*cast(k,arithClass);
                end
            else
                delta1 = (d2 - d1)/cast(size(y,2)-1,arithClass);
                for k = 1:size(y,2)-2
                    y(k+1) = d1 + cast(k,arithClass)*delta1;
                end
            end
        end
    end
end

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