105a0b428SJohn Marino /* $OpenBSD: s_roundf.c,v 1.1 2006/07/12 07:26:08 brad Exp $ */
205a0b428SJohn Marino
305a0b428SJohn Marino /*-
405a0b428SJohn Marino * Copyright (c) 2003, Steven G. Kargl
505a0b428SJohn Marino * All rights reserved.
605a0b428SJohn Marino *
705a0b428SJohn Marino * Redistribution and use in source and binary forms, with or without
805a0b428SJohn Marino * modification, are permitted provided that the following conditions
905a0b428SJohn Marino * are met:
1005a0b428SJohn Marino * 1. Redistributions of source code must retain the above copyright
1105a0b428SJohn Marino * notice unmodified, this list of conditions, and the following
1205a0b428SJohn Marino * disclaimer.
1305a0b428SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1405a0b428SJohn Marino * notice, this list of conditions and the following disclaimer in the
1505a0b428SJohn Marino * documentation and/or other materials provided with the distribution.
1605a0b428SJohn Marino *
1705a0b428SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1805a0b428SJohn Marino * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1905a0b428SJohn Marino * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2005a0b428SJohn Marino * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2105a0b428SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2205a0b428SJohn Marino * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2305a0b428SJohn Marino * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2405a0b428SJohn Marino * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2505a0b428SJohn Marino * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2605a0b428SJohn Marino * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2705a0b428SJohn Marino */
2805a0b428SJohn Marino
2905a0b428SJohn Marino #include "math.h"
3005a0b428SJohn Marino #include "math_private.h"
3105a0b428SJohn Marino
3205a0b428SJohn Marino float
roundf(float x)3305a0b428SJohn Marino roundf(float x)
3405a0b428SJohn Marino {
3505a0b428SJohn Marino float t;
36*6b6b4dd0SJohn Marino uint32_t hx;
3705a0b428SJohn Marino
38*6b6b4dd0SJohn Marino GET_FLOAT_WORD(hx, x);
39*6b6b4dd0SJohn Marino if ((hx & 0x7fffffff) == 0x7f800000)
40*6b6b4dd0SJohn Marino return (x + x);
4105a0b428SJohn Marino
42*6b6b4dd0SJohn Marino if (!(hx & 0x80000000)) {
4305a0b428SJohn Marino t = floorf(x);
44*6b6b4dd0SJohn Marino if (t - x <= -0.5F)
45*6b6b4dd0SJohn Marino t += 1;
4605a0b428SJohn Marino return (t);
4705a0b428SJohn Marino } else {
4805a0b428SJohn Marino t = floorf(-x);
49*6b6b4dd0SJohn Marino if (t + x <= -0.5F)
50*6b6b4dd0SJohn Marino t += 1;
5105a0b428SJohn Marino return (-t);
5205a0b428SJohn Marino }
5305a0b428SJohn Marino }
54