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