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
30257770Skargl#include <float.h>
31257770Skargl#ifdef __i386__
32257770Skargl#include <ieeefp.h>
33257770Skargl#endif
34144772Sdas
35257770Skargl#include "fpmath.h"
36257770Skargl#include "math.h"
37257770Skargl#include "math_private.h"
38257770Skargl
39144772Sdaslong double
40144772Sdasroundl(long double x)
41144772Sdas{
42144772Sdas	long double t;
43257770Skargl	uint16_t hx;
44144772Sdas
45257770Skargl	GET_LDBL_EXPSIGN(hx, x);
46257770Skargl	if ((hx & 0x7fff) == 0x7fff)
47257770Skargl		return (x + x);
48144772Sdas
49257770Skargl	ENTERI();
50257770Skargl
51257770Skargl	if (!(hx & 0x8000)) {
52153017Sbde		t = floorl(x);
53257770Skargl		if (t - x <= -0.5L)
54257770Skargl			t += 1;
55257770Skargl		RETURNI(t);
56144772Sdas	} else {
57153017Sbde		t = floorl(-x);
58257770Skargl		if (t + x <= -0.5L)
59257770Skargl			t += 1;
60257770Skargl		RETURNI(-t);
61144772Sdas	}
62144772Sdas}
63