Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: is this broken or what? Message-ID: <1990Feb2.172753.18062@utzoo.uucp> Organization: U of Toronto Zoology References: <2033@crdos1.crd.ge.COM> <572@idacrd.UUCP> <28941@amdcad.AMD.COM> <15793@haddock.ima.isc.com> <12981@cbnewsd.ATT.COM> Date: Fri, 2 Feb 90 17:27:53 GMT In article <12981@cbnewsd.ATT.COM> knudsen@cbnewsd.ATT.COM (michael.j.knudsen) writes: >My question is whether (i >= 0) is ALWAYS TRUE for unsigned i >in all dialects of C (including ANSI) and all hardware architectures, >or am I missing something? There is undoubtedly someone, somewhere, with a C compiler sufficiently broken that it's not true. All sane compilers consider it true. >I ask this partly because apparently ANSI has defined "overflow" >to require "change in sign", thus they can say with a straight face >that "unsigned ints never overflow". I'd like to see their faces >when they try telling that to the hardware (remember that?) >that generates interrupts on overflow. Standard C compilers must generate code that does not interrupt -- or at least acts as if it did not interrupt -- when an unsigned number wraps around. This is a guaranteed property of the language, not something that's left up to the implementation. >Seriously, unsigned declarations just control how the compiler >chooses conditional branch instructions when compiling |>| etc. >("higher" versus "greater"), so it's too late to call off >hardware overflow interrupts after a subtraction. Please justify this statement. It's not true in modern C. In fact, it was never true -- even K&R1 is explicit about unsigned arithmetic being modular arithmetic (in which there is no such thing as overflow). Don't confuse the bizarre properties of specific hardware with the documented behavior of the language. >Or does ANSI require that such interrupts be disabled before >executing unsigned comparisons, and re-enabled beofre the next signed >one? ANSI requires that unsigned numbers implement modular arithmetic. This requires that nothing untoward happen when unsigned arithmetic wraps around. If disabling interrupts is what it takes, then that's what has to be done. On most machines, there are easier ways. Most current C implementations ignore signed overflow and hence would always run with that interrupt disabled anyway. An implementation which catches signed overflow must arrange not to catch unsigned wraparound. -- 1972: Saturn V #15 flight-ready| Henry Spencer at U of Toronto Zoology 1990: birds nesting in engines | uunet!attcan!utzoo!henry henry@zoo.toronto.edu