[Vm-dev] usage of bitShift: in VMMaker & plugins

Igor Stasenko siguctua at gmail.com
Thu Jan 7 22:24:11 UTC 2010


I don't know how others, but i always avoid uncertain input , like
chance that value which would be shifted can be negative.
IMO, shifting makes sense only if you operating with positive integer values.

2010/1/7 Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com>:
>
> Folks,
> browsing generated code I see ugly things like this one:
>
>        byte = jpegDecodeValueFromsize(dcTable, dcTableSize);
>        if (byte < 0) {
>                return interpreterProxy->primitiveFail();
>        }
>        if (byte != 0) {
>                bits = getBits(byte);
>                /* begin scaleAndSignExtend:inFieldWidth: */
>                if (bits < ((((byte - 1) < 0) ? ((usqInt) 1 >> -(byte - 1)) :
> ((usqInt) 1 << (byte - 1))))) {
>                        byte = (bits - (((byte < 0) ? ((usqInt) 1 >> -byte) : ((usqInt) 1
> << byte)))) + 1;
>                        goto l1;
>                } else {
>                        byte = bits;
>                        goto l1;
>                }
>        l1:     /* end scaleAndSignExtend:inFieldWidth: */;
>        }
>
> You would never write such code by hand because it costs a useless
> runtime test ()?:
> And you know very well that byte > 0 when engaging the bitShift:
> operation, so it should be:
>
>        byte = jpegDecodeValueFromsize(dcTable, dcTableSize);
>        if (byte < 0) {
>                return interpreterProxy->primitiveFail();
>        }
>        if (byte != 0) {
>                bits = getBits(byte);
>                /* begin scaleAndSignExtend:inFieldWidth: */
>                if (bits < ((usqInt) 1 << (byte - 1))) {
>                        byte = (bits - ((usqInt) 1 << byte)) + 1;
>                        goto l1;
>                } else {
>                        byte = bits;
>                        goto l1;
>                }
>        l1:     /* end scaleAndSignExtend:inFieldWidth: */;
>        }
>
> Of course, the generator cannot guess that easily when feeded with a
> bitShift: and a non literal argument...
> That's why we should use explicit << and >> directly in slang when we
> know the argument will be positive.
>
> I did that for example in http://bugs.squeak.org/view.php?id=7109 but
> see it could be generalized...
> ... Of course with due care!
> Just my 2¢ tip of the day
>
> Nicolas
>



-- 
Best regards,
Igor Stasenko AKA sig.


More information about the Vm-dev mailing list