5de29b
# commit da13146da10360436941e843834c90a9aef5fd7a
5de29b
# Author: Alan Modra <amodra@gmail.com>
5de29b
# Date:   Sat Aug 17 18:30:23 2013 +0930
5de29b
# 
5de29b
#     PowerPC floating point little-endian [10 of 15]
5de29b
#     http://sourceware.org/ml/libc-alpha/2013-07/msg00201.html
5de29b
#     
5de29b
#     These two functions oddly test x+1>0 when a double x is >= 0.0, and
5de29b
#     similarly when x is negative.  I don't see the point of that since the
5de29b
#     test should always be true.  I also don't see any need to convert x+1
5de29b
#     to integer rather than simply using xr+1.  Note that the standard
5de29b
#     allows these functions to return any value when the input is outside
5de29b
#     the range of long long, but it's not too hard to prevent xr+1
5de29b
#     overflowing so that's what I've done.
5de29b
#     
5de29b
#     (With rounding mode FE_UPWARD, x+1 can be a lot more than what you
5de29b
#     might naively expect, but perhaps that situation was covered by the
5de29b
#     x - xrf < 1.0 test.)
5de29b
#     
5de29b
#         * sysdeps/powerpc/fpu/s_llround.c (__llround): Rewrite.
5de29b
#         * sysdeps/powerpc/fpu/s_llroundf.c (__llroundf): Rewrite.
5de29b
# 
12745e
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llround.c glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llround.c
12745e
--- glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llround.c	2014-05-27 22:38:55.000000000 -0500
12745e
+++ glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llround.c	2014-05-27 22:38:58.000000000 -0500
5de29b
@@ -19,29 +19,28 @@
5de29b
 #include <math.h>
5de29b
 #include <math_ldbl_opt.h>
5de29b
 
5de29b
-/* I think that what this routine is supposed to do is round a value
5de29b
-   to the nearest integer, with values exactly on the boundary rounded
5de29b
-   away from zero.  */
5de29b
-/* This routine relies on (long long)x, when x is out of range of a long long,
5de29b
-   clipping to MAX_LLONG or MIN_LLONG.  */
5de29b
+/* Round to the nearest integer, with values exactly on a 0.5 boundary
5de29b
+   rounded away from zero, regardless of the current rounding mode.
5de29b
+   If (long long)x, when x is out of range of a long long, clips at
5de29b
+   LLONG_MAX or LLONG_MIN, then this implementation also clips.  */
5de29b
 
5de29b
 long long int
5de29b
 __llround (double x)
5de29b
 {
5de29b
-  double xrf;
5de29b
-  long long int xr;
5de29b
-  xr = (long long int) x;
5de29b
-  xrf = (double) xr;
5de29b
+  long long xr = (long long) x;
5de29b
+  double xrf = (double) xr;
5de29b
+
5de29b
   if (x >= 0.0)
5de29b
-    if (x - xrf >= 0.5 && x - xrf < 1.0 && x+1 > 0)
5de29b
-      return x+1;
5de29b
-    else
5de29b
-      return x;
5de29b
+    {
5de29b
+      if (x - xrf >= 0.5)
5de29b
+	xr += (long long) ((unsigned long long) xr + 1) > 0;
5de29b
+    }
5de29b
   else
5de29b
-    if (xrf - x >= 0.5 && xrf - x < 1.0 && x-1 < 0)
5de29b
-      return x-1;
5de29b
-    else
5de29b
-      return x;
5de29b
+    {
5de29b
+      if (xrf - x >= 0.5)
5de29b
+	xr -= (long long) ((unsigned long long) xr - 1) < 0;
5de29b
+    }
5de29b
+  return xr;
5de29b
 }
5de29b
 weak_alias (__llround, llround)
5de29b
 #ifdef NO_LONG_DOUBLE
12745e
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llroundf.c glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llroundf.c
12745e
--- glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llroundf.c	2014-05-27 22:38:55.000000000 -0500
12745e
+++ glibc-2.17-c758a686/sysdeps/powerpc/fpu/s_llroundf.c	2014-05-27 22:38:58.000000000 -0500
5de29b
@@ -18,28 +18,27 @@
5de29b
 
5de29b
 #include <math.h>
5de29b
 
5de29b
-/* I think that what this routine is supposed to do is round a value
5de29b
-   to the nearest integer, with values exactly on the boundary rounded
5de29b
-   away from zero.  */
5de29b
-/* This routine relies on (long long)x, when x is out of range of a long long,
5de29b
-   clipping to MAX_LLONG or MIN_LLONG.  */
5de29b
+/* Round to the nearest integer, with values exactly on a 0.5 boundary
5de29b
+   rounded away from zero, regardless of the current rounding mode.
5de29b
+   If (long long)x, when x is out of range of a long long, clips at
5de29b
+   LLONG_MAX or LLONG_MIN, then this implementation also clips.  */
5de29b
 
5de29b
 long long int
5de29b
 __llroundf (float x)
5de29b
 {
5de29b
-  float xrf;
5de29b
-  long long int xr;
5de29b
-  xr = (long long int) x;
5de29b
-  xrf = (float) xr;
5de29b
+  long long xr = (long long) x;
5de29b
+  float xrf = (float) xr;
5de29b
+
5de29b
   if (x >= 0.0)
5de29b
-    if (x - xrf >= 0.5 && x - xrf < 1.0 && x+1 > 0)
5de29b
-      return x+1;
5de29b
-    else
5de29b
-      return x;
5de29b
+    {
5de29b
+      if (x - xrf >= 0.5)
5de29b
+	xr += (long long) ((unsigned long long) xr + 1) > 0;
5de29b
+    }
5de29b
   else
5de29b
-    if (xrf - x >= 0.5 && xrf - x < 1.0 && x-1 < 0)
5de29b
-      return x-1;
5de29b
-    else
5de29b
-      return x;
5de29b
+    {
5de29b
+      if (xrf - x >= 0.5)
5de29b
+	xr -= (long long) ((unsigned long long) xr - 1) < 0;
5de29b
+    }
5de29b
+  return xr;
5de29b
 }
5de29b
 weak_alias (__llroundf, llroundf)