comp.lang.ada
 help / color / mirror / Atom feed
* Q: type matching with IEEE 754 double and Intel endianess
@ 2012-12-07  8:54 gautier_niouzes
  2012-12-08  6:31 ` J-P. Rosen
  0 siblings, 1 reply; 3+ messages in thread
From: gautier_niouzes @ 2012-12-07  8:54 UTC (permalink / raw)


Hello,

For a software component (Excel Writer), I have a portable way of getting a binary output for a Long_Float, by using Dmitry's IEEE_754 package.
It is working perfectly fine (see IEEE_Double_Intel_Portable function below).

Now, assume the program is running on a machine where Long_Float is actually IEEE_754, and endianess is the Intel one.
Is there a "solid" way to prove with some check that it is the case ?
Then a rough type conversion would be sufficient (e.g. IEEE_Double_Intel_Native below). The only advantage of the latter is that it is faster.

Cheers
G.


  function IEEE_Double_Intel_Portable(x: Long_Float) return Byte_buffer is
    pragma Inline(IEEE_Double_Intel_Portable);
    d : Byte_buffer(1..8);
    --
    use IEEE_754.Long_Floats;
    f64: constant Float_64:= To_IEEE(x);
  begin
    for i in d'Range loop
      d(i):= f64(9-i); -- Order is reversed
    end loop;
    return d;
  end IEEE_Double_Intel_Portable;

  function IEEE_Double_Intel_Native(x: Long_Float) return Byte_buffer is
    pragma Inline(IEEE_Double_Intel_Native);
    d : Byte_buffer(1..8);
    for d'Address use x'Address;
    pragma Import (Ada, d);
  begin
    return d;
  end IEEE_Double_Intel_Native;



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

* Re: Q: type matching with IEEE 754 double and Intel endianess
  2012-12-07  8:54 Q: type matching with IEEE 754 double and Intel endianess gautier_niouzes
@ 2012-12-08  6:31 ` J-P. Rosen
  2012-12-08 20:17   ` gautier_niouzes
  0 siblings, 1 reply; 3+ messages in thread
From: J-P. Rosen @ 2012-12-08  6:31 UTC (permalink / raw)


Le 07/12/2012 09:54, gautier_niouzes@hotmail.com a �crit :
> Now, assume the program is running on a machine where Long_Float is actually IEEE_754, and endianess is the Intel one.
> Is there a "solid" way to prove with some check that it is the case ?
Uncheck_Convert it to an array of bits, and compare against the expected
pattern. Unchecked_Conversion is precisely for that: getting at the
underlying representation

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr



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

* Re: Q: type matching with IEEE 754 double and Intel endianess
  2012-12-08  6:31 ` J-P. Rosen
@ 2012-12-08 20:17   ` gautier_niouzes
  0 siblings, 0 replies; 3+ messages in thread
From: gautier_niouzes @ 2012-12-08 20:17 UTC (permalink / raw)


Le samedi 8 décembre 2012 07:31:27 UTC+1, J-P. Rosen a écrit :
> Le 07/12/2012 09:54, gautier_niouzes@hotmail.com a écrit :
> 
> > Now, assume the program is running on a machine where Long_Float is actually IEEE_754, and endianess is the Intel one.
> 
> > Is there a "solid" way to prove with some check that it is the case ?
> 
> Uncheck_Convert it to an array of bits, and compare against the expected
> 
> pattern. Unchecked_Conversion is precisely for that: getting at the
> 
> underlying representation

Thanks. Now the question becomes (hence the "solid"): how to eliminate false postives and false negatives ?

Say my test looks like:
x: constant Long_Float:= 1234.0e56;
Can_use_native: constant Boolean:=
  IEEE_Double_Intel_Portable(x) = IEEE_Double_Intel_Native(x);

For eliminating false negatives it is easy since I'm developing on Intel x86 or x64 machines (there are probably borderline cases with more than one binary representation for one decimal one, but it simply is a question of trying another value for the constant x).

Now for false positives, it is probably a question of avoiding some very unlikely cases, e.g. an ieee number with a palindromic byte sequence (where the endianess would be fooled).
Perhaps 2 or 3 constants are better for testing:

Can_use_native: constant Boolean:=
  IEEE_Double_Intel_Portable(x) = IEEE_Double_Intel_Native(x) and
  IEEE_Double_Intel_Portable(y) = IEEE_Double_Intel_Native(y) and
  IEEE_Double_Intel_Portable(z) = IEEE_Double_Intel_Native(z);

Any thought about good choices of constant(s) is welcome :-)
_________________________
Gautier's Ada programming
http://gautiersblog.blogspot.com/search/label/Ada
NB: follow the above link for a valid e-mail address



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

end of thread, other threads:[~2012-12-08 20:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-07  8:54 Q: type matching with IEEE 754 double and Intel endianess gautier_niouzes
2012-12-08  6:31 ` J-P. Rosen
2012-12-08 20:17   ` gautier_niouzes

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