summaryrefslogtreecommitdiff
path: root/usr/src/lib/libm/common/m9x/fmaf.c
blob: 507fb2e9eff9ded97e5013ef0b94a752be2f3595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma weak fmaf = __fmaf

#include "libm.h"
#include "fma.h"
#include "fenv_inlines.h"

#if defined(__sparc)

/*
 * fmaf for SPARC: 32-bit single precision, big-endian
 */
float
__fmaf(float x, float y, float z) {
	union {
		unsigned i[2];
		double d;
	} xy, zz;
	unsigned u, s;
	int exy, ez;

	/*
	 * the following operations can only raise the invalid exception,
	 * and then only if either x*y is of the form Inf*0 or one of x,
	 * y, or z is a signaling NaN
	 */
	xy.d = (double) x * y;
	zz.d = (double) z;

	/*
	 * if the sum xy + z will be exact, just compute it and cast the
	 * result to float
	 */
	exy = (xy.i[0] >> 20) & 0x7ff;
	ez = (zz.i[0] >> 20) & 0x7ff;
	if ((ez - exy <= 4 && exy - ez <= 28) || exy == 0x7ff || exy == 0 ||
		ez == 0x7ff || ez == 0) {
		return ((float) (xy.d + zz.d));
	}

	/*
	 * collapse the tail of the smaller summand into a "sticky bit"
	 * so that the sum can be computed without error
	 */
	if (ez > exy) {
		if (ez - exy < 31) {
			u = xy.i[1];
			s = 2 << (ez - exy);
			if (u & (s - 1))
				u |= s;
			xy.i[1] = u & ~(s - 1);
		} else if (ez - exy < 51) {
			u = xy.i[0];
			s = 1 << (ez - exy - 31);
			if ((u & (s - 1)) | xy.i[1])
				u |= s;
			xy.i[0] = u & ~(s - 1);
			xy.i[1] = 0;
		} else {
			/* collapse all of xy into a single bit */
			xy.i[0] = (xy.i[0] & 0x80000000) | ((ez - 51) << 20);
			xy.i[1] = 0;
		}
	} else {
		if (exy - ez < 31) {
			u = zz.i[1];
			s = 2 << (exy - ez);
			if (u & (s - 1))
				u |= s;
			zz.i[1] = u & ~(s - 1);
		} else if (exy - ez < 51) {
			u = zz.i[0];
			s = 1 << (exy - ez - 31);
			if ((u & (s - 1)) | zz.i[1])
				u |= s;
			zz.i[0] = u & ~(s - 1);
			zz.i[1] = 0;
		} else {
			/* collapse all of zz into a single bit */
			zz.i[0] = (zz.i[0] & 0x80000000) | ((exy - 51) << 20);
			zz.i[1] = 0;
		}
	}

	return ((float) (xy.d + zz.d));
}

#elif defined(__x86)

#if defined(__amd64)
#define	NI	4
#else
#define	NI	3
#endif

/*
 * fmaf for x86: 32-bit single precision, little-endian
 */
float
__fmaf(float x, float y, float z) {
	union {
		unsigned i[NI];
		long double e;
	} xy, zz;
	unsigned u, s, cwsw, oldcwsw;
	int exy, ez;

	/* set rounding precision to 64 bits */
	__fenv_getcwsw(&oldcwsw);
	cwsw = (oldcwsw & 0xfcffffff) | 0x03000000;
	__fenv_setcwsw(&cwsw);

	/*
	 * the following operations can only raise the invalid exception,
	 * and then only if either x*y is of the form Inf*0 or one of x,
	 * y, or z is a signaling NaN
	 */
	xy.e = (long double) x * y;
	zz.e = (long double) z;

	/*
	 * if the sum xy + z will be exact, just compute it and cast the
	 * result to float
	 */
	exy = xy.i[2] & 0x7fff;
	ez = zz.i[2] & 0x7fff;
	if ((ez - exy <= 15 && exy - ez <= 39) || exy == 0x7fff || exy == 0 ||
		ez == 0x7fff || ez == 0) {
		goto cont;
	}

	/*
	 * collapse the tail of the smaller summand into a "sticky bit"
	 * so that the sum can be computed without error
	 */
	if (ez > exy) {
		if (ez - exy < 31) {
			u = xy.i[0];
			s = 2 << (ez - exy);
			if (u & (s - 1))
				u |= s;
			xy.i[0] = u & ~(s - 1);
		} else if (ez - exy < 62) {
			u = xy.i[1];
			s = 1 << (ez - exy - 31);
			if ((u & (s - 1)) | xy.i[0])
				u |= s;
			xy.i[1] = u & ~(s - 1);
			xy.i[0] = 0;
		} else {
			/* collapse all of xy into a single bit */
			xy.i[0] = 0;
			xy.i[1] = 0x80000000;
			xy.i[2] = (xy.i[2] & 0x8000) | (ez - 62);
		}
	} else {
		if (exy - ez < 62) {
			u = zz.i[1];
			s = 1 << (exy - ez - 31);
			if ((u & (s - 1)) | zz.i[0])
				u |= s;
			zz.i[1] = u & ~(s - 1);
			zz.i[0] = 0;
		} else {
			/* collapse all of zz into a single bit */
			zz.i[0] = 0;
			zz.i[1] = 0x80000000;
			zz.i[2] = (zz.i[2] & 0x8000) | (exy - 62);
		}
	}

cont:
	xy.e += zz.e;

	/* restore the rounding precision */
	__fenv_getcwsw(&cwsw);
	cwsw = (cwsw & 0xfcffffff) | (oldcwsw & 0x03000000);
	__fenv_setcwsw(&cwsw);

	return ((float) xy.e);
}

#if 0
/*
 * another fmaf for x86: assumes return value will be left in
 * long double (80-bit double extended) precision
 */
long double
__fmaf(float x, float y, float z) {
	/*
	 * Note: This implementation assumes the rounding precision mode
	 * is set to the default, rounding to 64 bit precision.  If this
	 * routine must work in non-default rounding precision modes, do
	 * the following instead:
	 *
	 *   long double t;
	 *
	 *   <set rp mode to round to 64 bit precision>
	 *   t = x * y;
	 *   <restore rp mode>
	 *   return t + z;
	 *
	 * Note that the code to change rounding precision must not alter
	 * the exception masks or flags, since the product x * y may raise
	 * an invalid operation exception.
	 */
	return ((long double) x * y + z);
}
#endif

#else
#error Unknown architecture
#endif