user263210
user263210

Reputation: 447

Any Software to convert float to any-precision FPU? [or Matlab solution]

I want to convert a lot of float numbers to multiple-precision FPU like '0x4049000000.....', perform calculations, change the precision and then again perform calculations and so on...

I know the theory as here but I want a Software, Online tool or Matlab solution to convert 6000+ numbers to FPU format (like IEEE single precision) in 0.0 ~ 51.0 range.

Any Suggestions?

Note: I need custom precision where I can describe number of digits of Mentissa and Exponent.

EDIT: It is also called Radix-Independent Floating-Point as described here and here

2nd EDIT: IEEE single precision convert and IEEE double Precision convert are examples. You enter any float number e.g. 3.1454, you will get its IEEE (single or double precision) float value in binary/hex. @rick

Upvotes: 0

Views: 271

Answers (2)

rick
rick

Reputation: 1646

float_pkg should be all you need. It gives you some predefined floating-point types (e.g., IEEE single and double precision), plus the ability to define custom floating-point values with arbitrary number of bits for the fraction and the exponent.

Based on your numeric example, here is some code to convert from a real value to single, double, and quadruple precision floats.

library ieee;
use ieee.float_pkg.all;

entity floating_point_demo is
end;

architecture example of floating_point_demo is
begin
    process
        variable real_input_value: real := 49.0215463456;
        variable single_precision_float: float32;
        variable double_precision_float: float64;
        variable quadruple_precision_float: float128;
    begin
        single_precision_float := to_float(real_input_value, single_precision_float);
        report to_string(single_precision_float);
        report to_hstring(single_precision_float);

        double_precision_float := to_float(real_input_value, double_precision_float);
        report to_string(double_precision_float);
        report to_hstring(double_precision_float);

        quadruple_precision_float := to_float(real_input_value, quadruple_precision_float);
        report to_string(quadruple_precision_float);
        report to_hstring(quadruple_precision_float);

        wait;
    end process;
end;

The example above uses types float32, float64, and float128 from float_pkg. However, you can achieve the same effect using objects of type float, whose size can be defined at their declarations:

variable single_precision_float: float(8 downto -23);
variable double_precision_float: float(11 downto -52);
variable quadruple_precision_float: float(15 downto -112);

To convert from a float to a real value, you can use the to_real() function:

-- To print the real value of a float object:
report to_string(to_real(quadruple_precision_float));

-- To convert from a float and assign to a real:
real_value := to_real(quadruple_precision_float);

Upvotes: 1

user1818839
user1818839

Reputation:

A quick look at the VHDL-2008 floating point library float_pkg shows that it instantiates a generic package with generic parameters set to match IEEE single precision floats.

package float_pkg is new IEEE.float_generic_pkg (...)

You should find this library as part of your simulator installation, wherever you usually look for the standard libraries such as numeric_std. On my system it is at /opt/ghdl-0.32/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/vhdl/src/ieee2008/float_pkg.vhdl - if you can't find it on your system, it's available online, search for "VHDL 2008 float package" ought to get you there.

You can instantiate this generic package (using float_pkg as an example) for any precision you like (within reasonable limits).

A quick look at IEEE.float_generic_pkg shows that it declares functions to_float and to_real, I'd like to think they have the obvious behaviour.

So the answer is ... yes.

  -- real to float
  function to_float (
    arg                  : REAL;
    size_res             : UNRESOLVED_float;
    constant round_style : round_type := float_round_style;  -- rounding option
    constant denormalize : BOOLEAN    := float_denormalize)  -- Use IEEE extended FP
    return UNRESOLVED_float;

and

  -- float to real
  function to_real (
    arg                  : UNRESOLVED_float;  -- floating point input
    constant check_error : BOOLEAN    := float_check_error;  -- check for errors
    constant denormalize : BOOLEAN    := float_denormalize)  -- Use IEEE extended FP
    return REAL;

Upvotes: 1

Related Questions