xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_sub.c (revision 1da57d55)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "quad.h"
28 
29 #ifdef __sparcv9
30 
31 /*
32  * _Qp_sub(pz, ox, oy) sets *pz = *ox - *oy.
33  */
34 void
_Qp_sub(union longdouble * pz,const union longdouble * ox,const union longdouble * oy)35 _Qp_sub(union longdouble *pz, const union longdouble *ox,
36 	const union longdouble *oy)
37 
38 #else
39 
40 /*
41  * _Q_sub(ox, oy) returns *ox - *oy.
42  */
43 union longdouble
44 _Q_sub(const union longdouble *ox, const union longdouble *oy)
45 
46 #endif	/* __sparcv9 */
47 
48 {
49 	union longdouble	z;
50 	const union longdouble	*x, *y;
51 	unsigned int		xm, ym, tm, fsr;
52 	int			flip;
53 
54 	/* sort so |x| >= |y| */
55 	xm = ox->l.msw & 0x7fffffff;
56 	ym = oy->l.msw & 0x7fffffff;
57 	if (ym > xm || ym == xm && (oy->l.frac2 > ox->l.frac2 ||
58 	    oy->l.frac2 == ox->l.frac2 && (oy->l.frac3 > ox->l.frac3 ||
59 	    oy->l.frac3 == ox->l.frac3 && oy->l.frac4 > ox->l.frac4))) {
60 		y = ox;
61 		x = oy;
62 		tm = xm;
63 		xm = ym;
64 		ym = tm;
65 		flip = 0x80000000;
66 	} else {
67 		x = ox;
68 		y = oy;
69 		flip = 0;
70 	}
71 
72 	/* get the fsr */
73 	__quad_getfsrp(&fsr);
74 
75 	/* handle nan and inf cases */
76 	if (xm >= 0x7fff0000) {
77 		/* x is nan or inf */
78 		if (ym >= 0x7fff0000) {
79 			/* y is nan or inf */
80 			if ((ym & 0xffff) | y->l.frac2 | y->l.frac3 |
81 			    y->l.frac4) {
82 				/* y is nan; x must be nan too */
83 				/* the following logic implements V9 app. B */
84 				if (!(ym & 0x8000)) {
85 					/* y is snan, signal invalid */
86 					if (fsr & FSR_NVM) {
87 						__quad_fsubq(ox, oy, &Z);
88 					} else {
89 						Z = (xm & 0x8000)? *y : *oy;
90 						Z.l.msw |= 0x8000;
91 						fsr = (fsr & ~FSR_CEXC) |
92 						    FSR_NVA | FSR_NVC;
93 						__quad_setfsrp(&fsr);
94 					}
95 					QUAD_RETURN(Z);
96 				}
97 				/* x and y are both qnan */
98 				Z = *oy;
99 				QUAD_RETURN(Z);
100 			}
101 			if (!((xm & 0xffff) | x->l.frac2 | x->l.frac3 |
102 			    x->l.frac4)) {
103 				/* x and y are both inf */
104 				if (!((x->l.msw ^ y->l.msw) & 0x80000000)) {
105 					/* inf - inf, signal invalid */
106 					if (fsr & FSR_NVM) {
107 						__quad_fsubq(ox, oy, &Z);
108 					} else {
109 						Z.l.msw = 0x7fffffff;
110 						Z.l.frac2 = Z.l.frac3 =
111 						    Z.l.frac4 = 0xffffffff;
112 						fsr = (fsr & ~FSR_CEXC) |
113 						    FSR_NVA | FSR_NVC;
114 						__quad_setfsrp(&fsr);
115 					}
116 					QUAD_RETURN(Z);
117 				}
118 				/* inf + inf, return inf */
119 				Z = *x;
120 				Z.l.msw ^= flip;
121 				QUAD_RETURN(Z);
122 			}
123 		}
124 		if ((xm & 0xffff) | x->l.frac2 | x->l.frac3 | x->l.frac4) {
125 			/* x is nan */
126 			if (!(xm & 0x8000)) {
127 				/* snan, signal invalid */
128 				if (fsr & FSR_NVM) {
129 					__quad_fsubq(ox, oy, &Z);
130 				} else {
131 					Z = *x;
132 					Z.l.msw |= 0x8000;
133 					fsr = (fsr & ~FSR_CEXC) | FSR_NVA |
134 					    FSR_NVC;
135 					__quad_setfsrp(&fsr);
136 				}
137 				QUAD_RETURN(Z);
138 			}
139 			Z = *x;
140 			QUAD_RETURN(Z);
141 		}
142 		/* x is inf */
143 		Z = *x;
144 		Z.l.msw ^= flip;
145 		QUAD_RETURN(Z);
146 	}
147 
148 	/* now x and y are finite and |x| >= |y| */
149 	fsr &= ~FSR_CEXC;
150 	z.l.msw = (x->l.msw & 0x80000000) ^ flip;
151 	if ((x->l.msw ^ y->l.msw) & 0x80000000)
152 		__quad_mag_add(x, y, &z, &fsr);
153 	else
154 		__quad_mag_sub(x, y, &z, &fsr);
155 	if ((fsr & FSR_CEXC) & (fsr >> 23)) {
156 		__quad_setfsrp(&fsr);
157 		__quad_fsubq(ox, oy, &Z);
158 	} else {
159 		Z = z;
160 		fsr |= (fsr & 0x1f) << 5;
161 		__quad_setfsrp(&fsr);
162 	}
163 	QUAD_RETURN(Z);
164 }
165