1 /*	$OpenBSD: modf.c,v 1.6 2013/07/03 04:46:36 espie 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 <float.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
modf(double val,double * iptr)45 modf(double val, double *iptr)
46 {
47 	union doub {
48 		double v;
49 		struct ieee_double s;
50 	} u, v;
51 	u_int64_t frac;
52 
53 	/*
54 	 * If input is Inf or NaN, return it and leave i alone.
55 	 */
56 	u.v = val;
57 	if (u.s.dbl_exp == DBL_EXP_INFNAN)
58 		return (u.v);
59 
60 	/*
61 	 * If input can't have a fractional part, return
62 	 * (appropriately signed) zero, and make i be the input.
63 	 */
64 	if ((int)u.s.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
65 		*iptr = u.v;
66 		v.v = 0.0;
67 		v.s.dbl_sign = u.s.dbl_sign;
68 		return (v.v);
69 	}
70 
71 	/*
72 	 * If |input| < 1.0, return it, and set i to the appropriately
73 	 * signed zero.
74 	 */
75 	if (u.s.dbl_exp < DBL_EXP_BIAS) {
76 		v.v = 0.0;
77 		v.s.dbl_sign = u.s.dbl_sign;
78 		*iptr = v.v;
79 		return (u.v);
80 	}
81 
82 	/*
83 	 * There can be a fractional part of the input.
84 	 * If you look at the math involved for a few seconds, it's
85 	 * plain to see that the integral part is the input, with the
86 	 * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
87 	 * the fractional part is the part with the rest of the
88 	 * bits zeroed.  Just zeroing the high bits to get the
89 	 * fractional part would yield a fraction in need of
90 	 * normalization.  Therefore, we take the easy way out, and
91 	 * just use subtraction to get the fractional part.
92 	 */
93 	v.v = u.v;
94 	/* Zero the low bits of the fraction, the sleazy way. */
95 	frac = ((u_int64_t)v.s.dbl_frach << 32) + v.s.dbl_fracl;
96 	frac >>= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS);
97 	frac <<= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS);
98 	v.s.dbl_fracl = frac & 0xffffffff;
99 	v.s.dbl_frach = frac >> 32;
100 	*iptr = v.v;
101 
102 	u.v -= v.v;
103 	u.s.dbl_sign = v.s.dbl_sign;
104 	return (u.v);
105 }
106 
107 #if	LDBL_MANT_DIG == DBL_MANT_DIG
108 __strong_alias(modfl, modf);
109 #endif	/* LDBL_MANT_DIG == DBL_MANT_DIG */
110