comp.lang.ada
 help / color / mirror / Atom feed
* Reading Float Data from a binary file into ada
@ 2007-01-30 15:51 frikk
  2007-01-30 16:37 ` Dmitry A. Kazakov
                   ` (5 more replies)
  0 siblings, 6 replies; 31+ messages in thread
From: frikk @ 2007-01-30 15:51 UTC (permalink / raw)


Hello everyone! I am having a problem that I would love some help 
with.

Essentially I was given a Visual Basic program that dumps a binary 
configuration file with all of the variables in a set.  The variables 
are each 32 bit floats, with the first 16 bits being the integer part 
and the second 16 bits being a representation of the fraction (I'm not 
sure if this is stanard - but its just how VB dumps the data). The 
binary dump is basically a copy of the way VB stores the data in 
memory.  I need to be able to use this data in ada.  There is a C 
counterpart to this that makes use of a 'union' to grab the data 1 
byte (8 bits) at a time, put them into a char array of size 4, then 
use a 32 bit float to reference the data.  Is there somehow I can do 
this in ada as well?

Basically I need to be able to read in the binary data byte by byte 
but store it into a 32 bit Float.  The C union example above uses the 
same memory address for the Float as it does for the size 4 char 
array. I don't even know if the VB dump will correspond with the way 
ada handles floats or not, but I'll worry about that later.

I am also using Matlab/Simulink if that provides any additional tools 
to use for debugging.

Thank you for any help,
Blaine




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
@ 2007-01-30 16:37 ` Dmitry A. Kazakov
  2007-01-30 16:55 ` Jean-Pierre Rosen
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 31+ messages in thread
From: Dmitry A. Kazakov @ 2007-01-30 16:37 UTC (permalink / raw)


On 30 Jan 2007 07:51:47 -0800, frikk wrote:

> Hello everyone! I am having a problem that I would love some help 
> with.
> 
> Essentially I was given a Visual Basic program that dumps a binary 
> configuration file with all of the variables in a set.  The variables 
> are each 32 bit floats, with the first 16 bits being the integer part 
> and the second 16 bits being a representation of the fraction (I'm not 
> sure if this is stanard - but its just how VB dumps the data). The 
> binary dump is basically a copy of the way VB stores the data in 
> memory.  I need to be able to use this data in ada.  There is a C 
> counterpart to this that makes use of a 'union' to grab the data 1 
> byte (8 bits) at a time, put them into a char array of size 4, then 
> use a 32 bit float to reference the data.  Is there somehow I can do 
> this in ada as well?

Only if you could be sure about the format.

But there is a portable way to do such things. You read 4 octets. From them
(you should know the endianness of the machine that wrote the file) take
the sign bit, take the biased exponent, take the mantissa (as an integer).
The mantissa will probably have a hidden bit. (I don't know what for format
Basic uses.) After that use T'Compose to produce the number of
floating-point T.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
  2007-01-30 16:37 ` Dmitry A. Kazakov
@ 2007-01-30 16:55 ` Jean-Pierre Rosen
  2007-01-30 17:35   ` Ali Bendriss
  2007-02-01 16:28   ` Ali Bendriss
  2007-01-30 17:56 ` Jeffrey R. Carter
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 31+ messages in thread
From: Jean-Pierre Rosen @ 2007-01-30 16:55 UTC (permalink / raw)


frikk a �crit :
> Hello everyone! I am having a problem that I would love some help 
> with.
> 
> Essentially I was given a Visual Basic program that dumps a binary 
> configuration file with all of the variables in a set.  The variables 
> are each 32 bit floats, with the first 16 bits being the integer part 
> and the second 16 bits being a representation of the fraction (I'm not 
> sure if this is stanard - but its just how VB dumps the data). 
Hmm... This looks like a fixed point representation, not a floating 
point one.

> The 
> binary dump is basically a copy of the way VB stores the data in 
> memory.  I need to be able to use this data in ada.  There is a C 
> counterpart to this that makes use of a 'union' to grab the data 1 
> byte (8 bits) at a time, put them into a char array of size 4, then 
> use a 32 bit float to reference the data.  Is there somehow I can do 
> this in ada as well?
In general, Unchecked_Conversion is your friend when you need two 
different views, at differing levels of abstraction, of the same data.

> Basically I need to be able to read in the binary data byte by byte 
> but store it into a 32 bit Float.  
However, if you do an unchecked_conversion to Float, it will certainly 
not work, because your data don't look at all like floats!

-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 16:55 ` Jean-Pierre Rosen
@ 2007-01-30 17:35   ` Ali Bendriss
  2007-02-01 16:28   ` Ali Bendriss
  1 sibling, 0 replies; 31+ messages in thread
From: Ali Bendriss @ 2007-01-30 17:35 UTC (permalink / raw)
  To: comp.lang.ada

On Tuesday 30 January 2007 16:55, Jean-Pierre Rosen wrote:
> frikk a écrit :
> > Hello everyone! I am having a problem that I would love some help
> > with.
> >
> > Essentially I was given a Visual Basic program that dumps a binary
> > configuration file with all of the variables in a set.  The variables
> > are each 32 bit floats, with the first 16 bits being the integer part
> > and the second 16 bits being a representation of the fraction (I'm not
> > sure if this is stanard - but its just how VB dumps the data).
>
> Hmm... This looks like a fixed point representation, not a floating
> point one.
>
There is the same kind of representation in the tiff format,
The "type RATIONAL" two LONGs : the first represent the numerator of the 
fraction, the second the denominator.

> > The
> > binary dump is basically a copy of the way VB stores the data in
> > memory.  I need to be able to use this data in ada.  There is a C
> > counterpart to this that makes use of a 'union' to grab the data 1
> > byte (8 bits) at a time, put them into a char array of size 4, then
> > use a 32 bit float to reference the data.  Is there somehow I can do
> > this in ada as well?
>
> In general, Unchecked_Conversion is your friend when you need two
> different views, at differing levels of abstraction, of the same data.
>
I remember doing some Unchecked_Conversion to read a proprietary tiff file 
some time ago. But only to find the LONGs and then after I was just doing a 
simple division to get the result. 

> > Basically I need to be able to read in the binary data byte by byte
> > but store it into a 32 bit Float.
>
It may be helpfull to know some value in advance like a magic number.

> However, if you do an unchecked_conversion to Float, it will certainly
> not work, because your data don't look at all like floats!

-- 
Ali 



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
  2007-01-30 16:37 ` Dmitry A. Kazakov
  2007-01-30 16:55 ` Jean-Pierre Rosen
@ 2007-01-30 17:56 ` Jeffrey R. Carter
  2007-01-30 18:04   ` frikk
  2007-01-30 19:31 ` Niklas Holsti
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 31+ messages in thread
From: Jeffrey R. Carter @ 2007-01-30 17:56 UTC (permalink / raw)


frikk wrote:
> 
> Basically I need to be able to read in the binary data byte by byte 
> but store it into a 32 bit Float.  The C union example above uses the 
> same memory address for the Float as it does for the size 4 char 
> array. I don't even know if the VB dump will correspond with the way 
> ada handles floats or not, but I'll worry about that later.

Any reason you can't call this C function from Ada?

-- 
Jeff Carter
"I soiled my armor, I was so scared."
Monty Python & the Holy Grail
71



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 17:56 ` Jeffrey R. Carter
@ 2007-01-30 18:04   ` frikk
  2007-01-30 19:56     ` frikk
                       ` (2 more replies)
  0 siblings, 3 replies; 31+ messages in thread
From: frikk @ 2007-01-30 18:04 UTC (permalink / raw)


On Jan 30, 12:56 pm, "Jeffrey R. Carter" <jrcar...@acm.org> wrote:
> frikk wrote:
>
> > Basically I need to be able to read in the binary data byte by byte
> > but store it into a 32 bit Float.  The C union example above uses the
> > same memory address for the Float as it does for the size 4 char
> > array. I don't even know if the VB dump will correspond with the way
> > ada handles floats or not, but I'll worry about that later.
>
> Any reason you can't call this C function from Ada?
>
> --
> Jeff Carter
> "I soiled my armor, I was so scared."
> Monty Python & the Holy Grail
> 71

Thank you for the responses so far.

We are using simulink and calling ada through s-functions.  If we use 
C, we'd have to get the data back to the matlab workspace, then pass 
it into an ada module.  It is prefered to keep all of the data inside 
the ada modules without coming back to the workspace.

I've been working with ada for awhile, but I'm still not very good at 
the syntaxes of more complicated data management commands.  How does 
ada store its floating point data? Is it stored like an ieee standard?

And yes - it appears that the data dumped is not actually a float but 
fixed point. This is how VB apparently handles 'Float' data types.

If I had data in a binary file, even disregarding the fractional 
piece, how could I get this into an ada variable? If I could even get 
a round about syntax to help me get started in setting up the memory 
locations and such that would be helpful. Its hard to find specific 
examples sometimes.

Thanks!
Blaine




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
                   ` (2 preceding siblings ...)
  2007-01-30 17:56 ` Jeffrey R. Carter
@ 2007-01-30 19:31 ` Niklas Holsti
  2007-01-30 21:14   ` Cesar Rabak
  2007-02-01 21:11   ` Simon Wright
  2007-01-30 20:02 ` Martin Dowie
  2007-01-31  3:54 ` Steve
  5 siblings, 2 replies; 31+ messages in thread
From: Niklas Holsti @ 2007-01-30 19:31 UTC (permalink / raw)


frikk wrote:
> Hello everyone! I am having a problem that I would love some help 
> with.
> 
> Essentially I was given a Visual Basic program that dumps a binary 
> configuration file with all of the variables in a set.  The variables 
> are each 32 bit floats, with the first 16 bits being the integer part 
> and the second 16 bits being a representation of the fraction (I'm not 
> sure if this is stanard - but its just how VB dumps the data). The 
> binary dump is basically a copy of the way VB stores the data in 
> memory.  I need to be able to use this data in ada.  There is a C 
> counterpart to this that makes use of a 'union' to grab the data 1 
> byte (8 bits) at a time, put them into a char array of size 4, then 
> use a 32 bit float to reference the data.  Is there somehow I can do 
> this in ada as well?

If that approach works in C, the same approach should work in Ada, as 
follows:

- Read the file with Ada.Sequential_IO (instantiated for
   Interfaces.Unsigned_8) or with Ada.Streams.Stream_IO.
   If you haven't used Streams before, Sequential_IO may
   be easier to start with.

- Use the functions Shift_Left or Shift_Right together with
   the "or" operator (all from Interfaces) to assemble 4 octets
   (Unsigned_8 values) into one 32-bit value of type
   Interfaces.Unsigned_32. You may have to experiment to
   get the octets in the right order.

- Then use Unchecked_Conversion to go from Unsigned_32 to Float.

The portability of this approach is the same as for the C code: it 
depends on the byte order in the VB file, the order in which you 
concatenate the 4 octets into a 32-bit value, and the representation of 
Float values. But if it works in C, it should work in Ada, assuming that 
the Ada Float type has the same representation as the C float type, 
which is likely.

Hope this helps...

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 18:04   ` frikk
@ 2007-01-30 19:56     ` frikk
  2007-01-31 18:30       ` Jeffrey R. Carter
  2007-01-30 20:51     ` Robert A Duff
  2007-01-31 18:25     ` Jeffrey R. Carter
  2 siblings, 1 reply; 31+ messages in thread
From: frikk @ 2007-01-30 19:56 UTC (permalink / raw)


On Jan 30, 1:04 pm, "frikk" <frik...@gmail.com> wrote:
> On Jan 30, 12:56 pm, "Jeffrey R. Carter" <jrcar...@acm.org> wrote:
>
> > frikk wrote:
>
> > > Basically I need to be able to read in the binary data byte by byte
> > > but store it into a 32 bit Float.  The C union example above uses the
> > > same memory address for the Float as it does for the size 4 char
> > > array. I don't even know if the VB dump will correspond with the way
> > > ada handles floats or not, but I'll worry about that later.
>
> > Any reason you can't call this C function from Ada?
>
> > --
> > Jeff Carter
> > "I soiled my armor, I was so scared."
> > Monty Python & the Holy Grail
> > 71
>
> Thank you for the responses so far.
>
> We are using simulink and calling ada through s-functions.  If we use
> C, we'd have to get the data back to the matlab workspace, then pass
> it into an ada module.  It is prefered to keep all of the data inside
> the ada modules without coming back to the workspace.
>
> I've been working with ada for awhile, but I'm still not very good at
> the syntaxes of more complicated data management commands.  How does
> ada store its floating point data? Is it stored like an ieee standard?
>
> And yes - it appears that the data dumped is not actually a float but
> fixed point. This is how VB apparently handles 'Float' data types.
>
> If I had data in a binary file, even disregarding the fractional
> piece, how could I get this into an ada variable? If I could even get
> a round about syntax to help me get started in setting up the memory
> locations and such that would be helpful. Its hard to find specific
> examples sometimes.
>
> Thanks!
> Blaine

Actually - now that I look at it Visual Basic is using a 'Single' 
type, not 'Float'.  This may make more sense now.




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
                   ` (3 preceding siblings ...)
  2007-01-30 19:31 ` Niklas Holsti
@ 2007-01-30 20:02 ` Martin Dowie
  2007-01-30 20:09   ` frikk
  2007-01-31  3:54 ` Steve
  5 siblings, 1 reply; 31+ messages in thread
From: Martin Dowie @ 2007-01-30 20:02 UTC (permalink / raw)


