Journal of Universal Computer Science, vol. 3, no. 10 (1997), 1121-1125 submitted: 23/6/97, accepted: 2/9/97, appeared: 28/10/97 Springer Pub. Co.
A Note on Correctness Proofs for Over ow Detection Logic in Adders for d-th Complement Numbers Bernd Rederlechner
(Telekom Entwicklungszentrum Sudwest, Saarbrucken, Germany
[email protected])
Jorg Keller
(FernUniversitat-GH Hagen, Germany
[email protected])
Abstract: When adding -bit 2-th complement numbers, the result can be outside the n
range representable with bits. A well{known theorem justi es the common over ow logic: Let 2 f0 1gn be the 2-th complement representations of signed integers [ ] and [ ], respectively, and let 0 2 f0 1g be the carry-in bit. Then, [ ] + [ ] + 0 2 f,2n,1 2n,1 , 1g if and only if n = n,1 , where i denotes the carry{bit from position , 1 to position when adding the binary numbers and . We present a proof of this theorem which is much shorter than previous proofs. This simpli cation can save valuable time in computer science classes. With a small extension the proof even holds for -th complement numbers. Although the proof technique is known by some specialists, nobody seems to have written it up. With this note, it is once documented in a precise form, thus avoiding re-invention. Key Words: -ary arithmetic, correctness proof, computer science education, over ow testing Category: B.2, K.3.2 n
a; b
;
a
b
c
;:::;
;
c
i
a
c
b
c
c
i
a
b
d
d
1 Introduction Most microprocessors use 2-th complement numbers as internal representation for signed integers. The addition of 2-th complement numbers is done by adders for binary numbers. However, the sum of two -bit 2-th complement numbers could be outside the range representable by bits. There are two common methods to detect such an over ow. Either, one compares the two most signi cant carry bits. Alternatively, one doubles the most signi cant input bits, uses ( +1)bit arithmetic and compares the two most signi cant sum bits. In both cases, an over ow has occured if and only if the bits are not equal. To justify that adders for binary numbers correctly add 2-th complement numbers and that the above method to detect an over ow is correct, most proofs proceed in the following way: depending on the signs of the numbers to be added, dierent cases are discussed. [Hwang (79)], [Scott (85)] and [Spaniol (81)] introduce -th complement numbers (also called radix-d numbers). Hwang applies the above technique for a more general proof and lls about two pages. Dicult notations make readability worse. [Spaniol (81)] leaves the proof about over ow logic to the reader and only proves that summation is correct in the absence of over ows. [Scott (85)] uses a dierent de nition, argues informally about the correctness of summation in absence of over ows, and shifts the over ow detection for 2-th complement numbers into the excercises. The remaining texts that we n
n
n
d
1122
Rederlechner B., Keller J.: A Note on Correctness Proofs for Overflow ...
investigated treat only 2-th complement numbers, which is sucient for a basic course in computer architecture or arithmetic. [Hotz (72)], [Keller and Paul (95)] and [Omondi (94)] use the presented scheme with three cases, consuming about one to one and a half pages without notations. [Homann (83)] derives a formula depending on the most signi cant bits of the numbers to be added and the most signi cant bit of the sum. He completes the proof by putting in all possible values for these bits, and thus discusses nine cases. [Hotz (90)] and [Savage (76)] shift the theorem into the exercise part, [Koren (93)] shows the dierent cases only informally, and [Hennessy and Patterson (90)] even justify nothing at all. While none of these proofs is dicult, they are rather lengthy. Hence, presenting the proof in a computer architecture class takes away valuable time from other topics. We present a proof which is much shorter than previous proofs. It is easy to present as it only employs simple equivalence transformations and a sharp look at the last line. No cases are distinguished, and no additional lemmata are needed. Only some notations are introduced, the most of which will already be present when the proof is used in a course on the subject. With a small extension, the proof also works for -th complement numbers with arbitrary base 2. While the technique used is apparently known by some specialists, nobody seems to have written it up. To avoid that the technique is re-invented over and over, it is hereby once documented in a precise form. d
d
2 Notations We use notations from [Keller and Paul (95)], the de nition of -th complement is taken from [Hwang (79)] and [Spaniol (81)]. Let d = f0 P, 1g. For n,2 0 = ( n,2 0 ) 2 dn,1 , we de ne h 00id = h n,2 0 id = i=0 i i 0 and call the -ary representation of h id . To represent signed integers, we introduce a sign digit n,1 2 f0 , 1g =: d0 . By = ( n,1 0 ) 2 d0 dn,1 we represent the number [ ]d de ned as 0 0 [ ]d = [ n,1 0 ]d = , n,,11 n,1 + h 0 id = hh 0 iid , n,1 ifif nn,,11 = = ,1 d The string is called the -th complement representation of [ ]d . Obviously, n,1 , 1g and [ ] 2 d := f, n,1 n,1 , 1g. We h 0 id 2 nd,1 := f0 d n will omit the subscript if the base is clear from the context. For base = 2, string 0 P 2 f0 1gn,1 is the binary representation of the ,2 2i 2 0 non-negative integer h i2 = ni=0 2n,1 , 1g, and i n,1 = f0 0 n = ( n,1 ) 2 f0 1g is the 2-th complement representation of integer [ ]2 = , n,1 2n,1 + h 0 i2 2 n = f,2n,1 2n,1 , 1g. When adding numbers h id and h id with -ary representations 2 dn and a carry{in bit 0 2 f0 1g, the sum is h n id = h id + h id + 0 where = ( n,1 0 ) 2 dn and n 2 f0 1g. For all 1 , we denote the carry bit from position , 1 to position by i , i.e. h i i,1 0 id = i i + h i,1 0 id = h i,1 (1) 0 id + h i,1 0 id + 0 d
B
a
a
;:::;a
a
B
a
d
a
;:::;d
;:::;a
a
d
a
a
;d
B
a
a
;a
B
B
a
a
a
a
;a
d
d
a
a
a
;a
d
T
d
;
a
S
;:::;
a
a
T
;:::;
a
c
;s
B
b
d
;
c
c
i
c ;s
:
;:::;d
;
a
s
a
d
a
a
s
d
a
;:::;d d
a
a
a
d
S
d
a
a
a
;
i
i
;:::;s
c
a
a; b
;s
a
b
c
n
c
d
s
;:::;a
;:::;s b
;:::;b
c
:
B
1123
Rederlechner B., Keller J.: A Note on Correctness Proofs for Overflow ...
We have
= i+1 + i 0 , 1 (2) The common over ow logic and the use of -ary adders for -th complement numbers is justi ed by the following Theorem. Theorem 1. Let 2 d0 dn,1 and 0 2 f0 1g, and let n 2 f0 1g and n 2 d such that h i + h i + 0 = h n i. Then, (a) [ ] + [ ] + 0 2 n if and only if n = n,1 . (b) If [ ] + [ ] + 0 2 n, then [ ] + [ ] + 0 = [ ]. Before we prove Theorem 1, note that [Scott (85)] de nes d n 2 n 2 , 1g (assume to be even), n = f, and for 2 dn [ ]d = , n,21 n + h id = hh iid , n ifif nn,,11 22 d i + bi + ci
a
d
c
s ;
i
n
:
d
a; b
s
B
B
a
a
b
a
c
b
c
c
c
T
;
c
;
c ;s
T
c
T
a
B
b
d
a
c
b
d = ;:::;d
c
s
=
d
B
a
a
d
d=
a
a
a
d
a
< d=
a
d=
:
For example 410 = f,5000 4999g, [0000]10 = 0, [4999]10 = 4999, [5000]10 = ,5000, and [9999]10 = ,1. For = 2, this de nition equals the one given by [Hwang (79)] and [Spaniol (81)]. However, for 4, only part (b) of Theorem 1 holds with Scott's de nition. Part (a) does not hold as two counterexamples show. First, let = 2 , 1, 0 = , 2, 0 = 0, and = = ( 0 0) 2 dn . Then, [ ]d = [ ]d = n 2 , n,1 2 n , but [ ] + [ ] + n d d 0 = , 2 n,1n 2 dn. If we add and d with a -ary adder, we obtain = ( 0 0 0) 2 d , and for all = 1 , i = 0 and thus n = n,1 . Second, let ~ = , 1, = = (0 ~ ~) 2 dn , and 0 = 0. Then, [ ]d = [ ]d = n,1 , 1 2 dn , and [ ]d + [ ]d + 0 = 2 n,1 , 2 2 dn . If we add and with a -ary adder, we obtain = (1 ~ ~ 0 ) 2 dn , n = 0, and , 1, and thus n 6= n,1 . i = 1 for all = 1 T
; : : :;
d
d
x
x
d
d =
c
d
a
T
b
a
d
c
a
a
b
c
c
x
d
a
d
b
T
x;
b
c
d
s
x ;
;:::;
b
i
d
b
c
d=
a
b
= T
a
B
c
s
;:::;n
B
i
; x; : : : ; x
a
d
c
;:::;
B
b
;:::;n
c
d
T
; x; : : : ; x; x
B
c
c
3 Proof for 2-th Complement Numbers Proof. The de nition of 2-th complement numbers gives: [a] + [b] + c0 = ,2n,1(an,1 + bn,1 ) + ha0 i + hb0 i + c0 = ,2n,1(an,1 + bn,1 ) + hcn,1 ; s0 i n , 1 = ,2 (an,1 + bn,1 , cn,1 ) + |{z} hs0 i
(3)
2 Sn,1
With = 2 and = , 1, we can transform (2) to n,1 + n,1 = 2 n + Together with (3), we have [ ] + [ ] + 0 = ,2n( n , n,1 ) , 2n,1 n,1 + h 0 i = ,2n( n , n,1 ) + [] (4) |{z} d
i
n,1 , cn,1 .
s
a
b
n
c
a
c
c
c
c
s
s
2Tn
b
s
c
1124
Rederlechner B., Keller J.: A Note on Correctness Proofs for Overflow ...
If n = n,1 , then the term ( n , n,1) in (4) equals zero and the \if" direction of (a) as well as (b) holds. If n 6= n,1 , then ,2n ( n , n,1 ) 2 f,2n 2ng, and for all [ ] 2 n one observes that [ ] 2n 2 n . This proves the \only if" direction of (a). c
c
c
c
c
s
c
T
c
s
c
;
= T
4 Extension to d-th Complement Numbers If we repeat the proof for 2-th complement numbers with the formulae for arbitrary , we get the following equivalent to (4): d
n
[ ]d [ ]d + [ ]d + 0 = , , 1 ( n , n,1 ) + |{z} a
b
d
c
c
d
c
s
2Tnd
Again, we see that the \if" direction of (a) and (b) hold. However, [ ]d n ( ,1) is not necessarily outside nd because n ( , 1) 2 n,1 for 3. If n 6= n,1 , then either n,1 = 0 and n = 1 or vice versa. If n,1 = 0 and 0 0 n,1 , 1 n = 1, then n,1 = n,1 = , 1 because of (2), and h i + h i + 0 because of (1). It follows that [ ] + [ ] + 0 = , n,1 n,,11 + n,,11 + h| 0 i + {z h 0 i + }0 , n,1 , 1 | {z } s
T
c
d
c
c
c
a
a
= d
b
b
c