1*7be14faeStb /* $OpenBSD: s_lroundl.c,v 1.5 2021/05/31 17:39:24 tb 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 <sys/limits.h>
3049393c00Smartynas #include <fenv.h>
3149393c00Smartynas #include <math.h>
3249393c00Smartynas
3349393c00Smartynas #ifndef type
3449393c00Smartynas #define type long double
3549393c00Smartynas #define roundit roundl
3649393c00Smartynas #define dtype long
3749393c00Smartynas #define DTYPE_MIN LONG_MIN
3849393c00Smartynas #define DTYPE_MAX LONG_MAX
3949393c00Smartynas #define fn lroundl
4049393c00Smartynas #endif
4149393c00Smartynas
4249393c00Smartynas /*
4349393c00Smartynas * If type has more precision than dtype, the endpoints dtype_(min|max) are
4449393c00Smartynas * of the form xxx.5; they are "out of range" because lround() rounds away
4549393c00Smartynas * from 0. On the other hand, if type has less precision than dtype, then
4649393c00Smartynas * all values that are out of range are integral, so we might as well assume
4749393c00Smartynas * that everything is in range. At compile time, INRANGE(x) should reduce to
4849393c00Smartynas * two floating-point comparisons in the former case, or TRUE otherwise.
4949393c00Smartynas */
50*7be14faeStb static const type dtype_min = DTYPE_MIN - 0.5;
51*7be14faeStb static const type dtype_max = DTYPE_MAX + 0.5;
52*7be14faeStb #define INRANGE(x) (dtype_max - DTYPE_MAX != 0.5 || \
5349393c00Smartynas ((x) > dtype_min && (x) < dtype_max))
5449393c00Smartynas
5549393c00Smartynas dtype
fn(type x)5649393c00Smartynas fn(type x)
5749393c00Smartynas {
5849393c00Smartynas
5949393c00Smartynas if (INRANGE(x)) {
6049393c00Smartynas x = roundit(x);
6149393c00Smartynas return ((dtype)x);
6249393c00Smartynas } else {
6349393c00Smartynas feraiseexcept(FE_INVALID);
6449393c00Smartynas return (DTYPE_MAX);
6549393c00Smartynas }
6649393c00Smartynas }
67