11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* e_remainderf.c -- float version of e_remainder.c.
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ====================================================
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Developed at SunPro, a Sun Microsystems, Inc. business.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Permission to use, copy, modify, and distribute this
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * software is freely granted, provided that this notice
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * is preserved.
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ====================================================
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <sys/cdefs.h>
17a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$");
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "math.h"
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "math_private.h"
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic const float zero = 0.0;
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfloat
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project__ieee754_remainderf(float x, float p)
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	int32_t hx,hp;
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	u_int32_t sx;
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	float p_half;
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	GET_FLOAT_WORD(hx,x);
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	GET_FLOAT_WORD(hp,p);
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	sx = hx&0x80000000;
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hp &= 0x7fffffff;
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	hx &= 0x7fffffff;
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* purge off exception values */
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if(hp==0) return (x*p)/(x*p);	 	/* p = 0 */
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if((hx>=0x7f800000)||			/* x not finite */
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	  ((hp>0x7f800000)))			/* p is NaN */
42a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	    return ((long double)x*p)/((long double)x*p);
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (hp<=0x7effffff) x = __ieee754_fmodf(x,p+p);	/* now x < 2p */
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if ((hx-hp)==0) return zero*x;
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	x  = fabsf(x);
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	p  = fabsf(p);
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (hp<0x01000000) {
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    if(x+x>p) {
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		x-=p;
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if(x+x>=p) x -= p;
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    }
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	} else {
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    p_half = (float)0.5*p;
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    if(x>p_half) {
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		x-=p;
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		if(x>=p_half) x -= p;
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	    }
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	}
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	GET_FLOAT_WORD(hx,x);
62a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if ((hx&0x7fffffff)==0) hx = 0;
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	SET_FLOAT_WORD(x,hx^sx);
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return x;
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
66