Information Processing Letters Information Processing Letters 62 (1997) 161-163 Decimal adjustment of long numbers in constant time Andreas DGring, Wolfgang J. Paul* Fachbereich Informatik, Universitiit des Saarlandes, 66041 Saarbriicken, Germany Received 31 October 1996 Communicated by H. Ganzinger Abstract We propose a very simple method for adding and subtracting n-digit binary coded decimal (BCD) numbers with a small constant number of ordinary operations of a 4n-bit binary ALU. With this method addition/subtraction of 8-digit decimal numbers on an intel 486 processor is faster than programs that use the special built-in operations for decimal adjustment. @ 1997 Elsevier Science B.V. Keywords: Computer architecture; Performance evaluation; Addition; Decimal adjust after add; Decimal arithmetic 1. Decimal adjustment after addition Let II be a natural number, let p 2 2 be the base of a number system and let n = (x,-t,. . . ,x0) be a sequence of digits xi E (0,. . . ,p - 1). Then we denote by in the currys add x Let digits. c-1 number system with base /3. We denote by (x, y) the sequence of carries occurring if we and y in the number system with base j3. c = curryg( X, y) and s the sequence of result The basic properties of c and s are =o (1) n-1 [X]fl :=cXip’ 0 i=O i 1 the number represented by x in the number system with base p. Let x, y, z be sequences of digits. We write x+BY as a shorthand for the representation [xl/3 + [YIP * Corresponding author. if Xi + yi + Ci-_l < p, Ci = of ifXi+yi+Ci-1 si=(xi+Yi+Ci-1) >p, mod& (2) (3) foreach in {O,...,n1). We denote by 6” the sequence 6” = 6 - . .6 (n times). ’ For d E (0, 1)” we denote by 6 * d the result of multiplying vector d componentwise with the constant 6. The number 6 = 16 - 10 is the difference between the two bases, which are used in the next lemma. ’ Because 6 never occurs as a base here no confusion exponentiation will arise. 0020-0190/97/$17.00 @ 1997 Elsevier Science B.V. All rights reserved. PII s0020-0190(97)00052-5 with 162 A. Dhing. WJ. Paul/Information Processing Letters 62 (1997) 161-163 By an easy induction one concludes from Eqs. ( 1) (3). Lemma 1. Let a, b E (0,. . . ,9}“. Then a +IO b = u +I6 b +I6 6 * caq,(a, b). This lemma is implicitly used in [ 11. It justifies the classical procedure for decimal adjustment after binary addition: 1. Interpret n digit binary coded decimal numbers a and b as hexadecimal numbers and add them using an ordinary 4n bit binary addition. 2. Use special hardware to compute the decimal carries c~rry,~(u, b) and store them in dedicated flipflops. 3. Add 6 * currylo(u, b) to the result of the binary addition using a special decimal adjust after add instruction. The AM29332 ALU provides a decimal adjust after add instruction for n = 8 digits, i.e. 32 bits [ 31. The common Intel processors however process only 2 digits, i.e. 8 bits at a time [2]. Thus, after the binary addition of two n-digit binary coded decimals adjustment takes n/2 such operations plus some overhead e.g. for saving intermediate results. 2. Perform the hexadecimal addition a +I,5 d = a +I6 b +I,56” and save the carries e = curry16(u, d). 3. Perform the hexadecimal addition a +16 b +I6 6 * e. The result is the desired sum a +lo b. Example. a = 3175, b = 0928. 1. d = 0928 +I,5 6666 = 6F8E. 2. e=carry16(3175,6F8E) =0111,6*e=0666. 3. u+16b+166*e=u+lob=4103. 3. Computing binary carries So far we got rid of the extra hardware for the computation of decimal carries at the expense of a small number of extra arithmetic instructions. Moreover we can use the full word length of the ALU and do not have to process one byte at a time. Of course processors usually do not give the programmer access to intermediate carries of a binary addition. Only the overflow bit is visible for the programmer. But the intermediate carries can easily be computed with a small number of logical operations and shifts: Lemma 3. Let a’, b’ E (0, 1}4n be binuly numbers undc’= curryz (a’, b' ) . Then ~:=(u’@b’@(u’+2b’))i+l 2. Computing decimal carries The following easy lemma shows how to obtain the decimal carries c~rry,~(u, b) if one has access to the hexadecimal carries car~16(a, b), or equivalently, to every fourth’s carry of a binary addition. Lemma 2. Let a, b E {0, . . . ,9}“. Then for each i E (0,. . . , 4n - 2}, where @ denotes the bit-wise exclusive or. Lemmas l-3 together yield an algorithm which uses only common arithmetic and logical instructions as well as some shifts. Subtraction can be handled in a similar way. currylo(u, b) = curr7,&(u, b +I6 6”). 4. Performance Note that no carries occur in the computation of b +I6 6” in the number system of base 16. Thus we obtain the following very easy algorithm for the addition of two n digit binary numbers a = (a,- I, . . . , uo) and b= (bn_,....,bo): 1. Interpret a and b as hexadecimal numbers. Perform the hexadecimal addition d = b i-16 6”. No carry will occur. We compared the new algorithm and an algorithm using the native decimal adjust after add instruction daa on an intel486/DX2 processor (66 MHz) using the following benchmark Add componentwise two vectors A and B of length 1000, whose components are random 8 digit binary coded decimal numbers. A. Ddring, WJ. Paul/Information Processing Letters 62 (1997) 161-163 In protected mode the average run time of the new algorithm is 450 ,usec. Using the daa instruction and some optimizations we could not beat 579~~~. Acknowledgement We thank C. Leiserson for suggesting Lemma 3. 163 References [ 1 I D.K. Richards, Arithmetic Operations in Digital Computers (Van Nostrand, Princeton, NJ, 1955). 121 i486 Microprocessor (Intel Corporation, 1989). [ 31 Am29332 hstruction Manual -(Advanced b&o 1987). Devices,