1144772Sdas/*-
2144772Sdas * Copyright (c) 2003, Steven G. Kargl
3144772Sdas * All rights reserved.
4144772Sdas *
5144772Sdas * Redistribution and use in source and binary forms, with or without
6144772Sdas * modification, are permitted provided that the following conditions
7144772Sdas * are met:
8144772Sdas * 1. Redistributions of source code must retain the above copyright
9144772Sdas *    notice unmodified, this list of conditions, and the following
10144772Sdas *    disclaimer.
11144772Sdas * 2. Redistributions in binary form must reproduce the above copyright
12144772Sdas *    notice, this list of conditions and the following disclaimer in the
13144772Sdas *    documentation and/or other materials provided with the distribution.
14144772Sdas *
15144772Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16144772Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17144772Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18144772Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19144772Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20144772Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21144772Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22144772Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23144772Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24144772Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25144772Sdas */
26144772Sdas
27144772Sdas#include <sys/cdefs.h>
28144772Sdas__FBSDID("$FreeBSD$");
29144772Sdas
30271779Stijl#include <float.h>
31271779Stijl#ifdef __i386__
32271779Stijl#include <ieeefp.h>
33271779Stijl#endif
34144772Sdas
35271779Stijl#include "fpmath.h"
36271779Stijl#include "math.h"
37271779Stijl#include "math_private.h"
38271779Stijl
39144772Sdaslong double
40144772Sdasroundl(long double x)
41144772Sdas{
42144772Sdas	long double t;
43271779Stijl	uint16_t hx;
44144772Sdas
45271779Stijl	GET_LDBL_EXPSIGN(hx, x);
46271779Stijl	if ((hx & 0x7fff) == 0x7fff)
47271779Stijl		return (x + x);
48144772Sdas
49271779Stijl	ENTERI();
50271779Stijl
51271779Stijl	if (!(hx & 0x8000)) {
52153017Sbde		t = floorl(x);
53271779Stijl		if (t - x <= -0.5L)
54271779Stijl			t += 1;
55271779Stijl		RETURNI(t);
56144772Sdas	} else {
57153017Sbde		t = floorl(-x);
58271779Stijl		if (t + x <= -0.5L)
59271779Stijl			t += 1;
60271779Stijl		RETURNI(-t);
61144772Sdas	}
62144772Sdas}
63