Checking a counter = 0 is much cheaper than any >, < etc. All you
have to do is NOR all the bits together.
Not necessarily..., > or < comparisons to a static 0 can be the same
as, or even cheaper than = or /= comparisons. For instance, if count
is signed, only one bit need be checked for < 0 (and none if count is
and unsigned vector!). > 0 and /= 0 are usually optimized to the same
circuit, but you should check your synthesis tool's performance just
to make sure.
One of the tricks of integer arithmetic synthesis is that expressions
are promoted to 32 bit signed, no matter what the storage is (might be
unsigned/natural). This means that even though count may be declared
as a natural, count - 1 < 0 is a valid comparison, the arithmetic
equivalent of = 0 for a down counter. Furthermore, most synthesis
tools will recognize the decrement in the compare and the counter, and
share the resources:
variable count : natural range 0 to init;
....
if count - 1 < 0 then -- check the carry bit
do_something;
count := init; -- roll over the counter
else
count := count - 1; -shared from comparison
end if;
Note: this will only work with integer (sub)types, NOT VECTOR TYPES.
Operations on unsigned vectors return unsigned vectors, so (count - 1
< 0) is always false if count is an unsigned vector.
Don't worry, even though the integer decrement is promoted to 32 bits
signed, stripping it down to N bits unsigned to fit in count will
optimize out the extra bits (except the carry bit, and it is never
stored).
Andy