[Python-Dev] Mini-Pep: Simplifying the Integral ABC

Cesare Di Mauro cesare at pronto.it
Fri Jun 6 22:18:44 CEST 2008


In data 06 giugno 2008 alle ore 20:40:01, Alex Martelli <aleaxit at gmail.com> ha scritto:

> On Fri, Jun 6, 2008 at 11:01 AM, Guido van Rossum <guido at python.org> wrote:
>> On Thu, Jun 5, 2008 at 8:45 PM, Raymond Hettinger <python at rcn.com> wrote:
>>> Does anyone actually need an int lookalike with binary methods but
>>> cannot just inherit from int?
>>
>> Does anyone actually need an int lookalike with operations like +, -
>> etc. but cannot just inherit from int? If the answer is yes, is there
>> a compelling reason why they wouldn't want to support binary methods
>> as well?
>
> Yes, there's a use case for implementing long integers as arrays of
> decimal digits -- addition is roughly as efficient as for binary
> integers (x86 chips still have instructions to help with that), and
> emitting as decimal digits is MUCH more efficient of course -- so if
> I/O in decimal form is the most common operation, with a little
> arithmetic (particularly sums), you could gain performance; binary
> operations, however, would be as inefficient as decimal form
> conversion is for ordinary binary ints, and not needed for the typical
> applications that would use these "decimal coded integers"
> (accounting), so why not save the implementer of such an extension
> from having to write that unneeded and slow extra code?
>
>
> Alex

I don't know if you are talking about BCD numbers, but they are quite inefficient and slow in x86 architecture.
There are instructions only to add and subtract packed BCD numbers which uses just two decimal digits (packed in two nibbles into a single byte).
For unpacked BCDs, there are instructions to add, subtract, multiply and divide numbers, but which uses only one digit at the time.

So using packed BCDs to store 8 decimal digits in 32 bits, for example, requires 4 instructions to make addictions or subractions, plus the required shift & mask instructions to put every couple digits into the AL register to execute BCD operations.
Unpacked BCDs need double of them.

Also, these instructions still use microcode to execute on modern processors, slowing down the execution pipeline (most of the simpler instructions do not require microcode, and execute "directly").

Last but not least, on x86-64 architecture BCD instructions were completely removed from the ISA; opcodes are assigned to new instructions. Obviously, binary operations can be performed twice faster thanks to the 64 bit registers and ALUs.

The only practical advantage on using BCD numbers is the conversion-to-string operation, which can be done faster than binary numbers.

Binary addition, subtraction, multiplication and division are greatly faster than BCD ones, and should be the preferred way to do integer math.

Cesare


More information about the Python-Dev mailing list