110c9ffa4SDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
410c9ffa4SDavid Schultz * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
510c9ffa4SDavid Schultz * All rights reserved.
610c9ffa4SDavid Schultz *
710c9ffa4SDavid Schultz * Redistribution and use in source and binary forms, with or without
810c9ffa4SDavid Schultz * modification, are permitted provided that the following conditions
910c9ffa4SDavid Schultz * are met:
1010c9ffa4SDavid Schultz * 1. Redistributions of source code must retain the above copyright
1110c9ffa4SDavid Schultz * notice, this list of conditions and the following disclaimer.
1210c9ffa4SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright
1310c9ffa4SDavid Schultz * notice, this list of conditions and the following disclaimer in the
1410c9ffa4SDavid Schultz * documentation and/or other materials provided with the distribution.
1510c9ffa4SDavid Schultz *
1610c9ffa4SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1710c9ffa4SDavid Schultz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1810c9ffa4SDavid Schultz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1910c9ffa4SDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2010c9ffa4SDavid Schultz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2110c9ffa4SDavid Schultz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2210c9ffa4SDavid Schultz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2310c9ffa4SDavid Schultz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2410c9ffa4SDavid Schultz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2510c9ffa4SDavid Schultz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2610c9ffa4SDavid Schultz * SUCH DAMAGE.
2710c9ffa4SDavid Schultz */
2810c9ffa4SDavid Schultz
2910c9ffa4SDavid Schultz #include <fenv.h>
3010c9ffa4SDavid Schultz #include <math.h>
3110c9ffa4SDavid Schultz
3210c9ffa4SDavid Schultz #ifndef type
3310c9ffa4SDavid Schultz #define type double
3410c9ffa4SDavid Schultz #define roundit rint
3510c9ffa4SDavid Schultz #define dtype long
3610c9ffa4SDavid Schultz #define fn lrint
3710c9ffa4SDavid Schultz #endif
3810c9ffa4SDavid Schultz
3910c9ffa4SDavid Schultz /*
4010c9ffa4SDavid Schultz * C99 says we should not raise a spurious inexact exception when an
4110c9ffa4SDavid Schultz * invalid exception is raised. Unfortunately, the set of inputs
4210c9ffa4SDavid Schultz * that overflows depends on the rounding mode when 'dtype' has more
4310c9ffa4SDavid Schultz * significant bits than 'type'. Hence, we bend over backwards for the
4410c9ffa4SDavid Schultz * sake of correctness; an MD implementation could be more efficient.
4510c9ffa4SDavid Schultz */
4610c9ffa4SDavid Schultz dtype
fn(type x)4710c9ffa4SDavid Schultz fn(type x)
4810c9ffa4SDavid Schultz {
4910c9ffa4SDavid Schultz fenv_t env;
5010c9ffa4SDavid Schultz dtype d;
5110c9ffa4SDavid Schultz
5210c9ffa4SDavid Schultz feholdexcept(&env);
5310c9ffa4SDavid Schultz d = (dtype)roundit(x);
5410c9ffa4SDavid Schultz if (fetestexcept(FE_INVALID))
5510c9ffa4SDavid Schultz feclearexcept(FE_INEXACT);
5610c9ffa4SDavid Schultz feupdateenv(&env);
5710c9ffa4SDavid Schultz return (d);
5810c9ffa4SDavid Schultz }
59