1997-03-23 02:54:07 +01:00
|
|
|
/* Compute remainder and a congruent to the quotient.
|
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with the GNU C Library; see the file COPYING.LIB. If not,
|
|
|
|
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
Boston, MA 02111-1307, USA. */
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "math_private.h"
|
|
|
|
|
|
|
|
|
|
|
|
static const float zero = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
float
|
|
|
|
__remquof (float x, float y, int *quo)
|
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
int32_t hx,hy;
|
1997-03-23 02:54:07 +01:00
|
|
|
u_int32_t sx;
|
1997-03-25 03:25:35 +01:00
|
|
|
int cquo, qs;
|
1997-03-23 02:54:07 +01:00
|
|
|
|
|
|
|
GET_FLOAT_WORD (hx, x);
|
1997-03-25 03:25:35 +01:00
|
|
|
GET_FLOAT_WORD (hy, y);
|
1997-03-23 02:54:07 +01:00
|
|
|
sx = hx & 0x80000000;
|
1997-03-25 03:25:35 +01:00
|
|
|
qs = sx ^ (hy & 0x80000000);
|
|
|
|
hy &= 0x7fffffff;
|
1997-03-23 02:54:07 +01:00
|
|
|
hx &= 0x7fffffff;
|
|
|
|
|
|
|
|
/* Purge off exception values. */
|
1997-03-25 03:25:35 +01:00
|
|
|
if (hy == 0)
|
|
|
|
return (x * y) / (x * y); /* y = 0 */
|
1997-03-23 02:54:07 +01:00
|
|
|
if ((hx >= 0x7f800000) /* x not finite */
|
1997-03-25 03:25:35 +01:00
|
|
|
|| (hy > 0x7f800000)) /* y is NaN */
|
|
|
|
return (x * y) / (x * y);
|
1997-03-23 02:54:07 +01:00
|
|
|
|
1997-03-25 03:25:35 +01:00
|
|
|
if (hy <= 0x7dffffff)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
x = __ieee754_fmodf (x, 8 * y); /* now x < 8y */
|
1997-03-23 02:54:07 +01:00
|
|
|
|
1997-03-25 03:25:35 +01:00
|
|
|
if (fabs (x) >= 4 * fabs (y))
|
1997-03-23 02:54:07 +01:00
|
|
|
cquo += 4;
|
|
|
|
}
|
|
|
|
|
1997-03-25 03:25:35 +01:00
|
|
|
if ((hx - hy) == 0)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
|
|
|
*quo = qs ? -1 : 1;
|
|
|
|
return zero * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = fabsf (x);
|
1997-03-25 03:25:35 +01:00
|
|
|
y = fabsf (y);
|
1997-03-23 02:54:07 +01:00
|
|
|
cquo = 0;
|
|
|
|
|
1997-03-25 03:25:35 +01:00
|
|
|
if (x >= 2 * y)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
x -= 4 * y;
|
1997-03-23 02:54:07 +01:00
|
|
|
cquo += 2;
|
|
|
|
}
|
1997-03-25 03:25:35 +01:00
|
|
|
if (x >= y)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
x -= 2 * y;
|
1997-03-23 02:54:07 +01:00
|
|
|
++cquo;
|
|
|
|
}
|
|
|
|
|
1997-03-25 03:25:35 +01:00
|
|
|
if (hy < 0x01000000)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
if (x + x > y)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
x -= y;
|
|
|
|
if (x + x >= y)
|
|
|
|
x -= y;
|
1997-03-23 02:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
float y_half = 0.5 * y;
|
|
|
|
if(x > y_half)
|
1997-03-23 02:54:07 +01:00
|
|
|
{
|
1997-03-25 03:25:35 +01:00
|
|
|
x -= y;
|
|
|
|
if (x >= y_half)
|
|
|
|
x -= y;
|
1997-03-23 02:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*quo = qs ? -cquo : cquo;
|
|
|
|
|
|
|
|
GET_FLOAT_WORD (hx, x);
|
|
|
|
SET_FLOAT_WORD (x, hx ^ sx);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
weak_alias (__remquof, remquof)
|