Article View: comp.std.c
Article #4581Re: Undefined result vs. int's holding undefined values.
From: Paul Eggert
Date: Mon, 08 Jan 1996 02:20
Date: Mon, 08 Jan 1996 02:20
22 lines
1697 bytes
1697 bytes
thads@csn.net (Thad Smith) writes: > it is possible to rewrite the function so that it doesn't > invoke undefined behavior Yes. But it slows things down measurably for an interpreter to use such a clumsy method for integer overflow detection. The (x + y < x) != (y < 0) method requires a comparison, an exclusive OR of the comparison result bit with a sign bit, and a conditional branch. The method you suggested requires a comparison, a conditional branch, a subtraction, another comparison, and another conditional branch. Since this is comp.std.c, I'll mention that the (x + y < x) != (y < 0) method and Daniel Wood's example both port to any C implementation claiming conformance to LIA-1 (ISO/IEC 10967-1:1994(E), `Information technology -- Language independent arithmetic -- Part 1: Integer floating point arithmetic' <ftp://crl.dec.com/pub/misc/lia-1-dis.ps.Z>) if INT_MODULO is true (which is the only plausible value for INT_MODULO for real-world C compilers). I doubt whether Wood's compiler vendor claims LIA-1 conformance today, but this may become more important in the future, and perhaps Wood can point his vendor at LIA-1 and say that it's time to read the handwriting on the wall.
Technical Details
Message-ID:
<4cpv1n$apm@der.twinsun.com>Path:
arc1-ca.pugleaf.net!news.cs.ucla.edu!twinsun!not-for-mailReferences:
<4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.cs.ucla.edu!twinsun!not-for-mail From: eggert@twinsun.com (Paul Eggert) Newsgroups: comp.std.c Subject: Re: Undefined result vs. int's holding undefined values. Date: 7 Jan 1996 18:20:39 -0800 Organization: Twin Sun Inc, El Segundo, CA, USA Message-ID: <4cpv1n$apm@der.twinsun.com> References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net> NNTP-Posting-Host: der.twinsun.com