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