Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!bellcore!faline!ulysses!ggs From: ggs@ulysses.homer.nj.att.com (Griff Smith) Newsgroups: comp.lang.c Subject: constant expression rules Message-ID: <3018@ulysses.homer.nj.att.com> Date: Fri, 25-Sep-87 11:02:12 EDT Article-I.D.: ulysses.3018 Posted: Fri Sep 25 11:02:12 1987 Date-Received: Sun, 27-Sep-87 02:50:55 EDT Organization: AT&T Bell Laboratories, Murray Hill Lines: 44 Keywords: portability A user of our system grumbled recently that our C compiler is broken. He tried to compile the following on our 4.3BSD CCI POWER 6/32 C compiler: int j = ((int )(.5 * 4)); The response from the compiler was "foo.c", line 1: illegal initialization I then wrapped a function around it and compiled it on a 4.3BSD VAX and on the 4.3BSD tahoe compiler. The VAX produced the following assembly language fragment: movl $2,-4(fp) # 0.5 * 4 has been reduced to 2 movl -4(fp),r0 ret The tahoe did it the hard way: .data .align 2 L20: .long 0x41000000, 0x00000000 # .double 2 .text ldd L20 # note that .5 * 4 is reduced to 2.0 cvdl r0 # convert to long the hard way movl r0,-56(fp) movl -56(fp),r0 ret#1 I think this explains the problem with constant initialization on the tahoe compiler; the expression is "too complex". I also tried the 4.2BSD VAX C compiler and got output similar to the tahoe. The question is - what is a reasonable level of constant folding that I can assume when trying to write portable code? Is this a bug or a gotcha? K&R says that I can have almost any set of constants connected by +, -, *, ?: and a host of others (page 211). -- Griff Smith AT&T (Bell Laboratories), Murray Hill Phone: 1-201-582-7736 UUCP: {allegra|ihnp4}!ulysses!ggs Internet: ggs@ulysses.uucp