1*d37b9034Smartynas /* $OpenBSD: s_lroundf.c,v 1.2 2011/04/17 13:59:54 martynas Exp $ */
22d2c90e6Smartynas /* $NetBSD: lroundf.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 <math.h>
312d2c90e6Smartynas #include <sys/types.h>
322d2c90e6Smartynas #include <sys/limits.h>
332d2c90e6Smartynas #include <ieeefp.h>
342d2c90e6Smartynas #include <machine/ieee.h>
352d2c90e6Smartynas #include "math_private.h"
362d2c90e6Smartynas
372d2c90e6Smartynas #ifndef LROUNDNAME
382d2c90e6Smartynas #define LROUNDNAME lroundf
392d2c90e6Smartynas #define RESTYPE long int
402d2c90e6Smartynas #define RESTYPE_MIN LONG_MIN
412d2c90e6Smartynas #define RESTYPE_MAX LONG_MAX
422d2c90e6Smartynas #endif
432d2c90e6Smartynas
442d2c90e6Smartynas #define RESTYPE_BITS (sizeof(RESTYPE) * 8)
452d2c90e6Smartynas
462d2c90e6Smartynas RESTYPE
LROUNDNAME(float x)472d2c90e6Smartynas LROUNDNAME(float x)
482d2c90e6Smartynas {
492d2c90e6Smartynas u_int32_t i0;
502d2c90e6Smartynas int e, s, shift;
512d2c90e6Smartynas RESTYPE res;
522d2c90e6Smartynas
532d2c90e6Smartynas GET_FLOAT_WORD(i0, x);
542d2c90e6Smartynas e = i0 >> SNG_FRACBITS;
552d2c90e6Smartynas s = e >> SNG_EXPBITS;
562d2c90e6Smartynas e = (e & 0xff) - SNG_EXP_BIAS;
572d2c90e6Smartynas
582d2c90e6Smartynas /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */
592d2c90e6Smartynas if (e < -1)
602d2c90e6Smartynas return (0);
612d2c90e6Smartynas /* 1.0 x 2^31 (or 2^63) is already too large */
622d2c90e6Smartynas if (e >= (int)RESTYPE_BITS - 1)
632d2c90e6Smartynas return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */
642d2c90e6Smartynas
652d2c90e6Smartynas /* >= 2^23 is already an exact integer */
662d2c90e6Smartynas if (e < SNG_FRACBITS) {
672d2c90e6Smartynas /* add 0.5, extraction below will truncate */
682d2c90e6Smartynas x += (s ? -0.5 : 0.5);
692d2c90e6Smartynas }
702d2c90e6Smartynas
712d2c90e6Smartynas GET_FLOAT_WORD(i0, x);
722d2c90e6Smartynas e = ((i0 >> SNG_FRACBITS) & 0xff) - SNG_EXP_BIAS;
732d2c90e6Smartynas i0 &= 0x7fffff;
742d2c90e6Smartynas i0 |= (1 << SNG_FRACBITS);
752d2c90e6Smartynas
762d2c90e6Smartynas shift = e - SNG_FRACBITS;
772d2c90e6Smartynas if (shift >=0)
78*d37b9034Smartynas res = (shift < RESTYPE_BITS ? (RESTYPE)i0 << shift : 0);
792d2c90e6Smartynas else
80*d37b9034Smartynas res = (shift > -RESTYPE_BITS ? (RESTYPE)i0 >> -shift : 0);
812d2c90e6Smartynas
822d2c90e6Smartynas return (s ? -res : res);
832d2c90e6Smartynas }
84