Hi all,
In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed
that "x" will be equal to "a" only if the condition "cond" holds. Assuming that
"a" is a memory location, is it also guaranteed that "a" will not be accessed
in memory if the condition does not hold?
Or, in other words, is a compiler allowed to speculatively fetch a and b from
memory and then assign one of them to x based on the condition?
- Udo 20 2166
"Udo A. Steinberg" <st*******@doma in.invalidwrite s:
Hi all,
In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed
that "x" will be equal to "a" only if the condition "cond" holds. Assuming that
"a" is a memory location, is it also guaranteed that "a" will not be accessed
in memory if the condition does not hold?
Or, in other words, is a compiler allowed to speculatively fetch a and b from
memory and then assign one of them to x based on the condition?
- Udo
Is it handled any different from
if(cond)
x=a;
else
x=b;
I hope not : because if it was there would be hell to pay if "a" became
"a()", or "b" became "b()" - or if "a" or "b" were special memory locations
hardwired to slow response HW.
Udo A. Steinberg wrote:
In a ternary statement such as: x = (cond ? a : b);
(It's not a "ternary statement". It's an assignment expression where the
source operand is a ternary equiv. conditional expression.)
it is obviously guaranteed
that "x" will be equal to "a" only if the condition "cond" holds.
Not actually true: `a` and `b` may be equal.
Assuming that "a" is a memory location, is it also guaranteed
that "a" will not be accessed in memory if the condition does not hold?
`a` will be evaluated only if the condition compares unequal to 0.
But ...
Or, in other words, is a compiler allowed to speculatively fetch a and b from
memory and then assign one of them to x based on the condition?
Yes, if you can't tell the difference [with conforming C code]; this is the
"as if" rule. The standard specifies the semantics, and the implementation
can do anything that provides the same result so long as its sneakiness
is invisible.
So if `a` is a local or global variable [1], it can prefetch `a` - you
can't tell that it has. But if `a` is eg `pointer->aField` or `array[i]`
it can't -- because the pointer might be null, or the index out of
bounds -- unless it can /also/ prove that the pointer is valid, or
the index in range (or that wild fetches are harmless, which they might
be in some implementations ).
[1] Unless it's volatile.
--
Chris "the rule of invisible sneakiness" Dollin
Meaning precedes definition.
On Mon, 07 Aug 2006 14:43:47 +0100 Chris Dollin (CD) wrote:
CDUdo A. Steinberg wrote:
CD>
CD Or, in other words, is a compiler allowed to speculatively fetch a and b
CD from memory and then assign one of them to x based on the condition?
CD>
CDYes, if you can't tell the difference [with conforming C code]; this is the
CD"as if" rule. The standard specifies the semantics, and the implementation
CDcan do anything that provides the same result so long as its sneakiness
CDis invisible.
How well defined is "invisible sneakiness"? In my case "a" is only mapped in
virtual memory if the condition is true. Otherwise access to "a" causes a page
fault. Here the compiler sneakiness becomes visible to the user. However, if
the compiler can always assume that all memory is mapped, then the sneakiness
is indeed invisible.
- Udo
Udo A. Steinberg <st*******@doma in.invalidwrote :
>
In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed
that "x" will be equal to "a" only if the condition "cond" holds. Assuming that
"a" is a memory location, is it also guaranteed that "a" will not be accessed
in memory if the condition does not hold?
Yes. The ?: operator is defined to only evaluate one of a and b, it is
not permitted to evaluate both. On the other hand, compilers are free
to do whatever they want, as long as a strictly conforming program can't
tell any difference. So a particular implementation *could*
speculatively evaluate both a and b as long as it ensures that nothing
bad (like a trap) happens when evaluating something it shouldn't be
evaluating.
-Larry Jones
It must be sad being a species with so little imagination. -- Calvin la************@ ugs.com writes:
Udo A. Steinberg <st*******@doma in.invalidwrote :
>> In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed that "x" will be equal to "a" only if the condition "cond" holds. Assuming that "a" is a memory location, is it also guaranteed that "a" will not be accessed in memory if the condition does not hold?
Yes. The ?: operator is defined to only evaluate one of a and b, it is
not permitted to evaluate both. On the other hand, compilers are free
to do whatever they want, as long as a strictly conforming program can't
tell any difference. So a particular implementation *could*
speculatively evaluate both a and b as long as it ensures that nothing
bad (like a trap) happens when evaluating something it shouldn't be
evaluating.
And how would a compiler determine that at compile time? It cant as far
as I can see in the case of variable memory references. So it shouldnt
access the non matching expression.
>
-Larry Jones
It must be sad being a species with so little imagination. -- Calvin
la************@ ugs.com wrote:
(WRT the conditional operator)
tell any difference. So a particular implementation *could*
speculatively evaluate both a and b as long as it ensures that nothing
bad (like a trap) happens when evaluating something it shouldn't be
evaluating.
Not only must nothing bad happen, the program must not be able to
determine that the prohibited evaluation occurred.
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Richard <rg****@gmail.c omwrote:
Is it handled any different from
if(cond)
x=a;
else
x=b;
It is different, but in rather subtle ways. 6.5.15 of n869 places a
number of conditions on what a and b may be, as well as the type of
the result of (cond?a:b).
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Richard wrote On 08/07/06 10:24,:
la************@ ugs.com writes:
>>Udo A. Steinberg <st*******@doma in.invalidwrote :
>>>In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed that "x" will be equal to "a" only if the condition "cond" holds. Assuming that "a" is a memory location, is it also guaranteed that "a" will not be accessed in memory if the condition does not hold?
Yes. The ?: operator is defined to only evaluate one of a and b, it is not permitted to evaluate both. On the other hand, compilers are free to do whatever they want, as long as a strictly conforming program can't tell any difference. So a particular implementation *could* speculative ly evaluate both a and b as long as it ensures that nothing bad (like a trap) happens when evaluating something it shouldn't be evaluating.
And how would a compiler determine that at compile time? It cant as far
as I can see in the case of variable memory references. So it shouldnt
access the non matching expression.
int a = f();
int b = g();
int x = cond ? a : b;
"Hmmm. As the compiler, I happen to know that register
R0 still contains the value returned by g(), the thing I just
stored into b a moment ago. So if cond turns out to be false,
I don't need to fetch anything from memory; I've already got
exactly what's needed right here in R0. Hey, look: I've just
``evaluated'' b before testing cond, and in perfect safety!"
Generated code:
call f
store r0,a
call g
store r0,b
load r1,cond
jzero r1,label
load r0,a
label:
store r0,x
-- Er*********@sun .com
In article <87************ @mail.comRichar d <rg****@gmail.c omwrites:
la************@ ugs.com writes:
Udo A. Steinberg <st*******@doma in.invalidwrote :
In a ternary statement such as: x = (cond ? a : b); it is obviously
guaranteed that "x" will be equal to "a" only if the condition "cond"
holds. Assuming that "a" is a memory location, is it also guaranteed
that "a" will not be accessed in memory if the condition does not hold?
Yes. The ?: operator is defined to only evaluate one of a and b, it is
not permitted to evaluate both. On the other hand, compilers are free
to do whatever they want, as long as a strictly conforming program can't
tell any difference. So a particular implementation *could*
speculatively evaluate both a and b as long as it ensures that nothing
bad (like a trap) happens when evaluating something it shouldn't be
evaluating.
And how would a compiler determine that at compile time? It cant as far
as I can see in the case of variable memory references. So it shouldnt
access the non matching expression.
The 'a' in the code above is a variable, and so, as far as the compiler
is concerned, has been assigned an address in memory. If it has not
been assigned an address, the implementation appears to be very strange.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: praba kar |
last post by:
Dear All,
I am new to Python. I want to know how to
work with ternary operator in Python. I cannot
find any ternary operator in Python. So Kindly
clear my doubt regarding this
__________________________________
Yahoo! Messenger
|
by: glongword |
last post by:
As the assert macro should evaluate to a void expression, it should
not have an 'if statement' in its definition. Does this necessitate
the existence of a ternary conditional operator? Is there a way
assert.h can be implemented without using it? Are these decisions
related in anyway?
| |
by: Robert Zurer |
last post by:
In his paper on Coding Standard found on
http://www.idesign.net/idesign/DesktopDefault.aspx
Juval Lowy discourages the use of the ternary conditional operator with no
specific reason given.
I can understand that complicated code would be much less readable using nested
operators, but aside from that is there another reason? -- something going on
under the hood perhaps which make it inadvisable to use?
|
by: Daniel Crespo |
last post by:
Hi!
I would like to know how can I do the PHP ternary operator/statement
(... ? ... : ...) in Python...
I want to something like:
a = {'Huge': (quantity>90) ? True : False}
Any suggestions?
| |
by: Arthur Dent |
last post by:
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use expressions
which may fail if they are evaluated when they arent supposed to be, and it
would be nice to have a concise way of writing this instead of using a whole
If-Then-Else...
|
by: PerlPhi |
last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show you something first before i go to my delimma.
codes as follows using Mrs. If Else:
#!perl/bin/perl
use strict;
print "Are you sure you want to quit ('yes' or any key for 'no'): ";
chomp($_ = <STDIN>);
|
by: raiderdav |
last post by:
I understand how the ternary operator (question mark - ?) works in an
if/else setting, but what does it mean when used as a type?
For instance, I'm trying to add a get/set in some existing code, but
the return type doesn't match:
Rectangle? m_textArea = null;
public Rectangle TextArea {
set { m_textArea = value; }
get { return m_textArea; }
|
by: lawpoop |
last post by:
I'm trying to write some simple code, but I might be simplifying it
too much.
I have a function that either returns a string that I want, or FALSE.
In the code that I'm working on, I would like to default to another
string if the function returns false.
Here's the long way to do it:
$string = myFunction(2);
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |