xref: /freebsd/lib/msun/src/e_remainderf.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /* e_remainderf.c -- float version of e_remainder.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include <sys/cdefs.h>
17 #include "math.h"
18 #include "math_private.h"
19 
20 static const float zero = 0.0;
21 
22 
23 float
24 remainderf(float x, float p)
25 {
26 	int32_t hx,hp;
27 	u_int32_t sx;
28 	float p_half;
29 
30 	GET_FLOAT_WORD(hx,x);
31 	GET_FLOAT_WORD(hp,p);
32 	sx = hx&0x80000000;
33 	hp &= 0x7fffffff;
34 	hx &= 0x7fffffff;
35 
36     /* purge off exception values */
37 	if((hp==0)||			 	/* p = 0 */
38 	  (hx>=0x7f800000)||			/* x not finite */
39 	  ((hp>0x7f800000)))			/* p is NaN */
40 	    return nan_mix_op(x, p, *)/nan_mix_op(x, p, *);
41 
42 
43 	if (hp<=0x7effffff) x = fmodf(x,p+p);	/* now x < 2p */
44 	if ((hx-hp)==0) return zero*x;
45 	x  = fabsf(x);
46 	p  = fabsf(p);
47 	if (hp<0x01000000) {
48 	    if(x+x>p) {
49 		x-=p;
50 		if(x+x>=p) x -= p;
51 	    }
52 	} else {
53 	    p_half = (float)0.5*p;
54 	    if(x>p_half) {
55 		x-=p;
56 		if(x>=p_half) x -= p;
57 	    }
58 	}
59 	GET_FLOAT_WORD(hx,x);
60 	if ((hx&0x7fffffff)==0) hx = 0;
61 	SET_FLOAT_WORD(x,hx^sx);
62 	return x;
63 }
64