107f3bc5bSDavid Schultz /*- 2*5e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*5e53a4f9SPedro F. Giffuni * 407f3bc5bSDavid Schultz * Copyright (c) 2003, Steven G. Kargl 507f3bc5bSDavid Schultz * All rights reserved. 607f3bc5bSDavid Schultz * 707f3bc5bSDavid Schultz * Redistribution and use in source and binary forms, with or without 807f3bc5bSDavid Schultz * modification, are permitted provided that the following conditions 907f3bc5bSDavid Schultz * are met: 1007f3bc5bSDavid Schultz * 1. Redistributions of source code must retain the above copyright 1107f3bc5bSDavid Schultz * notice unmodified, this list of conditions, and the following 1207f3bc5bSDavid Schultz * disclaimer. 1307f3bc5bSDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright 1407f3bc5bSDavid Schultz * notice, this list of conditions and the following disclaimer in the 1507f3bc5bSDavid Schultz * documentation and/or other materials provided with the distribution. 1607f3bc5bSDavid Schultz * 1707f3bc5bSDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1807f3bc5bSDavid Schultz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1907f3bc5bSDavid Schultz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2007f3bc5bSDavid Schultz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2107f3bc5bSDavid Schultz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2207f3bc5bSDavid Schultz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2307f3bc5bSDavid Schultz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2407f3bc5bSDavid Schultz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2507f3bc5bSDavid Schultz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2607f3bc5bSDavid Schultz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2707f3bc5bSDavid Schultz */ 2807f3bc5bSDavid Schultz 2907f3bc5bSDavid Schultz #include <sys/cdefs.h> 3007f3bc5bSDavid Schultz __FBSDID("$FreeBSD$"); 3107f3bc5bSDavid Schultz 32db89cf8eSSteve Kargl #include <float.h> 33db89cf8eSSteve Kargl #ifdef __i386__ 34db89cf8eSSteve Kargl #include <ieeefp.h> 35db89cf8eSSteve Kargl #endif 36db89cf8eSSteve Kargl 37db89cf8eSSteve Kargl #include "fpmath.h" 38db89cf8eSSteve Kargl #include "math.h" 39db89cf8eSSteve Kargl #include "math_private.h" 4007f3bc5bSDavid Schultz 4107f3bc5bSDavid Schultz long double 4207f3bc5bSDavid Schultz roundl(long double x) 4307f3bc5bSDavid Schultz { 4407f3bc5bSDavid Schultz long double t; 45db89cf8eSSteve Kargl uint16_t hx; 4607f3bc5bSDavid Schultz 47db89cf8eSSteve Kargl GET_LDBL_EXPSIGN(hx, x); 48db89cf8eSSteve Kargl if ((hx & 0x7fff) == 0x7fff) 49db89cf8eSSteve Kargl return (x + x); 5007f3bc5bSDavid Schultz 51db89cf8eSSteve Kargl ENTERI(); 52db89cf8eSSteve Kargl 53db89cf8eSSteve Kargl if (!(hx & 0x8000)) { 545792e54aSBruce Evans t = floorl(x); 55db89cf8eSSteve Kargl if (t - x <= -0.5L) 56db89cf8eSSteve Kargl t += 1; 57db89cf8eSSteve Kargl RETURNI(t); 5807f3bc5bSDavid Schultz } else { 595792e54aSBruce Evans t = floorl(-x); 60db89cf8eSSteve Kargl if (t + x <= -0.5L) 61db89cf8eSSteve Kargl t += 1; 62db89cf8eSSteve Kargl RETURNI(-t); 6307f3bc5bSDavid Schultz } 6407f3bc5bSDavid Schultz } 65