]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/ldbl-96/s_remquol.c
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-96 / s_remquol.c
CommitLineData
bc9f6000 1/* Compute remainder and a congruent to the quotient.
568035b7 2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
bc9f6000
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
bc9f6000
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
bc9f6000 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
bc9f6000
UD
19
20#include <math.h>
21
1ed0291c 22#include <math_private.h>
bc9f6000
UD
23
24
63551311 25static const long double zero = 0.0;
bc9f6000
UD
26
27
28long double
63551311 29__remquol (long double x, long double p, int *quo)
bc9f6000
UD
30{
31 int32_t ex,ep,hx,hp;
32 u_int32_t sx,lx,lp;
63551311 33 int cquo,qs;
bc9f6000
UD
34
35 GET_LDOUBLE_WORDS (ex, hx, lx, x);
36 GET_LDOUBLE_WORDS (ep, hp, lp, p);
37 sx = ex & 0x8000;
38 qs = (sx ^ (ep & 0x8000)) >> 15;
39 ep &= 0x7fff;
40 ex &= 0x7fff;
41
42 /* Purge off exception values. */
43 if ((ep | hp | lp) == 0)
44 return (x * p) / (x * p); /* p = 0 */
45 if ((ex == 0x7fff) /* x not finite */
46 || ((ep == 0x7fff) /* p is NaN */
47 && ((hp | lp) != 0)))
48 return (x * p) / (x * p);
49
50 if (ep <= 0x7ffb)
63551311 51 x = __ieee754_fmodl (x, 8 * p); /* now x < 8p */
bc9f6000
UD
52
53 if (((ex - ep) | (hx - hp) | (lx - lp)) == 0)
54 {
55 *quo = qs ? -1 : 1;
56 return zero * x;
57 }
58
59 x = fabsl (x);
60 p = fabsl (p);
61 cquo = 0;
62
63551311 63 if (x >= 4 * p)
bc9f6000
UD
64 {
65 x -= 4 * p;
63551311
UD
66 cquo += 4;
67 }
68 if (x >= 2 * p)
69 {
70 x -= 2 * p;
bc9f6000
UD
71 cquo += 2;
72 }
bc9f6000
UD
73
74 if (ep < 0x0002)
75 {
76 if (x + x > p)
77 {
78 x -= p;
d705269e 79 ++cquo;
bc9f6000 80 if (x + x >= p)
63551311
UD
81 {
82 x -= p;
83 ++cquo;
84 }
bc9f6000
UD
85 }
86 }
87 else
88 {
89 long double p_half = 0.5 * p;
63551311 90 if (x > p_half)
bc9f6000
UD
91 {
92 x -= p;
d705269e 93 ++cquo;
bc9f6000 94 if (x >= p_half)
63551311
UD
95 {
96 x -= p;
97 ++cquo;
98 }
bc9f6000
UD
99 }
100 }
101
102 *quo = qs ? -cquo : cquo;
103
63551311
UD
104 if (sx)
105 x = -x;
bc9f6000
UD
106 return x;
107}
108weak_alias (__remquol, remquol)