function DataType = fixdtUpdate(DataType,Scaling,giveWarning)
%fixdtUpdate convert data type specification to numeric type object
%
% If the data type is specified using the deprecated structure format,
% then the specification will be converted to a numeric type object.
% Otherwise, the data type specification will be left unchanged.
%
% Usage
%    DataType = fixdtUpdate(DataType,Scaling,giveWarning);
%   
%    giveWarning (default false) 
%                Issue a warning when structure format is encountered.
%
%    See also FIXDT, NUMERICTYPE.
 
% Copyright 2012 The MathWorks, Inc.

if isstruct(DataType)

    switch DataType.Class
    
        case 'FIX'

          if nargin > 1 && ~isempty(Scaling)
              
              Slope = Scaling(1);
              
              if numel(Scaling) > 1
                  
                  Bias = Scaling(2);

                  DataType = fixdt(...
                      DataType.IsSigned,...
                      DataType.MantBits,...
                      Slope,...
                      Bias);
                  
              else

                  [ff,ee]=log2(Slope);
                  
                  if 0.5 == ff

                      FractionLength = 1 - ee;
                      
                      DataType = fixdt(...
                          DataType.IsSigned,...
                          DataType.MantBits,...
                          FractionLength);
                  else
                      DataType = fixdt(...
                          DataType.IsSigned,...
                          DataType.MantBits,...
                          Slope,...
                          0);
                  end
              end
          else
              DataType = fixdt(...
                  DataType.IsSigned,...
                  DataType.MantBits);
          end
          
        case 'INT'
            DataType = fixdt(...
                DataType.IsSigned,...
                DataType.MantBits,...
                0);

        case 'FRAC'
            DataType = fixdt(...
                DataType.IsSigned,...
                DataType.MantBits,...
                DataType.MantBits-DataType.IsSigned-DataType.GuardBits);

        case 'DOUBLE'
            DataType = fixdt('double');

        case 'SINGLE'
            DataType = fixdt('single');

        otherwise
            DAStudio.error('modelexplorer:numericType:fixptDataTypeClassNotSupported');
    end
    if nargin > 2 && giveWarning
       MSLDiagnostic('Simulink:DataType:OldDataTypeSpecification').reportAsWarning;
    end
end
