Reputation
4,067
Top tag
Next privilege 5,000 Rep.
Approve tag wiki edits
Badges
7 16
Newest
 Talkative
Impact
~299k people reached

  • 0 posts edited
  • 0 helpful flags
  • 173 votes cast
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
...generate needlessly-inefficient code on at least some platforms (compared with what those platforms could generate if given a free choice of whether or not to invoke do_something)? Even if loop optimizations were forbidden from yielding behavior inconsistent with a loose overflow model, programmers could write code in such a way as to allow compilers to generate optimal code. Is there any way to work around inefficiencies compelled by an "avoid overflow at all costs" model?
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
I wonder how language rules could best be written so that code which would be happy with a range of behaviors in case of overflow could say that, without impeding genuinely useful optimizations? There are many cases where code must produce strictly-defined output given valid input, but loosely-defined output when given invalid input. E.g. suppose a programmer who would be inclined to write if (x-y>z) do_something();` doesn't care whether do_something executes in case of overflow, provided the overflow has no other effect. Is there any way to rewrite the above which will not...
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
Upon further consideration, I can think of situations where assumptions about the laws of integer arithmetic could cause loops to terminate prematurely before any UB occurs and without the a loop doing any illegitimate address computations. In FORTRAN, there's a clear distinction between loop control variables and other variables, but C lacks that distinction, which is unfortunate since requiring programmers to always prevent overflow on loop control variables would be far less of an impediment to efficient coding than having to prevent overflow anywhere.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
...could deterministically affect program execution, then I would argue that a compiler should refrain from the indicated optimizations (though I'll readily acknowledge that some might not have been programmed to prevent such optimizations unless i was also made volatile). That would be a pretty rare behavioral corner case, though. If a and b are non-volatile, I'd suggest that there would be no plausible intended meaning for what the code should do if n is so large as to overwrite all of memory. By contrast, many other forms of UB have plausible intended meanings.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
If neither pointer is volatile, I wouldn't consider that a rewrite of the code. Compilers have for a long time been allowed to resequence writes to non-volatile pointers, so behavior in the case where n is so large that pointers would wrap would be equivalent to having an out-of-bounds store clobber a temporary storage location holding i before anything else happens. If a or b were volatile, the platform documented that volatile accesses generate physical load/store operations in the sequence requested, and it the platform defines any means via which such requests...
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
@gnasher729: I've used systems with 16-bit char, but never 24-bit int. I guess on such a system the first form would be "more" portable than the second, though I'd suggest the first form of the code is really targeting itself only toward systems where int is 17-25 bits, the second form toward systems where int is 26 bits or larger, and the non-listed choice (60*60*24L*365) as targeting systems with any legal size int that might be 25 bits or smaller.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
Prior to roughly 2009, a typical interpretation of many forms of UB would be e.g. "Even if executing a shift-right-by-N instruction on the target CPU could stall the bus for several minutes with interrupt disabled when N is -1 [I've heard of such a CPU], a compiler may pass any N to the shift-right-by-N instruction without validation, or mask N with 31, or arbitrarily choose among those options". In cases where any of the above behaviors would meet requirements, programmers didn't need to waste time (theirs or the machine's) preventing them from occurring. What prompted the change?
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
The reason C got its reputation for speed is that programmers who knew that even in some cases where the Standard imposed no requirements the behavior of their platform would satisfy their requirements could make use of such behavior. If a platform guarantees that x-y > z will arbitrarily yield 0 or 1 when x-y is not representable as "int", such a platform will have more optimization opportunities than a platform which requires the expression to be written as either UINT_MAX/2+1+x+y > UINT_MAX/2+1+z or (long long)x+y > z.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
My question is when did things shift from "x>>32` might arbitrarily yield x or 0, or might trap on some obscure platforms" to "x>>32 might cause the compiler to rewrite the meaning of other code"? The earliest evidence I can find is from 2009, but I'm curious if earlier evidence exists.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
I can think of very few platforms where an implementation of x>>y [for unsigned x] that would work when variable y held any value from 0 to 31, and did something other than yield 0 or x>>(y & 31) for other values, could be as efficient as one which did something else; I know of no platform where guaranteeing that no action other than one of the above would occur would add significant cost. The idea that programmers should use some more complicated formulation in code that would never have to be run on obscure machines would have been viewed as absurd.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
@rwong: I just tried that embedded C quiz. I'm not sure why (60 * 60 * 24 * 365UL) is in any practical sense more portable than (60 * 60 * 24 * 365) since very few machines can represent 86400 as an int without overflow without also being able to represent 31536000. A proper portable way to write it would have been (60 * 60 * 24L * 365) but that wasn't a choice.
Aug
2
comment How did programming work when programmers used punchcards?
@tcrosley: Hmm... maybe nobody thought of using two. There were a lot of hacks people invented back then which were never publicized until after they were pretty useless. My favorite was a carriage-control tape that was two pages long, with two 1's punched a page apart, but with an 8 and 9 punched just before the starts of opposite pages. Using an "8" as the carriage-control character would advance to the perf at the top of an up-facing page, and "9" would advance to the perf at the bottom of an up-facing page.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
@rwong: I don't quite understand your historical illusion; can you offer a brief summary? Otherwise, I wonder if your view that people should move away from C is shared by the current maintainers of the Standard. It would explain a lot.
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
@rwong: The code in the question was written to perform an action which on the vast majority of compilers would historically have had one of two behaviors, both of which would end up yielding the same effect. Someone writing such code might reasonably have figured "If someone ever needs to run this code on a processor where x>>32 does something other than yielding x or yielding zero, this code won't work, but there's no plausible reason to believe such a thing might occur".
Aug
2
comment When did Undefined Behavior in C jump the causality barrier
@rwong: With regard for my question, I wrote it in such a fashion that it should be answerable. Some recent papers have advocated the kind of "optimization" I describe and have been taken seriously, but presumably none did so prior to the invention of C. Consequently, someone must have advocated such behavior and been taken seriously before anyone else did so. Whether or not it is possible to prove that any documented advocacy of such behavior is the first, it would be likely that any advocacy of which no evidence is found probably wasn't taken too seriously.
Aug
1
comment When did Undefined Behavior in C jump the causality barrier
...recognize that the latter should only take one instruction. I don't know if the hyper-modernists are desperately trying to make C competitive in fields where it's been replaced, but they're undermining its utility in the only field where it's king, and are from what I can tell making useful optimizations harder rather than easier.
Aug
1
comment When did Undefined Behavior in C jump the causality barrier
@rwong: C is still the best language for many kinds of systems-programming tasks where most forms of Undefined Behavior have traditionally been mapped to "generate the instruction to the indicated operation on the indicated values without regard for the validity of the operands and let the underlying hardware do as it will". If code will only be meaningful on a processor where the result of x>>=y is equivalent to for (int i=0; i<(y & 255); x>>=1 but executes in two cycles for any y, I'd consider x>>y more readable than (y & 224) ? 0 : x >> (y & 31), even if an optimizer could...
Aug
1
comment Why hasn't a faster, “better” language than C come out?
@StephenCanon: Is there any mechanism in C to tell a compiler that a global variable may safely be cached in a register across a call to an external function? Is there any mechanism in C to indicate that a write to short* might clobber a particular int, other than by using memcpy which would force the compiler to flush to memory everything that could alias and then reload it after?
Aug
1
comment How did programming work when programmers used punchcards?
@tcrosley: Only one diagonal stripe per card? I would think that using two arbitrarily-sloped stripes per card would have made it much easier to spot cards that somehow got mixed in with the wrong group.
Aug
1
revised When did Undefined Behavior in C jump the causality barrier
Fixed `amount-y` in text to `32-amount`.