Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sfixed to ufixed conversition

Status
Not open for further replies.

fanwel

Full Member level 3
Joined
May 26, 2011
Messages
178
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,878
Hi all;

I have learn about ufixed and sfixed operation. I want to convert -0.129 to sfixed using the code below and its has no problem;

signal n1: ufixed (4 downto -4);
...
out_A <= to_sfixed(-0.129,n1);

Then, I want to convert this out_A(sfixed value) to ufixed..How can I do like that?
Need helps..thanks
 

ufixed means unsigned fixed
sfixed means signed fixed.

Therefore assigning an sfixed to a ufixed is inappropriate. You need to either use an abs function or declare out_a as an sfixed.
 

Sorry for the miss typing; signal n1: sfixed (4 downto -4);
How can I convert the sfixed to ufixed using abs function? Can you give any links or note on abs function to study..thanks for reply

---------- Post added at 03:25 ---------- Previous post was at 02:06 ----------

I write the code below and its work;
-------------------------------------------------------------
Library ieee;
USE ieee.std_logic_1164.all;
use ieee.numeric_std.all;
library ieee_proposed;
use ieee_proposed.fixed_pkg.all;

ENTITY try IS
PORT(
y : IN sfixed(4 downto -4);
Clock : IN STD_LOGIC;
alphaEstOut : OUT ufixed(5 downto -4));
END try;

ARCHITECTURE Behavior OF try IS
SIGNAL aa : sfixed(y'high +1 downto y'low);
BEGIN
PROCESS(Clock)
BEGIN
IF rising_edge(Clock) THEN

aa<=abs(y);
alphaEstOut<=ufixed(aa);

END IF;
END PROCESS;
END Behavior;
 

Now, I proceed to next stage which is multiply the conversion output with ufixed values (input).
library ieee;
library ieee_proposed;
use ieee_proposed.fixed_pkg.all;

package my_data_types is
type vector is array (natural range <>) of integer;
type ufixed_array_t is array (0 to 3) of ufixed (9 downto -10);
end my_data_types;

library ieee;
library ieee_proposed;
use ieee_proposed.fixed_pkg.all;
use work.my_data_types.all;

entity mul_four is
port (clk: in bit;
A: in vector (0 to 3);
out_A: out ufixed_array_t);
end mul_four;

architecture mul_four of mul_four is
signal n1: sfixed (4 downto -4);
signal n2: sfixed (n1'high + 1 downto n1'low);
signal n3: ufixed (5 downto -4);
signal u: ufixed (5 downto -4);
begin
process(clk)
begin
if (clk'event and clk='1') then
n1 <= to_sfixed(-0.129,n1);
n2 <= abs(n1);
n3 <= ufixed(n2);
end if;

for i in 0 to 3 loop
out_A(i) <= (to_ufixed (A(i),u)) * n3;
end loop;
end process;
end mul_four;

There are no error when I compile in quartus, but then an error occur in ModelSim:
# Cannot continue because of fatal error. ( at line 36).

Can anyone check my code?..do I write in wrong way?..need helps
 

move the for loop inside the clocked part of the process.

Plus, did you realise you had the sizing of the arrays wrong? the assignment out_A(i) is basically doing a /4.
 
  • Like
Reactions: fanwel

    fanwel

    Points: 2
    Helpful Answer Positive Rating
Oh, I not realise the size of the array is wrong. Thanks for remind me..

---------- Post added at 09:39 ---------- Previous post was at 09:15 ----------

This is the architecture code:
...
architecture mul_four of mul_four is
signal n1: sfixed (4 downto -4);
signal n2: sfixed (n1'high + 1 downto n1'low);
signal n3: ufixed (5 downto -4);
signal u: ufixed (5 downto -4);

begin
n1 <= to_sfixed(-0.129,n1);
n2 <= abs(n1);
n3 <= ufixed(n2);

process(clk)
begin
if (clk'event and clk='1') then
for i in 0 to 3 loop
out_A(i) <= (to_ufixed (A(i),u)) * n3;
end loop;
end if;
end process;
end mul_four;

I have simulate in ModelSim and the output seem like correct. I just want to confirm,do I convert the sfixed to ufixed in the right way?..thanks for helps
 

Yes you did, but I dont understand the point of what you're doing. why bother using -0.129? you're just converting it to 0.129, so all results will be A(i) * 0.129.

why not just use ufixed all the time and use 0.129?

---------- Post added at 10:32 ---------- Previous post was at 10:31 ----------

this code is pointless:

n1 <= to_sfixed(-0.129,n1);
n2 <= abs(n1);
n3 <= ufixed(n2);

you might aswell just have:

out_A(i) <= to_ufixed(A(i), u) * to_ufixed(0.129, u);
 
  • Like
Reactions: fanwel

    fanwel

    Points: 2
    Helpful Answer Positive Rating
Sorry Tricky, actually I have one fixed value (-0.129) that will be multiply by an input of integer.
I follows the rule where the signed number need to convert to sfixed. Then convert it to ufixed for multiply with the integer (ufixed).
I not realise that without doing 2's complement I can directly multiply -0.129 with the integer using only ufixed. You have show me the simple way to do this multiply operation..thanks for help
 

if you want -0.129, you need sfixed. Out_A needs to be an array of sfixed.

then:

out_A(i) <= to_sfixed(A(i), u) * to_sfixed(-0.129, u);
 

Both equation will give same final output right? I mean after convert it back to integer type..
1) out_A(i) <= to_ufixed(A(i), u) * to_ufixed(0.129, u);
2) out_A(i) <= to_sfixed(A(i), u) * to_sfixed(-0.129, u);
 

No.
1 will always be positive, because ufixed is treated as unsigned (ie. always positive). When you convert the integer to a ufixed, if A is negative, it will be converted to 0 (I think) or may just give you an error.
2 will be positive or negative, depending on the integer.
 

For example I have (13 x -0.129) + (11 x 0.32);
What the better way to do this operation? Using sfixed or ufixed? Can you give suggestion..thanks for reply
 

if you need signed, you need sfixed.
if you're always going to have positive, use ufixed.

with your equation - everything needs to be sfixed.
 
  • Like
Reactions: fanwel

    fanwel

    Points: 2
    Helpful Answer Positive Rating
Thanks for your helps..I really appreciate.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top