xref: /dflybsd-src/contrib/openbsd_libm/src/s_roundl.c (revision 6b6b4dd0d75b9cd8fd4deebd4046aae2f1fafb10)
105a0b428SJohn Marino /*	$OpenBSD: s_roundl.c,v 1.2 2012/12/05 23:20:04 deraadt Exp $	*/
205a0b428SJohn Marino 
305a0b428SJohn Marino /*-
405a0b428SJohn Marino  * Copyright (c) 2003, Steven G. Kargl
505a0b428SJohn Marino  * All rights reserved.
605a0b428SJohn Marino  *
705a0b428SJohn Marino  * Redistribution and use in source and binary forms, with or without
805a0b428SJohn Marino  * modification, are permitted provided that the following conditions
905a0b428SJohn Marino  * are met:
1005a0b428SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1105a0b428SJohn Marino  *    notice unmodified, this list of conditions, and the following
1205a0b428SJohn Marino  *    disclaimer.
1305a0b428SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1405a0b428SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1505a0b428SJohn Marino  *    documentation and/or other materials provided with the distribution.
1605a0b428SJohn Marino  *
1705a0b428SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1805a0b428SJohn Marino  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1905a0b428SJohn Marino  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2005a0b428SJohn Marino  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2105a0b428SJohn Marino  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2205a0b428SJohn Marino  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2305a0b428SJohn Marino  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2405a0b428SJohn Marino  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2505a0b428SJohn Marino  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2605a0b428SJohn Marino  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2705a0b428SJohn Marino  */
2805a0b428SJohn Marino 
2905a0b428SJohn Marino #include <math.h>
30*6b6b4dd0SJohn Marino #include "math_private.h"
3105a0b428SJohn Marino 
3205a0b428SJohn Marino long double
roundl(long double x)3305a0b428SJohn Marino roundl(long double x)
3405a0b428SJohn Marino {
3505a0b428SJohn Marino 	long double t;
36*6b6b4dd0SJohn Marino 	uint16_t hx;
3705a0b428SJohn Marino 
38*6b6b4dd0SJohn Marino 	GET_LDOUBLE_EXP(hx, x);
39*6b6b4dd0SJohn Marino 	if ((hx & 0x7fff) == 0x7fff)
40*6b6b4dd0SJohn Marino 		return (x + x);
4105a0b428SJohn Marino 
42*6b6b4dd0SJohn Marino 
43*6b6b4dd0SJohn Marino 	if (!(hx & 0x8000)) {
4405a0b428SJohn Marino 		t = floorl(x);
45*6b6b4dd0SJohn Marino 		if (t - x <= -0.5L)
46*6b6b4dd0SJohn Marino 			t += 1;
4705a0b428SJohn Marino 		return (t);
4805a0b428SJohn Marino 	} else {
4905a0b428SJohn Marino 		t = floorl(-x);
50*6b6b4dd0SJohn Marino 		if (t + x <= -0.5L)
51*6b6b4dd0SJohn Marino 			t += 1;
5205a0b428SJohn Marino 		return (-t);
5305a0b428SJohn Marino 	}
5405a0b428SJohn Marino }
55