Contenido principal

coder.dataAlignment

Specify data alignment for global or entry-point/exported function input and output arguments

Description

coder.dataAlignment('varName',align_value) specifies data alignment in MATLAB® code for the variable (varName), which is imported data or global (exported) data. The code generator aligns the imported or exported data to the alignment boundary (align_value).

Note

To use data alignment, you must specify the alignment information for the compiler by using a code replacement library registration. For more information, see Provide Data Alignment Specifications for Compilers.

example

Examples

collapse all

Generate code for an example function that specifies data alignment for exported data.

Create a function exportedDataExampleFun.m that aligns the data for a global variable z along an 8-byte boundary.

function a = exportedDataExampleFun(b)

global z;
coder.dataAlignment('z',8);

a = b + z;

end

Create a wrapper function wrapperFun.m that calls the example function.

function out1 = wrapperFun()

a = double([4,5;6,7]);

out1 = exportedDataExampleFun(a);

end

Create the following code replacement library file named crtl_table_align.m. You use this table because data alignment compiler specification requires that you register a code replacement library. The table does not contain code replacement entries, it only specifies the compiler information needed for data alignment.

function hTable = crl_table_align

%% Create a table object
hTable = RTW.TflTable;

Create and save the following registration file, rtwTargetInfo.m. The file specifies the data alignment information for the compiler and associates that information with the code replacement library that you created.

function rtwTargetInfo(cm)
% rtwTargetInfo function to register a code replacement library (CRL)
% for use with code generation

% Register the CRL defined in local function locCrlRegFcn
cm.registerTargetInfo(@locCrlRegFcn);

end % End of RTWTARGETINFO

% Local function to define a CRL containing crl_table_align
function thisCrl = locCrlRegFcn

% Create an alignment specification object, assume gcc
as = RTW.AlignmentSpecification;
as.AlignmentType = {'DATA_ALIGNMENT_LOCAL_VAR', ...
    'DATA_ALIGNMENT_GLOBAL_VAR', ...
    'DATA_ALIGNMENT_STRUCT_FIELD'};
as.AlignmentSyntaxTemplate = '__attribute__((aligned(%n)))';
as.SupportedLanguages={'c', 'c++'};

% Add the alignment specification object
da = RTW.DataAlignment;
da.addAlignmentSpecification(as);

% Add the data alignment object to target characteristics
tc = RTW.TargetCharacteristics;
tc.DataAlignment = da;

% Instantiate a CRL registry entry
thisCrl = RTW.TflRegistry;

% Define the CRL properties
thisCrl.Name = 'Data Alignment Example';
thisCrl.Description = 'Example of replacement with data alignment';
thisCrl.TableList = {'crl_table_align'};
thisCrl.TargetCharacteristics = tc;

end % End of LOCCRLREGFCN

Register the code replacement library.

RTW.TargetRegistry.getInstance('reset');

Create a code generation configuration that uses the code replacement library that you created and registered with the data alignment specification.

config = coder.EmbeddedCodeConfig;
config.CodeReplacementLibrary = "Data Alignment Example";

Generate code for the function using the global variable z.

global z;
z = double([1,2;3,4]);
codegen -config config wrapperFun.m

Examine the generated code. For this example, the global variable z is defined in the file wrapperFun_data.c with the alignment specification.

/* Variable Definitions */
__attribute__((aligned(8))) double z[4];

For comparison, when you generate code for this function without using data alignment, the global variable is defined without the alignment specification.

/* Variable Definitions */
double z[4];

Input Arguments

collapse all

The varName is a character array of the variable name that requires alignment information specification.

The align_value is an integer number which should be a power of 2. This number specifies the power-of-2 byte alignment boundary.

Limitations

Limitations on variables supported by coder.dataAlignment directive:

  • Only use coder.dataAlignment to specify alignment information for function inputs, outputs, and global variables.

  • coder.dataAlignment supports only matrix types, including matrix of complex types.

  • For exported custom storage classes (CSCs), coder.dataAlignment supports only ExportedGlobal. You can specify alignment information for any imported CSCs.

  • The code generator ignores coder.dataAlignment for non-ERT or non-ERT derived system target files.

  • Global variables tagged using the coder.dataAlignment directive from within a MATLAB function block are ignored. Set the alignment value on the corresponding Data Store Memory.

  • The coder.dataAlignment function generates an error if a code replacement library is not specified. For more information, see Provide Data Alignment Specifications for Compilers.

For more information about general limitations on data alignment, see Data Alignment Limitations.

Tips

  • To prevent inlining a function containing an aligned variable in the generated code, a best practice is to call coder.inline('never') in your code.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017a