Bug 28615 - Cannot optimize out -0.0 in array initializers
Summary: Cannot optimize out -0.0 in array initializers
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.1 M5   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-12-18 09:58 EST by Olivier Thomann CLA
Modified: 2003-02-07 11:44 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2002-12-18 09:58:23 EST
Using 1217, the following code doesn't return the right result when executed.
public class A {
    public static void main(String[] args) {
        double[] tab = new double[] {-0.0};
        System.out.print(tab[0]);
    }
}
When executed, we return: 0.0 instead of -0.0.

javac and jikes returns the right result.

The bug comes from the optimization for array initializers that optimizes out
the default values. We should check if the double value is equals to -0.0 or
different from 0.0 before we optimize out the constant. For now we simply check
different from 0.
Here is a suggested fix for this:

In org.eclipse.jdt.internal.compiler.ast.ArrayInitializer::generateCode:

	case T_int :
	case T_short :
	case T_byte :
	case T_char :
	case T_float :
	case T_long :
	case T_double :
		if (expr.constant.doubleValue() != 0) {
			codeStream.dup();
			codeStream.generateInlinedValue(i);
			expr.generateCode(currentScope, codeStream, true);
			codeStream.arrayAtPut(elementsTypeID, false);
		}
	break;

with:
	case T_int :
	case T_short :
	case T_byte :
	case T_char :
	case T_long :
		if (expr.constant.longValue() != 0) {
			codeStream.dup();
			codeStream.generateInlinedValue(i);
			expr.generateCode(currentScope, codeStream, true);
			codeStream.arrayAtPut(elementsTypeID, false);
		}
         break;
	case T_float :
	case T_double :
		double constantValue = expr.constant.doubleValue();
		if (constantValue == -0.0 || constantValue != 0) {
			codeStream.dup();
			codeStream.generateInlinedValue(i);
			expr.generateCode(currentScope, codeStream, true);
			codeStream.arrayAtPut(elementsTypeID, false);
		}
	break;

This would fix the same bug for float constants.

With this patch, we have the right result.
Comment 1 Philipe Mulet CLA 2002-12-19 06:15:23 EST
Ok by me.
Comment 2 Olivier Thomann CLA 2003-01-06 08:51:01 EST
Fixed and released in 2.1 stream.
Regression tests added.
Comment 3 David Audel CLA 2003-02-07 11:44:23 EST
Verified.