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,2004,2006 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 #include <math_ldbl_opt.h>
00026 
00027 
00028 static const long double zero = 0.0;
00029 
00030 
00031 long double
00032 __remquol (long double x, long double y, int *quo)
00033 {
00034   int64_t hx,hy;
00035   u_int64_t sx,lx,ly,qs;
00036   int cquo;
00037 
00038   GET_LDOUBLE_WORDS64 (hx, lx, x);
00039   GET_LDOUBLE_WORDS64 (hy, ly, y);
00040   sx = hx & 0x8000000000000000ULL;
00041   qs = sx ^ (hy & 0x8000000000000000ULL);
00042   hy &= 0x7fffffffffffffffLL;
00043   hx &= 0x7fffffffffffffffLL;
00044 
00045   /* Purge off exception values.  */
00046   if ((hy | (ly & 0x7fffffffffffffff)) == 0)
00047     return (x * y) / (x * y);                    /* y = 0 */
00048   if ((hx >= 0x7ff0000000000000LL)        /* x not finite */
00049       || ((hy >= 0x7ff0000000000000LL)           /* y is NaN */
00050          && (((hy - 0x7ff0000000000000LL) | ly) != 0)))
00051     return (x * y) / (x * y);
00052 
00053   if (hy <= 0x7fbfffffffffffffLL)
00054     x = __ieee754_fmodl (x, 8 * y);              /* now x < 8y */
00055 
00056   if (((hx - hy) | (lx - ly)) == 0)
00057     {
00058       *quo = qs ? -1 : 1;
00059       return zero * x;
00060     }
00061 
00062   x  = fabsl (x);
00063   y  = fabsl (y);
00064   cquo = 0;
00065 
00066   if (x >= 4 * y)
00067     {
00068       x -= 4 * y;
00069       cquo += 4;
00070     }
00071   if (x >= 2 * y)
00072     {
00073       x -= 2 * y;
00074       cquo += 2;
00075     }
00076 
00077   if (hy < 0x0020000000000000LL)
00078     {
00079       if (x + x > y)
00080        {
00081          x -= y;
00082          ++cquo;
00083          if (x + x >= y)
00084            {
00085              x -= y;
00086              ++cquo;
00087            }
00088        }
00089     }
00090   else
00091     {
00092       long double y_half = 0.5L * y;
00093       if (x > y_half)
00094        {
00095          x -= y;
00096          ++cquo;
00097          if (x >= y_half)
00098            {
00099              x -= y;
00100              ++cquo;
00101            }
00102        }
00103     }
00104 
00105   *quo = qs ? -cquo : cquo;
00106 
00107   if (sx)
00108     x = -x;
00109   return x;
00110 }
00111 long_double_symbol (libm, __remquol, remquol);