Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #65749

Re: Unspecified Behaviors in C90 - part 3

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Unspecified Behaviors in C90 - part 3
Date 2015-07-16 22:26 -0400
Organization A noiseless patient Spider
Message-ID <mo9p1a$of$1@dont-email.me> (permalink)
References (4 earlier) <d5963c50-6159-4137-940d-f5d08a0a257e@googlegroups.com> <mo3mq2$8g9$1@dont-email.me> <8a2d57b6-f355-42c4-bf91-8980916925bb@googlegroups.com> <mo7gkg$afp$1@dont-email.me> <df8afa86-cc07-4c71-ae8a-57166e1ea508@googlegroups.com>

Show all headers | View raw


On 07/16/2015 08:15 PM, David Kleinecke wrote:
> On Wednesday, July 15, 2015 at 10:50:40 PM UTC-7, James Kuyper wrote:
>> On 07/15/2015 10:46 PM, David Kleinecke wrote:
>>> On Tuesday, July 14, 2015 at 12:11:39 PM UTC-7, James Kuyper wrote:
>> ...
>>>> So? With an modern compiler with an aggressive optimizer, by far the
>>>> quickest way to predict what order will be used would be to compile the
>>>> program, and find out which order was actually used by examining the
>>>> generated program.
>>>  
>>> But if the order matters the optimizer msut respect it or it is in
>>> error. 
>>
>> No - if the order is specified, the optimizer must respect it, or it is
>> in error. 
> 
> That (apart for the "no") is exactly what I said. We are getting into
> pointless squabbles about trivia.

No, I said something quite different from what you said, and you've
missed the distinction I made. If the order is specified [by the
standard[, the optimizer must respect it. If the order matters [to you],
the optimizer has no obligation to respect it.

> If the order matters, but is not specified, it's the code
>> that's in error, not the optimizer. The code is in error by reason of
>> failing to specify the desired order. The C language provides all the
>> constructs you need to force things to be evaluated in a specific order,
>> if the order matters - all you have to do is use the right constructs.
>> Example: if it's important that f() be executed before g(), don't write
>>
>> 	a = f() + g();
>>
>> That would constitute a failure on your part to tell the compiler that
>> the order is important. Here's how to tell it that:
>>
>> 	temp = f();
>> 	a = temp + g();
> 
> But suppose the compiler already did that but failed to tell you.

When you write a = f() + g(), one possibility is that the compiler
generated code equivalent to the code I gave above. The other
possibility is that it generated the equivalent of

	temp = g();
	a = f() + temp;

The compiler will generate whichever of the two versions of that code
that it likes best, generally the one that executes fastest, though
there are other reasons why it might favor one over the other. In this
case, f() and g() appear to have identical interfaces, so it probably
doesn't matter, but in more complicated cases they might have different
argument lists and different return types, in which case the order might
make a big difference. Leaving implementations free to chose the best
order in such cases is part of the reason why the order is left unspecified.

Do you care whether f() is called before g()? Then tell the compiler
that you care, by using whichever of the two versions given above
matches the order that you want.
If you don't care whether f() is called before g(), then write a = f() +
g(), because that gives the compiler more freedom to optimize your code.
-- 
James Kuyper

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-09 11:21 -0700
  Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-09 14:54 -0400
    Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-09 21:38 +0100
  Re: Unspecified Behaviors in C90 - part 3 raltbos@xs4all.nl (Richard Bos) - 2015-07-13 21:22 +0000
    Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-14 08:21 -0700
      Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-14 08:48 -0700
        Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-14 09:33 -0700
          Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-14 18:08 +0100
            Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-15 19:38 -0700
              Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 00:47 -0400
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:10 -0700
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 21:17 -0400
                Re: Unspecified Behaviors in C90 - part 3 maddoxr@acm.org - 2015-07-17 07:43 -0700
              Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-16 09:35 +0100
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:18 -0700
                Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-17 01:45 +0100
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 21:19 -0400
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-17 12:14 -0400
                Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-17 11:00 -0700
                Re: Unspecified Behaviors in C90 - part 3 raltbos@xs4all.nl (Richard Bos) - 2015-07-19 21:24 +0000
              Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-16 07:40 -0700
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:20 -0700
                Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-17 01:49 +0100
          Re: Unspecified Behaviors in C90 - part 3 Keith Thompson <kst-u@mib.org> - 2015-07-14 11:23 -0700
          Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-14 15:11 -0400
            Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-15 19:46 -0700
              Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 01:50 -0400
                Re: Unspecified Behaviors in C90 - part 3 David Kleinecke <dkleinecke@gmail.com> - 2015-07-16 17:15 -0700
                Re: Unspecified Behaviors in C90 - part 3 Richard Heathfield <rjh@cpax.org.uk> - 2015-07-17 01:39 +0100
                Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-16 22:26 -0400
      Re: Unspecified Behaviors in C90 - part 3 raltbos@xs4all.nl (Richard Bos) - 2015-07-19 21:22 +0000
      Re: Unspecified Behaviors in C90 - part 3 James Kuyper <jameskuyper@verizon.net> - 2015-07-19 17:57 -0400

csiph-web