3 ms·
X86 has also had saturation arithmetic since MMX was introduced.
by anilakar 7y ago
X86 has also had saturation arithmetic since MMX was introduced.
- leni536 7y agoYou should never rely on the underlying CPU architecture's behavior on integer overflow when you write C or C++. It is undefined and not unspecified or implementation defined.
- leetcrew 7y agotrue, but the behavior of unsigned integers is defined such that they can't overflow. the "wrapping" behavior of unsigned ints is defined: you get the arithmetic result modulo the max value for that width.
- tom_ 7y agoUndefined behaviour is undefined only by the standard - an implementation may choose to handle undefined behaviour in some documented, specific fashion. Undefined behaviour exists to give implementations exactly this flexibility.
- leni536 7y ago> an implementation may choose to handle undefined behaviour in some documented, specific fashion Or they can leave it undefined and not make any guarantees. I do not think that any compilers on any platform makes any guarantee on signed overflow.
- pjc50 7y ago.... However, unless you use non portable intrinsics, you can't make the compiler use MMX. So in practice you get wrap on overflow. Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping.
- gpderetta 7y agoYou can build portable libraries on top of nonportable primitives.
- jstanley 7y ago> Worse, the compiler is entitled to use saturation arithmetic if you've assumed wrapping. C89 (draft[1], because the actual spec is not public) section 3.1.2.5 states: > ... a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type. So, unless I'm misunderstanding, the compiler is not entitled to use saturation arithmetic on unsigned integers. [1] http://port70.net/~nsz/c/c89/c89-draft.html http://port70.net/~nsz/c/c89/c89-draft.html
- ncmncm 7y agosigned and unsigned have different semantics. The compiler is allowed to do literally anything on signed overflow, including launching the missiles, and not running the program. Or both.