frikk wrote:
> Hello everyone! I am having a problem that I would love some help 
> with.
> 
> Essentially I was given a Visual Basic program that dumps a binary 
> configuration file with all of the variables in a set.  The variables 
> are each 32 bit floats, with the first 16 bits being the integer part 
> and the second 16 bits being a representation of the fraction (I'm not 
> sure if this is stanard - but its just how VB dumps the data). The 
> binary dump is basically a copy of the way VB stores the data in 
> memory.  I need to be able to use this data in ada.  There is a C 
> counterpart to this that makes use of a 'union' to grab the data 1 
> byte (8 bits) at a time, put them into a char array of size 4, then 
> use a 32 bit float to reference the data.  Is there somehow I can do 
> this in ada as well?
> 
> Basically I need to be able to read in the binary data byte by byte 
> but store it into a 32 bit Float.  The C union example above uses the 
> same memory address for the Float as it does for the size 4 char 
> array. I don't even know if the VB dump will correspond with the way 
> ada handles floats or not, but I'll worry about that later.
> 
> I am also using Matlab/Simulink if that provides any additional tools 
> to use for debugging.


Here's a small example that I think shows what you need (doesn't deal 
with any endian issues!) and goes the other way to what you need 
(fixed-point -> unsigned 32-bit) but you'll get the idea.

The one line I'm not sure about is if both 'Small and 'Size are needed - 
I always use both but I think 'Size may not strictly be needed.

Hope this helps!
Cheers
-- Martin

-- File: fp_example.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;

with Interfaces; use Interfaces;

procedure FP_Example is

    -- Defines the LSB
    FP_Delta : constant := 2.0**(-16);

    type FP is delta FP_Delta range -2.0**15 .. 2.0**15-FP_Delta;
    for FP'Small use FP_Delta;
    for FP'Size  use 32;

    F1 : FP := FP'First;
    F2 : FP := FP'Last;

    package FP_IO is
       new Ada.Text_IO.Fixed_IO (FP);
    use FP_IO;

    package Unsigned_32_IO is
       new Ada.Text_IO.Modular_IO (Unsigned_32);
    use Unsigned_32_IO;

    function To_Unsigned_32 is
       new Ada.Unchecked_Conversion (Source => FP,
                                     Target => Unsigned_32);
begin
    Put (F1, Aft => 16); New_Line;
    Put (F2, Aft => 16); New_Line;
    Put (To_Unsigned_32 (F1), Base => 16); New_Line;
    Put (To_Unsigned_32 (F2), Base => 16); New_Line;
end FP_Example;

c:\ada\fp_example\fp_example.exe
-32768.0000000000000000
  32767.9999847412109375
