Skip to content

Instantly share code, notes, and snippets.

@Gankra
Last active November 12, 2018 23:40
Show Gist options
  • Save Gankra/22d6048ab3ec7986f27341bdc2d1e3f5 to your computer and use it in GitHub Desktop.
Save Gankra/22d6048ab3ec7986f27341bdc2d1e3f5 to your computer and use it in GitHub Desktop.

Strategies for Corner Cases (using integer overflow for examples)

  • Defined Behaviour: all implementations must behave exactly like this!

    • Defined to a usually-unhelpful, but perhaps common, behaviour (wrap on overflow)

    • Defined to usually-desired, but perhaps expensive, behaviour (provide infinite precision)

    • Defined to be an error that prevents progress (crash on overflow)

    • Defined to the behaviour the author found aesthetically pleasing (saturate of course, it's so elegant!!)

  • Undefined Behaviour: implementations need-not accomodate the possibility (overflow is a logical contradiction, and so can't happen, nasal demons etc.)

  • Unspecified Behaviour: implementations can do whatever, within reason; AKA "soft undefined" (overflow can happen, and the resulting value is junk, but that's it)

  • Implementation-Defined Behaviour: implementations must declare a specific behaviour

    • So that it can do the thing conventional to each platform, but different impls on the same platform would generally be expected to make the same choice, and therefore should interoperate cleanly (do what the hardware's native instruction does)

    • So that different implementations may choose different points in the tradeoff space (compiler flags decide behaviour)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment