Support MATLAB Math Functions for Xilinx Vitis HLS
R2026bWhen generating HLS code from MATLAB® designs using AMD®
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
AMD
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 |
log2 | hls::frexp |
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 (HDL Coder).
Run these commands to generate the HLS code.
design = "hsDistance"; tb = "hsDistance_tb"; cfg = coder.config("hls"); cfg.SynthesisTool = "Xilinx Vitis HLS"; cfg.TestBenchName = tb; cfg.GenerateHLSTestBench = true; cfg.SimulateGeneratedCode = true; cfg.SynthesisToolChipFamily = "Artix UltraScale+"; cfg.SynthesisToolDeviceName = "xcau10p-ffvb676-1-e"; cfg.SynthesizeGeneratedCode = true; codegen("-config","cfg",design);
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 (HDL Coder) | hdlsetuphlstoolpath (HDL Coder) | Workflow Advisor (HDL Coder)
Topics
- Functions Supported for HDL and HLS Code Generation (HDL Coder)
- Get Started with MATLAB to High-Level Synthesis Workflow Using the Command Line Interface (HDL Coder)
- Get Started with MATLAB to High-Level Synthesis Workflow Using HDL Coder App (HDL Coder)
- Generate HLS Code for MATLAB Value Classes (HDL Coder)
- Generate HLS Code for MATLAB Handle Classes and System Objects (HDL Coder)