summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Roland Levillain <rpl@google.com> 2014-11-06 17:33:06 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2014-11-06 17:33:06 +0000
commitadc3e13db74c193302ac9d56735924cad32083cc (patch)
tree738db270af4fbf33a6dfa1a025edc1b1c640848a
parent9b6c62b82e3d40d70d541920d5f7f81ad517bc01 (diff)
parent3dbcb38a8b2237b0da290ae35dc0caab3cb47b3d (diff)
Merge "Support float & double negation in the optimizing compiler."
-rw-r--r--compiler/optimizing/builder.cc10
-rw-r--r--compiler/optimizing/code_generator_arm.cc11
-rw-r--r--compiler/optimizing/code_generator_x86.cc20
-rw-r--r--compiler/optimizing/code_generator_x86_64.cc39
-rw-r--r--test/415-optimizing-arith-neg/src/Main.java135
5 files changed, 183 insertions, 32 deletions
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index 1820a6a57c..a31ea29ae1 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -904,6 +904,16 @@ bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32
break;
}
+ case Instruction::NEG_FLOAT: {
+ Unop_12x<HNeg>(instruction, Primitive::kPrimFloat);
+ break;
+ }
+
+ case Instruction::NEG_DOUBLE: {
+ Unop_12x<HNeg>(instruction, Primitive::kPrimDouble);
+ break;
+ }
+
case Instruction::NOT_INT: {
Unop_12x<HNot>(instruction, Primitive::kPrimInt);
break;
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index adedf78605..fef7f0e80f 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -1189,7 +1189,8 @@ void LocationsBuilderARM::VisitNeg(HNeg* neg) {
case Primitive::kPrimFloat:
case Primitive::kPrimDouble:
- LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
+ locations->SetInAt(0, Location::RequiresFpuRegister());
+ locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
break;
default:
@@ -1229,8 +1230,14 @@ void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
break;
case Primitive::kPrimFloat:
+ DCHECK(in.IsFpuRegister());
+ __ vnegs(out.As<SRegister>(), in.As<SRegister>());
+ break;
+
case Primitive::kPrimDouble:
- LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
+ DCHECK(in.IsFpuRegisterPair());
+ __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
+ FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
break;
default:
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 2a8eded0cc..127ddbeab1 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1116,7 +1116,10 @@ void LocationsBuilderX86::VisitNeg(HNeg* neg) {
case Primitive::kPrimFloat:
case Primitive::kPrimDouble:
- LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
+ locations->SetInAt(0, Location::RequiresFpuRegister());
+ // Output overlaps as we need a fresh (zero-initialized)
+ // register to perform subtraction from zero.
+ locations->SetOut(Location::RequiresFpuRegister());
break;
default:
@@ -1131,11 +1134,13 @@ void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
switch (neg->GetResultType()) {
case Primitive::kPrimInt:
DCHECK(in.IsRegister());
+ DCHECK(in.Equals(out));
__ negl(out.As<Register>());
break;
case Primitive::kPrimLong:
DCHECK(in.IsRegisterPair());
+ DCHECK(in.Equals(out));
__ negl(out.AsRegisterPairLow<Register>());
// Negation is similar to subtraction from zero. The least
// significant byte triggers a borrow when it is different from
@@ -1147,8 +1152,19 @@ void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
break;
case Primitive::kPrimFloat:
+ DCHECK(!in.Equals(out));
+ // out = 0
+ __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
+ // out = out - in
+ __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
+ break;
+
case Primitive::kPrimDouble:
- LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
+ DCHECK(!in.Equals(out));
+ // out = 0
+ __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
+ // out = out - in
+ __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
break;
default:
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 41f3c59987..8c0842cb89 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1102,7 +1102,10 @@ void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
case Primitive::kPrimFloat:
case Primitive::kPrimDouble:
- LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
+ locations->SetInAt(0, Location::RequiresFpuRegister());
+ // Output overlaps as we need a fresh (zero-initialized)
+ // register to perform subtraction from zero.
+ locations->SetOut(Location::RequiresFpuRegister());
break;
default:
@@ -1117,17 +1120,49 @@ void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
switch (neg->GetResultType()) {
case Primitive::kPrimInt:
DCHECK(in.IsRegister());
+ DCHECK(in.Equals(out));
__ negl(out.As<CpuRegister>());
break;
case Primitive::kPrimLong:
DCHECK(in.IsRegister());
+ DCHECK(in.Equals(out));
__ negq(out.As<CpuRegister>());
break;
case Primitive::kPrimFloat:
+ DCHECK(in.IsFpuRegister());
+ DCHECK(out.IsFpuRegister());
+ DCHECK(!in.Equals(out));
+ // TODO: Instead of computing negation as a subtraction from
+ // zero, implement it with an exclusive or with value 0x80000000
+ // (mask for bit 31, representing the sign of a single-precision
+ // floating-point number), fetched from a constant pool:
+ //
+ // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
+
+ // out = 0
+ __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
+ // out = out - in
+ __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
+ break;
+
case Primitive::kPrimDouble:
- LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
+ DCHECK(in.IsFpuRegister());
+ DCHECK(out.IsFpuRegister());
+ DCHECK(!in.Equals(out));
+ // TODO: Instead of computing negation as a subtraction from
+ // zero, implement it with an exclusive or with value
+ // 0x8000000000000000 (mask for bit 63, representing the sign of
+ // a double-precision floating-point number), fetched from a
+ // constant pool:
+ //
+ // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
+
+ // out = 0
+ __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
+ // out = out - in
+ __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
break;
default:
diff --git a/test/415-optimizing-arith-neg/src/Main.java b/test/415-optimizing-arith-neg/src/Main.java
index b21b998235..e2850ca760 100644
--- a/test/415-optimizing-arith-neg/src/Main.java
+++ b/test/415-optimizing-arith-neg/src/Main.java
@@ -18,34 +18,61 @@
// it does compile the method.
public class Main {
- public static void expectEquals(int expected, int result) {
+ public static void assertEquals(int expected, int result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
}
}
- public static void expectEquals(long expected, long result) {
+ public static void assertEquals(long expected, long result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
}
}
+ public static void assertEquals(float expected, float result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
+ public static void assertEquals(double expected, double result) {
+ if (expected != result) {
+ throw new Error("Expected: " + expected + ", found: " + result);
+ }
+ }
+
+ public static void assertIsNaN(float result) {
+ if (!Float.isNaN(result)) {
+ throw new Error("Expected NaN: " + result);
+ }
+ }
+
+ public static void assertIsNaN(double result) {
+ if (!Double.isNaN(result)) {
+ throw new Error("Expected NaN: " + result);
+ }
+ }
+
public static void main(String[] args) {
negInt();
$opt$InplaceNegOneInt(1);
negLong();
$opt$InplaceNegOneLong(1L);
+
+ negFloat();
+ negDouble();
}
private static void negInt() {
- expectEquals(-1, $opt$NegInt(1));
- expectEquals(1, $opt$NegInt(-1));
- expectEquals(0, $opt$NegInt(0));
- expectEquals(51, $opt$NegInt(-51));
- expectEquals(-51, $opt$NegInt(51));
- expectEquals(2147483647, $opt$NegInt(-2147483647)); // (2^31 - 1)
- expectEquals(-2147483647, $opt$NegInt(2147483647)); // -(2^31 - 1)
+ assertEquals(-1, $opt$NegInt(1));
+ assertEquals(1, $opt$NegInt(-1));
+ assertEquals(0, $opt$NegInt(0));
+ assertEquals(51, $opt$NegInt(-51));
+ assertEquals(-51, $opt$NegInt(51));
+ assertEquals(2147483647, $opt$NegInt(-2147483647)); // (2^31 - 1)
+ assertEquals(-2147483647, $opt$NegInt(2147483647)); // -(2^31 - 1)
// From the Java 7 SE Edition specification:
// http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4
//
@@ -56,36 +83,84 @@ public class Main {
// int or long results in that same maximum negative number.
// Overflow occurs in this case, but no exception is thrown.
// For all integer values x, -x equals (~x)+1.''
- expectEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31)
+ assertEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31)
}
private static void $opt$InplaceNegOneInt(int a) {
a = -a;
- expectEquals(-1, a);
+ assertEquals(-1, a);
}
private static void negLong() {
- expectEquals(-1L, $opt$NegLong(1L));
- expectEquals(1L, $opt$NegLong(-1L));
- expectEquals(0L, $opt$NegLong(0L));
- expectEquals(51L, $opt$NegLong(-51L));
- expectEquals(-51L, $opt$NegLong(51L));
-
- expectEquals(2147483647L, $opt$NegLong(-2147483647L)); // (2^31 - 1)
- expectEquals(-2147483647L, $opt$NegLong(2147483647L)); // -(2^31 - 1)
- expectEquals(2147483648L, $opt$NegLong(-2147483648L)); // 2^31
- expectEquals(-2147483648L, $opt$NegLong(2147483648L)); // -(2^31)
-
- expectEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // (2^63 - 1)
- expectEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // -(2^63 - 1)
+ assertEquals(-1L, $opt$NegLong(1L));
+ assertEquals(1L, $opt$NegLong(-1L));
+ assertEquals(0L, $opt$NegLong(0L));
+ assertEquals(51L, $opt$NegLong(-51L));
+ assertEquals(-51L, $opt$NegLong(51L));
+
+ assertEquals(2147483647L, $opt$NegLong(-2147483647L)); // (2^31 - 1)
+ assertEquals(-2147483647L, $opt$NegLong(2147483647L)); // -(2^31 - 1)
+ assertEquals(2147483648L, $opt$NegLong(-2147483648L)); // 2^31
+ assertEquals(-2147483648L, $opt$NegLong(2147483648L)); // -(2^31)
+
+ assertEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // (2^63 - 1)
+ assertEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // -(2^63 - 1)
// See remark regarding the negation of the maximum negative
// (long) value in negInt().
- expectEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63)
+ assertEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63)
}
private static void $opt$InplaceNegOneLong(long a) {
a = -a;
- expectEquals(-1L, a);
+ assertEquals(-1L, a);
+ }
+
+ private static void negFloat() {
+ assertEquals(-1F, $opt$NegFloat(1F));
+ assertEquals(1F, $opt$NegFloat(-1F));
+ assertEquals(0F, $opt$NegFloat(0F));
+ assertEquals(51F, $opt$NegFloat(-51F));
+ assertEquals(-51F, $opt$NegFloat(51F));
+
+ assertEquals(-0.1F, $opt$NegFloat(0.1F));
+ assertEquals(0.1F, $opt$NegFloat(-0.1F));
+ assertEquals(343597.38362F, $opt$NegFloat(-343597.38362F));
+ assertEquals(-343597.38362F, $opt$NegFloat(343597.38362F));
+
+ assertEquals(-Float.MIN_NORMAL, $opt$NegFloat(Float.MIN_NORMAL));
+ assertEquals(Float.MIN_NORMAL, $opt$NegFloat(-Float.MIN_NORMAL));
+ assertEquals(-Float.MIN_VALUE, $opt$NegFloat(Float.MIN_VALUE));
+ assertEquals(Float.MIN_VALUE, $opt$NegFloat(-Float.MIN_VALUE));
+ assertEquals(-Float.MAX_VALUE, $opt$NegFloat(Float.MAX_VALUE));
+ assertEquals(Float.MAX_VALUE, $opt$NegFloat(-Float.MAX_VALUE));
+
+ assertEquals(Float.NEGATIVE_INFINITY, $opt$NegFloat(Float.POSITIVE_INFINITY));
+ assertEquals(Float.POSITIVE_INFINITY, $opt$NegFloat(Float.NEGATIVE_INFINITY));
+ assertIsNaN($opt$NegFloat(Float.NaN));
+ }
+
+ private static void negDouble() {
+ assertEquals(-1D, $opt$NegDouble(1D));
+ assertEquals(1D, $opt$NegDouble(-1D));
+ assertEquals(0D, $opt$NegDouble(0D));
+ assertEquals(51D, $opt$NegDouble(-51D));
+ assertEquals(-51D, $opt$NegDouble(51D));
+
+ assertEquals(-0.1D, $opt$NegDouble(0.1D));
+ assertEquals(0.1D, $opt$NegDouble(-0.1D));
+ assertEquals(343597.38362D, $opt$NegDouble(-343597.38362D));
+ assertEquals(-343597.38362D, $opt$NegDouble(343597.38362D));
+
+ assertEquals(-Double.MIN_NORMAL, $opt$NegDouble(Double.MIN_NORMAL));
+ assertEquals(Double.MIN_NORMAL, $opt$NegDouble(-Double.MIN_NORMAL));
+ assertEquals(-Double.MIN_VALUE, $opt$NegDouble(Double.MIN_VALUE));
+ assertEquals(Double.MIN_VALUE, $opt$NegDouble(-Double.MIN_VALUE));
+ assertEquals(-Double.MAX_VALUE, $opt$NegDouble(Double.MAX_VALUE));
+ assertEquals(Double.MAX_VALUE, $opt$NegDouble(-Double.MAX_VALUE));
+
+ assertEquals(Double.NEGATIVE_INFINITY, $opt$NegDouble(Double.POSITIVE_INFINITY));
+ assertEquals(Double.POSITIVE_INFINITY, $opt$NegDouble(Double.NEGATIVE_INFINITY));
+ assertIsNaN($opt$NegDouble(Double.NaN));
}
static int $opt$NegInt(int a){
@@ -95,4 +170,12 @@ public class Main {
static long $opt$NegLong(long a){
return -a;
}
+
+ static float $opt$NegFloat(float a){
+ return -a;
+ }
+
+ static double $opt$NegDouble(double a){
+ return -a;
+ }
}