1*eab01a26Sguenther /* $OpenBSD: s_roundf.c,v 1.2 2016/09/12 04:39:47 guenther Exp $ */
265850ae2Sbrad
365850ae2Sbrad /*-
465850ae2Sbrad * Copyright (c) 2003, Steven G. Kargl
565850ae2Sbrad * All rights reserved.
665850ae2Sbrad *
765850ae2Sbrad * Redistribution and use in source and binary forms, with or without
865850ae2Sbrad * modification, are permitted provided that the following conditions
965850ae2Sbrad * are met:
1065850ae2Sbrad * 1. Redistributions of source code must retain the above copyright
1165850ae2Sbrad * notice unmodified, this list of conditions, and the following
1265850ae2Sbrad * disclaimer.
1365850ae2Sbrad * 2. Redistributions in binary form must reproduce the above copyright
1465850ae2Sbrad * notice, this list of conditions and the following disclaimer in the
1565850ae2Sbrad * documentation and/or other materials provided with the distribution.
1665850ae2Sbrad *
1765850ae2Sbrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1865850ae2Sbrad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1965850ae2Sbrad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2065850ae2Sbrad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2165850ae2Sbrad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2265850ae2Sbrad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2365850ae2Sbrad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2465850ae2Sbrad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2565850ae2Sbrad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2665850ae2Sbrad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2765850ae2Sbrad */
2865850ae2Sbrad
2965850ae2Sbrad #include "math.h"
3065850ae2Sbrad #include "math_private.h"
3165850ae2Sbrad
3265850ae2Sbrad float
roundf(float x)3365850ae2Sbrad roundf(float x)
3465850ae2Sbrad {
3565850ae2Sbrad float t;
3665850ae2Sbrad
37*eab01a26Sguenther if (isinf(x) || isnan(x))
3865850ae2Sbrad return (x);
3965850ae2Sbrad
4065850ae2Sbrad if (x >= 0.0) {
4165850ae2Sbrad t = floorf(x);
4265850ae2Sbrad if (t - x <= -0.5)
4365850ae2Sbrad t += 1.0;
4465850ae2Sbrad return (t);
4565850ae2Sbrad } else {
4665850ae2Sbrad t = floorf(-x);
4765850ae2Sbrad if (t + x <= -0.5)
4865850ae2Sbrad t += 1.0;
4965850ae2Sbrad return (-t);
5065850ae2Sbrad }
5165850ae2Sbrad }
52