1 /* $NetBSD: compat_modf_ieee754.c,v 1.3 2010/01/27 14:10:41 drochner Exp $ */ 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 #ifdef USE_LIBM 31 #include "s_modf.c" 32 #else 33 #include <sys/types.h> 34 #include <machine/ieee.h> 35 #include <errno.h> 36 #include <math.h> 37 38 /* 39 * double modf(double val, double *iptr) 40 * returns: f and i such that |f| < 1.0, (f + i) = val, and 41 * sign(f) == sign(i) == sign(val). 42 * 43 * Beware signedness when doing subtraction, and also operand size! 44 */ 45 double 46 modf(double val, double *iptr) 47 { 48 union ieee_double_u u, v; 49 u_int64_t frac; 50 51 /* 52 * If input is +/-Inf or NaN, return +/-0 or NaN. 53 */ 54 u.dblu_d = val; 55 if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) { 56 *iptr = u.dblu_d; 57 return (0.0 / u.dblu_d); 58 } 59 60 /* 61 * If input can't have a fractional part, return 62 * (appropriately signed) zero, and make i be the input. 63 */ 64 if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) { 65 *iptr = u.dblu_d; 66 v.dblu_d = 0.0; 67 v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign; 68 return (v.dblu_d); 69 } 70 71 /* 72 * If |input| < 1.0, return it, and set i to the appropriately 73 * signed zero. 74 */ 75 if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) { 76 v.dblu_d = 0.0; 77 v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign; 78 *iptr = v.dblu_d; 79 return (u.dblu_d); 80 } 81 82 /* 83 * There can be a fractional part of the input. 84 * If you look at the math involved for a few seconds, it's 85 * plain to see that the integral part is the input, with the 86 * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed, 87 * the fractional part is the part with the rest of the 88 * bits zeroed. Just zeroing the high bits to get the 89 * fractional part would yield a fraction in need of 90 * normalization. Therefore, we take the easy way out, and 91 * just use subtraction to get the fractional part. 92 */ 93 v.dblu_d = u.dblu_d; 94 /* Zero the low bits of the fraction, the sleazy way. */ 95 frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl; 96 frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS); 97 frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS); 98 v.dblu_dbl.dbl_fracl = (unsigned int)frac & 0xffffffff; 99 v.dblu_dbl.dbl_frach = (unsigned int)(frac >> 32); 100 *iptr = v.dblu_d; 101 102 u.dblu_d -= v.dblu_d; 103 u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign; 104 return (u.dblu_d); 105 } 106 #endif 107