git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] stevedlawrence closed pull request #72: Fix modulus problem with ULong


stevedlawrence closed pull request #72: Fix modulus problem with ULong
URL: https://github.com/apache/incubator-daffodil/pull/72
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/daffodil-lib/src/main/scala/passera/unsigned/ULong.scala b/daffodil-lib/src/main/scala/passera/unsigned/ULong.scala
index f3f364162..9dbb6836d 100644
--- a/daffodil-lib/src/main/scala/passera/unsigned/ULong.scala
+++ b/daffodil-lib/src/main/scala/passera/unsigned/ULong.scala
@@ -146,14 +146,19 @@ case class ULong(override val longValue: Long) extends AnyVal with Unsigned[ULon
     val n = rep
     val d = x.rep
 
-    val t = d >> 63
-    val n1 = n & ~t
-    val a = n1 >>> 1
-    val b = a / d
-    val q0 = b << 1
-    val r = n - q0 * d
-    // val q = q0 + (if (ULong(r) >= ULong(d)) 1 else 0)
-    ULong(r.toLong)
+    if (d < 0) {
+      if (this < x)
+        ULong(n)
+      else
+        ULong(n - d)
+    } else {
+      val q = ((n >>> 1) / d) << 1
+      val r = n - q * d
+      if (ULong(r) >= x)
+        ULong(r - d)
+      else
+        ULong(r)
+    }
   }
 
   override def toString =
diff --git a/daffodil-lib/src/test/scala/passera/test/TestULong.scala b/daffodil-lib/src/test/scala/passera/test/TestULong.scala
index 9f9b04eec..7ed58b44a 100644
--- a/daffodil-lib/src/test/scala/passera/test/TestULong.scala
+++ b/daffodil-lib/src/test/scala/passera/test/TestULong.scala
@@ -38,5 +38,28 @@ class TestULong {
     assertEquals(ULong.MaxValueAsBigInt, mm1.toBigInt)
     assertEquals(BigInt(Long.MinValue).abs.toString, mm1.toString)
   }
+
+  // DAFFODIL-1714
+  @Test def testULongModulus1 {
+    for (i <- 0 to 16 ) {
+      val numerator = ULong(i)
+      val denominator = ULong(8)
+      val remainder = numerator % denominator
+      assertEquals(ULong(i%8), remainder)
+    }
+  }
+
+  @Test def testULongModulus2 {
+    val mm1 = ULong(-1L)
+    val remainder = mm1 % ULong(65536)
+    assertEquals(ULong(0x0000FFFF), remainder)
+  }
+  
+  @Test def testULongModulus3 {
+    val mm1 = ULong(-1L)
+    val mm2 = ULong(-2L)
+    val remainder = mm1 % mm2
+    assertEquals(ULong(1), remainder)
+  }
   
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@xxxxxxxxxxxxxxxx


With regards,
Apache Git Services