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