pTAL Guidelines for TAL Programmers

Coding Guidelines
pTAL Guidelines for TAL Programmers527256-002
2-9
Arithmetic Operations
Arithmetic Operations
Topics:
Signed and Unsigned 16-Bit Addition, Subtraction, and Negation on page 2-9
Overflow Traps on page 2-10
Signed and Unsigned 16-Bit Addition, Subtraction, and Negation
Guideline: Use unsigned operators if you expect modulo-2
16
results.
On TNS architecture, the numeric result—that is, the bit pattern of the result—that TAL
16-bit signed and unsigned addition and subtraction produce is the same, regardless of
whether a carry or overflow occurs, and regardless of whether a trap occurs for an
overflow. Signed addition or subtraction might detect overflow or carry. Unsigned
addition or subtraction might detect carry; therefore, the only difference between
signed and unsigned addition and subtraction is whether an overflow or carry occurs,
and for overflow, whether a trap occurs.
On native architecture, the result of multiplication, division, and negation operations
that overflow is undefined and might be different from one processor to another.
On native architecture, the behavior of TAL 16-bit signed and unsigned operators is the
same as that just described for TNS architecture except that if an overflow occurs and
traps are enabled, the numeric result of the operation is undefined.
When you run a pTAL program on native architecture, the numeric result of a 16-bit
operation that overflows is undefined, regardless of whether traps are enabled or
disabled. You must use unsigned operators to obtain modulo-2
16
results.
If your code depends on modulo arithmetic for signed operations, you must change the
signed operators to unsigned or otherwise redesign your algorithms.
The operators of primary concern are 16-bit addition, subtraction, and negation,
although 16-bit signed multiplication might be treated as a modulo-2
16
operation in
specific cases.