Get inverse of scalar in hdl code generation

4 views (last 30 days)
I am implementing an algorithm in matlab for hdl code generation which requires inverse of a variable scalar e.g 1/x . Most articles state that the divisor must be a constant and a power of two. Only thing I can find is reciprocal block but it is available in simulink and my design is in matlab mfile.
Is their any function or way of doing this in matlab mfile?

Answers (1)

Kiran Kintali
Kiran Kintali on 29 Oct 2023
There are multiple ways to generate HDL from HDL with reciprocal and divide operators.
If the scalar variable you are computing reciprocal for is of fixed-point type, you may want to consider use of approximations such as LUT approximation or CORDIC function based approximation.
You can also consider use of shift-add based division replacement if you want more accurate but a bit mroe resource heavy implementation. Shift-add performs divide operations on fixed-point types by using a non-restoring division algorithm that performs multiple shift and add operations to compute the quotient. This architecture provides improved accuracy compared to the LUT / CORDIC / Newton-Raphson approximation methods.
If you are writing fixed-point code manually for HDL code generation you can consider using fixed.cordicDivide function.
If you are using automatic float to fixed functionality available in MATLAB to HDL workflow you can take advantage of the automatic replacement operator workflow. See the attached tutorials with the examples for the various replacmenet methods.
See the attached MATLAB code and tutorial on how to perform the replacements either manually or automatically.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!