]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_nextafter.c
[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[thirdparty/glibc.git] / math / s_nextafter.c
CommitLineData
f7eac6eb
RM
1/* @(#)s_nextafter.c 5.1 93/09/24 */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
cccda09f 8 * software is freely granted, provided that this notice
f7eac6eb
RM
9 * is preserved.
10 * ====================================================
11 */
12
13#if defined(LIBM_SCCS) && !defined(lint)
14static char rcsid[] = "$NetBSD: s_nextafter.c,v 1.8 1995/05/10 20:47:58 jtc Exp $";
15#endif
16
17/* IEEE functions
18 * nextafter(x,y)
19 * return the next machine floating-point number of x in the
20 * direction toward y.
21 * Special cases:
22 */
23
249fd241
UD
24/* Ugly hack so that the aliasing works. */
25#define __nexttoward __internal___nexttoward
26#define nexttoward __internal_nexttoward
27
9d13fb24 28#include <math.h>
11bf311e 29#include "math_private.h"
64b02fd2 30#include <float.h>
f7eac6eb
RM
31
32#ifdef __STDC__
33 double __nextafter(double x, double y)
34#else
35 double __nextafter(x,y)
36 double x,y;
37#endif
38{
39 int32_t hx,hy,ix,iy;
40 u_int32_t lx,ly;
41
42 EXTRACT_WORDS(hx,lx,x);
43 EXTRACT_WORDS(hy,ly,y);
44 ix = hx&0x7fffffff; /* |x| */
45 iy = hy&0x7fffffff; /* |y| */
46
cccda09f
UD
47 if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */
48 ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */
49 return x+y;
bc9f6000 50 if(x==y) return y; /* x=y, return y */
f7eac6eb
RM
51 if((ix|lx)==0) { /* x == 0 */
52 INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */
11bf311e
UD
53 y = x*x;
54 if(y==x) return y; else return x; /* raise underflow flag */
cccda09f 55 }
f7eac6eb
RM
56 if(hx>=0) { /* x > 0 */
57 if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */
58 if(lx==0) hx -= 1;
59 lx -= 1;
60 } else { /* x < y, x += ulp */
61 lx += 1;
62 if(lx==0) hx += 1;
63 }
64 } else { /* x < 0 */
65 if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
66 if(lx==0) hx -= 1;
67 lx -= 1;
68 } else { /* x > y, x += ulp */
69 lx += 1;
70 if(lx==0) hx += 1;
71 }
72 }
73 hy = hx&0x7ff00000;
64b02fd2 74 if(hy>=0x7ff00000) {
07449987
UD
75 x = x+x; /* overflow */
76 if (FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1)
11bf311e 77 asm ("" : "=m"(x) : "m"(x));
07449987 78 return x; /* overflow */
64b02fd2 79 }
11bf311e
UD
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 }
f7eac6eb
RM
86 }
87 INSERT_WORDS(x,hx,lx);
88 return x;
89}
90weak_alias (__nextafter, nextafter)
cccda09f
UD
91#ifdef NO_LONG_DOUBLE
92strong_alias (__nextafter, __nextafterl)
93weak_alias (__nextafter, nextafterl)
90dd37a6
AJ
94strong_alias (__nextafter, __nexttowardl)
95weak_alias (__nexttowardl, nexttowardl)
d03db7f0 96#undef __nexttoward
abfbdde1 97strong_alias (__nextafter, __nexttoward)
d03db7f0 98#undef nexttoward
abfbdde1 99weak_alias (__nextafter, nexttoward)
cccda09f 100#endif