Support MATLAB Math Functions for Xilinx Vitis HLS
When generating HLS code from MATLAB® designs using Xilinx Vitis HLS, the code generator maps mathematical
operations that use floating-point data types to corresponding functions in the
hls_math library. For example, a call to sin
in MATLAB translates to a call to hls::sin in the generated HLS
code. Xilinx Vitis HLS provides the hls_math library, which contains
the synthesizable implementations of the corresponding math functions.
Supported Math Functions
The table shows the supported math functions in MATLAB and their corresponding
functions in the Xilinx Vitis HLS hls_math library.
The code generator automatically translates these MATLAB math functions to their
equivalents in the hls_math library during HLS code generation
for Xilinx Vitis HLS targets.
Note
Math functions with floating-point data types are not supported for the synthesis tool Cadence Stratus HLS.
| MATLAB Math Function | Vitis HLS Function |
|---|---|
abs | hls::abs |
acos | hls::acos |
asin | hls::asin |
atan | hls::atan |
atan2 | hls::atan2 |
ceil | hls::ceil |
cos | hls::cos |
cosh | hls::cosh |
exp | hls::exp |
floor | hls::floor |
isinf | hls::isinf |
isnan | hls::isnan |
log | hls::log |
log10 | hls::log10 |
max | hls::fmax |
min | hls::fmin |
mod | hls::fmod |
power | hls::pow |
rem | hls::remainder |
sin | hls::sin |
sinh | hls::sinh |
sqrt | hls::sqrt |
tan | hls::tan |
tanh | hls::tanh |
Generate Synthesizable HLS Code using Math Functions
This example shows how to implement the Haver sine formula in MATLAB to calculate the great circle distance between two geographic points. The MATLAB function hsDistance uses several math functions that are mapped to their synthesizable equivalents in Xilinx Vitis HLS. The test bench hsDistance_tb tests this function for two specific geographic locations.
type("hsDistance.m")function distanceKm = hsDistance(lat1, lon1, lat2, lon2)
% Mean Earth radius in kilometers (WGS-84 approximate mean)
R = 6371.0;
% Convert degrees to radians
phi1 = deg2rad(lat1);
phi2 = deg2rad(lat2);
dphi = deg2rad(lat2-lat1);
dlambda = deg2rad(lon2-lon1);
% Haversine formula
a = sin(dphi./2).^2+cos(phi1).*cos(phi2).*sin(dlambda./2).^2;
c = 2 .* atan2(sqrt(a),sqrt(1-a));
% Distance
distanceKm = R.*c;
end
type("hsDistance_tb.m")originLat = 12.9716; originLon = 77.5946; targetLat = [12.9800, 12.9352, 13.0100]; targetLon = [77.6400, 77.6245, 77.5550]; distances = hsDistance(originLat, originLon, targetLat, targetLon);
Set up the tool path to Xilinx Vitis HLS by using hdlsetuphlstoolpath.
Run these commands to generate the HLS code.
design = "hsDistance"; tb = "hsDistance_tb"; cfg = coder.config("hdl"); cfg.Workflow = "High Level Synthesis"; cfg.SynthesisTool = "Xilinx Vitis HLS"; cfg.TestBenchName = tb; cfg.GenerateHDLTestBench = true; cfg.SimulateGeneratedCode = true; cfg.SynthesisToolChipFamily = "Artix UltraScale+"; cfg.SynthesisToolDeviceName = "xcau10p-ffvb676-1-e"; cfg.SynthesizeGeneratedCode = true; codegen("-config","cfg",design);
### Begin MATLAB to HLS Code Generation...
### Working on DUT: hsDistance.
### Using TestBench: hsDistance_tb.
### Begin HLS Code Generation
### Generating Resource Utilization Report resource_report.html.
### Working on hsDistanceClass.hpp as hsDistanceClass.hpp.
### Working on hsDistance_wrapper.cpp as hsDistance_wrapper.cpp.
### Generating HLS Conformance Report hsDistance_hdl_conformance_report.html.
### HLS Conformance check complete with 0 errors, 0 warnings, and 0 messages.
### To rerun codegen evaluate the following commands...
---------------------
cgi = load('C:\ExampleManager\user.ex\hdlcoder-ex37962760\codegen\hsDistance\hdlsrc\codegen_info.mat');
inVals = cgi.CodeGenInfo.inVals;
cfg = cgi.CodeGenInfo.codegenSettings;
codegen -config cfg -args inVals -report
---------------------
### Begin TestBench generation.
Code generation successful.
### Collecting data...
### Begin HLS test bench file generation with logged samples
### Generating test bench file: hsDistanceClass_tb.hpp
### Generating main function file: hsDistance_main.cpp
### Simulating the design 'hsDistance' using 'Xilinx Vitis HLS'.
### Generating sim_script.tcl file: sim_script.tcl
### Generating Simulation Report sim_log.txt
### Simulation successful.
### Synthesizing the design "hsDistance".
### Generating syn_script.tcl file: syn_script.tcl
### Generating Xilinx Vitis HLS project: C:\ExampleManager\user.ex\hdlcoder-ex37962760\codegen\hsDistance\hdlsrc
### Generating synthesis report syn_log.txt.
### Synthesis successful.
### Code generation successful: View report
In the generated HLS code, the code generator translates MATLAB math function calls to sin, cos, atan2, and sqrt with their synthesizable equivalents from the hls_math library (hls::sin, hls::cos, hls::atan2, and hls::sqrt). To view the generated HLS code, open the code generation report and click hsDistanceClass.hpp under Generated Code Source Files.
/* Mean Earth radius in kilometers (WGS-84 approximate mean) */ /* Convert degrees to radians */ phi1 = (real_T)0.017453292519943295 * lat1; /* Haversine formula */ L1: for (int32_T k = 0; k < 3; k = k + 1) { real_T e_x; real_T div_temp; div_temp = (real_T)0.017453292519943295 * (lat2[k] - lat1) / (real_T)2.0; e_x = hls::sin(div_temp); y[k] = e_x * e_x; } L2: for (int32_T b_k = 0; b_k < 3; b_k = b_k + 1) { real_T b_x; real_T a; real_T c_x; real_T d_x; real_T r; real_T b_div_temp; boolean_T f; boolean_T g; real_T h; b_div_temp = (real_T)0.017453292519943295 * (lon2[b_k] - lon1) / (real_T) 2.0; b_x = hls::sin(b_div_temp); h = hls::cos((real_T)0.017453292519943295 * lat2[b_k]); a = y[b_k] + x * h * (b_x * b_x); c_x = hls::sqrt(a); d_x = hls::sqrt((real_T)1.0 - a); f = hls::isnan(c_x); g = hls::isnan(d_x);
See Also
codegen | hdlsetuphlstoolpath | Workflow
Advisor
Topics
- Functions Supported for HDL and HLS Code Generation
- Get Started with MATLAB to High-Level Synthesis Workflow Using the Command Line Interface
- Get Started with MATLAB to High-Level Synthesis Workflow Using HDL Coder App
- Generate HLS Code for MATLAB Value Classes
- Generate HLS Code for MATLAB Handle Classes and System Objects