function lbxfi = lowerbound(xfi)
% MATLAB Code Generation Library function for @fi/lowerbound.
%
% LOWERBOUND(A) will return true if A is signed, false  if unsigned.

% $INCLUDE(DOC) toolbox/eml/lib/fixedpoint/lowerbound.m $
% Copyright 2002-2014 The MathWorks, Inc.
%#codegen

eml_allow_mx_inputs;

% Check for nargin and assert if not 1
eml_invariant(nargin==1, 'fixed:coder:incorrectNumberOfInputs');

if eml_ambiguous_types
    lbxfi = eml_not_const(0);
    return;
end

eml_invariant(isfi(xfi), ...
    'fixed:fi:undefinedFunctionGeneral', 'LOWERBOUND', class(xfi));

Tx = eml_typeof(xfi);
Fx = eml_fimath(xfi);

if isfixed(xfi)
    % Fixed FI
    outHasLocalFimath = coder.const(eml_fimathislocal(xfi));
    if issigned(xfi)
        lbxfi1 = eml_dress(uint32(0),Tx,Fx);
        lbxfi2 = stripscaling(lbxfi1);
        lbxfi3 = bitset(lbxfi2,Tx.WordLength);
        lbxfi = eml_fimathislocal(eml_dress(lbxfi3,Tx,Fx),outHasLocalFimath);
    else % is unsigned
        lbxfi = eml_fimathislocal(eml_dress(uint32(0),Tx,Fx),outHasLocalFimath);
    end

elseif isscaleddouble(xfi)
    xWl = Tx.WordLength;
    xFl = Tx.FractionLength;
    xSign = Tx.Signed;
    xBias = Tx.Bias;
    xSaf = Tx.SlopeAdjustmentFactor;
    lbxVal = -2^(xWl-xFl-1)*xSign*xSaf + xBias;                 
    lbxfi = eml_fimathislocal(fi(lbxVal,Tx,Fx),eml_fimathislocal(xfi));    
    
elseif isfloat(xfi)
    % True Double or True Single FI
    dType    = eml_fi_getDType(xfi);
    lbxfi    = eml_cast(-realmax(dType),Tx,Fx);
else
    % FI datatype not supported
    eml_fi_assert_dataTypeNotSupported('LOWERBOUND');
end