16#80000000#
16#7FFFFFFF#
process terminated successfully



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 20:02 ` Martin Dowie
@ 2007-01-30 20:09   ` frikk
  2007-01-30 20:23     ` Martin Dowie
  0 siblings, 1 reply; 31+ messages in thread
From: frikk @ 2007-01-30 20:09 UTC (permalink / raw)


On Jan 30, 3:02 pm, Martin Dowie <martin.do...@btopenworld.com.delete> 
wrote:
> frikk wrote:
> > Hello everyone! I am having a problem that I would love some help
> > with.
>
> > Essentially I was given a Visual Basic program that dumps a binary
> > configuration file with all of the variables in a set.  The variables
> > are each 32 bit floats, with the first 16 bits being the integer part
> > and the second 16 bits being a representation of the fraction (I'm not
> > sure if this is stanard - but its just how VB dumps the data). The
> > binary dump is basically a copy of the way VB stores the data in
> > memory.  I need to be able to use this data in ada.  There is a C
> > counterpart to this that makes use of a 'union' to grab the data 1
> > byte (8 bits) at a time, put them into a char array of size 4, then
> > use a 32 bit float to reference the data.  Is there somehow I can do
> > this in ada as well?
>
> > Basically I need to be able to read in the binary data byte by byte
> > but store it into a 32 bit Float.  The C union example above uses the
> > same memory address for the Float as it does for the size 4 char
> > array. I don't even know if the VB dump will correspond with the way
> > ada handles floats or not, but I'll worry about that later.
>
> > I am also using Matlab/Simulink if that provides any additional tools
> > to use for debugging.
>
> Here's a small example that I think shows what you need (doesn't deal
> with any endian issues!) and goes the other way to what you need
> (fixed-point -> unsigned 32-bit) but you'll get the idea.
>
> The one line I'm not sure about is if both 'Small and 'Size are needed -
> I always use both but I think 'Size may not strictly be needed.
>
> Hope this helps!
> Cheers
> -- Martin
>
> -- File: fp_example.adb
> with Ada.Text_IO; use Ada.Text_IO;
> with Ada.Unchecked_Conversion;
>
> with Interfaces; use Interfaces;
>
> procedure FP_Example is
>
>     -- Defines the LSB
>     FP_Delta : constant := 2.0**(-16);
>
>     type FP is delta FP_Delta range -2.0**15 .. 2.0**15-FP_Delta;
>     for FP'Small use FP_Delta;
>     for FP'Size  use 32;
>
>     F1 : FP := FP'First;
>     F2 : FP := FP'Last;
>
>     package FP_IO is
>        new Ada.Text_IO.Fixed_IO (FP);
>     use FP_IO;
>
>     package Unsigned_32_IO is
>        new Ada.Text_IO.Modular_IO (Unsigned_32);
>     use Unsigned_32_IO;
>
>     function To_Unsigned_32 is
>        new Ada.Unchecked_Conversion (Source => FP,
>                                      Target => Unsigned_32);
> begin
>     Put (F1, Aft => 16); New_Line;
>     Put (F2, Aft => 16); New_Line;
>     Put (To_Unsigned_32 (F1), Base => 16); New_Line;
>     Put (To_Unsigned_32 (F2), Base => 16); New_Line;
> end FP_Example;
>
> c:\ada\fp_example\fp_example.exe
> -32768.0000000000000000
>   32767.9999847412109375
> 16#80000000#
> 16#7FFFFFFF#
> process terminated successfully- Hide quoted text -
>
> - Show quoted text -

Niklas - I think this is exactly what I need to do.

Martin - Thank you for the example. I will go through that and see 
what I can use - it looks very useful though.  May I get some 
clarification on exactly what this line does:
    type FP is delta FP_Delta range -2.0**15 .. 2.0**15-FP_Delta;
    for FP'Small use FP_Delta;
    for FP'Size  use 32;

What is the delta used for?

Thanks!
Blaine




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 20:09   ` frikk
@ 2007-01-30 20:23     ` Martin Dowie
  0 siblings, 0 replies; 31+ messages in thread
From: Martin Dowie @ 2007-01-30 20:23 UTC (permalink / raw)


frikk wrote:
[snip]
> Niklas - I think this is exactly what I need to do.
> 
> Martin - Thank you for the example. I will go through that and see 
> what I can use - it looks very useful though.  May I get some 
> clarification on exactly what this line does:
>     type FP is delta FP_Delta range -2.0**15 .. 2.0**15-FP_Delta;
>     for FP'Small use FP_Delta;
>     for FP'Size  use 32;
> 
> What is the delta used for?

The delta means the type is a 'fixed point' type (a real but not a 
'float'). It's an abstraction of an integer with a fractional LSB.

See http://www.adapower.com/rm95.php for a technical specification, or 
see 
http://www.adapower.com/launch.php?URL=http%3A%2F%2Fwww.it.bton.ac.uk%2Fstaff%2Fje%2Fadacraft%2F 
for an introduction to then.

With the 'delta' you specify the minimum "LSB" your willing to accept in 
terms of accuracy - the compiler is free to give you better! In practice 
the 'delta' is best defined as a power-of-2 (and a compiler isn't likely 
to change that!).

If you need an "LSB" that's a power-of-10, use a 'decimal type'. All 
covered in John's book.
Cheers
-- Martin



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 18:04   ` frikk
  2007-01-30 19:56     ` frikk
@ 2007-01-30 20:51     ` Robert A Duff
  2007-01-31 18:25     ` Jeffrey R. Carter
  2 siblings, 0 replies; 31+ messages in thread
From: Robert A Duff @ 2007-01-30 20:51 UTC (permalink / raw)


"frikk" <frikker@gmail.com> writes:

> I've been working with ada for awhile, but I'm still not very good at 
> the syntaxes of more complicated data management commands.  How does 
> ada store its floating point data? Is it stored like an ieee standard?

Ada and C are the same in this regard -- Ada and C compilers will
typically store Floats in the way the hardware normally stores them,
which is IEEE on many machines.  Of course, neither language standard
requires any particular representation, but it's usually safe to trust
that C float and Ada Float are represented the same way on the same
machine.

The VB format you mentioned doesn't sound like IEEE float, but anyway,
if you want to write an Ada algorithm that mimics the C one, you should
use Unchecked_Conversion to do the "cast" from array-of-4-bytes to
Float, if that's what it does.

- Bob



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 19:31 ` Niklas Holsti
@ 2007-01-30 21:14   ` Cesar Rabak
  2007-01-30 21:36     ` frikk
  2007-02-01 21:11   ` Simon Wright
  1 sibling, 1 reply; 31+ messages in thread
From: Cesar Rabak @ 2007-01-30 21:14 UTC (permalink / raw)


Niklas Holsti escreveu:
[snipped]

> 
> The portability of this approach is the same as for the C code: it 
> depends on the byte order in the VB file, the order in which you 
> concatenate the 4 octets into a 32-bit value, and the representation of 
> Float values. But if it works in C, it should work in Ada, assuming that 
> the Ada Float type has the same representation as the C float type, 
> which is likely.
> 

0.01999: since there are not VB implementations on machines other than 
Intel, I think once the 'right' endianness is found it will survive all 
implementations in Ada, right?



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 21:14   ` Cesar Rabak
@ 2007-01-30 21:36     ` frikk
  0 siblings, 0 replies; 31+ messages in thread
From: frikk @ 2007-01-30 21:36 UTC (permalink / raw)


On Jan 30, 4:14 pm, Cesar Rabak <csra...@yahoo.com.br> wrote:
> Niklas Holsti escreveu:
> [snipped]
>
>
>
> > The portability of this approach is the same as for the C code: it
> > depends on the byte order in the VB file, the order in which you
> > concatenate the 4 octets into a 32-bit value, and the representation of
> > Float values. But if it works in C, it should work in Ada, assuming that
> > the Ada Float type has the same representation as the C float type,
> > which is likely.
>
> 0.01999: since there are not VB implementations on machines other than
> Intel, I think once the 'right' endianness is found it will survive all
> implementations in Ada, right?

Yes - you are correct. There will only be one windows machine that 
this VB app will dump from.

Thanks for the help, I'll be back tomorrow!




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
                   ` (4 preceding siblings ...)
  2007-01-30 20:02 ` Martin Dowie
@ 2007-01-31  3:54 ` Steve
  2007-01-31  8:42   ` Maciej Sobczak
  2007-01-31 14:12   ` frikk
  5 siblings, 2 replies; 31+ messages in thread
From: Steve @ 2007-01-31  3:54 UTC (permalink / raw)


"frikk" <frikker@gmail.com> wrote in message 
news:1170172307.292500.256090@m58g2000cwm.googlegroups.com...
> Hello everyone! I am having a problem that I would love some help
> with.
>
> Essentially I was given a Visual Basic program that dumps a binary
> configuration file with all of the variables in a set.  The variables
> are each 32 bit floats, with the first 16 bits being the integer part
> and the second 16 bits being a representation of the fraction (I'm not
> sure if this is stanard - but its just how VB dumps the data). The
> binary dump is basically a copy of the way VB stores the data in
> memory.  I need to be able to use this data in ada.  There is a C
> counterpart to this that makes use of a 'union' to grab the data 1
> byte (8 bits) at a time, put them into a char array of size 4, then
> use a 32 bit float to reference the data.  Is there somehow I can do
> this in ada as well?

Your description of how the C counterpart works is inconsistant with the way 
you have described the data format in the file.

If the C program is using a union to view the data as either a float or a 4 
character array then it is very likely that the data is in fact stored in 
IEEE 754 floating point format.  If you are using Microsoft's C compiler 
this is certainly the case.

The following snippet contains the pieces you need to do the conversion.

      TYPE aByte IS MOD 256;

      FOR aByte'SIZE USE 8;

      TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;

      PRAGMA PACK( aByteArray );

      TYPE aFourBytes IS NEW aByteArray(1..4);

      FUNCTION Conv IS NEW Ada.Unchecked_Conversion( aFourBytes, float );

      ...

      a : float;
      b : aFourBytes;

      ...

      a := Conv( b )

Use unchecked conversion to convert from a type that contains 4 bytes to the 
float value you're looking for.

Regards,
Steve
(The Duck)

>
> Basically I need to be able to read in the binary data byte by byte
> but store it into a 32 bit Float.  The C union example above uses the
> same memory address for the Float as it does for the size 4 char
> array. I don't even know if the VB dump will correspond with the way
> ada handles floats or not, but I'll worry about that later.
>
> I am also using Matlab/Simulink if that provides any additional tools
> to use for debugging.
>
> Thank you for any help,
> Blaine
> 





