POK
e_remainderf.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_remainderf.c -- float version of e_remainder.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 #ifdef POK_NEEDS_LIBMATH
33 
34 #include <libm.h>
35 #include "math_private.h"
36 
37 static const float zero = 0.0;
38 
39 
40 float
41 __ieee754_remainderf(float x, float p)
42 {
43  int32_t hx,hp;
44  uint32_t sx;
45  float p_half;
46 
47  GET_FLOAT_WORD(hx,x);
48  GET_FLOAT_WORD(hp,p);
49  sx = hx&0x80000000;
50  hp &= 0x7fffffff;
51  hx &= 0x7fffffff;
52 
53  /* purge off exception values */
54  if(hp==0) return (x*p)/(x*p); /* p = 0 */
55  if((hx>=0x7f800000)|| /* x not finite */
56  ((hp>0x7f800000))) /* p is NaN */
57  return (x*p)/(x*p);
58 
59 
60  if (hp<=0x7effffff) x = __ieee754_fmodf(x,p+p); /* now x < 2p */
61  if ((hx-hp)==0) return zero*x;
62  x = fabsf(x);
63  p = fabsf(p);
64  if (hp<0x01000000) {
65  if(x+x>p) {
66  x-=p;
67  if(x+x>=p) x -= p;
68  }
69  } else {
70  p_half = (float)0.5*p;
71  if(x>p_half) {
72  x-=p;
73  if(x>=p_half) x -= p;
74  }
75  }
76  GET_FLOAT_WORD(hx,x);
77  SET_FLOAT_WORD(x,hx^sx);
78  return x;
79 }
80 #endif