[BACK]Return to modf_ieee754.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/modf_ieee754.c, Revision 1.4

1.4     ! he          1: /* $NetBSD: modf_ieee754.c,v 1.3 2010/01/27 14:10:41 drochner Exp $ */
1.1       kleink      2:
                      3: /*
                      4:  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Author: Chris G. Demetriou
                      8:  *
                      9:  * Permission to use, copy, modify and distribute this software and
                     10:  * its documentation is hereby granted, provided that both the copyright
                     11:  * notice and this permission notice appear in all copies of the
                     12:  * software, derivative works or modified versions, and any portions
                     13:  * thereof, and that both notices appear in supporting documentation.
                     14:  *
                     15:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     16:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
                     17:  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
                     18:  *
                     19:  * Carnegie Mellon requests users of this software to return to
                     20:  *
                     21:  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
                     22:  *  School of Computer Science
                     23:  *  Carnegie Mellon University
                     24:  *  Pittsburgh PA 15213-3890
                     25:  *
                     26:  * any improvements or extensions that they make and grant Carnegie the
                     27:  * rights to redistribute these changes.
                     28:  */
                     29:
                     30: #include <sys/types.h>
                     31: #include <machine/ieee.h>
                     32: #include <errno.h>
                     33: #include <math.h>
                     34:
                     35: /*
                     36:  * double modf(double val, double *iptr)
                     37:  * returns: f and i such that |f| < 1.0, (f + i) = val, and
                     38:  *     sign(f) == sign(i) == sign(val).
                     39:  *
                     40:  * Beware signedness when doing subtraction, and also operand size!
                     41:  */
                     42: double
                     43: modf(double val, double *iptr)
                     44: {
1.2       kleink     45:        union ieee_double_u u, v;
1.1       kleink     46:        u_int64_t frac;
                     47:
                     48:        /*
1.3       drochner   49:         * If input is +/-Inf or NaN, return +/-0 or NaN.
1.1       kleink     50:         */
1.2       kleink     51:        u.dblu_d = val;
1.3       drochner   52:        if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) {
                     53:                *iptr = u.dblu_d;
                     54:                return (0.0 / u.dblu_d);
                     55:        }
1.1       kleink     56:
                     57:        /*
                     58:         * If input can't have a fractional part, return
                     59:         * (appropriately signed) zero, and make i be the input.
                     60:         */
1.2       kleink     61:        if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
                     62:                *iptr = u.dblu_d;
                     63:                v.dblu_d = 0.0;
                     64:                v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
                     65:                return (v.dblu_d);
1.1       kleink     66:        }
                     67:
                     68:        /*
                     69:         * If |input| < 1.0, return it, and set i to the appropriately
                     70:         * signed zero.
                     71:         */
1.2       kleink     72:        if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) {
                     73:                v.dblu_d = 0.0;
                     74:                v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
                     75:                *iptr = v.dblu_d;
                     76:                return (u.dblu_d);
1.1       kleink     77:        }
                     78:
                     79:        /*
                     80:         * There can be a fractional part of the input.
                     81:         * If you look at the math involved for a few seconds, it's
                     82:         * plain to see that the integral part is the input, with the
                     83:         * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
                     84:         * the fractional part is the part with the rest of the
                     85:         * bits zeroed.  Just zeroing the high bits to get the
                     86:         * fractional part would yield a fraction in need of
                     87:         * normalization.  Therefore, we take the easy way out, and
                     88:         * just use subtraction to get the fractional part.
                     89:         */
1.2       kleink     90:        v.dblu_d = u.dblu_d;
1.1       kleink     91:        /* Zero the low bits of the fraction, the sleazy way. */
1.2       kleink     92:        frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl;
                     93:        frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
                     94:        frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
1.4     ! he         95:        v.dblu_dbl.dbl_fracl = (u_int) (frac & 0xffffffffULL);
        !            96:        v.dblu_dbl.dbl_frach = (u_int) (frac >> 32);
1.2       kleink     97:        *iptr = v.dblu_d;
                     98:
                     99:        u.dblu_d -= v.dblu_d;
                    100:        u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign;
                    101:        return (u.dblu_d);
1.1       kleink    102: }

CVSweb <webmaster@jp.NetBSD.org>