xref: /openbsd-src/lib/libm/src/s_lrintl.c (revision e44725a8dd99f82f94f37ecff5c0e710c4dba97e)
1*e44725a8Skettenis /*	$OpenBSD: s_lrintl.c,v 1.4 2021/10/14 21:30:00 kettenis Exp $	*/
249393c00Smartynas 
349393c00Smartynas /*-
449393c00Smartynas  * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
549393c00Smartynas  * All rights reserved.
649393c00Smartynas  *
749393c00Smartynas  * Redistribution and use in source and binary forms, with or without
849393c00Smartynas  * modification, are permitted provided that the following conditions
949393c00Smartynas  * are met:
1049393c00Smartynas  * 1. Redistributions of source code must retain the above copyright
1149393c00Smartynas  *    notice, this list of conditions and the following disclaimer.
1249393c00Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
1349393c00Smartynas  *    notice, this list of conditions and the following disclaimer in the
1449393c00Smartynas  *    documentation and/or other materials provided with the distribution.
1549393c00Smartynas  *
1649393c00Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1749393c00Smartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1849393c00Smartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1949393c00Smartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2049393c00Smartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2149393c00Smartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2249393c00Smartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2349393c00Smartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2449393c00Smartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2549393c00Smartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2649393c00Smartynas  * SUCH DAMAGE.
2749393c00Smartynas  */
2849393c00Smartynas 
2949393c00Smartynas #include <fenv.h>
3049393c00Smartynas #include <math.h>
3149393c00Smartynas 
3249393c00Smartynas #ifndef type
3349393c00Smartynas #define type		long double
3449393c00Smartynas #define roundit		rintl
3549393c00Smartynas #define dtype		long
3649393c00Smartynas #define fn		lrintl
3749393c00Smartynas #endif
3849393c00Smartynas 
3949393c00Smartynas /*
4049393c00Smartynas  * C99 says we should not raise a spurious inexact exception when an
4149393c00Smartynas  * invalid exception is raised.  Unfortunately, the set of inputs
4249393c00Smartynas  * that overflows depends on the rounding mode when 'dtype' has more
4349393c00Smartynas  * significant bits than 'type'.  Hence, we bend over backwards for the
4449393c00Smartynas  * sake of correctness; an MD implementation could be more efficient.
4549393c00Smartynas  */
4649393c00Smartynas dtype
fn(type x)4749393c00Smartynas fn(type x)
4849393c00Smartynas {
4949393c00Smartynas 	fenv_t env;
5049393c00Smartynas 	dtype d;
5149393c00Smartynas 
5249393c00Smartynas 	feholdexcept(&env);
5349393c00Smartynas 	d = (dtype)roundit(x);
5449393c00Smartynas 	if (fetestexcept(FE_INVALID))
5549393c00Smartynas 		feclearexcept(FE_INEXACT);
5649393c00Smartynas 	feupdateenv(&env);
5749393c00Smartynas 	return (d);
5849393c00Smartynas }
59