xref: /netbsd-src/lib/libm/src/e_remainderf.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
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 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: e_remainderf.c,v 1.6 1999/07/02 15:37:41 simonb Exp $");
19 #endif
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 #ifdef __STDC__
25 static const float zero = 0.0;
26 #else
27 static float zero = 0.0;
28 #endif
29 
30 
31 #ifdef __STDC__
32 	float __ieee754_remainderf(float x, float p)
33 #else
34 	float __ieee754_remainderf(x,p)
35 	float x,p;
36 #endif
37 {
38 	int32_t hx,hp;
39 	u_int32_t sx;
40 	float p_half;
41 
42 	GET_FLOAT_WORD(hx,x);
43 	GET_FLOAT_WORD(hp,p);
44 	sx = hx&0x80000000;
45 	hp &= 0x7fffffff;
46 	hx &= 0x7fffffff;
47 
48     /* purge off exception values */
49 	if(hp==0) return (x*p)/(x*p);	 	/* p = 0 */
50 	if((hx>=0x7f800000)||			/* x not finite */
51 	  ((hp>0x7f800000)))			/* p is NaN */
52 	    return (x*p)/(x*p);
53 
54 
55 	if (hp<=0x7effffff) x = __ieee754_fmodf(x,p+p);	/* now x < 2p */
56 	if ((hx-hp)==0) return zero*x;
57 	x  = fabsf(x);
58 	p  = fabsf(p);
59 	if (hp<0x01000000) {
60 	    if(x+x>p) {
61 		x-=p;
62 		if(x+x>=p) x -= p;
63 	    }
64 	} else {
65 	    p_half = (float)0.5*p;
66 	    if(x>p_half) {
67 		x-=p;
68 		if(x>=p_half) x -= p;
69 	    }
70 	}
71 	GET_FLOAT_WORD(hx,x);
72 	SET_FLOAT_WORD(x,hx^sx);
73 	return x;
74 }
75