Type punning in C

Richard O'Keefe ok at cs.mu.oz.au
Wed Oct 11 15:29:55 AEST 1989


In article <1989Oct10.185851.6490 at agate.berkeley.edu>, jerry at violet.berkeley.edu ( Jerry Berkman ) 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?

: Why not use equivalence?
: 	INTEGER I
: 	REAL X, IX
: 	EQUIVALENCE (X,IX)

Because he was asking how to do it in C.

I understand that the original problem was something that could be expressed	
quite clearly and simply as an ordinary C arithmetic expression operating
exclusively on integers, and that straightforward C code would _work_ on the
Cray, but because some operation (bitwise shifts?) didn't have a vector
equivalent it wouldn't vectorise.  But a hairy hack involving floats _would_
vectorise because the Cray system is good at vectorising float operations.

The interesting point here is that there are a lot of things you can do IF
you have the functions required and recommended in the IEEE 754 standard.
For example, if there is some reason why you want to avoid
	p << i		use	(int) scalb((float)p, i)
	p >> j		use	(int) scalb((float)p, -j)

This is why I think that it would be interesting for people with such
problems (and this includes Herman Rubin) to give us more detail about
the problem they are really trying to solve (e.g. the original poster's
problem was "how do I get results equal to these in a way that will
vectorise on a Cray?").  It may be that some of these problems have
solutions which are less machine-specific than hand-crafted bit-twiddling,
or would be if functional equivalents of the IEEE functions were provided.



More information about the Comp.lang.c mailing list