xref: /titanic_41/usr/src/lib/libm/common/C/floor.c (revision a9d3dcd5820128b4f34bf38f447e47aa95c004e8)
15b2ba9d3SPiotr Jasiukajtis /*
25b2ba9d3SPiotr Jasiukajtis  * CDDL HEADER START
35b2ba9d3SPiotr Jasiukajtis  *
45b2ba9d3SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
55b2ba9d3SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
65b2ba9d3SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
75b2ba9d3SPiotr Jasiukajtis  *
85b2ba9d3SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95b2ba9d3SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
105b2ba9d3SPiotr Jasiukajtis  * See the License for the specific language governing permissions
115b2ba9d3SPiotr Jasiukajtis  * and limitations under the License.
125b2ba9d3SPiotr Jasiukajtis  *
135b2ba9d3SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
145b2ba9d3SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155b2ba9d3SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
165b2ba9d3SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
175b2ba9d3SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
185b2ba9d3SPiotr Jasiukajtis  *
195b2ba9d3SPiotr Jasiukajtis  * CDDL HEADER END
205b2ba9d3SPiotr Jasiukajtis  */
215b2ba9d3SPiotr Jasiukajtis /*
225b2ba9d3SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
235b2ba9d3SPiotr Jasiukajtis  */
245b2ba9d3SPiotr Jasiukajtis /*
255b2ba9d3SPiotr Jasiukajtis  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
265b2ba9d3SPiotr Jasiukajtis  * Use is subject to license terms.
275b2ba9d3SPiotr Jasiukajtis  */
285b2ba9d3SPiotr Jasiukajtis 
29*a9d3dcd5SRichard Lowe #pragma weak __floor = floor
305b2ba9d3SPiotr Jasiukajtis 
315b2ba9d3SPiotr Jasiukajtis /*
325b2ba9d3SPiotr Jasiukajtis  * floor(x) returns the biggest integral value less than or equal to x.
335b2ba9d3SPiotr Jasiukajtis  * NOTE: floor(x) returns result with the same sign as x's, including 0.
345b2ba9d3SPiotr Jasiukajtis  *
355b2ba9d3SPiotr Jasiukajtis  * Modified 8/4/04 for performance.
365b2ba9d3SPiotr Jasiukajtis  */
375b2ba9d3SPiotr Jasiukajtis 
385b2ba9d3SPiotr Jasiukajtis #include "libm.h"
395b2ba9d3SPiotr Jasiukajtis 
405b2ba9d3SPiotr Jasiukajtis static const double
415b2ba9d3SPiotr Jasiukajtis 	zero = 0.0,
425b2ba9d3SPiotr Jasiukajtis 	one = 1.0,
435b2ba9d3SPiotr Jasiukajtis 	two52 = 4503599627370496.0;
445b2ba9d3SPiotr Jasiukajtis 
455b2ba9d3SPiotr Jasiukajtis double
floor(double x)465b2ba9d3SPiotr Jasiukajtis floor(double x) {
475b2ba9d3SPiotr Jasiukajtis 	double	t, w;
485b2ba9d3SPiotr Jasiukajtis 	int	hx, lx, ix;
495b2ba9d3SPiotr Jasiukajtis 
505b2ba9d3SPiotr Jasiukajtis 	hx = ((int *)&x)[HIWORD];
515b2ba9d3SPiotr Jasiukajtis 	lx = ((int *)&x)[LOWORD];
525b2ba9d3SPiotr Jasiukajtis 	ix = hx & ~0x80000000;
535b2ba9d3SPiotr Jasiukajtis 	if (ix >= 0x43300000)	/* return x if |x| >= 2^52, or x is NaN */
545b2ba9d3SPiotr Jasiukajtis 		return (x * one);
555b2ba9d3SPiotr Jasiukajtis 	t = (hx >= 0)? two52 : -two52;
565b2ba9d3SPiotr Jasiukajtis 	w = x + t;
575b2ba9d3SPiotr Jasiukajtis 	t = w - t;
585b2ba9d3SPiotr Jasiukajtis 	if (ix < 0x3ff00000) {
595b2ba9d3SPiotr Jasiukajtis 		if ((ix | lx) == 0)
605b2ba9d3SPiotr Jasiukajtis 			return (x);
615b2ba9d3SPiotr Jasiukajtis 		else
625b2ba9d3SPiotr Jasiukajtis 			return ((hx < 0)? -one : zero);
635b2ba9d3SPiotr Jasiukajtis 	}
645b2ba9d3SPiotr Jasiukajtis 	return ((t <= x)? t : t - one);
655b2ba9d3SPiotr Jasiukajtis }
66