Need help for implementing arithmetic operations !

Status
Not open for further replies.

savour

Newbie level 6
Joined
Sep 7, 2008
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,359
Which is the most effective way to implement the following arithmetic operations in HDL?

Code:
evaluate the expressions 

|x-y| < z 

and

f(x,z) = -x ; z <-x
             x ; z > x
             x ; otherwise
 

For |x-y| < z

Let's take x/y/z as 8 bits signed number as example:

wire [8:0] sub_xy; //signed nnumber
wire [8:0] abs_xy; //unsigned number
wire [9:0] sub_abs_z; //signed number

wire res; //1'b0: |x-y| < z is false; 1'b1: true

assign sub_xy = {x[7], x[7:0]} + ~{y[7], y[7:0]} + 1'b1; //do x-y
assign abs_xy = (sub_xy[8])? (~{sub_xy[8:0} + 1'b1) : sub_xy[8:0];
assign sub_abs_z = {1'b0, abs_xy} + ~{z[7], z[7], z[7:0]} + 1'b1;
assign res = sub_abs_z[9];

Added after 42 seconds:

For:f(x,z) = -x ; z <-x
x ; z > x
x ; otherwise

It's may simplified to: if(z < -x)
res = -x;
else res = x;

Then simplified to: if(z + x < 0)
res = -x;
else res = x;

Let's also take x/z as 8 bits signd number as example:

wire [8:0] x_bar; //signed number
wire [8:0] add_zx; //signed number
wire [8:0] res; //singed number

assign x_bar = (x[7])? (~{x[7], x[7:0]} + 1'b1) : {1'b0, x[7:0]};
assign add_zx = {1'b0, z[7:0]} + {1'b0, x[7:0]};
assign res = (add_zx[8])? x_bar : {x[7], x[7:0]};
 

    savour

    Points: 2
    Helpful Answer Positive Rating
Thanks for your reply yx.yang

I made a spelling error on the second function, i meant the following one:

Code:
f(x,z) = -x ; z <-x
          x ; z > x
          z ; otherwise
 

The function choices aren't mutual exclusive, e. g. what's the intended result for x=-1, z=0 ? It could be +1 or -1.
 

You are right , x should be greater or equal to zero.

The f(x,z) is used for clipping the z value within the range of -x,x (x>=0).

Code:
f(x,z) = -x ; z <-x
          x ; z > x
          z ; otherwise 
with x>=0
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…