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