xref: /titanic_44/usr/src/lib/libm/common/m9x/modf.c (revision a05fd0c9b9aa46cf66ddea7617e56facdf1f4aaf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #if defined(ELFOBJ)
31 #pragma weak modf = __modf
32 #pragma weak _modf = __modf
33 #endif
34 
35 /*
36  * modf(x, iptr) decomposes x into an integral part and a fractional
37  * part both having the same sign as x.  It stores the integral part
38  * in *iptr and returns the fractional part.
39  *
40  * If x is infinite, modf sets *iptr to x and returns copysign(0.0,x).
41  * If x is NaN, modf sets *iptr to x and returns x.
42  *
43  * If x is a signaling NaN, this code does not attempt to raise the
44  * invalid operation exception.
45  */
46 
47 #include "libm.h"
48 
49 double
50 __modf(double x, double *iptr) {
51 	union {
52 		unsigned i[2];
53 		double d;
54 	} xx, yy;
55 	unsigned hx, s;
56 
57 	xx.d = x;
58 	hx = xx.i[HIWORD] & ~0x80000000;
59 
60 	if (hx >= 0x43300000) {	/* x is NaN, infinite, or integral */
61 		*iptr = x;
62 		if (hx < 0x7ff00000 || (hx == 0x7ff00000 &&
63 			xx.i[LOWORD] == 0)) {
64 			xx.i[HIWORD] &= 0x80000000;
65 			xx.i[LOWORD] = 0;
66 		}
67 		return (xx.d);
68 	}
69 
70 	if (hx < 0x3ff00000) {	/* |x| < 1 */
71 		xx.i[HIWORD] &= 0x80000000;
72 		xx.i[LOWORD] = 0;
73 		*iptr = xx.d;
74 		return (x);
75 	}
76 
77 	/* split x at the binary point */
78 	s = xx.i[HIWORD] & 0x80000000;
79 	if (hx < 0x41400000) {
80 		yy.i[HIWORD] = xx.i[HIWORD] & ~((1 << (0x413 - (hx >> 20))) -
81 			1);
82 		yy.i[LOWORD] = 0;
83 	} else {
84 		yy.i[HIWORD] = xx.i[HIWORD];
85 		yy.i[LOWORD] = xx.i[LOWORD] & ~((1 << (0x433 - (hx >> 20))) -
86 			1);
87 	}
88 	*iptr = yy.d;
89 	xx.d -= yy.d;
90 	xx.i[HIWORD] = (xx.i[HIWORD] & ~0x80000000) | s;
91 							/* keep sign of x */
92 	return (xx.d);
93 }
94