Support float & double negation in the optimizing compiler.

- Add support for the neg-float and neg-double Dex
  instructions in the optimizing compiler.
- Generate x86, x86-64 and ARM (but not ARM64) code for
  float and double HNeg nodes.
- Add related tests to test/415-optimizing-arith-neg.

Change-Id: I29739a86e13dbe6f64e191641d01637c867cba6c
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index e43841a..a4af900 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -896,6 +896,16 @@
       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 dd595d9..1e9fad0 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -1189,7 +1189,8 @@
 
     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 @@
       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 b2d9187..42e6072 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1116,7 +1116,10 @@
 
     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 @@
   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 @@
       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 2bd76c1..4401b9a 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1102,7 +1102,10 @@
 
     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 @@
   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 b21b998..e2850ca 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 @@
     //   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));
+    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));
 
-    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)
+    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)
 
-    expectEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L));  // (2^63 - 1)
-    expectEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L));  // -(2^63 - 1)
+    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 @@
   static long $opt$NegLong(long a){
     return -a;
   }
+
+  static float $opt$NegFloat(float a){
+    return -a;
+  }
+
+  static double $opt$NegDouble(double a){
+    return -a;
+  }
 }