POK
e_fmodf.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* e_fmodf.c -- float version of e_fmod.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 /*
33  * __ieee754_fmodf(x,y)
34  * Return x mod y in exact arithmetic
35  * Method: shift and subtract
36  */
37 
38 #ifdef POK_NEEDS_LIBMATH
39 
40 #include "math_private.h"
41 
42 static const float one = 1.0, Zero[] = {0.0, -0.0,};
43 
44 float
45 __ieee754_fmodf(float x, float y)
46 {
47  int32_t n,hx,hy,hz,ix,iy,sx,i;
48 
49  GET_FLOAT_WORD(hx,x);
50  GET_FLOAT_WORD(hy,y);
51  sx = hx&0x80000000; /* sign of x */
52  hx ^=sx; /* |x| */
53  hy &= 0x7fffffff; /* |y| */
54 
55  /* purge off exception values */
56  if(hy==0||(hx>=0x7f800000)|| /* y=0,or x not finite */
57  (hy>0x7f800000)) /* or y is NaN */
58  return (x*y)/(x*y);
59  if(hx<hy) return x; /* |x|<|y| return x */
60  if(hx==hy)
61  return Zero[(uint32_t)sx>>31]; /* |x|=|y| return x*0*/
62 
63  /* determine ix = ilogb(x) */
64  if(hx<0x00800000) { /* subnormal x */
65  for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
66  } else ix = (hx>>23)-127;
67 
68  /* determine iy = ilogb(y) */
69  if(hy<0x00800000) { /* subnormal y */
70  for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
71  } else iy = (hy>>23)-127;
72 
73  /* set up {hx,lx}, {hy,ly} and align y to x */
74  if(ix >= -126)
75  hx = 0x00800000|(0x007fffff&hx);
76  else { /* subnormal x, shift x to normal */
77  n = -126-ix;
78  hx = hx<<n;
79  }
80  if(iy >= -126)
81  hy = 0x00800000|(0x007fffff&hy);
82  else { /* subnormal y, shift y to normal */
83  n = -126-iy;
84  hy = hy<<n;
85  }
86 
87  /* fix point fmod */
88  n = ix - iy;
89  while(n--) {
90  hz=hx-hy;
91  if(hz<0){hx = hx+hx;}
92  else {
93  if(hz==0) /* return sign(x)*0 */
94  return Zero[(uint32_t)sx>>31];
95  hx = hz+hz;
96  }
97  }
98  hz=hx-hy;
99  if(hz>=0) {hx=hz;}
100 
101  /* convert back to floating value and restore the sign */
102  if(hx==0) /* return sign(x)*0 */
103  return Zero[(uint32_t)sx>>31];
104  while(hx<0x00800000) { /* normalize x */
105  hx = hx+hx;
106  iy -= 1;
107  }
108  if(iy>= -126) { /* normalize output */
109  hx = ((hx-0x00800000)|((iy+127)<<23));
110  SET_FLOAT_WORD(x,hx|sx);
111  } else { /* subnormal output */
112  n = -126 - iy;
113  hx >>= n;
114  SET_FLOAT_WORD(x,hx|sx);
115  x *= one; /* create necessary signal */
116  }
117  return x; /* exact output */
118 }
119 
120 #endif
121