viraj
viraj

Reputation: 59

How to get the sign, mantissa and exponent of a real/shortreal in system verilog

I need to model floating point math in system verilog code which basically requires me to manipulate floating numbers in system verilog: real/shortreal. I need the ability to determine the mantissa, exponent & sign of a floating point number.

A c solution to this problem is to typcast it into an int pointer and then use shift operators: How to get the sign, mantissa and exponent of a floating point number

If system verilog supported part/bit select operators on short real, this is how the function would look:

function automatic shortreal conv2shortreal (bit sign, bit[7:0] exp, bit[22:0] mantissa);
conv2shortreal[31] = sign;
conv2shortreal[30:23] = exp;
conv2shortreal[22:0] = mantissa;
endfunction

the output should be a shortreal/real

Upvotes: 1

Views: 2617

Answers (1)

Unn
Unn

Reputation: 5108

As this is a useful feature when debugging floating-point implementations, SystemVerilog has the conversion functions builtin (for IEEE754):

shortreal  r;
bit        sign;
bit [7:0]  exp;
bit [22:0] mantissa;

r = $bitstoshortreal({sign, exp, mantissa}); // Convert from floating point parts to shortreal
{sign, exp, mantissa} = $shortrealtobits(r); // Convert from short real to floating point parts

Upvotes: 3

Related Questions