105a0b428SJohn Marino /* $OpenBSD: s_lround.c,v 1.7 2013/07/03 04:46:36 espie Exp $ */
205a0b428SJohn Marino /* $NetBSD: lround.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */
305a0b428SJohn Marino
405a0b428SJohn Marino /*-
505a0b428SJohn Marino * Copyright (c) 2004
605a0b428SJohn Marino * Matthias Drochner. All rights reserved.
705a0b428SJohn Marino *
805a0b428SJohn Marino * Redistribution and use in source and binary forms, with or without
905a0b428SJohn Marino * modification, are permitted provided that the following conditions
1005a0b428SJohn Marino * are met:
1105a0b428SJohn Marino * 1. Redistributions of source code must retain the above copyright
1205a0b428SJohn Marino * notice, this list of conditions and the following disclaimer.
1305a0b428SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1405a0b428SJohn Marino * notice, this list of conditions and the following disclaimer in the
1505a0b428SJohn Marino * documentation and/or other materials provided with the distribution.
1605a0b428SJohn Marino *
1705a0b428SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1805a0b428SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1905a0b428SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2005a0b428SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2105a0b428SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2205a0b428SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2305a0b428SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2405a0b428SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2505a0b428SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2605a0b428SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2705a0b428SJohn Marino * SUCH DAMAGE.
2805a0b428SJohn Marino */
2905a0b428SJohn Marino
3005a0b428SJohn Marino #include <sys/types.h>
3105a0b428SJohn Marino #include <sys/limits.h>
3205a0b428SJohn Marino #include <float.h>
3305a0b428SJohn Marino #include <math.h>
3405a0b428SJohn Marino #include <ieeefp.h>
3505a0b428SJohn Marino #include <machine/ieee.h>
3605a0b428SJohn Marino #include "math_private.h"
3705a0b428SJohn Marino
3805a0b428SJohn Marino #ifndef LROUNDNAME
3905a0b428SJohn Marino #define LROUNDNAME lround
4005a0b428SJohn Marino #define RESTYPE long int
4105a0b428SJohn Marino #define RESTYPE_MIN LONG_MIN
4205a0b428SJohn Marino #define RESTYPE_MAX LONG_MAX
4305a0b428SJohn Marino #endif
4405a0b428SJohn Marino
45*74b7c7a8SJohn Marino #define RESTYPE_BITS (int)(sizeof(RESTYPE) * 8)
4605a0b428SJohn Marino
4705a0b428SJohn Marino RESTYPE
LROUNDNAME(double x)4805a0b428SJohn Marino LROUNDNAME(double x)
4905a0b428SJohn Marino {
5005a0b428SJohn Marino u_int32_t i0, i1;
5105a0b428SJohn Marino int e, s, shift;
5205a0b428SJohn Marino RESTYPE res;
5305a0b428SJohn Marino
5405a0b428SJohn Marino GET_HIGH_WORD(i0, x);
5505a0b428SJohn Marino e = i0 >> DBL_FRACHBITS;
5605a0b428SJohn Marino s = e >> DBL_EXPBITS;
5705a0b428SJohn Marino e = (e & 0x7ff) - DBL_EXP_BIAS;
5805a0b428SJohn Marino
5905a0b428SJohn Marino /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */
6005a0b428SJohn Marino if (e < -1)
6105a0b428SJohn Marino return (0);
6205a0b428SJohn Marino /* 1.0 x 2^31 (or 2^63) is already too large */
63*74b7c7a8SJohn Marino if (e >= RESTYPE_BITS - 1)
6405a0b428SJohn Marino return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */
6505a0b428SJohn Marino
6605a0b428SJohn Marino /* >= 2^52 is already an exact integer */
6705a0b428SJohn Marino if (e < DBL_FRACBITS) {
6805a0b428SJohn Marino /* add 0.5, extraction below will truncate */
6905a0b428SJohn Marino x += (s ? -0.5 : 0.5);
7005a0b428SJohn Marino }
7105a0b428SJohn Marino
7205a0b428SJohn Marino EXTRACT_WORDS(i0, i1, x);
7305a0b428SJohn Marino e = ((i0 >> DBL_FRACHBITS) & 0x7ff) - DBL_EXP_BIAS;
7405a0b428SJohn Marino i0 &= 0xfffff;
7505a0b428SJohn Marino i0 |= (1 << DBL_FRACHBITS);
7605a0b428SJohn Marino
7705a0b428SJohn Marino shift = e - DBL_FRACBITS;
7805a0b428SJohn Marino if (shift >=0)
7905a0b428SJohn Marino res = (shift < RESTYPE_BITS ? (RESTYPE)i1 << shift : 0);
8005a0b428SJohn Marino else
8105a0b428SJohn Marino res = (shift > -RESTYPE_BITS ? (RESTYPE)i1 >> -shift : 0);
8205a0b428SJohn Marino shift += 32;
8305a0b428SJohn Marino if (shift >=0)
8405a0b428SJohn Marino res |= (shift < RESTYPE_BITS ? (RESTYPE)i0 << shift : 0);
8505a0b428SJohn Marino else
8605a0b428SJohn Marino res |= (shift > -RESTYPE_BITS ? (RESTYPE)i0 >> -shift : 0);
8705a0b428SJohn Marino
8805a0b428SJohn Marino return (s ? -res : res);
8905a0b428SJohn Marino }
9005a0b428SJohn Marino
9105a0b428SJohn Marino #if LDBL_MANT_DIG == DBL_MANT_DIG
9205a0b428SJohn Marino __strong_alias(lroundl, lround);
9305a0b428SJohn Marino #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */
94