Re: Reading Float Data from a binary file into ada



"frikk" <frikker@xxxxxxxxx> wrote in message
news:1170268699.548152.214890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jan 31, 9:12 am, "frikk" <frik...@xxxxxxxxx> wrote:
On Jan 30, 10:54 pm, "Steve" <nospam_steve...@xxxxxxxxxxx> 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



.



Relevant Pages

  • Be careful when using MMX or Float point instructions in Delphi 2007
    ... For example, FillChar uses ... FLD/FST to fill memory. ... and it also uses X87 instructions. ... There is nothing wrong for a procedure to use float instructions, ...
    (borland.public.delphi.non-technical)
  • Re: Is VC++ 6.0 faster than VC++ .Net and VC# .Net ?
    ... will result in a memory allocation in C# for each iteration but will simply ... result into the re-utilisation of an object allocated on the stack in the ... it's very easy in C++ to use or access unallocated memory or to forget to ... public float Length ...
    (microsoft.public.vstudio.general)
  • Re: Basics on real floating types
    ... With memory at low, low prices it seem hard to come up with sensible reasons to prefer float over double for the vast majority of applications. ... I've just tested the overhead of adding one extra level of function call to a function. ...
    (comp.lang.c)
  • Re: Vector efficiency
    ... <float>.get(i) to retrieve each record versus aligning the data into an array declared as float, and noticed about 2x computational speed improvement. ... I guess there are a number of step performing a look up with vector has to do as opposed to simple addressing through an array. ... There's also the memory issue (c.f. another ... Expenditure of computer time is not the only ...
    (comp.lang.java.help)
  • Re: Conceptual Issue OLE and SQL Server
    ... Word.Document is a memory construct. ... Steve Hudson - Word Heretic ... >I would like to pull binary data from SQL Server and use it to create Word ...
    (microsoft.public.word.vba.general)