POK
nextafter.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 /* @(#)s_nextafter.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 #ifdef POK_NEEDS_LIBMATH
30 
31 /* IEEE functions
32  * nextafter(x,y)
33  * return the next machine floating-point number of x in the
34  * direction toward y.
35  * Special cases:
36  */
37 
38 #include <libm.h>
39 #include "math_private.h"
40 
41 double
42 nextafter(double x, double y)
43 {
44  int32_t hx,hy,ix,iy;
45  uint32_t lx,ly;
46 
47  EXTRACT_WORDS(hx,lx,x);
48  EXTRACT_WORDS(hy,ly,y);
49  ix = hx&0x7fffffff; /* |x| */
50  iy = hy&0x7fffffff; /* |y| */
51 
52  if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
53  ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
54  return x+y;
55  if(x==y) return x; /* x=y, return x */
56  if((ix|lx)==0) { /* x == 0 */
57  INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
58  y = x*x;
59  if(y==x) return y; else return x; /* raise underflow flag */
60  }
61  if(hx>=0) { /* x > 0 */
62  if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
63  if(lx==0) hx -= 1;
64  lx -= 1;
65  } else { /* x < y, x += ulp */
66  lx += 1;
67  if(lx==0) hx += 1;
68  }
69  } else { /* x < 0 */
70  if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
71  if(lx==0) hx -= 1;
72  lx -= 1;
73  } else { /* x > y, x += ulp */
74  lx += 1;
75  if(lx==0) hx += 1;
76  }
77  }
78  hy = hx&0x7ff00000;
79  if(hy>=0x7ff00000) return x+x; /* overflow */
80  if(hy<0x00100000) { /* underflow */
81  y = x*x;
82  if(y!=x) { /* raise underflow flag */
83  INSERT_WORDS(y,hx,lx);
84  return y;
85  }
86  }
87  INSERT_WORDS(x,hx,lx);
88  return x;
89 }
90 
91 #endif