function iA = storedInteger(A)
% MATLAB Code Generation library function for the @fi/storedInteger

% $INCLUDE(DOC) toolbox/eml/lib/fixedpoint/@embedded/@fi/storedInteger.m $
% Copyright 2011-2014 The MathWorks, Inc.
%#codegen

  
% Check for ambiguous types and return with the correct size output
if eml_ambiguous_types
    iA = eml_not_const(zeros(size(A)));
    return;
end

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

if isfi(A) && isfixed(A)
    % FI object with Fixed datatype

    % Get the word length & signedness of A
    Ta  = numerictype(A);
    wlA = coder.const(get(Ta,'WordLength'));
    isSignedA = coder.const(get(Ta,'Signed'));

    % Switch on the wordlength & signedness and return
    % the correct storedInteger
    if isSignedA
        if wlA <= 8
            iA = eml_fi_getStoredIntValAsDType(A,'int8');
        elseif wlA <= 16
            iA = eml_fi_getStoredIntValAsDType(A,'int16');
        elseif wlA <= 32
            iA = eml_fi_getStoredIntValAsDType(A,'int32');
        elseif wlA <= 64
            iA = eml_fi_getStoredIntValAsDType(A,'int64'); 
        else 
            eml_invariant(false, ...
                'fixed:fi:storedIntegerOver64bits', 'storedInteger');
        end
    else
        if wlA <= 8
            iA = eml_fi_getStoredIntValAsDType(A,'uint8');
        elseif wlA <= 16
            iA = eml_fi_getStoredIntValAsDType(A,'uint16');
        elseif wlA <= 32
            iA = eml_fi_getStoredIntValAsDType(A,'uint32');
        elseif wlA <= 64
            iA = eml_fi_getStoredIntValAsDType(A,'uint64');
        else 
            eml_invariant(false, ...
                'fixed:fi:storedIntegerOver64bits', 'storedInteger');
        end
    end

elseif isfi(A) && isscaleddouble(A)
    % Scaled Double fi
    Ta  = numerictype(A);
    flA = Ta.FractionLength;
    biasA = Ta.Bias;
    safA = Ta.SlopeAdjustmentFactor;
    iA = ((double(A)-biasA).*2^flA)./safA;    
    
elseif isfi(A) && isfloat(A)
    % True Double or True Single FI

    dType = eml_fi_getDType(A);
    iA    = eml_cast(A,dType);
    
else
    %The input must be a fi object with fixed-point, double or single data type
    eml_fi_assert_dataTypeNotSupported('storedInteger');

end