^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-31  3:54 ` Steve
@ 2007-01-31  8:42   ` Maciej Sobczak
  2007-01-31 14:12   ` frikk
  1 sibling, 0 replies; 31+ messages in thread
From: Maciej Sobczak @ 2007-01-31  8:42 UTC (permalink / raw)


Steve wrote:

> The following snippet contains the pieces you need to do the conversion.
> 
>       TYPE aByte IS MOD 256;
> 
>       FOR aByte'SIZE USE 8;
> 
>       TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;
> 
>       PRAGMA PACK( aByteArray );
> 
>       TYPE aFourBytes IS NEW aByteArray(1..4);
> 
>       FUNCTION Conv IS NEW Ada.Unchecked_Conversion( aFourBytes, float );
> 
>       ...
> 
>       a : float;
>       b : aFourBytes;
> 
>       ...
> 
>       a := Conv( b )
> 
> Use unchecked conversion to convert from a type that contains 4 bytes to the 
> float value you're looking for.

You have typed lots of lines of code to ensure that 4 bytes are treated 
as 4 bytes, but you did not provide any guarantee that Float has 4 bytes 
as well. You *can* rely on some assumptions about your compiler, but 
having these assumptions already in place anyway, is there a way to make 
the conversion simpler? Cannot you just read four bytes from the file 
directly to the Float variable using for example sequential or direct I/O?


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-31  3:54 ` Steve
  2007-01-31  8:42   ` Maciej Sobczak
@ 2007-01-31 14:12   ` frikk
  2007-01-31 18:38     ` frikk
  1 sibling, 1 reply; 31+ messages in thread
From: frikk @ 2007-01-31 14:12 UTC (permalink / raw)


On Jan 30, 10:54 pm, "Steve" <nospam_steve...@comcast.net> wrote:
> "frikk" <frik...@gmail.com> wrote in message
>
> news:1170172307.292500.256090@m58g2000cwm.googlegroups.com...
>
> > Hello everyone! I am having a problem that I would love some help
> > with.
>
> > Essentially I was given a Visual Basic program that dumps a binary
> > configuration file with all of the variables in a set.  The variables
> > are each 32 bit floats, with the first 16 bits being the integer part
> > and the second 16 bits being a representation of the fraction (I'm not
> > sure if this is stanard - but its just how VB dumps the data). The
> > binary dump is basically a copy of the way VB stores the data in
> > memory.  I need to be able to use this data in ada.  There is a C
> > counterpart to this that makes use of a 'union' to grab the data 1
> > byte (8 bits) at a time, put them into a char array of size 4, then
> > use a 32 bit float to reference the data.  Is there somehow I can do
> > this in ada as well?
>
> Your description of how the C counterpart works is inconsistant with the way
> you have described the data format in the file.
>
> If the C program is using a union to view the data as either a float or a 4
> character array then it is very likely that the data is in fact stored in
> IEEE 754 floating point format.  If you are using Microsoft's C compiler
> this is certainly the case.
>
> The following snippet contains the pieces you need to do the conversion.
>
>       TYPE aByte IS MOD 256;
>
>       FOR aByte'SIZE USE 8;
>
>       TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;
>
>       PRAGMA PACK( aByteArray );
>
>       TYPE aFourBytes IS NEW aByteArray(1..4);
>
>       FUNCTION Conv IS NEW Ada.Unchecked_Conversion( aFourBytes, float );
>
>       ...
>
>       a : float;
>       b : aFourBytes;
>
>       ...
>
>       a := Conv( b )
>
> Use unchecked conversion to convert from a type that contains 4 bytes to the
> float value you're looking for.
>
> Regards,
> Steve
> (The Duck)
>
>
>
>
>
> > Basically I need to be able to read in the binary data byte by byte
> > but store it into a 32 bit Float.  The C union example above uses the
> > same memory address for the Float as it does for the size 4 char
> > array. I don't even know if the VB dump will correspond with the way
> > ada handles floats or not, but I'll worry about that later.
>
> > I am also using Matlab/Simulink if that provides any additional tools
> > to use for debugging.
>
> > Thank you for any help,
> > Blaine- Hide quoted text -
>
> - Show quoted text -

Thank you for the example code! I'll play around with this and see how
it works.  Yes, you are correct that the C code and the VB code don't
seem to make sense.  I'm going to verify that this C code does work
exactly as we want it to.

Thank you!
Blaine




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 18:04   ` frikk
  2007-01-30 19:56     ` frikk
  2007-01-30 20:51     ` Robert A Duff
@ 2007-01-31 18:25     ` Jeffrey R. Carter
  2007-01-31 18:57       ` frikk
  2 siblings, 1 reply; 31+ messages in thread
From: Jeffrey R. Carter @ 2007-01-31 18:25 UTC (permalink / raw)


frikk wrote:
> 
> We are using simulink and calling ada through s-functions.  If we use 
> C, we'd have to get the data back to the matlab workspace, then pass 
> it into an ada module.  It is prefered to keep all of the data inside 
> the ada modules without coming back to the workspace.

If you're going to say "C", you'd better also say "Ada", or we'll be 
very rude to you.

-- 
Jeff Carter
"Alms for an ex-leper!"
Monty Python's Life of Brian
75



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 19:56     ` frikk
@ 2007-01-31 18:30       ` Jeffrey R. Carter
  0 siblings, 0 replies; 31+ messages in thread
From: Jeffrey R. Carter @ 2007-01-31 18:30 UTC (permalink / raw)


frikk wrote:
>>
> We are using simulink and calling ada through s-functions.  If we use
> C, we'd have to get the data back to the matlab workspace, then pass
> it into an ada module.  It is prefered to keep all of the data inside
> the ada modules without coming back to the workspace.

OK.

> And yes - it appears that the data dumped is not actually a float but
> fixed point. This is how VB apparently handles 'Float' data types.

>  > Actually - now that I look at it Visual Basic is using a 'Single'
> type, not 'Float'.  This may make more sense now.

I'm not sure what Single is. Are you sure C is using float? You should 
probably use an Ada fixed-point type with the same representation.

-- 
Jeff Carter
"Alms for an ex-leper!"
Monty Python's Life of Brian
75



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-31 14:12   ` frikk
@ 2007-01-31 18:38     ` frikk
  2007-02-01  0:05       ` Jeffrey R. Carter
  2007-02-01  2:59       ` Steve
  0 siblings, 2 replies; 31+ messages in thread
From: frikk @ 2007-01-31 18:38 UTC (permalink / raw)


