Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libc / gen / modf_ieee754.c
blob3b5799b58343fd5ddcefb513cdb4aa1d07f34dac
1 /* $NetBSD: modf_ieee754.c,v 1.1 2003/05/12 15:15:16 kleink Exp $ */
3 /*
4 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 * All rights reserved.
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.
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.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
30 #include <sys/types.h>
31 #include <machine/ieee.h>
32 #include <errno.h>
33 #include <math.h>
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).
40 * Beware signedness when doing subtraction, and also operand size!
42 double
43 modf(double val, double *iptr)
45 union ieee_double_u u, v;
46 u_int64_t frac;
49 * If input is Inf or NaN, return it and leave i alone.
51 u.dblu_d = val;
52 if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN)
53 return (u.dblu_d);
56 * If input can't have a fractional part, return
57 * (appropriately signed) zero, and make i be the input.
59 if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
60 *iptr = u.dblu_d;
61 v.dblu_d = 0.0;
62 v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
63 return (v.dblu_d);
67 * If |input| < 1.0, return it, and set i to the appropriately
68 * signed zero.
70 if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) {
71 v.dblu_d = 0.0;
72 v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
73 *iptr = v.dblu_d;
74 return (u.dblu_d);
78 * There can be a fractional part of the input.
79 * If you look at the math involved for a few seconds, it's
80 * plain to see that the integral part is the input, with the
81 * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
82 * the fractional part is the part with the rest of the
83 * bits zeroed. Just zeroing the high bits to get the
84 * fractional part would yield a fraction in need of
85 * normalization. Therefore, we take the easy way out, and
86 * just use subtraction to get the fractional part.
88 v.dblu_d = u.dblu_d;
89 /* Zero the low bits of the fraction, the sleazy way. */
90 frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl;
91 frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
92 frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
93 v.dblu_dbl.dbl_fracl = frac & 0xffffffff;
94 v.dblu_dbl.dbl_frach = frac >> 32;
95 *iptr = v.dblu_d;
97 u.dblu_d -= v.dblu_d;
98 u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign;
99 return (u.dblu_d);