JDK-6915548 : Integer division returns incorrect results in some cases
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 6u14
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2010-01-09
  • Updated: 2010-04-03
  • Resolved: 2010-01-11
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01, mixed mode)

FULL OS VERSION :
Linux greycloud 2.6.31-17-generic #54-Ubuntu SMP Thu Dec 10 17:01:44 UTC 2009 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
See the test code below. Basically, sometimes simple integer multiplication and division fails.

THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Yes

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the JavaTest class given below (note that I've pasted two classes in there -- there's also a helper class).  It only fails some of the time. Sometimes you have to run it several times (as many as 10) before you see the error.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected:
64 * 64 / 8 == 512

Actual:
64 * 64 / 8 == 519
ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error message, just wrong results.

REPRODUCIBILITY :
This bug can be reproduced often.

---------- BEGIN SOURCE ----------
package com.keith;

import java.io.IOException;

public class JavaTest
{
	public static void main(String[] args) throws Exception
	{
		JavaTest macTest = new JavaTest();
		for(long i = 0; i < 100000000l; i++)
		{
			macTest.doStuff();
		}
	}
	
	private void doStuff() throws IOException {
		JavaTestHelper dbbm = new JavaTestHelper();
		dbbm.doMultiplicationAndDivision();
	}

}


package com.keith;

import java.io.IOException;

public class JavaTestHelper
{
	public int dimension = 4096;

	public void doMultiplicationAndDivision() throws IOException
	{
		int length = (this.dimension + 63) / 64;
		int arraySize = length * 64 / 8;
		if(arraySize %2 == 1)
		{
			throw new RuntimeException("bad division!!");
		}
	}

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
If I do the division before the multiplication, I don't get the problem.  That is, if I do:
  int arraySize = length * (64 / 8);
instead of:
  int arraySize = length * 64 / 8;
then the problem doesn't show up.
But I can't realistically track down and test every place in our application where we do multiplication and division. I basically have zero confidence in our application because of this.

Release Regression From : 6u13-rev
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Release Regression From : 6u13-rev
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION It is duplicate of 6910484. The expression (((x) / 64) * 64) / 8 is converted to ((x) & -64) / 8 which is the case in 6910484.
11-01-2010