Type punning in C

Norman Diamond diamond at csl.sony.co.jp
Wed Oct 11 14:33:09 AEST 1989


In article <475 at idacrd.UUCP> desj at idacrd.UUCP (David desJardins) writes:

>   Does anyone have any ideas on how one should go about converting an
>integer to the floating-point number with the same bit pattern?
>The best way I have found to do this in FORTRAN is to write
>      INTEGER I
>      REAL X
>      X = OR (I, 0).
>(The Cray FORTRAN compiler optimizes out the trivial OR instruction.)

Looks like the Cray Fortran compiler has a bug.  The result of OR(I,0)
should be an integer, and the conversion from integer to floating
(across the assignment) should usually have a different bit pattern.

>I don't want to involve pointers
>      int i;
>      float x;
>      * (int *) & x = i;

This would look a little bit better as
       x = * (float *) & i;
although the effect is the same.

In fact with an optimizing compiler this should not be an efficiency
problem.  The problem comes about if you DECLARE register float x;
because it's illegal to apply the "&" operator to a register operand.
If your variables are auto but the optimizer is USING registers, it
will generally figure out register-to-register transfers.

On the other hand, how many hardware architectures have instructions
to transfer directly between integer registers and floating registers?
Usually you have to store it to RAM and load it back anyway.

Another solution is to use unions
       union {
	   int   i;
	   float x;
       } converter;
       converter.i = i;
       x = converter.x;
but most compilers will not put converter in a register for you.  This
time you could try it yourself, "register union ....." because you
aren't applying "&" to it, but I cannot imagine a compiler obeying your
"register" request.  Again, how many architectures put integers and
floats in the same register?

-- 
Norman Diamond, Sony Corp. (diamond%ws.sony.junet at uunet.uu.net seems to work)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.
Newsgroups: comp.lang.c,comp.sys.super
Subject: Re: Type punning in C
Summary: 
Expires: 
References: <475 at idacrd.UUCP>
Sender: 
Reply-To: diamond at riks. (Norman Diamond)
Followup-To: 
Distribution: 
Organization: Sony Computer Science Laboratory Inc., Tokyo, Japan
Keywords: 

In article <475 at idacrd.UUCP> desj at idacrd.UUCP (David desJardins) writes:

>   Does anyone have any ideas on how one should go about converting an
>integer to the floating-point number with the same bit pattern?
>The best way I have found to do this in FORTRAN is to write
>      INTEGER I
>      REAL X
>      X = OR (I, 0).
>(The Cray FORTRAN compiler optimizes out the trivial OR instruction.)

Looks like the Cray Fortran compiler has a bug.  The result of OR(I,0)
should be an integer, and the conversion from integer to floating
(across the assignment) should usually have a different bit pattern.

>I don't want to involve pointers
>      int i;
>      float x;
>      * (int *) & x = i;

This would look a little bit better as
       x = * (float *) & i;
although the effect is the same.

In fact with an optimizing compiler this should not be an efficiency
problem.  The problem comes about if you DECLARE register float x;
because it's illegal to apply the "&" operator to a register operand.
If your variables are auto but the optimizer is USING registers, it
will generally figure out register-to-register transfers.

On the other hand, how many hardware architectures have instructions
to transfer directly between integer registers and floating registers?
Usually you have to store it to RAM and load it back anyway.

Another solution is to use unions
       union {
	   int   i;
	   float x;
       } converter;
       converter.i = i;
       x = converter.x;
but most compilers will not put converter in a register for you.  This
time you could try it yourself, "register union ....." because you
aren't applying "&" to it, but I cannot imagine a compiler obeying your
"register" request.  Again, how many architectures put integers and
floats in the same register?

-- 
Norman Diamond, Sony Corp. (diamond%ws.sony.junet at uunet.uu.net seems to work)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.



More information about the Comp.lang.c mailing list