On Jan 31, 9:12 am, "frikk" <frik...@gmail.com> wrote:
> On Jan 30, 10:54 pm, "Steve" <nospam_steve...@comcast.net> wrote:
>
>
>
>
>
> > "frikk" <frik...@gmail.com> wrote in message
>
> >news:1170172307.292500.256090@m58g2000cwm.googlegroups.com...
>
> > > Hello everyone! I am having a problem that I would love some help
> > > with.
>
> > > Essentially I was given a Visual Basic program that dumps a binary
> > > configuration file with all of the variables in a set.  The variables
> > > are each 32 bit floats, with the first 16 bits being the integer part
> > > and the second 16 bits being a representation of the fraction (I'm not
> > > sure if this is stanard - but its just how VB dumps the data). The
> > > binary dump is basically a copy of the way VB stores the data in
> > > memory.  I need to be able to use this data in ada.  There is a C
> > > counterpart to this that makes use of a 'union' to grab the data 1
> > > byte (8 bits) at a time, put them into a char array of size 4, then
> > > use a 32 bit float to reference the data.  Is there somehow I can do
> > > this in ada as well?
>
> > Your description of how the C counterpart works is inconsistant with the way
> > you have described the data format in the file.
>
> > If the C program is using a union to view the data as either a float or a 4
> > character array then it is very likely that the data is in fact stored in
> > IEEE 754 floating point format.  If you are using Microsoft's C compiler
> > this is certainly the case.
>
> > The following snippet contains the pieces you need to do the conversion.
>
> >       TYPE aByte IS MOD 256;
>
> >       FOR aByte'SIZE USE 8;
>
> >       TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;
>
> >       PRAGMA PACK( aByteArray );
>
> >       TYPE aFourBytes IS NEW aByteArray(1..4);
>
> >       FUNCTION Conv IS NEW Ada.Unchecked_Conversion( aFourBytes, float );
>
> >       ...
>
> >       a : float;
> >       b : aFourBytes;
>
> >       ...
>
> >       a := Conv( b )
>
> > Use unchecked conversion to convert from a type that contains 4 bytes to the
> > float value you're looking for.
>
> > Regards,
> > Steve
> > (The Duck)
>
> > > Basically I need to be able to read in the binary data byte by byte
> > > but store it into a 32 bit Float.  The C union example above uses the
> > > same memory address for the Float as it does for the size 4 char
> > > array. I don't even know if the VB dump will correspond with the way
> > > ada handles floats or not, but I'll worry about that later.
>
> > > I am also using Matlab/Simulink if that provides any additional tools
> > > to use for debugging.
>
> > > Thank you for any help,
> > > Blaine- Hide quoted text -
>
> > - Show quoted text -
>
> Thank you for the example code! I'll play around with this and see how
> it works.  Yes, you are correct that the C code and the VB code don't
> seem to make sense.  I'm going to verify that this C code does work
> exactly as we want it to.
>
> Thank you!
> Blaine- Hide quoted text -
>
> - Show quoted text -


Alright - so I figured some things out. Don't worry about what I said
about fixed point, I was very confused on how floating point numbers
were stored and it turns out it was stored exactly as it was supposed
to be. I was analyzing the floating point binary like it was a normal
number.  Oops... :(

Anyway, I notice that when dumping a binary file and when reading it
in, we are swapping the bytes around. Something like: for (i = 0; i<4;
i++){out[3-i] = in[i];).  Is this because of the way the hardware
would interpret the binary data for processing? This would mean that
in memory the binary resembles nothing like the actual ieee format,
even though it works correctly.

So my main question at this point is: How do I create the equivelant
of this:
union float_union
{
  float x;
  unsigned char c[4];
};

Would the examples given like for this?
Thank you,
Blaine




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-31 18:25     ` Jeffrey R. Carter
@ 2007-01-31 18:57       ` frikk
  0 siblings, 0 replies; 31+ messages in thread
From: frikk @ 2007-01-31 18:57 UTC (permalink / raw)



> If you're going to say "C", you'd better also say "Ada", or we'll be
> very rude to you.
>
> --
> Jeff Carter
> "Alms for an ex-leper!"
> Monty Python's Life of Brian
> 75

Haha, I apologize. Ada it is :)




