105a0b428SJohn Marino /* $OpenBSD: s_lroundf.c,v 1.2 2011/04/17 13:59:54 martynas Exp $ */
205a0b428SJohn Marino /* $NetBSD: lroundf.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 <math.h>
3105a0b428SJohn Marino #include <sys/types.h>
3205a0b428SJohn Marino #include <sys/limits.h>
3305a0b428SJohn Marino #include <ieeefp.h>
3405a0b428SJohn Marino #include <machine/ieee.h>
3505a0b428SJohn Marino #include "math_private.h"
3605a0b428SJohn Marino
3705a0b428SJohn Marino #ifndef LROUNDNAME
3805a0b428SJohn Marino #define LROUNDNAME lroundf
3905a0b428SJohn Marino #define RESTYPE long int
4005a0b428SJohn Marino #define RESTYPE_MIN LONG_MIN
4105a0b428SJohn Marino #define RESTYPE_MAX LONG_MAX
4205a0b428SJohn Marino #endif
4305a0b428SJohn Marino
44*74b7c7a8SJohn Marino #define RESTYPE_BITS (int)(sizeof(RESTYPE) * 8)
4505a0b428SJohn Marino
4605a0b428SJohn Marino RESTYPE
LROUNDNAME(float x)4705a0b428SJohn Marino LROUNDNAME(float x)
4805a0b428SJohn Marino {
4905a0b428SJohn Marino u_int32_t i0;
5005a0b428SJohn Marino int e, s, shift;
5105a0b428SJohn Marino RESTYPE res;
5205a0b428SJohn Marino
5305a0b428SJohn Marino GET_FLOAT_WORD(i0, x);
5405a0b428SJohn Marino e = i0 >> SNG_FRACBITS;
5505a0b428SJohn Marino s = e >> SNG_EXPBITS;
5605a0b428SJohn Marino e = (e & 0xff) - SNG_EXP_BIAS;
5705a0b428SJohn Marino
5805a0b428SJohn Marino /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */
5905a0b428SJohn Marino if (e < -1)
6005a0b428SJohn Marino return (0);
6105a0b428SJohn Marino /* 1.0 x 2^31 (or 2^63) is already too large */
62*74b7c7a8SJohn Marino if (e >= RESTYPE_BITS - 1)
6305a0b428SJohn Marino return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */
6405a0b428SJohn Marino
6505a0b428SJohn Marino /* >= 2^23 is already an exact integer */
6605a0b428SJohn Marino if (e < SNG_FRACBITS) {
6705a0b428SJohn Marino /* add 0.5, extraction below will truncate */
6805a0b428SJohn Marino x += (s ? -0.5 : 0.5);
6905a0b428SJohn Marino }
7005a0b428SJohn Marino
7105a0b428SJohn Marino GET_FLOAT_WORD(i0, x);
7205a0b428SJohn Marino e = ((i0 >> SNG_FRACBITS) & 0xff) - SNG_EXP_BIAS;
7305a0b428SJohn Marino i0 &= 0x7fffff;
7405a0b428SJohn Marino i0 |= (1 << SNG_FRACBITS);
7505a0b428SJohn Marino
7605a0b428SJohn Marino shift = e - SNG_FRACBITS;
7705a0b428SJohn Marino if (shift >=0)
7805a0b428SJohn Marino res = (shift < RESTYPE_BITS ? (RESTYPE)i0 << shift : 0);
7905a0b428SJohn Marino else
8005a0b428SJohn Marino res = (shift > -RESTYPE_BITS ? (RESTYPE)i0 >> -shift : 0);
8105a0b428SJohn Marino
8205a0b428SJohn Marino return (s ? -res : res);
8305a0b428SJohn Marino }
84