xref: /openbsd-src/lib/libm/src/s_lrint.c (revision e44725a8dd99f82f94f37ecff5c0e710c4dba97e)
1*e44725a8Skettenis /*	$OpenBSD: s_lrint.c,v 1.12 2021/10/14 21:30:00 kettenis Exp $	*/
2d7c6e841Skettenis 
3d7c6e841Skettenis /*-
4*e44725a8Skettenis  * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5*e44725a8Skettenis  * All rights reserved.
6d7c6e841Skettenis  *
7d7c6e841Skettenis  * Redistribution and use in source and binary forms, with or without
8d7c6e841Skettenis  * modification, are permitted provided that the following conditions
9d7c6e841Skettenis  * are met:
10d7c6e841Skettenis  * 1. Redistributions of source code must retain the above copyright
11d7c6e841Skettenis  *    notice, this list of conditions and the following disclaimer.
12d7c6e841Skettenis  * 2. Redistributions in binary form must reproduce the above copyright
13d7c6e841Skettenis  *    notice, this list of conditions and the following disclaimer in the
14d7c6e841Skettenis  *    documentation and/or other materials provided with the distribution.
15d7c6e841Skettenis  *
16d7c6e841Skettenis  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17d7c6e841Skettenis  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d7c6e841Skettenis  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d7c6e841Skettenis  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20d7c6e841Skettenis  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d7c6e841Skettenis  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d7c6e841Skettenis  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d7c6e841Skettenis  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d7c6e841Skettenis  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d7c6e841Skettenis  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d7c6e841Skettenis  * SUCH DAMAGE.
27d7c6e841Skettenis  */
28d7c6e841Skettenis 
29*e44725a8Skettenis #include <fenv.h>
30d7c6e841Skettenis #include <math.h>
3149393c00Smartynas 
32*e44725a8Skettenis #ifndef type
33*e44725a8Skettenis #define type		double
34*e44725a8Skettenis #define roundit		rint
35*e44725a8Skettenis #define dtype		long
36*e44725a8Skettenis #define fn		lrint
37d7c6e841Skettenis #endif
38d7c6e841Skettenis 
39*e44725a8Skettenis /*
40*e44725a8Skettenis  * C99 says we should not raise a spurious inexact exception when an
41*e44725a8Skettenis  * invalid exception is raised.  Unfortunately, the set of inputs
42*e44725a8Skettenis  * that overflows depends on the rounding mode when 'dtype' has more
43*e44725a8Skettenis  * significant bits than 'type'.  Hence, we bend over backwards for the
44*e44725a8Skettenis  * sake of correctness; an MD implementation could be more efficient.
45*e44725a8Skettenis  */
46*e44725a8Skettenis dtype
fn(type x)47*e44725a8Skettenis fn(type x)
48d7c6e841Skettenis {
49*e44725a8Skettenis 	fenv_t env;
50*e44725a8Skettenis 	dtype d;
51d7c6e841Skettenis 
52*e44725a8Skettenis 	feholdexcept(&env);
53*e44725a8Skettenis 	d = (dtype)roundit(x);
54*e44725a8Skettenis 	if (fetestexcept(FE_INVALID))
55*e44725a8Skettenis 		feclearexcept(FE_INEXACT);
56*e44725a8Skettenis 	feupdateenv(&env);
57*e44725a8Skettenis 	return (d);
58d7c6e841Skettenis }
59*e44725a8Skettenis DEF_STD(fn);
60*e44725a8Skettenis LDBL_MAYBE_CLONE(fn);
61