Back to index

glibc  2.9
s_roundl.c
Go to the documentation of this file.
00001 /* Round long double to integer away from zero.
00002    Copyright (C) 1997, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <math.h>
00022 
00023 #include "math_private.h"
00024 
00025 
00026 static const long double huge = 1.0e4930L;
00027 
00028 
00029 long double
00030 __roundl (long double x)
00031 {
00032   int32_t j0;
00033   u_int32_t se, i1, i0;
00034 
00035   GET_LDOUBLE_WORDS (se, i0, i1, x);
00036   j0 = (se & 0x7fff) - 0x3fff;
00037   if (j0 < 31)
00038     {
00039       if (j0 < 0)
00040        {
00041          if (huge + x > 0.0)
00042            {
00043              se &= 0x8000;
00044              i0 = i1 = 0;
00045              if (j0 == -1)
00046               {
00047                 se |= 0x3fff;
00048                 i0 = 0x80000000;
00049               }
00050            }
00051        }
00052       else
00053        {
00054          u_int32_t i = 0x7fffffff >> j0;
00055          if (((i0 & i) | i1) == 0)
00056            /* X is integral.  */
00057            return x;
00058          if (huge + x > 0.0)
00059            {
00060              /* Raise inexact if x != 0.  */
00061              u_int32_t j = i0 + (0x40000000 >> j0);
00062              if (j < i0)
00063               se += 1;
00064              i0 = (j & ~i) | 0x80000000;
00065              i1 = 0;
00066            }
00067        }
00068     }
00069   else if (j0 > 62)
00070     {
00071       if (j0 == 0x4000)
00072        /* Inf or NaN.  */
00073        return x + x;
00074       else
00075        return x;
00076     }
00077   else
00078     {
00079       u_int32_t i = 0xffffffff >> (j0 - 31);
00080       if ((i1 & i) == 0)
00081        /* X is integral.  */
00082        return x;
00083 
00084       if (huge + x > 0.0)
00085        {
00086          /* Raise inexact if x != 0.  */
00087          u_int32_t j = i1 + (1 << (62 - j0));
00088          if (j < i1)
00089            {
00090              u_int32_t k = i0 + 1;
00091              if (k < i0)
00092               {
00093                 se += 1;
00094                 k |= 0x80000000;
00095               }
00096              i0 = k;
00097            }
00098          i1 = j;
00099        }
00100       i1 &= ~i;
00101     }
00102 
00103   SET_LDOUBLE_WORDS (x, se, i0, i1);
00104   return x;
00105 }
00106 weak_alias (__roundl, roundl)