Xref: utzoo comp.lang.c:22826 comp.lang.fortran:2575 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!ginosko!brutus.cs.uiuc.edu!apple!bloom-beacon!eru!luth!sunic!mcsun!ukc!dcl-cs!gdt!gdr!exspes From: exspes@gdr.bath.ac.uk (P E Smee) Newsgroups: comp.lang.c,comp.lang.fortran Subject: Re: Type punning in C Message-ID: <1989Oct13.090311.9760@gdt.bath.ac.uk> Date: 13 Oct 89 09:03:11 GMT References: <475@idacrd.UUCP> <1989Oct10.185851.6490@agate.berkeley.edu> <1989Oct11.091619.18336@gdt.bath.ac.uk> <1654@l.cc.purdue.edu> Reply-To: exspes@gdr.bath.ac.uk (P E Smee) Organization: University of Bristol c/o University of Bath Lines: 50 In article <1654@l.cc.purdue.edu> cik@l.cc.purdue.edu (Herman Rubin) writes: >In article <1989Oct11.091619.18336@gdt.bath.ac.uk>, exspes@gdr.bath.ac.uk (P E Smee) writes: >> In article <1989Oct10.185851.6490@agate.berkeley.edu> jerry@violet.berkeley.edu ( Jerry Berkman ) writes: >< > >< >Why not use equivalence? >< > INTEGER I >< > REAL X, IX >< > EQUIVALENCE (X,IX) >< > >< >The Fortran standard specifies that a REAL and INTEGER occupy the same space. >> Fortran equivalence was designed toallow reuse of storage on the early >> small memory machines -- not to allow type punning. Usually you can >> get away with punning, but it doesn't always work and so is a bad habit. > >This is another example of those "gurus" who can not envision an intelligent >user using the machine in an intelligent manner, and prevent that use. I >have deliberately used "type punning" on various machines, and I consider it >an extremely useful tool. Now, I do not mind the compiler asking me if I >really wanted to do it, but I object to anyone telling me that I CAN NOT >use instructions that do what I want to do. Trust me, 'cause I don't want to get into a long discussion of FORTRAN in a C newsgroup. There are an incredible number of things which may cause aliasing in FORTRAN, some of it invisible to the compiler at any given moment due to separate compilation of modules, and so the optimizer needs all the help it can get. '77 FORTRAN is even more restrictive on the user as regards possible punning and aliasing than '66 FORTRAN was, in order primarily to permit more reliable optimization. The need for a construct to permit type punning is clear, particularly if you're doing 'systemmy' work. People working on the Algol compiler on Multics (which does even more stringent, and cross-module, type checking) resorted to writing little PL/1 routines which simply took whatever they were given and passed it straight back, unchanged(bitwise) -- and declared them in the Algol code as 'foreign' ('other language module, suspend type checking') 'takes integer returns real'. Lies, in other words. I tend to like things which match PL/1's 'unspec' in concept. This provides an explicit instruction to the compiler, and (more importantly) a warning to future maintainers of the code, that you are about to willfully, knowledgeably, and intentionally, cheat the data typing and conversion rules. Every serious language should have such a thing. (Unfortunately, it seems that most don't, sigh...) -- Paul Smee | JANET: Smee@uk.ac.bristol Computer Centre | BITNET: Smee%uk.ac.bristol@ukacrl.bitnet University of Bristol | Internet: Smee%uk.ac.bristol@nsfnet-relay.ac.uk (Phone: +44 272 303132) | UUCP: ...!mcvax!ukc!gdr.bath.ac.uk!exspes