1130179Sdas/*-
2130179Sdas * Copyright (c) 2003, Steven G. Kargl
3130179Sdas * All rights reserved.
4130179Sdas *
5130179Sdas * Redistribution and use in source and binary forms, with or without
6130179Sdas * modification, are permitted provided that the following conditions
7130179Sdas * are met:
8130179Sdas * 1. Redistributions of source code must retain the above copyright
9130179Sdas *    notice unmodified, this list of conditions, and the following
10130179Sdas *    disclaimer.
11130179Sdas * 2. Redistributions in binary form must reproduce the above copyright
12130179Sdas *    notice, this list of conditions and the following disclaimer in the
13130179Sdas *    documentation and/or other materials provided with the distribution.
14130179Sdas *
15130179Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16130179Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17130179Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18130179Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19130179Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20130179Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21130179Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22130179Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23130179Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24130179Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25130179Sdas */
26130179Sdas
27130179Sdas#include <sys/cdefs.h>
28130179Sdas__FBSDID("$FreeBSD$");
29130179Sdas
30257770Skargl#include "math.h"
31257770Skargl#include "math_private.h"
32130179Sdas
33130179Sdasfloat
34130179Sdasroundf(float x)
35130179Sdas{
36130179Sdas	float t;
37257770Skargl	uint32_t hx;
38130179Sdas
39257770Skargl	GET_FLOAT_WORD(hx, x);
40257770Skargl	if ((hx & 0x7fffffff) == 0x7f800000)
41257770Skargl		return (x + x);
42130179Sdas
43257770Skargl	if (!(hx & 0x80000000)) {
44153017Sbde		t = floorf(x);
45257770Skargl		if (t - x <= -0.5F)
46257770Skargl			t += 1;
47130179Sdas		return (t);
48130179Sdas	} else {
49153017Sbde		t = floorf(-x);
50257770Skargl		if (t + x <= -0.5F)
51257770Skargl			t += 1;
52130179Sdas		return (-t);
53130179Sdas	}
54130179Sdas}
55