105a0b428SJohn Marino /* $OpenBSD: s_round.c,v 1.6 2013/07/03 04:46:36 espie 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 <float.h>
3005a0b428SJohn Marino #include <math.h>
3105a0b428SJohn Marino
3205a0b428SJohn Marino #include "math_private.h"
3305a0b428SJohn Marino
3405a0b428SJohn Marino double
round(double x)3505a0b428SJohn Marino round(double x)
3605a0b428SJohn Marino {
3705a0b428SJohn Marino double t;
38*6b6b4dd0SJohn Marino uint32_t hx;
3905a0b428SJohn Marino
40*6b6b4dd0SJohn Marino GET_HIGH_WORD(hx, x);
41*6b6b4dd0SJohn Marino if ((hx & 0x7fffffff) == 0x7ff00000)
42*6b6b4dd0SJohn Marino return (x + x);
4305a0b428SJohn Marino
44*6b6b4dd0SJohn Marino if (!(hx & 0x80000000)) {
4505a0b428SJohn Marino t = floor(x);
4605a0b428SJohn Marino if (t - x <= -0.5)
47*6b6b4dd0SJohn Marino t += 1;
4805a0b428SJohn Marino return (t);
4905a0b428SJohn Marino } else {
5005a0b428SJohn Marino t = floor(-x);
5105a0b428SJohn Marino if (t + x <= -0.5)
52*6b6b4dd0SJohn Marino t += 1;
5305a0b428SJohn Marino return (-t);
5405a0b428SJohn Marino }
5505a0b428SJohn Marino }
5605a0b428SJohn Marino
5705a0b428SJohn Marino #if LDBL_MANT_DIG == DBL_MANT_DIG
5805a0b428SJohn Marino __strong_alias(roundl, round);
5905a0b428SJohn Marino #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */
60