Home | History | Annotate | Line # | Download | only in noieee_src
n_floor.c revision 1.4
      1 /*      $NetBSD: n_floor.c,v 1.4 2002/06/15 00:10:17 matt 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 #if 0
     37 static char sccsid[] = "@(#)floor.c	8.1 (Berkeley) 6/4/93";
     38 #endif
     39 #endif /* not lint */
     40 
     41 #define _LIBM_STATIC
     42 #include "mathimpl.h"
     43 
     44 vc(L, 4503599627370496.0E0 ,0000,5c00,0000,0000, 55, 1.0) /* 2**55 */
     45 
     46 ic(L, 4503599627370496.0E0, 52, 1.0)			  /* 2**52 */
     47 
     48 #ifdef vccast
     49 #define	L	vccast(L)
     50 #endif
     51 
     52 /*
     53  * floor(x) := the largest integer no larger than x;
     54  * ceil(x) := -floor(-x), for all real x.
     55  *
     56  * Note: Inexact will be signaled if x is not an integer, as is
     57  *	customary for IEEE 754.  No other signal can be emitted.
     58  */
     59 double
     60 floor(double x)
     61 {
     62 	volatile double y;
     63 
     64 	if (
     65 #if !defined(__vax__)&&!defined(tahoe)
     66 		x != x ||	/* NaN */
     67 #endif	/* !defined(__vax__)&&!defined(tahoe) */
     68 		x >= L)		/* already an even integer */
     69 		return x;
     70 	else if (x < (double)0)
     71 		return -ceil(-x);
     72 	else {			/* now 0 <= x < L */
     73 		y = L+x;		/* destructive store must be forced */
     74 		y -= L;			/* an integer, and |x-y| < 1 */
     75 		return x < y ? y-(double)1 : y;
     76 	}
     77 }
     78 
     79 double
     80 ceil(double x)
     81 {
     82 	volatile double y;
     83 
     84 	if (
     85 #if !defined(__vax__)&&!defined(tahoe)
     86 		x != x ||	/* NaN */
     87 #endif	/* !defined(__vax__)&&!defined(tahoe) */
     88 		x >= L)		/* already an even integer */
     89 		return x;
     90 	else if (x < (double)0)
     91 		return -floor(-x);
     92 	else {			/* now 0 <= x < L */
     93 		y = L+x;		/* destructive store must be forced */
     94 		y -= L;			/* an integer, and |x-y| < 1 */
     95 		return x > y ? y+(double)1 : y;
     96 	}
     97 }
     98 
     99 #ifndef ns32000			/* rint() is in ./NATIONAL/support.s */
    100 /*
    101  * algorithm for rint(x) in pseudo-pascal form ...
    102  *
    103  * real rint(x): real x;
    104  *	... delivers integer nearest x in direction of prevailing rounding
    105  *	... mode
    106  * const	L = (last consecutive integer)/2
    107  * 	  = 2**55; for VAX D
    108  * 	  = 2**52; for IEEE 754 Double
    109  * real	s,t;
    110  * begin
    111  * 	if x != x then return x;		... NaN
    112  * 	if |x| >= L then return x;		... already an integer
    113  * 	s := copysign(L,x);
    114  * 	t := x + s;				... = (x+s) rounded to integer
    115  * 	return t - s
    116  * end;
    117  *
    118  * Note: Inexact will be signaled if x is not an integer, as is
    119  *	customary for IEEE 754.  No other signal can be emitted.
    120  */
    121 double
    122 rint(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