[Vm-dev] how the h**l do I generate a signed shift?

David T. Lewis lewis at mail.msen.com
Mon Jun 30 23:27:56 UTC 2014


On Mon, Jun 30, 2014 at 12:24:12PM -0700, Eliot Miranda wrote:
>  
> Hi All,
> 
>     I recently eliminated the optimization in Slang that replaces a
> division by a power of two with a shift, because the code cast the argument
> to signed, and hence broke unsigned division.  That's what used to be
> controlled by the UseRightShiftForDivide class var of CCodeGenerator.

<slightly OT>

I'm just catching up with the conversation, and I see that the issue is now resolved,
but it's worth noting that John Maloney's method comment from August 1998 is worthy
of our appreciation :)

CCodeGenerator class>>initialize
	"CCodeGenerator initialize"

	UseRightShiftForDivide := true.
		"If UseRightShiftForDivide is true, the translator will generate a right-shift when it encounters a division by a constant that is a small power of two. For example, 'x / 8' will generate '((int) x >> 3)'. The coercion to int is done to make it clear that the C compiler should generate a signed shift."
		"Note: The Kernighan and Ritchie 2nd Edition C manual, p. 49, leaves the semantics of right-shifting a negative number open to the discretion of the compiler implementor. However, it strongly suggests that most compilers should generate an arithmetic right shift (i.e., shifting in the sign bit), which is the same as dividing by a power of two. If your compiler does not generate or simulate an arithmetic shift, then make this class variable false and re-translate."

</slightly OT>

Dave



More information about the Vm-dev mailing list