Redundant code in Integer protocol arithmetic?

Stephan Rudlof sr at evolgo.de
Thu Dec 23 13:24:21 UTC 1999


Andres Valloud wrote:
> 
> Hi.
> 
> > In Integer protocol arithmetic (similar in '-'):
> > Does anybody knows the sense of the message #normalize here? I think it
> > is senseless for integer arithmetics, because #digitAdd: results in
> > correct normalized numbers. The only problem could be that someone calls
> > #+ with a non normalized number as argument, but I think...
> 
> I think #normalize is there because of LargeIntegers... they can get
> very big with leading zeroes, so #normalize takes care of that... yet,
> the lines you specifically referred to point to that if in a+b, a sign
> == b sign, then it normalizes... I think it should be the other way
> around. When adding, normalize becomes useless because you add bits, you
> can't reduce the amount of bits of a sum by adding numbers of the same
> sign. But if a sign ~= b sign, then you may end up with less bits, hence
> you really need #normalize.
> 
> Perhaps it should be the other way around?

At the end of #digitSubtract: which is called by #+ and #- there is
already a call of #normalize. This could be the reason why it isn't
called after a call of #digitSubtract.

I _don't_ believe now - in opposite to my previous mail regarding this
topic - that #normalize after #digitAdd: is necessary because of the
given example:

byteSwapped: w 
        "Return the given integer with its bytes in the reverse order."
        ^ ((w bitShift: -24)
                bitAnd: 255)
                + ((w bitShift: -8)
                                bitAnd: 65280) + ((w bitShift: 8)
                        bitAnd: 16711680) + ((w bitShift: 24)
                        bitAnd: 4278190080)

.. Methods #bitShift: and #bitAnd: normalize already, so it isn't
necessary later.

So I suppose that indeed #normalize makes no sense after #digitAdd:. But
I'm careful, because here we are dealing with a core part of
ST/Squeak...


I would like to hear other opinions, too!

Greetings,

Stephan
-- 
Stephan Rudlof (sr at evolgo.de)
   "Genius doesn't work on an assembly line basis.
    You can't simply say, 'Today I will be brilliant.'"
    -- Kirk, "The Ultimate Computer", stardate 4731.3





More information about the Squeak-dev mailing list