xref: /netbsd-src/lib/libm/noieee_src/n_floor.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*      $NetBSD: n_floor.c,v 1.1 1995/10/10 23:36:48 ragge Exp $ */
2 /*
3  * Copyright (c) 1985, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static char sccsid[] = "@(#)floor.c	8.1 (Berkeley) 6/4/93";
37 #endif /* not lint */
38 
39 #include "mathimpl.h"
40 
41 vc(L, 4503599627370496.0E0 ,0000,5c00,0000,0000, 55, 1.0) /* 2**55 */
42 
43 ic(L, 4503599627370496.0E0, 52, 1.0)			  /* 2**52 */
44 
45 #ifdef vccast
46 #define	L	vccast(L)
47 #endif
48 
49 /*
50  * floor(x) := the largest integer no larger than x;
51  * ceil(x) := -floor(-x), for all real x.
52  *
53  * Note: Inexact will be signaled if x is not an integer, as is
54  *	customary for IEEE 754.  No other signal can be emitted.
55  */
56 double
57 floor(x)
58 double x;
59 {
60 	volatile double y;
61 
62 	if (
63 #if !defined(vax)&&!defined(tahoe)
64 		x != x ||	/* NaN */
65 #endif	/* !defined(vax)&&!defined(tahoe) */
66 		x >= L)		/* already an even integer */
67 		return x;
68 	else if (x < (double)0)
69 		return -ceil(-x);
70 	else {			/* now 0 <= x < L */
71 		y = L+x;		/* destructive store must be forced */
72 		y -= L;			/* an integer, and |x-y| < 1 */
73 		return x < y ? y-(double)1 : y;
74 	}
75 }
76 
77 double
78 ceil(x)
79 double x;
80 {
81 	volatile double y;
82 
83 	if (
84 #if !defined(vax)&&!defined(tahoe)
85 		x != x ||	/* NaN */
86 #endif	/* !defined(vax)&&!defined(tahoe) */
87 		x >= L)		/* already an even integer */
88 		return x;
89 	else if (x < (double)0)
90 		return -floor(-x);
91 	else {			/* now 0 <= x < L */
92 		y = L+x;		/* destructive store must be forced */
93 		y -= L;			/* an integer, and |x-y| < 1 */
94 		return x > y ? y+(double)1 : y;
95 	}
96 }
97 
98 #ifndef ns32000			/* rint() is in ./NATIONAL/support.s */
99 /*
100  * algorithm for rint(x) in pseudo-pascal form ...
101  *
102  * real rint(x): real x;
103  *	... delivers integer nearest x in direction of prevailing rounding
104  *	... mode
105  * const	L = (last consecutive integer)/2
106  * 	  = 2**55; for VAX D
107  * 	  = 2**52; for IEEE 754 Double
108  * real	s,t;
109  * begin
110  * 	if x != x then return x;		... NaN
111  * 	if |x| >= L then return x;		... already an integer
112  * 	s := copysign(L,x);
113  * 	t := x + s;				... = (x+s) rounded to integer
114  * 	return t - s
115  * end;
116  *
117  * Note: Inexact will be signaled if x is not an integer, as is
118  *	customary for IEEE 754.  No other signal can be emitted.
119  */
120 double
121 rint(x)
122 double x;
123 {
124 	double s;
125 	volatile double t;
126 	const double one = 1.0;
127 
128 #if !defined(vax)&&!defined(tahoe)
129 	if (x != x)				/* NaN */
130 		return (x);
131 #endif	/* !defined(vax)&&!defined(tahoe) */
132 	if (copysign(x,one) >= L)		/* already an integer */
133 	    return (x);
134 	s = copysign(L,x);
135 	t = x + s;				/* x+s rounded to integer */
136 	return (t - s);
137 }
138 #endif	/* not national */
139