]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/libm-ieee754/s_remquof.c
1997-03-22 04:53 Ulrich Drepper <drepper@cygnus.com>
[thirdparty/glibc.git] / sysdeps / libm-ieee754 / s_remquof.c
CommitLineData
bc9f6000
UD
1/* Compute remainder and a congruent to the quotient.
2 Copyright (C) 1997 Free Software Foundation, Inc.
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
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
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
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include <math.h>
22
23#include "math_private.h"
24
25
26static const float zero = 0.0;
27
28
29float
30__remquof (float x, float y, int *quo)
31{
32 int32_t hx,hp;
33 u_int32_t sx;
34 int cquo;
35
36 GET_FLOAT_WORD (hx, x);
37 GET_FLOAT_WORD (hp, p);
38 sx = hx & 0x80000000;
39 qs = (sx ^ (hp & 0x80000000)) >> 31;
40 hp &= 0x7fffffff;
41 hx &= 0x7fffffff;
42
43 /* Purge off exception values. */
44 if (hp == 0)
45 return (x * p) / (x * p); /* p = 0 */
46 if ((hx >= 0x7f800000) /* x not finite */
47 || (hp > 0x7f800000)) /* p is NaN */
48 return (x * p) / (x * p);
49
50 if (hp <= 0x7dffffff)
51 {
52 x = __ieee754_fmodf (x, 8 * p); /* now x < 8p */
53
54 if (fabs (x) >= 4 * fabs (p))
55 cquo += 4;
56 }
57
58 if ((hx - hp) == 0)
59 {
60 *quo = qs ? -1 : 1;
61 return zero * x;
62 }
63
64 x = fabsf (x);
65 p = fabsf (p);
66 cquo = 0;
67
68 if (x >= 2 * p)
69 {
70 x -= 4 * p;
71 cquo += 2;
72 }
73 if (x >= p)
74 {
75 x -= 2 * p;
76 ++cquo;
77 }
78
79 if (hp < 0x01000000)
80 {
81 if (x + x > p)
82 {
83 x -= p;
84 if (x + x >= p)
85 x -= p;
86 }
87 }
88 else
89 {
90 float p_half = 0.5 * p;
91 if(x > p_half)
92 {
93 x -= p;
94 if (x >= p_half)
95 x -= p;
96 }
97 }
98
99 *quo = qs ? -cquo : cquo;
100
101 GET_FLOAT_WORD (hx, x);
102 SET_FLOAT_WORD (x, hx ^ sx);
103 return x;
104}
105weak_alias (__remquof, remquof)