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