1d0f13633SDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
4d0f13633SDavid Schultz * Copyright (c) 2003, Steven G. Kargl
5d0f13633SDavid Schultz * All rights reserved.
6d0f13633SDavid Schultz *
7d0f13633SDavid Schultz * Redistribution and use in source and binary forms, with or without
8d0f13633SDavid Schultz * modification, are permitted provided that the following conditions
9d0f13633SDavid Schultz * are met:
10d0f13633SDavid Schultz * 1. Redistributions of source code must retain the above copyright
11d0f13633SDavid Schultz * notice unmodified, this list of conditions, and the following
12d0f13633SDavid Schultz * disclaimer.
13d0f13633SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright
14d0f13633SDavid Schultz * notice, this list of conditions and the following disclaimer in the
15d0f13633SDavid Schultz * documentation and/or other materials provided with the distribution.
16d0f13633SDavid Schultz *
17d0f13633SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d0f13633SDavid Schultz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d0f13633SDavid Schultz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d0f13633SDavid Schultz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d0f13633SDavid Schultz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d0f13633SDavid Schultz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d0f13633SDavid Schultz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d0f13633SDavid Schultz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d0f13633SDavid Schultz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d0f13633SDavid Schultz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d0f13633SDavid Schultz */
28d0f13633SDavid Schultz
29db89cf8eSSteve Kargl #include "math.h"
30db89cf8eSSteve Kargl #include "math_private.h"
31d0f13633SDavid Schultz
32d0f13633SDavid Schultz float
roundf(float x)33d0f13633SDavid Schultz roundf(float x)
34d0f13633SDavid Schultz {
35d0f13633SDavid Schultz float t;
36db89cf8eSSteve Kargl uint32_t hx;
37d0f13633SDavid Schultz
38db89cf8eSSteve Kargl GET_FLOAT_WORD(hx, x);
39db89cf8eSSteve Kargl if ((hx & 0x7fffffff) == 0x7f800000)
40db89cf8eSSteve Kargl return (x + x);
41d0f13633SDavid Schultz
42db89cf8eSSteve Kargl if (!(hx & 0x80000000)) {
435792e54aSBruce Evans t = floorf(x);
44db89cf8eSSteve Kargl if (t - x <= -0.5F)
45db89cf8eSSteve Kargl t += 1;
46d0f13633SDavid Schultz return (t);
47d0f13633SDavid Schultz } else {
485792e54aSBruce Evans t = floorf(-x);
49db89cf8eSSteve Kargl if (t + x <= -0.5F)
50db89cf8eSSteve Kargl t += 1;
51d0f13633SDavid Schultz return (-t);
52d0f13633SDavid Schultz }
53d0f13633SDavid Schultz }
54