EmbDev.net

Forum: FPGA, VHDL & Verilog effitient code


von nick (Guest)


Rate this post
0 useful
not useful
hi evryone,

is there any best way to check if the difference between two integers is 
greater than a threshold


i find the code witten by me is quite big..need ur inputs thanks
if((unsigned(a)>unsigned(b))and((unsigned(a)-unsigned(b))>=500)) or ((unsigned(a)<unsigned(b)) and ((unsigned(b)-unsigned(a))>=500))then
---my code
end if;

von Boris O. (bohnsorg)


Rate this post
0 useful
not useful
Absolute value of difference of absolute integer values? Depending on 
your compiler it may recognize the pattern abs(abs(a)-abs(b)) and do 
some sign bit optimization. If you need to be certain of clock cycles: 
do it in assembler, otherwise trust the compiler.

von H-G S. (haenschen)


Rate this post
0 useful
not useful
It seems as if you could simply subtract b from a and then somehow 
ignore the possible negative sign if the first value was smaller than 
the second. Check only if the difference is bigger than 500.

von Lothar M. (lkmiller) (Moderator)


Rate this post
0 useful
not useful
nick wrote:
> i find the code witten by me is quite big..
What means "big" here?

> check if the difference between two integers
Are those two integers unsigned integers (aka naturals)?

nick wrote:
> if((unsigned(a)>unsigned(b))and((unsigned(a)-unsigned(b))>=500)) or
> ((unsigned(a)<unsigned(b)) and ((unsigned(b)-unsigned(a))>=500))then
> ---my code
> end if;
With integers simply this:
if abs(a-b)>=500 then
...
end if;

Boris O. wrote:
> If you need to be certain of clock cycles: do it in assembler
We are in real hardware here. It is done in one clock cycle... ;-)

H-G S. wrote:
> and then somehow ignore the possible negative sign
Have a closer look at the chapter "twos complement and integers". There 
is no such thing like a "negative sign" on a integer. All of the bits 
are "signs". So you cannot "cut away" or "simply ignore" the sign. Look 
at this "8-bit-integer":
 2 = 00000010
 1 = 00000001
 0 = 00000000
-1 = 11111111
-2 = 11111110
See it: you don't see a "negative sign"

: Edited by Moderator
von H-G S. (haenschen)


Rate this post
0 useful
not useful
As i read your code again now it seems it lacks something. Does it work 
the way it is now ?

To me it seems you have to implement more code for this task, especially 
for the case that the subtraction causes a rollover. Simple asking if 
the first value is bigger than the subtracted one does not always mean 
that the subtraction of 500 does not cause a rollover.

But it may be that the higher language compiler has a solution for this 
problem and counts that all in (the "bigger than"-routine for example 
could automaticalle handle rollovers).

: Edited by User
von nick (Guest)


Rate this post
0 useful
not useful
Lothar M. wrote:
> Are those two integers unsigned integers (aka naturals)?

they are declared as standard logic vectors.

von Lothar M. (lkmiller) (Moderator)


Rate this post
0 useful
not useful
nick wrote:
> they are declared as standard logic vectors.
Then why do you write somewhat about "integers"? An integer is a 
predefined data type in VHDL. Never ever call a std_logic_vector an 
integer!

Ok so you must use the numeric_std.all and write it that way:
if abs(unsigned(a)-unsigned(b))>=500 then
...
end if;

: Edited by Moderator
von nick (Guest)


Rate this post
0 useful
not useful
Lothar M. wrote:
> if abs(unsigned(a)-unsigned(b))>=500 then
> ...
> end if;

but abs works only with signed operand

so..

if abs(signed(a)-signed(b))>=500 then
...
end if;

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.