about summary refs log tree commit diff
path: root/REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c
diff options
context:
space:
mode:
authorZack Weinberg <zackw@panix.com>2017-06-08 15:39:03 -0400
committerZack Weinberg <zackw@panix.com>2017-06-08 15:39:03 -0400
commit5046dbb4a7eba5eccfd258f92f4735c9ffc8d069 (patch)
tree4470480d904b65cf14ca524f96f79eca818c3eaf /REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c
parent199fc19d3aaaf57944ef036e15904febe877fc93 (diff)
downloadglibc-zack/build-layout-experiment.tar.gz
glibc-zack/build-layout-experiment.tar.xz
glibc-zack/build-layout-experiment.zip
Prepare for radical source tree reorganization. zack/build-layout-experiment
All top-level files and directories are moved into a temporary storage
directory, REORG.TODO, except for files that will certainly still
exist in their current form at top level when we're done (COPYING,
COPYING.LIB, LICENSES, NEWS, README), all old ChangeLog files (which
are moved to the new directory OldChangeLogs, instead), and the
generated file INSTALL (which is just deleted; in the new order, there
will be no generated files checked into version control).
Diffstat (limited to 'REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c')
-rw-r--r--REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c b/REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c
new file mode 100644
index 0000000000..fbf38bf717
--- /dev/null
+++ b/REORG.TODO/sysdeps/ieee754/ldbl-128ibm/s_llrintl.c
@@ -0,0 +1,140 @@
+/* Round to long long int long double floating-point values.
+   IBM extended format long double version.
+   Copyright (C) 2006-2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <math.h>
+#include <fenv.h>
+#include <math_ldbl_opt.h>
+#include <float.h>
+#include <ieee754.h>
+
+
+long long
+__llrintl (long double x)
+{
+  double xh, xl;
+  long long res, hi, lo;
+  int save_round;
+
+  ldbl_unpack (x, &xh, &xl);
+
+  /* Limit the range of values handled by the conversion to long long.
+     We do this because we aren't sure whether that conversion properly
+     raises FE_INVALID.  */
+  if (__builtin_expect
+      ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
+#if !defined (FE_INVALID)
+      || 1
+#endif
+    )
+    {
+      save_round = fegetround ();
+
+      if (__glibc_unlikely ((xh == -(double) (-__LONG_LONG_MAX__ - 1))))
+	{
+	  /* When XH is 9223372036854775808.0, converting to long long will
+	     overflow, resulting in an invalid operation.  However, XL might
+	     be negative and of sufficient magnitude that the overall long
+	     double is in fact in range.  Avoid raising an exception.  In any
+	     case we need to convert this value specially, because
+	     the converted value is not exactly represented as a double
+	     thus subtracting HI from XH suffers rounding error.  */
+	  hi = __LONG_LONG_MAX__;
+	  xh = 1.0;
+	}
+      else
+	{
+	  hi = (long long) xh;
+	  xh -= hi;
+	}
+      ldbl_canonicalize (&xh, &xl);
+
+      lo = (long long) xh;
+
+      /* Peg at max/min values, assuming that the above conversions do so.
+         Strictly speaking, we can return anything for values that overflow,
+         but this is more useful.  */
+      res = hi + lo;
+
+      /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
+      if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
+	goto overflow;
+
+      xh -= lo;
+      ldbl_canonicalize (&xh, &xl);
+
+      hi = res;
+      switch (save_round)
+	{
+	case FE_TONEAREST:
+	  if (fabs (xh) < 0.5
+	      || (fabs (xh) == 0.5
+		  && ((xh > 0.0 && xl < 0.0)
+		      || (xh < 0.0 && xl > 0.0)
+		      || (xl == 0.0 && (res & 1) == 0))))
+	    return res;
+
+	  if (xh < 0.0)
+	    res -= 1;
+	  else
+	    res += 1;
+	  break;
+
+	case FE_TOWARDZERO:
+	  if (res > 0 && (xh < 0.0 || (xh == 0.0 && xl < 0.0)))
+	    res -= 1;
+	  else if (res < 0 && (xh > 0.0 || (xh == 0.0 && xl > 0.0)))
+	    res += 1;
+	  return res;
+	  break;
+
+	case FE_UPWARD:
+	  if (xh > 0.0 || (xh == 0.0 && xl > 0.0))
+	    res += 1;
+	  break;
+
+	case FE_DOWNWARD:
+	  if (xh < 0.0 || (xh == 0.0 && xl < 0.0))
+	    res -= 1;
+	  break;
+	}
+
+      if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
+	goto overflow;
+
+      return res;
+    }
+  else
+    {
+      if (xh > 0.0)
+	hi = __LONG_LONG_MAX__;
+      else if (xh < 0.0)
+	hi = -__LONG_LONG_MAX__ - 1;
+      else
+	/* Nan */
+	hi = 0;
+    }
+
+overflow:
+#ifdef FE_INVALID
+  feraiseexcept (FE_INVALID);
+#endif
+  return hi;
+}
+
+long_double_symbol (libm, __llrintl, llrintl);