All,

I found the following interesting.  Thought you might also.

Charles


This message is forwarded from the newsgroup "comp.sys.ibm.as400.misc".

----------- Begin Forwarded Message -----------

Subject: Re: Parameter passing, signed numerics and packed values.
From: Dave McKenzie <davemck@xxxxxxxxxxxxx>
Newsgroups: comp.sys.ibm.as400.misc

Saml wrote:

> This tends to happen when a field is defined in a file as signed.  
When it
> comes into the program RPG automatically converts it to packed since 
this
> is the efficent form that is needed for arithmetic.

I don't mean to take exception to this, but I just wanted to mention 
some
interesting (to me anyway) trivia about packed arithmetic on AS/400.

(This comes under the heading "news for nerds," or rather "trivia only a
nerd could love," so anyone who isn't familiar with the term "Slashdot", 
and
is therefore not a nerd, will probably want to skip the rest of this 
post.)

Actually, packed arithmetic was the efficient form on CISC AS/400's, 
which
had machine language instructions for it, but it's not as efficient on
PowerPC's--they do all their arithmetic in binary.

Here's an example.  In the following RPG code:

  C     Z-ADD 0          PACKNUM     11 2
  C     ADD   PACKNUM    PACKT0T     11 2

The first statement generates 6 machine instructions (24 bytes).
The second statement generates 42 machine instructions (168 bytes).

However, I'm not advocating anyone avoid packed decimal.  The 
inefficiencies
are trivial compared to, say, bad database design.  I'd guess the dollar
value of all the manhours spent worrying about AS/400 arithmetic 
efficiency
in the history of the world vastly exceeds the dollar value of the CPU
cycles saved.

Now the trivia.  Since PowerPC has no decimal arithmetic instructions,
compiler writers had to resort to what I think is a very clever kludge. 
Because decimal digits (0-9) are a subset of hexadecimal digits (0-F),
adding decimal digits with a hexadecimal (binary) adder gives the same
result.  Decimal 2 + 3 = 5, the same as x'2' + x'3' = x'5'.  The problem
comes when there's a carry: decimal causes a carry when the result is
greater than 9, while hexadecimal causes a carry when the result is 
greater
than 15 (x'F').  Since 15 - 9 = 6, here's the kludge: add 6 to every 
digit
before doing hexadecimal addition.  Then the carries will occur for the
same digits as decimal addition.  Of course, you have to compensate for 
the
fact that all the digits that didn't carry are 6 too big!

You've heard that AS/400 PowerPC chips have extra machine instructions 
to
handle AS/400 stuff, for example tagged pointers.  They also have an
instruction whose mnemonic is DSIXES.  You'll see it if you use SST to 
dump
a program containing decimal arithmetic.  Here's how it works:

If you add two 15-digit (8-byte) packed numbers (where the lo-order sign 
has
been replaced with 0):

  1234567890123450
+ 0000009009009090
+ 6666666666666666  added by software to generate decimal carries
  ----------------
= 789ABD6EFF792B46
- 6666660666660606  DSIXES loads 6's for digits that DIDN't have carries
  ----------------
= 1234576899132540

The additions and subtraction are hexadecimal.  The DSIXES instruction
generates an 8-byte value that has 6 in each digit that DIDN't generate 
a
carry in the previous addition, and 0 elsewhere.  Then that's subtracted
from the result to compensate for adding the 6's initially.

I find it amusing that this kludge takes place in the AS/400 every time 
it
executes a packed decimal arithmetic statement.  (I know--warped sense 
of
humor.)

--Dave

Saml wrote:

> If you then change the
> program and add an external data structure based on the file, RPG is
> forced
> to revert to signed, because that's what the data structure says.  
Then
> you have problems with the CALL.
> 
> To satisify the called program parameter requirements you either have 
to
> add a move from a packed field to a signed field, or use a CALLP and
> prototype
> the field as being pass by value.  The compiler will automatically 
include
> the code to convert from signed to packed for you.
> 
> Sam
> 
> "William A Pack" <tonyp@xxxxxxxxx> wrote in message
> news:o_ednZVg8KHHc1TdRVn-hQ@xxxxxxxxxxxxxx
>> I have a program with a packed numeric value defined as 11,2 in 
length.
>> When I call another program, also expecting an 11,2 packed numeric 
value,
>> the program works fine.  When the value is listed in a data 
structure,
>> the compiler apparently changes the value from packed numeric to 
signed
> numeric.
>> Is this default behaiviour?  is there a workaround without having to 
dim
> the
>> values on the data structure as packed 11,2?
>>
>> TIA,
>> Tony
>>
>> tony p at roman dot net.
>>
>>


------------------------------------------------------------

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.