#include <sys/cdefs.h>
#if defined(LIBM_SCCS) && !defined(lint)
__RCSID("$NetBSD: e_remainder.c,v 1.11 2002/05/26 22:01:52 wiz Exp $");
#endif
#include "math.h"
#include "math_private.h"
static const double zero = 0.0;
double
__ieee754_remainder(double x, double p)
{
int32_t hx,hp;
u_int32_t sx,lx,lp;
double p_half;
EXTRACT_WORDS(hx,lx,x);
EXTRACT_WORDS(hp,lp,p);
sx = hx&0x80000000;
hp &= 0x7fffffff;
hx &= 0x7fffffff;
if((hp|lp)==0) return (x*p)/(x*p);
if((hx>=0x7ff00000)||
((hp>=0x7ff00000)&&
(((hp-0x7ff00000)|lp)!=0)))
return (x*p)/(x*p);
if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p);
if (((hx-hp)|(lx-lp))==0) return zero*x;
x = fabs(x);
p = fabs(p);
if (hp<0x00200000) {
if(x+x>p) {
x-=p;
if(x+x>=p) x -= p;
}
} else {
p_half = 0.5*p;
if(x>p_half) {
x-=p;
if(x>=p_half) x -= p;
}
}
GET_HIGH_WORD(hx,x);
SET_HIGH_WORD(x,hx^sx);
return x;
}