You can define code replacement entries for multiplication and division operations on fixed-point data types such that they match the net slope between operator inputs and output. The net slope entries can map a range of slope and bias values to a replacement function for multiplication or division.
This example shows how to develop a code replacement library to optimize the performance
of fixed point net slope operations by providing information on how to define code replacement
for a divide
operation. To develop a code replacement library use either
the interactive or programmatic approach. For more information, see Develop a Code Replacement Library.
Open the Code Replacement Tool (crtool), from the MATLAB command line with the following command:
>>crtool
Create a table.
From the crtool context menu, select File > New Table.
In the right pane, name the table
crl_table_netslopeScaling
. 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 Divide
.
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
, and the input argument, u1
and
u2
with the Data Type of
fixdt(true,16,*,*)
and the Argument Type
of Scalar.
Create the implementation representation. The implementation representation
describes the signature of the optimization function. In the Function
arguments subsection of the crtool, specify the return argument,
y1
, and the input argument, u1
and
u2
with the Data Type of int
16
. Also set the Net slope Adjustment Factor as 1
and Net Fixed Exponent as 0.
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.
Open the programmatic interface from the MATLAB menu by selecting New > Function.
Create a table.
Create a function with the name of your code replacement library table that does not have arguments and returns a table object. You can use this function to call your code replacement library table.
Create a table object by calling RTW.TflTable
.
function hTable = crl_table_netslopeScaling % 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.TflCOperationEntryGenerator_NetSlope
.
function hTable = crl_table_netslopeScaling % Create a function to call the code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntryGenerator_NetSlope;
Set operator entry parameters with a call to the setTflCOperationEntryParameters
function. The
parameters specify the type of operation as division, the saturation
mode as wrap on overflow, rounding modes as unspecified, and the
name of the replacement function as user_div_*
.
NetSlopeAdjustmentFactor
and
NetFixedExponent
specify the
F
and E
parts of the
net slope
.F
2E
function hTable = crl_table_netslopeScaling % Create a function to call the code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntryGenerator_NetSlope; %% Create entry parameters hEntry.setTflCOperationEntryParameters(... 'Key', 'RTW_OP_DIV', ... 'Priority', 90, ... 'SaturationMode', 'RTW_WRAP_ON_OVERFLOW', ... 'RoundingModes', {'RTW_ROUND_CEILING'}, ... 'NetSlopeAdjustmentFactor', 1.0, ... 'NetFixedExponent', 0.0, ... 'ImplementationName', 's16_div_s16_s16', ... 'ImplementationHeaderFile', 's16_div_s16_s16.h', ... 'ImplementationSourceFile', 's16_div_s16_s16.c');
Create conceptual arguments y1
,
u1
, and u2
. This
example uses calls to the createAndAddConceptualArg
function to create and
add an argument with one function call. Specify each argument as
fixed-point, 16 bits, and signed. Also, for each argument, specify
that code replacement request processing does
not check for an exact match to the
call-site slope and bias values.
function hTable = crl_table_netslopeScaling % Create a function to call the code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntryGenerator_NetSlope; %% Create entry parameters hEntry.setTflCOperationEntryParameters(... 'Key', 'RTW_OP_DIV', ... 'Priority', 90, ... 'SaturationMode', 'RTW_WRAP_ON_OVERFLOW', ... 'RoundingModes', {'RTW_ROUND_CEILING'}, ... 'NetSlopeAdjustmentFactor', 1.0, ... 'NetFixedExponent', 0.0, ... 'ImplementationName', 's16_div_s16_s16', ... 'ImplementationHeaderFile', 's16_div_s16_s16.h', ... 'ImplementationSourceFile', 's16_div_s16_s16.c'); %% Create the conceptual representation createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'y1', ... 'IOType', 'RTW_IO_OUTPUT', ... 'CheckSlope', false, ... 'CheckBias', false, ... 'DataType', 'Fixed', ... 'IsSigned', true, ... 'WordLength', 16); createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'u1', ... 'IOType', 'RTW_IO_INPUT', ... 'CheckSlope', false, ... 'CheckBias', false, ... 'DataType', 'Fixed', ... 'IsSigned', true, ... 'WordLength', 16); createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'u2', ... 'IOType', 'RTW_IO_INPUT', ... 'CheckSlope', false, ... 'CheckBias', false, ... 'DataType', 'Fixed', ... 'IsSigned', true, ... 'WordLength', 16);
Create the implementation representation. The implementation
representation describes the signature of the optimization function.
This example uses calls to the createAndSetCImplementationReturn
and createAndAddImplementationArg
functions to
create and add implementation arguments to the entry. Implementation
arguments must describe fundamental numeric data types (not
fixed-point data types). In this case, the output and input
arguments are 16 bits and signed (int16
).
function hTable = crl_table_netslopeScaling % Create a function to call the code replacement library table %% Create a table object hTable = RTW.TflTable; %% Create an entry hEntry = RTW.TflCOperationEntryGenerator_NetSlope; %% Create entry parameters hEntry.setTflCOperationEntryParameters(... 'Key', 'RTW_OP_DIV', ... 'Priority', 90, ... 'SaturationMode', 'RTW_WRAP_ON_OVERFLOW', ... 'RoundingModes', {'RTW_ROUND_CEILING'}, ... 'NetSlopeAdjustmentFactor', 1.0, ... 'NetFixedExponent', 0.0, ... 'ImplementationName', 's16_div_s16_s16', ... 'ImplementationHeaderFile', 's16_div_s16_s16.h', ... 'ImplementationSourceFile', 's16_div_s16_s16.c'); %% Create the conceptual representation createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'y1', ... 'IOType', 'RTW_IO_OUTPUT', ... 'CheckSlope', false, ... 'CheckBias', false, ... 'DataType', 'Fixed', ... 'IsSigned', true, ... 'WordLength', 16); createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'u1', ... 'IOType', 'RTW_IO_INPUT', ... 'CheckSlope', false, ... 'CheckBias', false, ... 'DataType', 'Fixed', ... 'IsSigned', true, ... 'WordLength', 16); createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'u2', ... 'IOType', 'RTW_IO_INPUT', ... 'CheckSlope', false, ... 'CheckBias', false, ... 'DataType', 'Fixed', ... 'IsSigned', true, ... 'WordLength', 16); %% Create the Implementation Representation createAndSetCImplementationReturn(hEntry, 'RTW.TflArgNumeric', ... 'Name', 'y1', ... 'IOType', 'RTW_IO_OUTPUT', ... 'IsSigned', true, ... 'WordLength', 16, ... 'FractionLength', 0); createAndAddImplementationArg(hEntry, 'RTW.TflArgNumeric',... 'Name', 'u1', ... 'IOType', 'RTW_IO_INPUT', ... 'IsSigned', true, ... 'WordLength', 16, ... 'FractionLength', 0); createAndAddImplementationArg(hEntry, 'RTW.TflArgNumeric',... 'Name', 'u2', ... 'IOType', 'RTW_IO_INPUT', ... 'IsSigned', true, ... 'WordLength', 16, ... 'FractionLength', 0); %% 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_netslopeScaling
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 by using 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 net slope scaling’; this(1).TableList = {'crl_table_netslopeScaling.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.