1 /* $NetBSD: s_nextafterl.c,v 1.2 2010/09/17 20:39:39 christos Exp $ */
3 /* @(#)s_nextafter.c 5.1 93/09/24 */
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
12 * ====================================================
17 #include <machine/ieee.h>
20 #if LDBL_MAX_EXP != 0x4000
21 #error "Unsupported long double format"
27 * return the next machine floating-point number of x in the
30 * If x == y, y shall be returned
31 * If x or y is NaN, a NaN shall be returned
34 nextafterl(long double x, long double y)
36 volatile long double t;
37 union ieee_ext_u ux, uy;
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 */
48 if (x == y) return y; /* x=y, return y */
51 ux.extu_frach = 0; /* return +-minsubnormal */
53 ux.extu_sign = uy.extu_sign;
54 t = ux.extu_ld * ux.extu_ld;
58 return ux.extu_ld; /* raise underflow flag */
61 if ((x>0.0) ^ (x<y)) { /* x -= ulp */
62 if (ux.extu_fracl == 0) {
63 if ((ux.extu_frach & ~LDBL_NBIT) == 0)
65 ux.extu_frach = (ux.extu_frach - 1) |
66 (ux.extu_frach & LDBL_NBIT);
69 } else { /* x += ulp */
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)
79 if (ux.extu_exp == EXT_EXP_INF)
80 return x+x; /* overflow */
82 if (ux.extu_exp == 0) { /* underflow */
84 t = ux.extu_ld * ux.extu_ld;
85 if (t != ux.extu_ld) /* raise underflow flag */