How expensive is data type conversion vs. bit array manipulation in VHDL?
Asked Answered
S

3

5

In VHDL, if you want to increment a std_logic_vector that represents a real number by one, I have come across a few options.

1) Use typecasting datatype conversion functions to change the std_logic vector to a signed or unsigned value, then convert it to an integer, add one to that integer, and convert it back to a std_logic_vector the opposite way than before. The chart below is handy when trying to do this.

Number to Vector Conversion Chart

2) Check to see the value of the LSB. If it is a '0', make it a '1'. If it is a '1', do a "shift left" and concatenate a '0' to the LSB. Ex: (For a 16 bit vector) vector(15 downto 1) & '0';

In an FPGA, as compared to a microprocessor, physical hardware resources seem to be the limiting factor instead of actual processing time. There is always the risk that you could run out of physical gates.

So my real question is this: which one of these implementations is "more expensive" in an FPGA and why? Are the compilers robust enough to implement the same physical representation?

Spirometer answered 10/12, 2014 at 14:43 Comment(1)
Your second algorithm does NOT increment a value by 1. What happens if your value is "01001", for example?Landwaiter
C
15

None of the type conversions cost.

The different types are purely about expressing the design as clearly as possible - not only to other readers (or yourself, next year:-) but also to the compiler, letting it catch as many errors as possible (such as, this integer value is out of range)

Type conversions are your way of telling the compiler "yes, I meant to do that".

Use the type that best expresses the design intent.

If you're using too many type conversions, that usually means something has been declared as the wrong type; stop and think about the design for a bit and it will often simplify nicely. If you want to increment a std_logic_vector, it should probably be an unsigned, or even a natural.

Then convert when you have to : often at top level ports or other people's IP.

Conversions may infinitesimally slow down simulations, but that's another matter.

As for your option 2 : low level detailed descriptions are not only harder to understand than a <= a + 1; but they are no easier for synth tools to translate, and more likely to contain bugs.

Combative answered 10/12, 2014 at 14:55 Comment(1)
Who says you can't?Zeldazelde
Z
0

I am giving another answer to better answer why in terms of gates and FPGA resources, it really doesn't matter which method you use. At the end, the logic will be implemented in Look-Up-Tables and flip flops. Usually (or always?) there are no native counters in the FPGA fabric. The synthesis will turn your code into LUTs, period. I always recommend trying to express the code as simple as possible. The more you try to write your code in RTL (vs. behavioral) the more error prone it will be. KISS is the appropriate course of action everytime, The synthesis tool, if any good, will simplify your intent as much as possible.

Zeldazelde answered 17/7, 2017 at 10:25 Comment(0)
Z
0

The only reason to implement arithmetic by hand is if you:

  • Think you can do a better job than the synthesis tool (where better could be smaller, faster, less power consuming, etc)
  • and you think the reduced portability and maintainability of your code does not matter too much in the long run
  • and it actually matters if you do a better job than the synthesis tool (e.g. you can reach your desired operating frequency only by doing this by hand rather than letting the synthesis tool do it for you).

In many cases you can also rewrite your RTL code slightly or use synthesis attributes such as KEEP to persuade the synthesis tool to make more optimal implementation choices rather than hand-implementing arithmetic components.

By the way, a fairly standard trick to reduce the cost of hardware counters is to avoid normal binary arithmetic and instead use for example LFSR counters. See Xilinx XAPP 052 for some inspiration in this area if you are interested in FPGAs (it is quite old but the general principles is the same in current FPGAs).

Zirconium answered 22/12, 2017 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.