views:

158

answers:

3

If I have a 32 bit two's complement number and I want to know what is the easiest way to know of two numbers are equal... what would be the fastest bitwise operator to know this? I know xor'ing both numbers and check if the results are zero works well... any other one's?

how about if a number is greater than 0?? I can check the 31'st bit to see if it's greater or equal to 0..but how about bgtz?

+6  A: 

Contrary to your comments, '==' is part of Verilog, and unless my memory is a lot worse than usual tonight, it should synthesize just fine. Just for example, you could write something like:

// warning: untested, incomplete and utterly useless in any case.
// It's been a while since I wrote much Verilog, so my syntax is probably a bit off
// anyway (might well be more like VHDL than it should be).
//
module add_when_equal(clock, a, b, x, y, z);
input clock;
input [31:0] a, b, x, y;
output [31:0] z;
reg [31:0] a, b, x, y, z;

always begin: main
   @(posedge clock);
   if (a == b)
       z <= x + y;
end
endmodule;

Verilog also supports the other comparison operators you'd normally expect (!=, <=, etc.). Synthesizers are fairly "smart", so something like x != 0 will normally synthesize to an N-input OR gate instead of a comparator.

Jerry Coffin
Yep. Premature optimization is especially bad on FPGAs, where common operations such as `==` are likely to have a known best mapping, and decomposing it any further can only make things worse.
Potatoswatter
A: 

if you can xor and then compare the result with zero then you can compare a result with some value and if you can compare something to a value then you can just compare the two values without using an xor and a 32 bit zero.

dwelch
+1  A: 

// this should work as comparator for Equality

wire [31:0] Cmp1, Cmp2;

wire Equal;

assign Equal = &{Cmp1 ~^ Cmp2}; // using XNOR

assign Equal = ~|{Cmp1 ^ Cmp2}; // using XOR

HyGIN