|
|
Subscribe / Log in / New account

Undefined behaviour

Undefined behaviour

Posted Sep 7, 2018 18:41 UTC (Fri) by Wol (subscriber, #4433)
In reply to: Undefined behaviour by dvdeug
Parent article: C considered dangerous

> At the extreme, C compilers, when encountering implementation defined behavior, have stopped compiling and ran a game.

Given that C has always been considered a "super-assembly" language, and most of the undefined behaviours were, I believe, down to the fact that the original writers of C knew that different hardware behaves in different ways, I would have thought that the C spec should have said "When behaviours are undefined because different hardware behaves differently, it is down to the compiler writers to provide a definition for their implementation".

Bit like the Z80 scenario where the right shift instruction behaved as expected (integer divide by two) but thanks to a bug the left shift did a "double and increment" operation. If the C language didn't specify the behaviour of a left shift thanks to this, then it should have placed the onus on the compiler writers to specify it, not just leaving the behaviour in doubt.

(And this silicon bug really did exist, and quite a few assembler programs took advantage of it, I believe ... left-shift-and-increment it got known as :-)

Cheers,
Wol


to post comments

Undefined behaviour

Posted Sep 9, 2018 15:40 UTC (Sun) by jem (subscriber, #24231) [Link] (3 responses)

The Z-80 did have a working shift left instruction (SLA), in addition to two working shift right instructions (SRA and SRL). The "bug" you are referring to is an undocumented opcode which, by chance, behaved like you describe.

Undefined behaviour

Posted Sep 10, 2018 18:14 UTC (Mon) by Wol (subscriber, #4433) [Link] (2 responses)

As I heard it, it was undocumented BECAUSE it didn't work.

You mention SRA and SRL. Therefore you should have a matching SLA and SLL. The story as I heard it was that some programmers used that exact logic, worked out what the opcode for SLL should be, and discovered what I called "shift left and increment".

In other words, a perusal of the documentation led to people deducing a "missing" opcode, and when they tried it, it ALMOST did what was expected.

Cheers,
Wol

Undefined behaviour

Posted Sep 10, 2018 19:17 UTC (Mon) by mgb (guest, #3226) [Link]

> You mention SRA and SRL. Therefore you should have a matching SLA and SLL.

How would you have expected the functionality of these hypothetical SLA and SLL to have differed?

Undefined behaviour

Posted Sep 10, 2018 22:28 UTC (Mon) by anselm (subscriber, #2796) [Link]

The main difference between an arithmetic shift to the right and a logical shift to the right is that the logical shift will make the MSB zero while the arithmetic shift will keep the MSB (the sign of a signed number) whatever it was before the shift. This doesn't make a huge amount of sense when shifting to the left, because the LSB in a signed number isn't special, so processor makers often didn't bother with arithmetic shifts to the left (especially with the simpler late-20th-century-vintage CPUs).

Undefined behaviour

Posted Sep 10, 2018 15:08 UTC (Mon) by anselm (subscriber, #2796) [Link]

I would have thought that the C spec should have said "When behaviours are undefined because different hardware behaves differently, it is down to the compiler writers to provide a definition for their implementation".

The C standard, at least back when I was still programming in C, did in fact make that distinction. When dealing with a C implementation, you distinguish between behaviour that is specified in the standard, “implementation-defined behaviour” that is not specified in the standard but requires a consistent (and documented) approach by the implementation, and “undefined behaviour”, where anything can happen, including unpredictable and inconsistent effects.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds