Data Type Conversions (Casts) and Operator Code Replacement
You can use code replacement entries to replace code that the code generator produces for data type conversion (cast) operations. To develop a code replacement library use either the interactive or programmatic approach. For more information, see Develop a Code Replacement Library.
Interactively Develop a Code Replacement Library
Open the Code Replacement Tool (crtool), at the MATLAB command line, enter:
>>crtool
Create a table.
From the crtool context menu, select File > New Table.
In the right pane, name the table
crl_table_cast_int32_to_int16
. Click Apply.
Create an entry. From the crtool context menu, select File > New entry > Fixed Point Net Slope Operation.
Create entry parameters. In the Operation drop-down list, select
Cast
.Create the conceptual representation. The conceptual representation describes the signature of the function that you want to replace. In the Conceptual function subsection of the crtool, specify the return argument,
y1
with the Data Type ofint16
, and the input argument,u1
with the Data Type ofint32
and the Argument Type ofScalar
.Create the implementation representation. The implementation representation describes the signature of the optimization function. For this example, to specify that the implementation arguments have the same order and properties as the conceptual arguments, select the Make conceptual and implementation argument types the same check box. In the Function arguments subsection of the crtool, specify the return argument,
y1
, and the input argument,u1
with the Data Type ofint16
andint32
.Specify a Name for the replacement function under Function prototype.
Specify build information. Click the Build Information tab to open the build requirements pane. Specify the files (source, header, object) that the code generator requires for code replacement. For this example, you do not need to specify build information.
Validate and save the table. In the Mapping Information tab, click Validate entry. In the crtool context menu, select File > Save table > Save.
Register a code replacement library. Registration creates a library composed of the tables that you specify. Select File > Generate registration file. In the Generate registration file dialog box, fill out these fields:
To use your code replacement library, refresh your current MATLAB session with the command:
>>sl_refresh_customizations
Verify the code replacement library. From the MATLAB command line, open the library by using the Code Replacement Viewer and verify that the table and entry are correctly specified. For more information, see Verify Code Replacement Library. Configure your model to use the code replacement library, generate code, and verify that replacement occurs as expected. If unexpected behavior occurs, examine the hit and miss logs to troubleshoot the issues.
Programmatically Develop a Code Replacement Library
Open the programmatic interface from the MATLAB menu by selecting New > Function.
Create a table.
Create a function to call your code replacement library table. The function should not have arguments and return a table object.
Create a table object by calling
RTW.TflTable
.
function hTable = crl_table_cast_int32_to_int16() % Create a function to call the code replacement library table %% Create a table object hTable = RTW.TflTable;
Create an entry. Because this example replaces a function, create a code replacement entry in your table by calling the entry function
RTW.TflCOperationEntry
.function hTable = crl_table_cast_int32_to_int16() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntry;
Create entry parameters. Because this examples replaces a function, create entry parameters by calling the function
setTflCOperationEntryParameters
. The parameters specify the type of operation as cast, the saturation mode as saturate on integer overflow, rounding modes as toward negative infinity, and the name of the replacement function asmy_sat_cast
.function hTable = crl_table_cast_int32_to_int16() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntry; %% Create entry parameters hEntry.setTflCOperationEntryParameters(... 'Key', 'RTW_OP_CAST', ... 'Priority', 50, ... 'ImplementationName', 'my_sat_cast', ... 'SaturationMode', 'RTW_SATURATE_ON_OVERFLOW', ... 'RoundingModes', {'RTW_ROUND_FLOOR'}, ... 'ImplementationHeaderFile', 'some_hdr.h', ... 'ImplementationSourceFile', 'some_hdr.c');
Create the conceptual representation. The conceptual representation describes the signature of the function that you want to replace. To explicitly specify argument properties, call the function
getTflArgFromString
.function hTable = crl_table_cast_int32_to_int16() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntry; %% Create entry parameters hEntry.setTflCOperationEntryParameters(... 'Key', 'RTW_OP_CAST', ... 'Priority', 50, ... 'ImplementationName', 'my_sat_cast', ... 'SaturationMode', 'RTW_SATURATE_ON_OVERFLOW', ... 'RoundingModes', {'RTW_ROUND_FLOOR'}, ... 'ImplementationHeaderFile', 'some_hdr.h', ... 'ImplementationSourceFile', 'some_hdr.c'); %% Create the conceptual representation arg = getTflArgFromString(hTable, 'y1', 'int16'); arg.IOType = 'RTW_IO_OUTPUT'; addConceptualArg(hEntry, arg); hEntry.Implementation.setReturn(arg);
Create the implementation representation. The implementation representation describes the signature of the optimization function. To specify that the implementation arguments have the same order and properties as the conceptual arguments, call the function
getTflArgFromString
. Add the complete entry to the table by calling the functionaddEntry
.function hTable = crl_table_cast_int32_to_int16() % Create a code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntry; %% Create entry parameters hEntry.setTflCOperationEntryParameters(... 'Key', 'RTW_OP_CAST', ... 'Priority', 50, ... 'ImplementationName', 'my_sat_cast', ... 'SaturationMode', 'RTW_SATURATE_ON_OVERFLOW', ... 'RoundingModes', {'RTW_ROUND_FLOOR'}, ... 'ImplementationHeaderFile', 'some_hdr.h', ... 'ImplementationSourceFile', 'some_hdr.c'); %% Create the conceptual and implementation representation arg = getTflArgFromString(hTable, 'y1', 'int16'); arg.IOType = 'RTW_IO_OUTPUT'; addConceptualArg(hEntry, arg); hEntry.Implementation.setReturn(arg); arg = getTflArgFromString(hTable, 'u1', 'int32'); addConceptualArg(hEntry, arg); hEntry.Implementation.addArgument(arg); %% Add the entry to the table addEntry(hTable,hEntry);
Specify build information. In the entry parameters, specify files (header, source, object) that the code generator needs for code replacement. For this example, build information is not required.
Validate and save the customization file. From the MATLAB menu, save this customization file by selecting File > Save. From the command line, validate the code replacement library table by calling it:
>> hTable = crl_table_cast_int32_to_int16
Register the code replacement library. Registration creates a code replacement library by defining the library name, code replacement tables, and other information. Create a registration file with these specifications:
function rtwTargetInfo(cm) cm.registerTargetInfo(@loc_register_crl); end function this = loc_register_crl this(1) = RTW.TflRegistry; this(1).Name = 'CRL for data type conversions(casts) and operator code replacement’; this(1).TableList = {'crl_table_cast_int32_to_int16.m'}; % table created in this example this(1).TargetHWDeviceType = {'*'}; this(1).Description = 'Example code replacement library'; end
To use your code replacement library, refresh your current MATLAB session with the command:
>>sl_refresh_customizations
Verify the code replacement library. From the MATLAB command line, open the library by using the Code Replacement Viewer and verify that the table and entry are correctly specified. For more information, see Verify Code Replacement Library. Configure your model to use the code replacement library, generate code, and verify that replacement occurs as expected. If unexpected behavior occurs, examine the hit and miss logs to troubleshoot the issues.