2 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 * Author: Chris G. Demetriou
7 * Permission to use, copy, modify and distribute this software and
8 * its documentation is hereby granted, provided that both the copyright
9 * notice and this permission notice appear in all copies of the
10 * software, derivative works or modified versions, and any portions
11 * thereof, and that both notices appear in supporting documentation.
13 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
14 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
15 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 * Carnegie Mellon requests users of this software to return to
19 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
20 * School of Computer Science
21 * Carnegie Mellon University
22 * Pittsburgh PA 15213-3890
24 * any improvements or extensions that they make and grant Carnegie the
25 * rights to redistribute these changes.
27 * $NetBSD: modf.c,v 1.1 1995/02/10 17:50:25 cgd Exp $
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
36 #include <machine/ieee.h>
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).
43 * Beware signedness when doing subtraction, and also operand size!
56 * If input is Inf or NaN, return it and leave i alone.
59 if (u.s.dbl_exp == DBL_EXP_INFNAN)
63 * If input can't have a fractional part, return
64 * (appropriately signed) zero, and make i be the input.
66 if ((int)u.s.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
69 v.s.dbl_sign = u.s.dbl_sign;
74 * If |input| < 1.0, return it, and set i to the appropriately
77 if (u.s.dbl_exp < DBL_EXP_BIAS) {
79 v.s.dbl_sign = u.s.dbl_sign;
85 * There can be a fractional part of the input.
86 * If you look at the math involved for a few seconds, it's
87 * plain to see that the integral part is the input, with the
88 * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
89 * the fractional part is the part with the rest of the
90 * bits zeroed. Just zeroing the high bits to get the
91 * fractional part would yield a fraction in need of
92 * normalization. Therefore, we take the easy way out, and
93 * just use subtraction to get the fractional part.
96 /* Zero the low bits of the fraction, the sleazy way. */
97 frac = ((u_int64_t)v.s.dbl_frach << 32) + v.s.dbl_fracl;
98 frac >>= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS);
99 frac <<= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS);
100 v.s.dbl_fracl = frac & 0xffffffff;
101 v.s.dbl_frach = frac >> 32;
105 u.s.dbl_sign = v.s.dbl_sign;