Main Content

Disable Nonfinite Checks or Inlining for Math Functions

When the code generator produces code for math functions:

  • If the model option Support non-finite numbers is selected, nonfinite number checking is generated uniformly for math functions, without the ability to specify that nonfinite number checking should be generated for some functions, but not for others.

  • By default, inlining is applied uniformly for math functions, without the ability to specify that inlining should be generated for some functions, while invocations should be generated for others.

You can use code replacement library (CRL) customization entries to:

  • Selectively disable nonfinite checks for math functions. This can improve the execution speed of the generated code.

  • Selectively disable inlining of math functions. This can increase code readability and decrease code size.

The functions for which these customizations are supported include the following:

  • Floating-point only: atan2, copysign, fix, hypot, log, log10, round, sincos, and sqrt

  • Floating-point and integer: abs, max, min, mod, rem, saturate, and sign

The general workflow for disabling nonfinite number checking and/or inlining is as follows:

  1. If you can disable nonfinite number checking for a particular math function, or if you want to disable inlining for a particular math function and instead generate a function invocation, you can copy the following MATLAB® function code into a MATLAB file with an .m file name extension, for example, crl_table_customization.m.

    function hTable = crl_table_customization
        
    % Create an instance of the Code Replacement Library table for controlling
    % function intrinsic inlining and nonfinite support
    
    hTable = RTW.TflTable;
    
    % Inline - true (if function needs to be inline)
    %          false (if function should not be inlined)
    % SNF (support nonfinite) - ENABLE (if non-finite checking should be performed)
    %                           DISABLE (if non-finite checking should NOT be performed)
    %                           UNSPECIFIED (Default behavior)
    
    % registerCustomizationEntry(hTable, ...
    %       Priority, numInputs, key, inType, outType, Inline, SNF);
    
    registerCustomizationEntry(hTable, ...
            100, 2, 'atan2', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'atan2', 'single', 'single', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'sincos', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sincos', 'single', 'single', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'double', 'double', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'single', 'single', true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'int32',  'int32',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'int16',  'int16',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'int8',   'int8',   true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'integer','integer',true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'uint32', 'uint32', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'uint16', 'uint16', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'abs', 'uint8',  'uint8',  true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 2, 'hypot', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'hypot', 'single', 'single', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'log', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'log', 'single', 'double', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'log10', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'log10', 'single', 'double', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'double', 'double', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'single', 'single', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'int32',  'int32',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'int16',  'int16',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'int8',   'int8',   true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'uint32', 'uint32', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'uint16', 'uint16', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'uint8',  'uint8',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'min', 'integer','integer',true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'double', 'double', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'single', 'single', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'int32',  'int32',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'int16',  'int16',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'int8',   'int8',   true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'uint32', 'uint32', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'uint16', 'uint16', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'uint8',  'uint8',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'max', 'integer','integer',true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'single', 'single', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'int32',  'int32',  false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'int16',  'int16',  false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'int8',   'int8',   false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'uint32', 'uint32', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'uint16', 'uint16', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'mod', 'uint8',  'uint8',  false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'single', 'single', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'int32',  'int32',  false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'int16',  'int16',  false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'int8',   'int8',   false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'uint32', 'uint32', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'uint16', 'uint16', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 2, 'rem', 'uint8',  'uint8',  false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'round', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'round', 'single', 'single', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'double', 'double', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'single', 'single', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'int32',  'int32',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'int16',  'int16',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'int8',   'int8',   true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'uint32', 'uint32', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'uint16', 'uint16', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'uint8',  'uint8',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 3, 'saturate', 'integer','integer',true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'double', 'double',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'single', 'single',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'int32',  'integer', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'int16',  'integer', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'int8',   'integer', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'uint32', 'uint32',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'uint16', 'uint16',  true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'uint8',  'uint8',   true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sign', 'integer','integer', true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'sqrt', 'double', 'double', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'sqrt', 'single', 'single', true, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'fix', 'double', 'double', false, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'fix', 'single', 'single', false, 'UNSPECIFIED');
    
    registerCustomizationEntry(hTable, ...
            100, 1, 'copysign', 'double', 'double', true, 'UNSPECIFIED');
    registerCustomizationEntry(hTable, ...
            100, 1, 'copysign', 'single', 'single', true, 'UNSPECIFIED');
  2. To reduce the size of the file, you can delete the registerCustomizationEntry lines for functions for which the default nonfinite number checking and inlining behavior is acceptable.

  3. For each remaining entry,

    • Set the Inline argument to true if the function should be inlined or false if it should not be inlined.

    • Set the SNF argument to ENABLE if nonfinite checking should be generated, DISABLE if nonfinite checking should not be generated, or UNSPECIFIED to accept the default behavior based on the model option settings.

    Save the file.

  4. Optionally, perform a quick check of the syntactic validity of the customization table entries by invoking the table definition file at the MATLAB command line (>> tbl = crl_table_customization). Fix syntax errors that are flagged.

  5. Optionally, view the customization table entries in the Code Replacement Viewer (>> crviewer(crl_table_customization)). For more information about viewing code replacement tables, see Configure a Code Replacement Library.

  6. To register these changes and make them appear in the Code replacement library drop-down list located on the Code Generation > Interface pane of the Configuration Parameters dialog box, first copy the following MATLAB function code into an instance of the file rtwTargetInfo.m.

    Note

    For the example below, specify the argument 'RTW' if a GRT target is selected for your model, otherwise omit the argument.

    function rtwTargetInfo(cm)
    % rtwTargetInfo function to register a code replacement library (CRL)
    
      % Register the CRL defined in local function locCrlRegFcn
      cm.registerTargetInfo(@locCrlRegFcn);
    
    end % End of RTWTARGETINFO
    
    
    % Local function to define a CRL containing crl_table_customization
    function thisCrl = locCrlRegFcn
    
      % Instantiate a CRL registry entry - specify 'RTW' for GRT
      thisCrl = RTW.TflRegistry('RTW');
    
      % Define the CRL properties
      thisCrl.Name = 'CRL Customization Example';
      thisCrl.Description = 'Example  of CRL Customization';
      thisCrl.TableList = {'crl_table_customization'};
      thisCrl.TargetHWDeviceType = {'*'};
      
    end % End of LOCCRLREGFCN

    You can edit the Name field to specify the library name that appears in the Code replacement library drop-down list. Also, the file name in the TableList field must match the name of the file you created in step 1.

    To register your changes, with both of the MATLAB files you created present in the MATLAB path, enter the following command at the MATLAB command line:

    sl_refresh_customizations
  7. Create or open a model that generates function code corresponding to one of the math functions for which you specified a change in nonfinite number checking or inlining behavior.

  8. Open the Configuration Parameters dialog box, go to the Code Generation > Interface pane, and use the Code replacement library drop-down list to select the code replacement entry you registered in step 6, for example, CRL Customization Example.

  9. Generate code for the model and examine the generated code to verify that the math functions are generated as expected.