Type punning in C

Herman Rubin cik at l.cc.purdue.edu
Wed Oct 11 00:25:15 AEST 1989


In article <2165 at dataio.Data-IO.COM>, bright at Data-IO.COM (Walter Bright) writes:
> 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?
> 
> Try a union:
> 	union blah { int i; float f; } x;
> 	x.f = f;
> 	/* now manipulate x.i */

			.......................

| <      int i;
| <      float x;
| <      * (int *) & x = i;
| <to produce a single register-to-register transfer (or not even that).
> 
> Not at all. It's a pretty much standard and trivial optimization to
> convert *(int*)&x=i into MOV X,I. Feel free to use it, with the caveat
> that float format is not the same from machine to machine. I've done
> it many times to do special manipulations on doubles.

This may or may not work.  It is likely that the "&" by itself will force
x to memory.  But event if a #define is used to eliminate that, al least on
may compilers the MOV X,I or equivalent will not work.  Reluctantly, I would
have to say that it might be necessary to put in a kludge and then edit the
.s files, which is what I find myself forced to do in these situations.
-- 
Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907
Phone: (317)494-6054
hrubin at l.cc.purdue.edu (Internet, bitnet, UUCP)



More information about the Comp.lang.c mailing list