主要内容

Support MATLAB Math Functions for Xilinx Vitis HLS

R2026b
Since R2026a

When 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 FunctionVitis HLS Function
abshls::abs
acoshls::acos
asinhls::asin
atanhls::atan
atan2hls::atan2
ceilhls::ceil
coshls::cos
coshhls::cosh
exphls::exp
floorhls::floor
isinfhls::isinf
isnanhls::isnan
loghls::log
log2hls::frexp
log10hls::log10
maxhls::fmax
minhls::fmin
modhls::fmod
powerhls::pow
remhls::remainder
sinhls::sin
sinhhls::sinh
sqrthls::sqrt
tanhls::tan
tanhhls::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

(HDL Coder) | (HDL Coder) | (HDL Coder)

Topics