]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sysdeps/ieee754/ldbl-96/s_llrintl.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-96 / s_llrintl.c
index e927a8af4015ce30fa04d49312b049558ff73525..e52746d08c2fbf76cff926c892f3e8cd682400e2 100644 (file)
@@ -1,6 +1,6 @@
 /* Round argument to nearest integral value according to current rounding
    direction.
-   Copyright (C) 1997, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1997-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
 
    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, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
+#include <fenv.h>
+#include <limits.h>
 #include <math.h>
 
-#include "math_private.h"
+#include <math_private.h>
+#include <libm-alias-ldouble.h>
 
 static const long double two63[2] =
 {
@@ -34,9 +36,9 @@ long long int
 __llrintl (long double x)
 {
   int32_t se,j0;
-  u_int32_t i0, i1;
+  uint32_t i0, i1;
   long long int result;
-  volatile long double w;
+  long double w;
   long double t;
   int sx;
 
@@ -47,18 +49,31 @@ __llrintl (long double x)
 
   if (j0 < (int32_t) (8 * sizeof (long long int)) - 1)
     {
-      if (j0 < -1)
-       return 0;
-      else if (j0 >= 63)
+      if (j0 >= 63)
        result = (((long long int) i0 << 32) | i1) << (j0 - 63);
       else
        {
-         w = two63[sx] + x;
-         t = w - two63[sx];
+#if defined FE_INVALID || defined FE_INEXACT
+         /* X < LLONG_MAX + 1 implied by J0 < 63.  */
+         if (x > (long double) LLONG_MAX)
+           {
+             /* In the event of overflow we must raise the "invalid"
+                exception, but not "inexact".  */
+             t = __nearbyintl (x);
+             feraiseexcept (t == LLONG_MAX ? FE_INEXACT : FE_INVALID);
+           }
+         else
+#endif
+           {
+             w = two63[sx] + x;
+             t = w - two63[sx];
+           }
          GET_LDOUBLE_WORDS (se, i0, i1, t);
          j0 = (se & 0x7fff) - 0x3fff;
 
-         if (j0 <= 31)
+         if (j0 < 0)
+           result = 0;
+         else if (j0 <= 31)
            result = i0 >> (31 - j0);
          else
            result = ((long long int) i0 << (j0 - 31)) | (i1 >> (63 - j0));
@@ -74,4 +89,4 @@ __llrintl (long double x)
   return sx ? -result : result;
 }
 
-weak_alias (__llrintl, llrintl)
+libm_alias_ldouble (__llrint, llrint)