> tvs'` 2bjbj{P{P 7v::*D1111D2r9222223338888888$h:h<933333922,9555322853855522oW~13R56B90r95=4B=5=53353333399W5F333r9333311$Fixed Point Arithmetic and the Packed Decimal Format
This set of lectures discusses the idea of fixed point arithmetic and its implementation by Packed Decimal Format on the IBM Mainframes.
The topics include
1. A review of IBM S/370 floating point formats, focusing on the precision with which real numbers can be stored.
2. The difference between the precision requirements of business applications and those of scientific applications.
3. An overview of the Packed Decimal format, as implemented on the IBM S/370 and predecessors.
We begin with a review of IBM notation for denoting bit numbers in a byte or word. From our viewpoint, it is a bit nonstandard.
IBM S/370: Terminology and Notation
The IBM 370 is a byteaddressable machine; each byte has a unique address.
The standard storage sizes on the IBM 370 are byte, halfword, and fullword.
Byte 8 binary bits
Halfword 16 binary bits 2 bytes
Fullword 32 binary bits 4 bytes.
In IBM terminology, the leftmost bit is bit zero, so we have the following.
Byte
01234567Halfword
0123456789101112131415Fullword
0 78 1516 2324 31Comment: The IBM 370 seems to be a big endian machine.
S/370: Available Floating Point Formats
There are three available formats for representing floating point numbers.
Single precision 4 bytes 32 bits: 0 31
Double precision 8 bytes 64 bits: 0 63
Extended precision 16 bytes 128 bits; 0 127.
The standard representation of the fields is as follows.
FormatSign bitExponent bitsFraction bitsSingle01 78 31Double01 78 63Extended01 78 127NOTE: Unlike the IEEE754 format, greater precision is not accompanied by a greater range of exponents.
The precision of the format depends on the number of bits used for the fraction.
Single precision 24 bit fraction 1 part in 224 7 digits precision *
Double precision 56 bit fraction 1 part in 256 16 digits precision **
*224 = 16,777,216 ** 256 ( (100.30103)56 ( 1016.86 ( 7(1016.
Precision Example: Slightly Exaggerated
Consider a banking problem. Banks lend each other money overnight.
At 3% annual interest, the overnight interest on $1,000,000 is $40.492.
Suppose my bank lends your bank $10,000,000 (ten million).
You owe me $404.92 in interest; $10,000,404.92 in total.
With seven significant digits, the amount might be calculated as $10,000,400.My bank loses $4.92.
I want my books to balance to the penny. I do not like floating point arithmetic.
TRUE STORY
When DEC (the Digital Equipment Corporation) was marketing their PDP11to a large New York bank, it supported integer and floating point arithmetic.
At this time, the PDP11 did not support decimal arithmetic.
The bank told DEC something like this: Add decimal arithmetic and we shall buy a few thousand. Without it no sale.
What do you think that DEC did?
Precision Example: Weather Modeling
Suppose a weather model that relies on three data to describe each point.
1. The temperature in Kelvins. Possible values are 200 K to 350 K.
2. The pressure in millibars. Typical values are around 1000.
3. The percent humidity. Possible ranges are 0.00 through 1.00 (100%).
Consider the errors associated with single precision floating point arithmetic.The values are precise to 1 part in 107.
The maximum temperature errors resulting at any step in the calculation would be:
3.5(105 Kelvins
1.0(104 millibars
1.0(105 percent in humidity.
As cumulative errors tend to cancel each other out, it is hard to imagine the cumulated error in the computation of any of these values as becoming significant to the result.
Example: A weather prediction accurate to (1 Kelvin is considered excellent.
Encoding Decimal Digits
There are ten decimal digits. As 23 < 10 ( 24, we require four binary bits in order to represent each of the digits.
Here are the standard encodings. Note the resemblance to hexadecimal, except that the nondecimal hexadecimal digits are not shown.
0 0000 5 0101
1 0001 6 0110
2 0010 7 0111
3 0011 8 1000
4 0100 9 1001
Note that the binary codes 1010, 1011, 1100, 1101, 1110, 1111for hexadecimal digits A B C D E Fare not used to represent decimal digits.
Packed decimal representation will have other uses for these binary codes.
Zoned Decimal Data
Remember that all textual data in computing are input and output as character codes. IBM S/370 uses 8bit EBCDIC to represent characters.
On input, these character codes are immediately converted to Zoned Decimal format, which uses 8bit (one byte) codes to represent each digit. With a slight exception, it is identical to EBCDIC.
Here are the EBCDIC representations of each digit, shown as 2 hex digits.
Digit Code Digit Code
Hex Binary Hex Binary
0 F0 1111 0000 5 F5 1111 0101
1 F1 1111 0001 6 F6 1111 0110
2 F2 1111 0010 7 F7 1111 0111
3 F3 1111 0011 8 F8 1111 1000
4 F4 1111 0100 9 F9 1111 1001
Packed Decimal Data
Packed decimal representation makes use of the fact that only four binary bits are required to represent any decimal digit.
Numbers can be packed, two digits to a byte.
How do we represent signed numbers in this representation?The answer is that we must include a sign half byte.
The IBM format for packed decimal allows for an arbitrary number of digits in each number stored. The range is from 1 to 31, inclusive.
After adding the sign half byte, the number of hexadecimal digits used to represent any number ranges from 2 through 32.
Numbers with an even digit count are converted to proper format by the addition of a leading zero; 1234 becomes 01234.
The system must allocate an integral number of bytes to store each datum, so each number stored in Packed Decimal format must have an odd number of digits.
Packed Decimal: The Sign Half Byte
In the S/370 Packed Decimal format, the sign is stored to the right of the string of digits as the least significant hexadecimal digit.
The standard calls for use of all six nondecimal hexadecimal digits as sign digits. The standard is as follows:
Binary Hex Sign Comments
1010 A +
1011 B
1100 C + The standard plus sign
1101 D The standard minus sign
1110 E +
1111 F + A common plus sign, resulting from a shortcut in translation from Zoned format.
Zoned Decimal Data
The zoned decimal format is a modification of the EBCDIC format.
The zoned decimal format seems to be a modification to facilitateprocessing decimal strings of variable length.
The length of zoned data may be from 1 to 16 digits, stored in 1 to 16 bytes.
We have the address of the first byte for the decimal data, but need some tag to denote the last (rightmost) byte.
The assembler places a sign zone for the rightmost byte of the zoned data. The common standard is XC for nonnegative numbers, and XD for negative numbers.
The format is used for constants possibly containing a decimal point, butit does not store the decimal point.
As an example, we consider the string 123.45.
Note that the format requires one byte per digit stored.
Creating the Zoned Representation
Here is how the assembler generates the zoned decimal format.
Consider the string 123.45.
The EBCDIC character representation is as follows.
Character123.45Code6DF1F2F34BF4F5The decimal point (code 4B) is not stored.
A bit later we shall see the reason for this.
The sign character is implicitly stored in the rightmost digit.
The zoned data representation is as follows.
12345F1F2F3F4D5The string F1 F2 F3 F4 C5 would indicate a positive number,with digits 12345.
Packed Decimal Data
The packed decimal format is the one preferred by business for financial use.Zoned decimal format is not used for arithmetic, but just for conversions.
As is suggested by the name, the packed format is more compact.
Zoned format one digit per byte
Packed format two digits per byte (mostly)
In the packed format, the rightmost byte stores the sign in its rightmost part,so the rightmost byte of packed format data contains only one digit.
All other bytes in the packed format contain two digits, each with value in 0 9.This implies that each packed constants always has an odd number of digits.
A leading 0 may be inserted, as needed.
The standard sign fields are: negative XD nonnegative XC
The length may be from 1 to 16 bytes, or 1 to 31 decimal digits.
Examples +7  7C 
13  01  3D 
Example: Addition of Two Packed Decimal Values
Consider two integer values, stored in packed decimal format.
Note that 32bit twos complement representation would limit the integer to a bit more than nine digits: 2, 147, 483, 648 through 2, 147, 483, 647.
Integers stored as packed decimals can have 31 digits and vary between 9, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999 and +9, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999 (9.99(1030)
Consider the addition of the numbers 97 and 12,541.
97 would be expanded to 097 and stored as 097D.
12,541 would be stored as 12541C.
The CPU does what we would do. It first pads the smaller number with 0s.
00097D
12541C
It then performs the operation, denoted as 12541 97, and stores theresult as 12444, or 12444C in packed decimal format.
Fixed Point: Where is the Decimal Point?
We shall now consider a sequence of numbers, in which each member is divided by ten to get the next one. This will be a short finite sequence.
The number 12345 is represented in packed decimal as 12345C.
The number 1234.5 is represented in packed decimal as 12345C.
The number 123.45 is represented in packed decimal as 12345C.
The number 12.345 is represented in packed decimal as 12345C.
The number 1.2345 is represented in packed decimal as 12345C.
The number .12345 is represented in packed decimal as 12345C.
Note that the format does not store the decimal point or any indication of the location of the decimal point. That is up to the code.
Put another way, what is the sum of 012C and 4C?
Is this really 12 + 4, with sum 16, represented as 016C?
Is this really 12.0 + 0.4, with sum 12.4, represented as 124C?
Fixed Point: Where is the Decimal Point? (Page 2)
Consider the following addition problem: 1.23 + 10.11405.
The answer is obviously 11.34405.
But the Packed Decimal format does not store the decimal point, so
1.23 is stored as 123C and 10.11405 is stored as 1011405C.
Using our previous logic, we line up the numbers 0000123C 1011405C
The result is denoted 10111528C, which might be 10.111528.It cannot represent the correct answer.
This is a feature of fixed point arithmetic, in which all numbers are stored with the decimal point in the same place. In this system, the code must guarantee that 1.23 is stored as 0123000C.
The result is now 0123000C 1011405C, stored as 1134405C.
The code must interpret and output this as the value 11.34405.
45`v#
"+R[y
ߵsh_SKh"vCJ$aJ$hTh"v5CJ0aJ0h"v5C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v v v v v v v v v
v v v
v v v7N!J$'*$(($% "
/)
25H#cy!"+/13579;=?BEHKNQR[ahpxy&Q")*139@AJLRZ[\ M l `
[*s>
P
d
4l1{.Nb~K2<c'Du.akmoqsuwyz.[]_acefiloruvw; e !!H!!""#L#p####J$s$%A%%%%9&w&&0'i''(7(z(()j).*n**222t2t2t2t2t2t2t2222222d```220222222h
h
h
h
22222E2Y222222222222222222E2E2E2t222t2t2t2t2t2t2t2t2t2t2t2t2t2t2t2t2t2t2Y2t2t2t2t2t2t2t2t2t2t2t2t2t2t2t2t2t22222222222WW222222222222222M2M2Y2t2t22t2?2?2t2c2c2z2Y2t2?2?2?2?2?2?2t2?2?2?2t2t2t2?22z2t2?2t5H#cy!"+/13579;=?BEHKNQR[ahpxy&Q")*139@AJLRZ[\ M l `
6[*s>
P
d
4l1{.Nb~K2<c'Du.akmoqsuwyz.[]_acefiloruvw; e !!H!!""#L#p####J$s$%A%%%%9&w&&0'i'''(7(z(()j).*n**00000000000000000 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 00@0@0@0@0@0@0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0@0@0@0@0@0@0@0@0@0@0@0@0@0@0@0000000000000000000000000000000000000000000000000000000@0@0@0@0@0@0@0@0@0@0@0@0@0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 00@0@0@0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 @0 0@000@0@0@0@0@0@0@0@0@0000000000000000000000000000000000
)9X22#)*,68:!"xQ
)@ZPa$y$z$$$e%r%%+02 !"$%&'(+./0123457928@0(
B
S ?*
/ZUV((*::::::xy[Es}23<alv##r$(*l$'***Kjd4&+*w2@z6i=*E&Y7^_b"vC1\;Y'`u?[7q#Vj x+g!"+/13579;=?BEHKNQR[ahpxy")*139@AJLRZ[akmoqsuwyz[]_acefiloruv*@\T*P@UnknownGz Times New Roman5Symbol3&z Arial?5 z Courier New"qhѦᆻu^O$M^O$M#24**3HP ?@z62Fixed Point Arithmetic"Packed Decimal as on the IBM S/370Edward L. Bosworth, Ph.D.Edward L. Bosworth, Ph.D.Oh+'0(< `l
Fixed Point Arithmetic$Packed Decimal as on the IBM S/370Edward L. Bosworth, Ph.D.Normal.dotEdward L. Bosworth, Ph.D.19Microsoft Office Word@>X@">~@`}@">~^O$՜.+,0@
px
Wayne C. Summers, Ph.D. Columbus State UniversityM*'Fixed Point ArithmeticTitle
!"#$%&'()*+,./0123456789:;=>?@ABCEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abdefghijlmnopqruRoot Entry F`iX~wData
<1TableD=WordDocument7vSummaryInformation(cDocumentSummaryInformation8kCompObjq
FMicrosoft Office Word Document
MSWordDocWord.Document.89q