Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: ****ft oper...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 7 Topic 9493 of 9775
Post > Topic >>

Re: ****ft operator undefined behavior

by Greg Herlihy <greghe@[EMAIL PROTECTED] > Apr 14, 2008 at 01:13 PM

On Apr 12, 11:13 am, Jack Klein <jackkl...@[EMAIL PROTECTED]
> wrote:
> On Fri, 11 Apr 2008 15:33:41 CST, Olivier Langlois
> <olangl...@[EMAIL PROTECTED]
> wrote in comp.lang.c++.moderated:
> > The root for this behavior probably originates from C but I am really
> > curious why the standard makes the ****ft operator so unintuitive.
>
> The ****ft operator mimics the underlying behavior of the hardware,
> which differs on various architectures.  This is exactly the same
> reason that right ****fting negative signed values and signed value
> overflow have undefined results, namely because what the underlying
> hardware does can be different.

Only ****fts to the left can produce undefined behavior in C++ - ****fts
to the right produce - at the very least - "implementation-defined"
results. In other words, ****fts to the right always lead to well-
defined and documented values, whereas even ****fting one-too-many bits
to the left - might well be fatal to the program. Now, perhaps thirty
years ago there were machines that could ****ft bits reliably only in
one direction - but those machines are long gone and forgotten - by
everyone, that is, except the C++ Standard.

> Nowadays, most modern processors mask the ****ft value before it goes
> to the ****fter hardware.  When ****fting a 32-bit register, they mask
> the ****ft argument with 0x1f.  This is especially true of processors
> with barrel ****fters that can ****ft by any number in a single cycle,
> instead of one cycle per bit ****fted.
>
> In many ways, C++ shares one of the most im****tant features of C,
> namely, you don't pay for what you don't use.  The vast majority of
> programs never try to ****ft a value by too many bits, so the source
> statement turns into the minimal object code to use the hardware ****ft
> instruction.

The issue is not the code generated, but whether the generated code
will produce well-defined results. Clearly, leftward ****fts are just
as well-defined as rightward ****fts on any conceivable modern hardware
- so the bit-****fting description of the C++ Standard should be
updated (with "implementation-defined" or even "unspecified" in place
of "undefined" behavior) to reflect that reality.

Greg


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 7 Posts in Topic:
shift operator undefined behavior
Olivier Langlois <olan  2008-04-11 15:33:41 
Re: shift operator undefined behavior
Jack Klein <jackklein@  2008-04-12 12:13:12 
Re: shift operator undefined behavior
"Bo Persson" &l  2008-04-13 00:43:14 
Re: shift operator undefined behavior
Olivier Langlois <olan  2008-04-13 00:43:12 
Re: shift operator undefined behavior
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-13 00:43:11 
Re: shift operator undefined behavior
Jack Klein <jackklein@  2008-04-14 10:43:12 
Re: shift operator undefined behavior
Greg Herlihy <greghe@[  2008-04-14 13:13:15 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:45:09 CDT 2008.