function x = gammaln(x)
%MATLAB Code Generation Library Function

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

eml_invariant(nargin == 1,'MATLAB:minrhs');
eml_invariant(isfloat(x),'Coder:toolbox:gammaln_2');
eml_invariant(isreal(x) && ~issparse(x),'MATLAB:gammaln:notFullReal');
x = coder.internal.applyScalarFunctionInPlace(mfilename,@scalar_gammaln,x);

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

function x = scalar_gammaln(x)
if isa(x,'single')
    frtbig = single(1.42e9);
    xbig = single(4.08e36);
else
    frtbig = 2.25e76;
    xbig = 2.55e305;
end
d1 = cast(-5.772156649015328605195174e-1,'like',real(x));
p1 = cast([4.945235359296727046734888e0; 2.018112620856775083915565e2;
    2.290838373831346393026739e3; 1.131967205903380828685045e4;
    2.855724635671635335736389e4; 3.848496228443793359990269e4;
    2.637748787624195437963534e4; 7.225813979700288197698961e3], ...
    'like',real(x));
q1 = cast([6.748212550303777196073036e1; 1.113332393857199323513008e3;
    7.738757056935398733233834e3; 2.763987074403340708898585e4;
    5.499310206226157329794414e4; 6.161122180066002127833352e4;
    3.635127591501940507276287e4; 8.785536302431013170870835e3], ...
    'like',real(x));
d2 = cast(4.227843350984671393993777e-1,'like',real(x));
p2 = cast([4.974607845568932035012064e0; 5.424138599891070494101986e2;
    1.550693864978364947665077e4; 1.847932904445632425417223e5;
    1.088204769468828767498470e6; 3.338152967987029735917223e6;
    5.106661678927352456275255e6; 3.074109054850539556250927e6], ...
    'like',real(x));
q2 = cast([1.830328399370592604055942e2; 7.765049321445005871323047e3;
    1.331903827966074194402448e5; 1.136705821321969608938755e6;
    5.267964117437946917577538e6; 1.346701454311101692290052e7;
    1.782736530353274213975932e7; 9.533095591844353613395747e6], ...
    'like',real(x));
d4 = cast(1.791759469228055000094023e0,'like',real(x));
p4 = cast([1.474502166059939948905062e4; 2.426813369486704502836312e6;
    1.214755574045093227939592e8; 2.663432449630976949898078e9;
    2.940378956634553899906876e10; 1.702665737765398868392998e11;
    4.926125793377430887588120e11; 5.606251856223951465078242e11], ...
    'like',real(x));
q4 = cast([2.690530175870899333379843e3; 6.393885654300092398984238e5;
    4.135599930241388052042842e7; 1.120872109616147941376570e9;
    1.488613728678813811542398e10; 1.016803586272438228077304e11;
    3.417476345507377132798597e11; 4.463158187419713286462081e11], ...
    'like',real(x));
c = cast([-1.910444077728e-03; 8.4171387781295e-04;
    -5.952379913043012e-04; 7.93650793500350248e-04;
    -2.777777777777681622553e-03; 8.333333333333333331554247e-02;
    5.7083835261e-03], ...
    'like',real(x));
