Microsoft KB Archive/214621

= FIX: JIT Loses Shift Results in Arrays =

Article ID: 214621

Article Last Modified on 7/23/2001

-

APPLIES TO


 * Microsoft Java Virtual Machine

-



This article was previously published under Q214621



SYMPTOMS
When you perform a left-shift, a right-shift, or execute code that would result in a right-shift (such as multiplication on an element of a byte or short array where the result is immediately assigned back to the array), you receive incorrect results. This problem only occurs in the Just In Time (JIT) compiler found in versions 3100 - 3158 of the Microsoft virtual machine.



CAUSE
This is caused by a bug in the JIT where certain shift operations are incorrectly optimized. As a result, the shift operation is not executed, effectively "commenting out" the instructions.



RESOLUTION
Create a temporary variable where the results of the operation can be stored and then assigned back to the array element.

Another possible workaround is to turn off the JIT, although this can cause some Java programs to run at a slower speed.

For additional information about the JIT, please see the following article in the Microsoft Knowledge Base:

154580 Description of the Just-In- Time Compiler



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft virtual machine builds 3165 and greater.



Steps to Reproduce Behavior
The following code reproduces the JIT bug: public class jitbug {  static byte b[] = new byte[1];

public static void main(String args[]) {     System.out.println("Reproduces JIT Bug");

System.out.println ; System.out.println("Multiple by 8:"); b[0]=8; b[0] = (byte) (b[0] * 8); System.out.println("b[0] = b[0] * 8"); System.out.println("Results = " + b[0]);

System.out.println ; System.out.println("Left-shift by 3:"); b[0]=8; b[0] = (byte) (b[0] << 3); System.out.println("b[0] = b[0] << 3"); System.out.println("Results = " + b[0]);

System.out.println ; System.out.println("Right-shift by 3:"); b[0]=8; b[0] = (byte) (b[0] >> 3); System.out.println("b[0] = b[0] >> 3"); System.out.println("Results = " + b[0]);

// Remove the comment block from around the following line to //  see the workaround. /*     System.out.println ; System.out.println("Workaround to JIT Bug");

System.out.println ; System.out.println("Multiple by 8:"); b[0]=8; byte answer = (byte) (b[0] * 8); b[0]=answer; System.out.println("answer = (byte) (b[0] * 8)"); System.out.println("b[0] = answer"); System.out.println("Results = " + b[0]);

System.out.println ; System.out.println("Left-shift by 3:"); b[0]=8; byte answer2 = (byte) (b[0] << 3); b[0]=answer2; System.out.println("answer2 = (byte) (b[0] << 3)"); System.out.println("b[0] = answer2"); System.out.println("Results = " + b[0]);

System.out.println ; System.out.println("Right-shift by 3:"); b[0]=8; byte answer3 = (byte) (b[0] >> 3); b[0]=answer3; System.out.println("answer3 = (byte) (b[0] >> 3)"); System.out.println("b[0] = answer3"); System.out.println("Results = " + b[0]); } }

NOTE: This issue applies to any algorithm that performs bit operations in the manner shown above, which is common in image manipulation.

