1*464b3254Schristos /*-
2*464b3254Schristos * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
3*464b3254Schristos * All rights reserved.
4*464b3254Schristos *
5*464b3254Schristos * Redistribution and use in source and binary forms, with or without
6*464b3254Schristos * modification, are permitted provided that the following conditions
7*464b3254Schristos * are met:
8*464b3254Schristos * 1. Redistributions of source code must retain the above copyright
9*464b3254Schristos * notice, this list of conditions and the following disclaimer.
10*464b3254Schristos * 2. Redistributions in binary form must reproduce the above copyright
11*464b3254Schristos * notice, this list of conditions and the following disclaimer in the
12*464b3254Schristos * documentation and/or other materials provided with the distribution.
13*464b3254Schristos *
14*464b3254Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*464b3254Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*464b3254Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*464b3254Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*464b3254Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*464b3254Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*464b3254Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*464b3254Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*464b3254Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*464b3254Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*464b3254Schristos * SUCH DAMAGE.
25*464b3254Schristos */
26*464b3254Schristos
27*464b3254Schristos #include "namespace.h"
28*464b3254Schristos
29*464b3254Schristos #include <sys/cdefs.h>
30*464b3254Schristos #include <limits.h>
31*464b3254Schristos #include <fenv.h>
32*464b3254Schristos #include <math.h>
33*464b3254Schristos
34*464b3254Schristos #ifndef stype
35*464b3254Schristos #ifdef __FreeBSD__
36*464b3254Schristos __FBSDID("$FreeBSD: head/lib/msun/src/s_lround.c 144770 2005-04-08 00:52:16Z das $");
37*464b3254Schristos #else
38*464b3254Schristos __RCSID("$NetBSD: s_lround.c,v 1.1 2017/05/06 18:03:24 christos Exp $");
39*464b3254Schristos #endif
40*464b3254Schristos #define stype double
41*464b3254Schristos #define roundit round
42*464b3254Schristos #define dtype long
43*464b3254Schristos #define DTYPE_MIN LONG_MIN
44*464b3254Schristos #define DTYPE_MAX LONG_MAX
45*464b3254Schristos #define fn lround
46*464b3254Schristos #endif
47*464b3254Schristos
48*464b3254Schristos /*
49*464b3254Schristos * If stype has more precision than dtype, the endpoints dtype_(min|max) are
50*464b3254Schristos * of the form xxx.5; they are "out of range" because lround() rounds away
51*464b3254Schristos * from 0. On the other hand, if stype has less precision than dtype, then
52*464b3254Schristos * all values that are out of range are integral, so we might as well assume
53*464b3254Schristos * that everything is in range. At compile time, INRANGE(x) should reduce to
54*464b3254Schristos * two floating-point comparisons in the former case, or TRUE otherwise.
55*464b3254Schristos */
56*464b3254Schristos static const stype dtype_min = DTYPE_MIN - 0.5;
57*464b3254Schristos static const stype dtype_max = DTYPE_MAX + 0.5;
58*464b3254Schristos #define INRANGE(x) (dtype_max - DTYPE_MAX != 0.5 || \
59*464b3254Schristos ((x) > dtype_min && (x) < dtype_max))
60*464b3254Schristos
61*464b3254Schristos dtype
fn(stype x)62*464b3254Schristos fn(stype x)
63*464b3254Schristos {
64*464b3254Schristos
65*464b3254Schristos if (INRANGE(x)) {
66*464b3254Schristos x = roundit(x);
67*464b3254Schristos return ((dtype)x);
68*464b3254Schristos } else {
69*464b3254Schristos feraiseexcept(FE_INVALID);
70*464b3254Schristos return (DTYPE_MAX);
71*464b3254Schristos }
72*464b3254Schristos }
73