Merge branch 'vendor/TCPDUMP'
[dragonfly.git] / lib / libm / src / s_nextafterl.c
1 /*      $NetBSD: s_nextafterl.c,v 1.2 2010/09/17 20:39:39 christos Exp $        */
2
3 /* @(#)s_nextafter.c 5.1 93/09/24 */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunPro, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14
15 #include <float.h>
16 #include <math.h>
17 #include <machine/ieee.h>
18
19 #ifdef EXT_EXP_INFNAN
20 #if LDBL_MAX_EXP != 0x4000
21 #error "Unsupported long double format"
22 #endif
23
24 /*
25  * IEEE functions
26  *      nextafterl(x,y)
27  *      return the next machine floating-point number of x in the
28  *      direction toward y.
29  *   Special cases:
30  *      If x == y, y shall be returned
31  *      If x or y is NaN, a NaN shall be returned
32  */
33 long double
34 nextafterl(long double x, long double y)
35 {
36         volatile long double t;
37         union ieee_ext_u ux, uy;
38
39         ux.extu_ld = x;
40         uy.extu_ld = y;
41
42         if ((ux.extu_exp == EXT_EXP_NAN &&
43                 ((ux.extu_frach &~ LDBL_NBIT)|ux.extu_fracl) != 0) ||
44             (uy.extu_exp == EXT_EXP_NAN &&
45                 ((uy.extu_frach &~ LDBL_NBIT)|uy.extu_fracl) != 0))
46                 return x+y;                     /* x or y is nan */
47
48         if (x == y) return y;                   /* x=y, return y */
49
50         if (x == 0.0) {
51                 ux.extu_frach = 0;              /* return +-minsubnormal */
52                 ux.extu_fracl = 1;
53                 ux.extu_sign = uy.extu_sign;
54                 t = ux.extu_ld * ux.extu_ld;
55                 if (t == ux.extu_ld)
56                         return t;
57                 else
58                         return ux.extu_ld;      /* raise underflow flag */
59         }
60
61         if ((x>0.0) ^ (x<y)) {                  /* x -= ulp */
62                 if (ux.extu_fracl == 0) {
63                         if ((ux.extu_frach & ~LDBL_NBIT) == 0)
64                                 ux.extu_exp -= 1;
65                         ux.extu_frach = (ux.extu_frach - 1) |
66                                         (ux.extu_frach & LDBL_NBIT);
67                 }
68                 ux.extu_fracl -= 1;
69         } else {                                /* x += ulp */
70                 ux.extu_fracl += 1;
71                 if (ux.extu_fracl == 0) {
72                         ux.extu_frach = (ux.extu_frach + 1) |
73                                         (ux.extu_frach & LDBL_NBIT);
74                         if ((ux.extu_frach & ~LDBL_NBIT) == 0)
75                                 ux.extu_exp += 1;
76                 }
77         }
78
79         if (ux.extu_exp == EXT_EXP_INF)
80                 return x+x;                     /* overflow  */
81
82         if (ux.extu_exp == 0) {                 /* underflow */
83                 mask_nbit_l(ux);
84                 t = ux.extu_ld * ux.extu_ld;
85                 if (t != ux.extu_ld)            /* raise underflow flag */
86                         return ux.extu_ld;
87         }
88
89         return ux.extu_ld;
90 }
91 #endif