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