table100 = cast([
    0.00000000000000000e+00;     0.00000000000000000e+00;
    6.93147180559945309e-01;     1.79175946922805500e+00;
    3.17805383034794562e+00;     4.78749174278204599e+00;
    6.57925121201010100e+00;     8.52516136106541430e+00;
    1.06046029027452502e+01;     1.28018274800814696e+01;
    1.51044125730755153e+01;     1.75023078458738858e+01;
    1.99872144956618861e+01;     2.25521638531234229e+01;
    2.51912211827386815e+01;     2.78992713838408916e+01;
    3.06718601060806728e+01;     3.35050734501368889e+01;
    3.63954452080330536e+01;     3.93398841871994940e+01;
    4.23356164607534850e+01;     4.53801388984769080e+01;
    4.84711813518352239e+01;     5.16066755677643736e+01;
    5.47847293981123192e+01;     5.80036052229805199e+01;
    6.12617017610020020e+01;     6.45575386270063311e+01;
    6.78897431371815350e+01;     7.12570389671680090e+01;
    7.46582363488301644e+01;     7.80922235533153106e+01;
    8.15579594561150372e+01;     8.50544670175815174e+01;
    8.85808275421976788e+01;     9.21361756036870925e+01;
    9.57196945421432025e+01;     9.93306124547874269e+01;
    1.02968198614513813e+02;     1.06631760260643459e+02;
    1.10320639714757395e+02;     1.14034211781461703e+02;
    1.17771881399745072e+02;     1.21533081515438634e+02;
    1.25317271149356895e+02;     1.29123933639127215e+02;
    1.32952575035616310e+02;     1.36802722637326368e+02;
    1.40673923648234259e+02;     1.44565743946344886e+02;
    1.48477766951773032e+02;     1.52409592584497358e+02;
    1.56360836303078785e+02;     1.60331128216630907e+02;
    1.64320112263195181e+02;     1.68327445448427652e+02;
    1.72352797139162802e+02;     1.76395848406997352e+02;
    1.80456291417543771e+02;     1.84533828861449491e+02;
    1.88628173423671591e+02;     1.92739047287844902e+02;
    1.96866181672889994e+02;     2.01009316399281527e+02;
    2.05168199482641199e+02;     2.09342586752536836e+02;
    2.13532241494563261e+02;     2.17736934113954227e+02;
    2.21956441819130334e+02;     2.26190548323727593e+02;
    2.30439043565776952e+02;     2.34701723442818268e+02;
    2.38978389561834323e+02;     2.43268849002982714e+02;
    2.47572914096186884e+02;     2.51890402209723194e+02;
    2.56221135550009525e+02;     2.60564940971863209e+02;
    2.64921649798552801e+02;     2.69291097651019823e+02;
    2.73673124285693704e+02;     2.78067573440366143e+02;
    2.82474292687630396e+02;     2.86893133295426994e+02;
    2.91323950094270308e+02;     2.95766601350760624e+02;
    3.00220948647014132e+02;     3.04686856765668715e+02;
    3.09164193580146922e+02;     3.13652829949879062e+02;
    3.18152639620209327e+02;     3.22663499126726177e+02;
    3.27185287703775217e+02;     3.31717887196928473e+02;
    3.36261181979198477e+02;     3.40815058870799018e+02;
    3.45379407062266854e+02;     3.49954118040770237e+02;
    3.54539085519440809e+02;     3.59134205369575399e+02], ...
    'like',real(x));
ONE = ones('like',x);
ZERO = zeros('like',x);
if isnan(x)
elseif x < 0
    x = coder.internal.nan('like',x);
    coder.internal.error('MATLAB:gammaln:negativeVal');
elseif x > xbig
    x = coder.internal.inf('like',x);
elseif x <= eps(class(x)) % 0 <= x <= eps
    x = -log(x);
elseif x <= 0.5 % eps < x <= 0.5
    xden = ONE;
    xnum = ZERO;
    for i = 1:8
        xnum = xnum*x + p1(i);
        xden = xden*x + q1(i);
    end
    x = -log(x) + (x*(d1 + x*eml_rdivide(xnum,xden)));
elseif x <= 0.6796875 % 0.5 < x <= 0.6796875
    xm1 = (x - 0.5) - 0.5;
    xden = ONE;
    xnum = ZERO;
    for i = 1:8
        xnum = xnum*xm1 + p2(i);
        xden = xden*xm1 + q2(i);
    end
    x = -log(x) + xm1*(d2 + xm1*eml_rdivide(xnum,xden));
elseif x == floor(x) && x <= 100
    x = cast(table100(coder.internal.indexInt(x)),'like',x);
elseif x <= 1.5 % 0.6796875 < x <= 1.5
    xm1 = (x - 0.5) - 0.5;
    xden = ONE;
    xnum = ZERO;
    for i = 1:8
        xnum = xnum*xm1 + p1(i);
        xden = xden*xm1 + q1(i);
    end
    x = xm1*(d1 + xm1*eml_rdivide(xnum,xden));
elseif x <= 4 % 1.5 < x <= 4
    xm2 = x - 2;
    xden = ONE;
    xnum = ZERO;
    for i = 1:8
        xnum = xnum*xm2 + p2(i);
        xden = xden*xm2 + q2(i);
    end
    x = xm2*(d2 + xm2*eml_rdivide(xnum,xden));
elseif x <= 12 % 4 < x <= 12
    xm4 = x - 4;
    xden = -ONE;
    xnum = ZERO;
    for i = 1:8
        xnum = xnum*xm4 + p4(i);
        xden = xden*xm4 + q4(i);
    end
    x = d4 + xm4*eml_rdivide(xnum,xden);
else % x > 12
    if x <= frtbig
        r = cast(c(7),'like',x);
        t = 1/(x*x);
        for i = 1:6
            r = r*t + c(i);
        end
        r = r/x;
    else
        r = ZERO;
    end
    corr = log(x);
    spi = cast(0.9189385332046727417803297,'like',real(x));
    x = r + spi - 0.5*corr + x*(corr - 1);
end

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