xref: /freebsd/lib/msun/src/s_lround.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
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 <sys/limits.h>
3010c9ffa4SDavid Schultz #include <fenv.h>
3110c9ffa4SDavid Schultz #include <math.h>
3210c9ffa4SDavid Schultz 
3310c9ffa4SDavid Schultz #ifndef type
3410c9ffa4SDavid Schultz #define type		double
3510c9ffa4SDavid Schultz #define	roundit		round
3610c9ffa4SDavid Schultz #define dtype		long
3710c9ffa4SDavid Schultz #define	DTYPE_MIN	LONG_MIN
3810c9ffa4SDavid Schultz #define	DTYPE_MAX	LONG_MAX
3910c9ffa4SDavid Schultz #define	fn		lround
4010c9ffa4SDavid Schultz #endif
4110c9ffa4SDavid Schultz 
4210c9ffa4SDavid Schultz /*
4310c9ffa4SDavid Schultz  * If type has more precision than dtype, the endpoints dtype_(min|max) are
4410c9ffa4SDavid Schultz  * of the form xxx.5; they are "out of range" because lround() rounds away
4510c9ffa4SDavid Schultz  * from 0.  On the other hand, if type has less precision than dtype, then
4610c9ffa4SDavid Schultz  * all values that are out of range are integral, so we might as well assume
47fc879867SDavid Schultz  * that everything is in range.  At compile time, INRANGE(x) should reduce to
48fc879867SDavid Schultz  * two floating-point comparisons in the former case, or TRUE otherwise.
4910c9ffa4SDavid Schultz  */
50df3b437cSDimitry Andric static const type type_min = (type)DTYPE_MIN;
51df3b437cSDimitry Andric static const type type_max = (type)DTYPE_MAX;
520bcd49c1SDimitry Andric static const type dtype_min = (type)DTYPE_MIN - 0.5;
530bcd49c1SDimitry Andric static const type dtype_max = (type)DTYPE_MAX + 0.5;
54df3b437cSDimitry Andric #define	INRANGE(x)	(dtype_max - type_max != 0.5 || \
5510c9ffa4SDavid Schultz 			 ((x) > dtype_min && (x) < dtype_max))
5610c9ffa4SDavid Schultz 
5710c9ffa4SDavid Schultz dtype
fn(type x)5810c9ffa4SDavid Schultz fn(type x)
5910c9ffa4SDavid Schultz {
6010c9ffa4SDavid Schultz 
6110c9ffa4SDavid Schultz 	if (INRANGE(x)) {
6210c9ffa4SDavid Schultz 		x = roundit(x);
6310c9ffa4SDavid Schultz 		return ((dtype)x);
6410c9ffa4SDavid Schultz 	} else {
6510c9ffa4SDavid Schultz 		feraiseexcept(FE_INVALID);
6610c9ffa4SDavid Schultz 		return (DTYPE_MAX);
6710c9ffa4SDavid Schultz 	}
6810c9ffa4SDavid Schultz }
69