xref: /minix3/lib/libm/src/s_roundl.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*-
2*84d9c625SLionel Sambuc  * Copyright (c) 2003, Steven G. Kargl
3*84d9c625SLionel Sambuc  * All rights reserved.
4*84d9c625SLionel Sambuc  *
5*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
7*84d9c625SLionel Sambuc  * are met:
8*84d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
9*84d9c625SLionel Sambuc  *    notice unmodified, this list of conditions, and the following
10*84d9c625SLionel Sambuc  *    disclaimer.
11*84d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12*84d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13*84d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14*84d9c625SLionel Sambuc  *
15*84d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*84d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*84d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*84d9c625SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*84d9c625SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*84d9c625SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*84d9c625SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*84d9c625SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*84d9c625SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*84d9c625SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*84d9c625SLionel Sambuc  */
26*84d9c625SLionel Sambuc 
27*84d9c625SLionel Sambuc #include <sys/cdefs.h>
28*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_roundl.c,v 1.1 2013/11/11 23:57:34 joerg Exp $");
29*84d9c625SLionel Sambuc #if 0
30*84d9c625SLionel Sambuc __FBSDID("$FreeBSD: head/lib/msun/src/s_roundl.c 153017 2005-12-02 13:45:06Z bde $");
31*84d9c625SLionel Sambuc #endif
32*84d9c625SLionel Sambuc 
33*84d9c625SLionel Sambuc #include "namespace.h"
34*84d9c625SLionel Sambuc #include <math.h>
35*84d9c625SLionel Sambuc 
36*84d9c625SLionel Sambuc #ifdef __HAVE_LONG_DOUBLE
37*84d9c625SLionel Sambuc 
38*84d9c625SLionel Sambuc #ifdef __weak_alias
__weak_alias(roundl,_roundl)39*84d9c625SLionel Sambuc __weak_alias(roundl, _roundl)
40*84d9c625SLionel Sambuc #endif
41*84d9c625SLionel Sambuc 
42*84d9c625SLionel Sambuc long double
43*84d9c625SLionel Sambuc roundl(long double x)
44*84d9c625SLionel Sambuc {
45*84d9c625SLionel Sambuc 	long double t;
46*84d9c625SLionel Sambuc 
47*84d9c625SLionel Sambuc 	if (!isfinite(x))
48*84d9c625SLionel Sambuc 		return (x);
49*84d9c625SLionel Sambuc 
50*84d9c625SLionel Sambuc 	if (x >= 0.0) {
51*84d9c625SLionel Sambuc 		t = floorl(x);
52*84d9c625SLionel Sambuc 		if (t - x <= -0.5)
53*84d9c625SLionel Sambuc 			t += 1.0;
54*84d9c625SLionel Sambuc 		return (t);
55*84d9c625SLionel Sambuc 	} else {
56*84d9c625SLionel Sambuc 		t = floorl(-x);
57*84d9c625SLionel Sambuc 		if (t + x <= -0.5)
58*84d9c625SLionel Sambuc 			t += 1.0;
59*84d9c625SLionel Sambuc 		return (-t);
60*84d9c625SLionel Sambuc 	}
61*84d9c625SLionel Sambuc }
62*84d9c625SLionel Sambuc 
63*84d9c625SLionel Sambuc #endif /* __HAVE_LONG_DOUBLE */
64