xref: /freebsd/lib/msun/src/s_roundf.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1d0f13633SDavid Schultz /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro 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 
29d0f13633SDavid Schultz #include <sys/cdefs.h>
30d0f13633SDavid Schultz __FBSDID("$FreeBSD$");
31d0f13633SDavid Schultz 
32db89cf8eSSteve Kargl #include "math.h"
33db89cf8eSSteve Kargl #include "math_private.h"
34d0f13633SDavid Schultz 
35d0f13633SDavid Schultz float
36d0f13633SDavid Schultz roundf(float x)
37d0f13633SDavid Schultz {
38d0f13633SDavid Schultz 	float t;
39db89cf8eSSteve Kargl 	uint32_t hx;
40d0f13633SDavid Schultz 
41db89cf8eSSteve Kargl 	GET_FLOAT_WORD(hx, x);
42db89cf8eSSteve Kargl 	if ((hx & 0x7fffffff) == 0x7f800000)
43db89cf8eSSteve Kargl 		return (x + x);
44d0f13633SDavid Schultz 
45db89cf8eSSteve Kargl 	if (!(hx & 0x80000000)) {
465792e54aSBruce Evans 		t = floorf(x);
47db89cf8eSSteve Kargl 		if (t - x <= -0.5F)
48db89cf8eSSteve Kargl 			t += 1;
49d0f13633SDavid Schultz 		return (t);
50d0f13633SDavid Schultz 	} else {
515792e54aSBruce Evans 		t = floorf(-x);
52db89cf8eSSteve Kargl 		if (t + x <= -0.5F)
53db89cf8eSSteve Kargl 			t += 1;
54d0f13633SDavid Schultz 		return (-t);
55d0f13633SDavid Schultz 	}
56d0f13633SDavid Schultz }
57