xref: /openbsd-src/lib/libm/src/s_lround.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_lround.c,v 1.8 2016/09/12 19:47:02 guenther Exp $	*/
22d2c90e6Smartynas /* $NetBSD: lround.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */
32d2c90e6Smartynas 
42d2c90e6Smartynas /*-
52d2c90e6Smartynas  * Copyright (c) 2004
62d2c90e6Smartynas  *	Matthias Drochner. All rights reserved.
72d2c90e6Smartynas  *
82d2c90e6Smartynas  * Redistribution and use in source and binary forms, with or without
92d2c90e6Smartynas  * modification, are permitted provided that the following conditions
102d2c90e6Smartynas  * are met:
112d2c90e6Smartynas  * 1. Redistributions of source code must retain the above copyright
122d2c90e6Smartynas  *    notice, this list of conditions and the following disclaimer.
132d2c90e6Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
142d2c90e6Smartynas  *    notice, this list of conditions and the following disclaimer in the
152d2c90e6Smartynas  *    documentation and/or other materials provided with the distribution.
162d2c90e6Smartynas  *
172d2c90e6Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
182d2c90e6Smartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192d2c90e6Smartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202d2c90e6Smartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
212d2c90e6Smartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222d2c90e6Smartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232d2c90e6Smartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242d2c90e6Smartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252d2c90e6Smartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262d2c90e6Smartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272d2c90e6Smartynas  * SUCH DAMAGE.
282d2c90e6Smartynas  */
292d2c90e6Smartynas 
302d2c90e6Smartynas #include <sys/types.h>
312d2c90e6Smartynas #include <sys/limits.h>
3249393c00Smartynas #include <float.h>
332d2c90e6Smartynas #include <math.h>
342d2c90e6Smartynas #include <ieeefp.h>
352d2c90e6Smartynas #include <machine/ieee.h>
362d2c90e6Smartynas #include "math_private.h"
372d2c90e6Smartynas 
382d2c90e6Smartynas #ifndef LROUNDNAME
392d2c90e6Smartynas #define LROUNDNAME lround
402d2c90e6Smartynas #define RESTYPE long int
412d2c90e6Smartynas #define RESTYPE_MIN LONG_MIN
422d2c90e6Smartynas #define RESTYPE_MAX LONG_MAX
432d2c90e6Smartynas #endif
442d2c90e6Smartynas 
452d2c90e6Smartynas #define RESTYPE_BITS (sizeof(RESTYPE) * 8)
462d2c90e6Smartynas 
472d2c90e6Smartynas RESTYPE
LROUNDNAME(double x)482d2c90e6Smartynas LROUNDNAME(double x)
492d2c90e6Smartynas {
502d2c90e6Smartynas 	u_int32_t i0, i1;
512d2c90e6Smartynas 	int e, s, shift;
522d2c90e6Smartynas 	RESTYPE res;
532d2c90e6Smartynas 
542d2c90e6Smartynas 	GET_HIGH_WORD(i0, x);
55c6b9fef0Smartynas 	e = i0 >> DBL_FRACHBITS;
562d2c90e6Smartynas 	s = e >> DBL_EXPBITS;
572d2c90e6Smartynas 	e = (e & 0x7ff) - DBL_EXP_BIAS;
582d2c90e6Smartynas 
592d2c90e6Smartynas 	/* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */
602d2c90e6Smartynas 	if (e < -1)
612d2c90e6Smartynas 		return (0);
622d2c90e6Smartynas 	/* 1.0 x 2^31 (or 2^63) is already too large */
632d2c90e6Smartynas 	if (e >= (int)RESTYPE_BITS - 1)
642d2c90e6Smartynas 		return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */
652d2c90e6Smartynas 
662d2c90e6Smartynas 	/* >= 2^52 is already an exact integer */
672d2c90e6Smartynas 	if (e < DBL_FRACBITS) {
682d2c90e6Smartynas 		/* add 0.5, extraction below will truncate */
692d2c90e6Smartynas 		x += (s ? -0.5 : 0.5);
702d2c90e6Smartynas 	}
712d2c90e6Smartynas 
722d2c90e6Smartynas 	EXTRACT_WORDS(i0, i1, x);
73c6b9fef0Smartynas 	e = ((i0 >> DBL_FRACHBITS) & 0x7ff) - DBL_EXP_BIAS;
742d2c90e6Smartynas 	i0 &= 0xfffff;
75c6b9fef0Smartynas 	i0 |= (1 << DBL_FRACHBITS);
762d2c90e6Smartynas 
772d2c90e6Smartynas 	shift = e - DBL_FRACBITS;
782d2c90e6Smartynas 	if (shift >=0)
79d37b9034Smartynas 		res = (shift < RESTYPE_BITS ? (RESTYPE)i1 << shift : 0);
802d2c90e6Smartynas 	else
81d37b9034Smartynas 		res = (shift > -RESTYPE_BITS ? (RESTYPE)i1 >> -shift : 0);
822d2c90e6Smartynas 	shift += 32;
832d2c90e6Smartynas 	if (shift >=0)
84d37b9034Smartynas 		res |= (shift < RESTYPE_BITS ? (RESTYPE)i0 << shift : 0);
852d2c90e6Smartynas 	else
86d37b9034Smartynas 		res |= (shift > -RESTYPE_BITS ? (RESTYPE)i0 >> -shift : 0);
872d2c90e6Smartynas 
882d2c90e6Smartynas 	return (s ? -res : res);
892d2c90e6Smartynas }
90*2f2c0062Sguenther DEF_STD(LROUNDNAME);
91*2f2c0062Sguenther LDBL_MAYBE_UNUSED_CLONE(LROUNDNAME);
92