^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-31 18:38     ` frikk
@ 2007-02-01  0:05       ` Jeffrey R. Carter
  2007-02-01  2:59       ` Steve
  1 sibling, 0 replies; 31+ messages in thread
From: Jeffrey R. Carter @ 2007-02-01  0:05 UTC (permalink / raw)


frikk wrote:
> 
> So my main question at this point is: How do I create the equivelant
> of this:
> union float_union
> {
>   float x;
>   unsigned char c[4];
> };

That's pretty simple. You put your input bytes into

subtype Raw_Bytes is System.Storage_Elements.Storage_Array (1 .. 4);

Raw : constant Raw_Bytes := Reorder (Get_Bytes (Raw_Bytes'Length) );

(Raw is in the proper order, of course). You define

function To_Float is new Ada.Unchecked_Conversion
    (Source => Raw_Bytes, Target => Interfaces.C.C_Float);

and then

Result : constant Interfaces.C.C_Float := To_Float (Raw);

You probably want that in some application-friendly type, though, so 
you'd really do something like

Result : constant My_Type := My_Type (To_Float (Raw) );

Finally

return Result;

This can end up being one of those functions where everything's done in 
the declarations.

-- 
Jeff Carter
"Alms for an ex-leper!"
Monty Python's Life of Brian
75



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-31 18:38     ` frikk
  2007-02-01  0:05       ` Jeffrey R. Carter
@ 2007-02-01  2:59       ` Steve
  2007-02-01 16:05         ` Bob Spooner
  1 sibling, 1 reply; 31+ messages in thread
From: Steve @ 2007-02-01  2:59 UTC (permalink / raw)


"frikk" <frikker@gmail.com> wrote in message 
news:1170268699.548152.214890@q2g2000cwa.googlegroups.com...
> On Jan 31, 9:12 am, "frikk" <frik...@gmail.com> wrote:
>> On Jan 30, 10:54 pm, "Steve" <nospam_steve...@comcast.net> wrote:
>>
[snip]
> Anyway, I notice that when dumping a binary file and when reading it
> in, we are swapping the bytes around. Something like: for (i = 0; i<4;
> i++){out[3-i] = in[i];).  Is this because of the way the hardware
> would interpret the binary data for processing? This would mean that
> in memory the binary resembles nothing like the actual ieee format,
> even though it works correctly.
>
> So my main question at this point is: How do I create the equivelant
> of this:
> union float_union
> {
>  float x;
>  unsigned char c[4];
> };

It is interesting that the application is "swizzling" the floating point 
values when writing them to disk.
Our application does the same thing before sending values across a TCP/IP 
socket.

      SUBTYPE u_long IS WinSock.u_long;

      SUBTYPE s_float IS float;

      TYPE aByte IS MOD 256;

      FOR aByte'SIZE USE 8;

      TYPE aByteArray IS ARRAY( Positive RANGE <> ) OF aByte;

      PRAGMA PACK( aByteArray );

      FUNCTION nltohf( value : u_long ) RETURN s_float IS
         TYPE aFourBytes IS NEW aByteArray(1..4);
         FUNCTION Conv IS
           NEW Ada.Unchecked_Conversion( aFourBytes, s_float );
         FUNCTION Conv IS
           NEW Ada.Unchecked_Conversion( u_long, aFourBytes );
         temp : aFourBytes := Conv( value );
      BEGIN
         RETURN Conv( aFourBytes'( temp(4), temp(3), temp(2), temp(1) ) );
      END nltohf;

Sorry for the cryptic function name it is consistant with the naming 
convention of similar C functions.
nltohf is: network long to host float.

Unchecked conversion is the preferred way of copying the memory image from 
one value to another that is of a different type.  This is the preferred way 
to handle what C was doing with a union.

If you really really really want to alias the same memory from two 
variables, you can do something like this:

  x : aliased float;
  c : aFourBytes;
  for c'address use x'address;

But don't do it.  As a matter of fact I fully expect that someone else 
following this thread will complain about me even mentioning it.  This 
"trick" is very unsafe, and moves away from some of the reasons for using 
Ada in the first place.

Regards,
Steve
(The Duck)

>
> Would the examples given like for this?
> Thank you,
> Blaine
> 





^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-02-01  2:59       ` Steve
@ 2007-02-01 16:05         ` Bob Spooner
  0 siblings, 0 replies; 31+ messages in thread
From: Bob Spooner @ 2007-02-01 16:05 UTC (permalink / raw)



"Steve" <nospam_steved94@comcast.net> wrote in message 
news:45GdnVwAocsGylzYnZ2dnUVZ_oannZ2d@comcast.com...
> [snip] Unchecked conversion is the preferred way of copying the memory 
> image from one value to another that is of a different type.  This is the 
> preferred way to handle what C was doing with a union.
>
Well, it's the common way to do it, but the C standard doesn't say that 
you'll get what you expect if you write the data one way and read it another 
way. Both K & R and the MISRA standard warn about this, but "everybody" 
seems to rely on it working. Unions were originally created to allow variant 
records, but I haven't encountered them being used that way in actual code 
in my admittedly limited experience.
Regards,
Bob





^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 16:55 ` Jean-Pierre Rosen
  2007-01-30 17:35   ` Ali Bendriss
@ 2007-02-01 16:28   ` Ali Bendriss
  2007-02-01 17:27     ` Jean-Pierre Rosen
  1 sibling, 1 reply; 31+ messages in thread
From: Ali Bendriss @ 2007-02-01 16:28 UTC (permalink / raw)
  To: comp.lang.ada

On Tuesday 30 January 2007 16:55, Jean-Pierre Rosen wrote:
> frikk a écrit :
> > Hello everyone! I am having a problem that I would love some help
> > with.
> >
> > Essentially I was given a Visual Basic program that dumps a binary
> > configuration file with all of the variables in a set.  The variables
> > are each 32 bit floats, with the first 16 bits being the integer part
> > and the second 16 bits being a representation of the fraction (I'm not
> > sure if this is stanard - but its just how VB dumps the data).
>
> Hmm... This looks like a fixed point representation, not a floating
> point one.
>
> [...]

Hello,

I was not aware that VB has some fixed point built-in. 
(... In fact I never use the fixed point at all).
I was thinking that it's mostly a facility offered by Ada.
I undersand that it could be usefull for the monetary conversion for example 
but is it an important feature for a language to offer a built-in fixed point 
representation ? does a library can offer the same level of facility ?

Thanks.

-- 
Ali 



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-02-01 16:28   ` Ali Bendriss
@ 2007-02-01 17:27     ` Jean-Pierre Rosen
  0 siblings, 0 replies; 31+ messages in thread
From: Jean-Pierre Rosen @ 2007-02-01 17:27 UTC (permalink / raw)


Ali Bendriss a �crit :
> I was not aware that VB has some fixed point built-in. 
> (... In fact I never use the fixed point at all).
> I was thinking that it's mostly a facility offered by Ada.
> I undersand that it could be usefull for the monetary conversion for example 
> but is it an important feature for a language to offer a built-in fixed point 
> representation ? does a library can offer the same level of facility ?
> 
IANAL, but I heard that it is forbidden by law to make monetary 
computations in floating point. Even when you are computing the budget 
of the DoD :-), you are not allowed to loose cents just because the 
amount is too big.

Time is another interesting example: except when you are modelling the 
big bang, the origin of time is arbitrary, and you'd better not loose 
precision as time flows.

Of course, if you are using a poor language without fixed point, you can 
get the facility with a library. However, it is much more convenient to 
have it as a first class type. There are really two ways of modelling 
mathematical reals; the fact that most languages provide only one does 
not imply that the other one is not useful.
-- 
---------------------------------------------------------
            J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-01-30 19:31 ` Niklas Holsti
  2007-01-30 21:14   ` Cesar Rabak
@ 2007-02-01 21:11   ` Simon Wright
  2007-02-01 21:44     ` Niklas Holsti
  1 sibling, 1 reply; 31+ messages in thread
From: Simon Wright @ 2007-02-01 21:11 UTC (permalink / raw)


Niklas Holsti <niklas.holsti@nospam.please> writes:

> - Use the functions Shift_Left or Shift_Right together with
>   the "or" operator (all from Interfaces) to assemble 4 octets
>   (Unsigned_8 values) into one 32-bit value of type
>   Interfaces.Unsigned_32. You may have to experiment to
>   get the octets in the right order.
>
> - Then use Unchecked_Conversion to go from Unsigned_32 to Float.

IF the VB view splits on octet boundaries, it can be easier to read
into an array of 4 octets (32 bits read), do the unchecked conversion
from array-of-4-octets to float with reordering; something like

  v : four_byte_array;
  function convert is new ada.unchecked_conversion (four_byte_array, float);

  read (v);
  result := convert ((1 => v (4), 2 => v (3), 3 => v (2); 4 => v (1)));

If not, why not read into unsigned_32 in the first place?



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-02-01 21:11   ` Simon Wright
@ 2007-02-01 21:44     ` Niklas Holsti
  2007-02-03 12:13       ` Simon Wright
  0 siblings, 1 reply; 31+ messages in thread
From: Niklas Holsti @ 2007-02-01 21:44 UTC (permalink / raw)


Simon Wright wrote:
> Niklas Holsti <niklas.holsti@nospam.please> writes:
> 
> 
>>- Use the functions Shift_Left or Shift_Right together with

