is this broken or what?

Henry Spencer henry at utzoo.uucp
Sat Feb 3 04:27:53 AEST 1990


In article <12981 at cbnewsd.ATT.COM> knudsen at 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 at zoo.toronto.edu



More information about the Comp.lang.c mailing list