Back to index

glibc  2.9
s_remquol.c
Go to the documentation of this file.
00001 /* Compute remainder and a congruent to the quotient.
00002    Copyright (C) 1997, 1999, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997 and
00005                 Jakub Jelinek <jj@ultra.linux.cz>, 1999.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <math.h>
00023 
00024 #include "math_private.h"
00025 
00026 
00027 static const long double zero = 0.0;
00028 
00029 
00030 long double
00031 __remquol (long double x, long double y, int *quo)
00032 {
00033   int64_t hx,hy;
00034   u_int64_t sx,lx,ly,qs;
00035   int cquo;
00036 
00037   GET_LDOUBLE_WORDS64 (hx, lx, x);
00038   GET_LDOUBLE_WORDS64 (hy, ly, y);
00039   sx = hx & 0x8000000000000000ULL;
00040   qs = sx ^ (hy & 0x8000000000000000ULL);
00041   hy &= 0x7fffffffffffffffLL;
00042   hx &= 0x7fffffffffffffffLL;
00043 
00044   /* Purge off exception values.  */
00045   if ((hy | ly) == 0)
00046     return (x * y) / (x * y);                    /* y = 0 */
00047   if ((hx >= 0x7fff000000000000LL)        /* x not finite */
00048       || ((hy >= 0x7fff000000000000LL)           /* y is NaN */
00049          && (((hy - 0x7fff000000000000LL) | ly) != 0)))
00050     return (x * y) / (x * y);
00051 
00052   if (hy <= 0x7ffbffffffffffffLL)
00053     x = __ieee754_fmodl (x, 8 * y);              /* now x < 8y */
00054       
00055   if (((hx - hy) | (lx - ly)) == 0)
00056     {
00057       *quo = qs ? -1 : 1;
00058       return zero * x;
00059     }
00060 
00061   x  = fabsl (x);
00062   y  = fabsl (y);
00063   cquo = 0;
00064 
00065   if (x >= 4 * y)
00066     {
00067       x -= 4 * y;
00068       cquo += 4;
00069     }
00070   if (x >= 2 * y)
00071     {
00072       x -= 2 * y;
00073       cquo += 2;
00074     }
00075 
00076   if (hy < 0x0002000000000000LL)
00077     {
00078       if (x + x > y)
00079        {
00080          x -= y;
00081          ++cquo;
00082          if (x + x >= y)
00083            {
00084              x -= y;
00085              ++cquo;
00086            }
00087        }
00088     }
00089   else
00090     {
00091       long double y_half = 0.5L * y;
00092       if (x > y_half)
00093        {
00094          x -= y;
00095          ++cquo;
00096          if (x >= y_half)
00097            {
00098              x -= y;
00099              ++cquo;
00100            }
00101        }
00102     }
00103 
00104   *quo = qs ? -cquo : cquo;
00105 
00106   if (sx)
00107     x = -x;
00108   return x;
00109 }
00110 weak_alias (__remquol, remquol)