function iAmax = intmax(A)
% MATLAB Code Generation library function for the @fi/intmax

% $INCLUDE(DOC) toolbox/eml/lib/fixedpoint/@embedded/@fi/intmax.m $
% Copyright 2002-2017 The MathWorks, Inc.
%#codegen
  
eml_allow_mx_inputs;

% Check for correct number of input arguments i.e. 1
eml_invariant(nargin==1, 'fixed:coder:incorrectNumberOfInputs');

if isfixed(A)
    % Fixed FI

    % Get the word length & signedness of A
    Ta  = numerictype(A);
    Fa = eml_fimath(A);
    wlA = Ta.WordLength;
    isSignedA = Ta.Signed;
    
    % Create the output fi. If A is unsigned then it is enough to create
    % an initial input of all zeros and then to a bitcmp. If A is signed, then doing bitset of the MSB 
    % only and then the bitcmp will return the intmax value.
    Tout = numerictype(isSignedA,wlA,0);
    iTemp = eml_cast(0,Tout,Fa);
    if ~coder.const(eml_fimathislocal(A))
        iAmax = eml_fimathislocal(iTemp,false);
    else
        iAmax = iTemp;
    end
    if isSignedA
        iAmax = bitset(iAmax,wlA);
    end
    iAmax = bitcmp(iAmax);

elseif isscaleddouble(A)
    Ta  = numerictype(A);
    Fa = eml_fimath(A);
    wlA = Ta.WordLength;
    isSignedA = Ta.Signed;
    Tout = numerictype(isSignedA, wlA, 0, 'DataType', 'ScaledDouble');

    iTemp = fi(2 ^ (wlA - (1*isSignedA)) - 1, Tout, Fa);
    if ~coder.const(eml_fimathislocal(A))
        iAmax = eml_fimathislocal(iTemp,false);
    else
        iAmax = iTemp;
    end
    
elseif isfloat(A)
    % True Double or True Single FI
    iAmax = upperbound(A);
else
    % FI datatype not supported
    eml_fi_assert_dataTypeNotSupported('INTMAX');
end