(I don't know why I wrote Shift_Right; only Shift_Left is useful here.)

>>  the "or" operator (all from Interfaces) to assemble 4 octets
>>  (Unsigned_8 values) into one 32-bit value of type
>>  Interfaces.Unsigned_32. You may have to experiment to
>>  get the octets in the right order.
>>
>>- Then use Unchecked_Conversion to go from Unsigned_32 to Float.
> 
> 
> IF the VB view splits on octet boundaries, it can be easier to read
> into an array of 4 octets (32 bits read), do the unchecked conversion
> from array-of-4-octets to float with reordering; something like
> 
>   v : four_byte_array;
>   function convert is new ada.unchecked_conversion (four_byte_array, float);
> 
>   read (v);
>   result := convert ((1 => v (4), 2 => v (3), 3 => v (2); 4 => v (1)));
> 
> If not, why not read into unsigned_32 in the first place?

Reading into Unsigned_32 would perhaps not give the right octet order 
for conversion to Float, depending on how the file was written. If the 
file is written in the right order, it is easiest to just read into 
Float with no conversions.

The 4-octet-array method has two portability drawbacks, I think:

- It should be packed, and the compiler might accept that, or not.
   (I admit that GNAT will accept it on PCs.)

- The compiler defines the endianness of the array after packing,
   so the order in which octets are moved from the file to the
   array might have to be compiler-dependent.
   (I admit that this is probably irrelevant for octet-addressed memory.)

I agree that a 4-octet array is a good method, and the aggregate syntax 
is nicer than a nest of Shift_Lefts and "or"s. But I wanted to give the 
advice without talking about packing and possible compiler-dependent 
endianness of packed arrays.

I think the best method is to use Streams, but that takes some study to 
get started.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-02-01 21:44     ` Niklas Holsti
@ 2007-02-03 12:13       ` Simon Wright
  2007-02-03 14:59         ` Gautier
  0 siblings, 1 reply; 31+ messages in thread
From: Simon Wright @ 2007-02-03 12:13 UTC (permalink / raw)


Niklas Holsti <niklas.holsti@nospam.please> writes:

> I think the best method is to use Streams, but that takes some study
> to get started.

And endianness-handling isn't easy and (on GNAT) isn't on by default.

You can build your own GNAT RTL with XDR support on streaming by
replacing s-stratt.adb with the supplied s-strxdr.adb -- and then
streams Just Work across platforms. Well, all the platforms I'm
interested in.

We need to stream betweem PowerPC and x86, and would much prefer to
work with AdaCore's supplied RTL than one we rebuilt ourselves, so we
shall be using the standard RTL on the deliverable PPC target and a
modified one on the Intel host (we can't use AdaCore's, because it
streams one data type (Wide_Character???) in 4 bytes where the native
code streams in 2).



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-02-03 12:13       ` Simon Wright
@ 2007-02-03 14:59         ` Gautier
  2007-02-03 17:01           ` Simon Wright
  0 siblings, 1 reply; 31+ messages in thread
From: Gautier @ 2007-02-03 14:59 UTC (permalink / raw)


Simon Wright:

> And endianness-handling isn't easy and (on GNAT) isn't on by default.
> 
> You can build your own GNAT RTL with XDR support on streaming by
> replacing s-stratt.adb with the supplied s-strxdr.adb -- and then
> streams Just Work across platforms. Well, all the platforms I'm
> interested in.
> 
> We need to stream betweem PowerPC and x86, and would much prefer to
> work with AdaCore's supplied RTL than one we rebuilt ourselves, so we
> shall be using the standard RTL on the deliverable PPC target and a
> modified one on the Intel host (we can't use AdaCore's, because it
> streams one data type (Wide_Character???) in 4 bytes where the native
> code streams in 2).

It seems complicated, depends on GNAT and furthermore needs modifying its RTL.
What about a deterministic way where data is sliced into bytes in the Ada code ?
You can take a look at GLOBE_3D-IO.adb in GLOBE_3D
( http://www.mysunrise.ch/users/gdm/g3d.htm ), it's working fine: a .g3d 
object can be written on a PowerPC machine with a GLOBE_3D engine compiled by 
GNAT, then read on a PC with a GLOBE_3D compiled by ObjectAda (succesfully 
tested). A single source, no tweak, no compiler dependence...
BTW, there is also a portable way (single-source) of transferring Floats use 
by GLOBE_3D.IO.

G.



^ permalink raw reply	[flat|nested] 31+ messages in thread

* Re: Reading Float Data from a binary file into ada
  2007-02-03 14:59         ` Gautier
@ 2007-02-03 17:01           ` Simon Wright
  0 siblings, 0 replies; 31+ messages in thread
From: Simon Wright @ 2007-02-03 17:01 UTC (permalink / raw)


Gautier <gautier@fakeaddress.nil> writes:

> It seems complicated, depends on GNAT and furthermore needs
> modifying its RTL.  What about a deterministic way where data is
> sliced into bytes in the Ada code ?
> You can take a look at GLOBE_3D-IO.adb in GLOBE_3D (
> http://www.mysunrise.ch/users/gdm/g3d.htm ), it's working fine: a
> .g3d object can be written on a PowerPC machine with a GLOBE_3D
> engine compiled by GNAT, then read on a PC with a GLOBE_3D compiled
> by ObjectAda (succesfully tested). A single source, no tweak, no
> compiler dependence...

But doing it the way I described means that I can use
My_Arbitrary_Nonlimited_Type'Output and 'Input (or even
Your_Arbitrary_Nonlimited_Type'Output and 'Input) without further
ado. Worth some low-level compiler RTL messing.

I'm sure that if we had to migrate to OA we could pay Aonix to provide
such an RTL (of course they may provide it already for all I know).



^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2007-02-03 17:01 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-30 15:51 Reading Float Data from a binary file into ada frikk
2007-01-30 16:37 ` Dmitry A. Kazakov
2007-01-30 16:55 ` Jean-Pierre Rosen
2007-01-30 17:35   ` Ali Bendriss
2007-02-01 16:28   ` Ali Bendriss
2007-02-01 17:27     ` Jean-Pierre Rosen
2007-01-30 17:56 ` Jeffrey R. Carter
2007-01-30 18:04   ` frikk
2007-01-30 19:56     ` frikk
2007-01-31 18:30       ` Jeffrey R. Carter
2007-01-30 20:51     ` Robert A Duff
2007-01-31 18:25     ` Jeffrey R. Carter
2007-01-31 18:57       ` frikk
2007-01-30 19:31 ` Niklas Holsti
2007-01-30 21:14   ` Cesar Rabak
2007-01-30 21:36     ` frikk
2007-02-01 21:11   ` Simon Wright
2007-02-01 21:44     ` Niklas Holsti
2007-02-03 12:13       ` Simon Wright
2007-02-03 14:59         ` Gautier
2007-02-03 17:01           ` Simon Wright
2007-01-30 20:02 ` Martin Dowie
2007-01-30 20:09   ` frikk
2007-01-30 20:23     ` Martin Dowie
2007-01-31  3:54 ` Steve
2007-01-31  8:42   ` Maciej Sobczak
2007-01-31 14:12   ` frikk
2007-01-31 18:38     ` frikk
2007-02-01  0:05       ` Jeffrey R. Carter
2007-02-01  2:59       ` Steve
2007-02-01 16:05         ` Bob Spooner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox