[MLton-commit] r6291

Vesa Karvonen vesak at mlton.org
Fri Dec 28 11:10:13 PST 2007


Optimized min, max and compareReal.

The idea is to "delay" treatment of NaNs, which usually are very rare, by
relying on the property that ordering predicates on reals return false
when at least one of the reals being compared is NaN.

The optimization of min and max should not cause any regressions compared
to the previous implementation.  However, the new version of compareReal
no longer makes a call to C, which means that on the x86, MLton may keep
reals in registers, and this may cause some corner cases to be evaluated
differently, due to the use of 80-bit reals by the x86 FPU.

Note that all of min, max and compareReal could be implemented using just
a single floating point comparison on the x86.

----------------------------------------------------------------------

U   mlton/trunk/basis-library/real/real.sml

----------------------------------------------------------------------

Modified: mlton/trunk/basis-library/real/real.sml
===================================================================
--- mlton/trunk/basis-library/real/real.sml	2007-12-23 16:45:44 UTC (rev 6290)
+++ mlton/trunk/basis-library/real/real.sml	2007-12-28 19:10:12 UTC (rev 6291)
@@ -141,18 +141,16 @@
              | _ => R.== (x, y)
 
       fun min (x, y) =
-         if isNan x
-            then y
-         else if isNan y
-                 then x
-              else if x < y then x else y
+         if x <= y then x
+         else if x > y then y
+         else if isNan y then x
+         else y
 
       fun max (x, y) =
-         if isNan x
-            then y
-         else if isNan y
-                 then x
-              else if x > y then x else y
+         if x >= y then x
+         else if x < y then y
+         else if isNan y then x
+         else y
 
       fun sign (x: real): int =
          case class x of
@@ -171,13 +169,10 @@
          datatype z = datatype IEEEReal.real_order
       in
          fun compareReal (x, y) =
-            case (class x, class y) of
-               (NAN, _) => UNORDERED
-             | (_, NAN) => UNORDERED
-             | (ZERO, ZERO) => EQUAL
-             | _ => if x < y then LESS
-                    else if x > y then GREATER
-                         else EQUAL
+            if x < y then LESS
+            else if x > y then GREATER
+            else if x == y then EQUAL
+            else UNORDERED
       end
 
       local




More information about the MLton-commit mailing list