]> git.ipfire.org Git - thirdparty/glibc.git/blame - soft-fp/op-common.h
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / soft-fp / op-common.h
CommitLineData
d876f532 1/* Software floating-point emulation. Common operations.
2b778ceb 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
d876f532
UD
3 This file is part of the GNU C Library.
4 Contributed by Richard Henderson (rth@cygnus.com),
5 Jakub Jelinek (jj@ultra.linux.cz),
6 David S. Miller (davem@redhat.com) and
7 Peter Maydell (pmaydell@chiark.greenend.org.uk).
8
9 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
d876f532 13
638a783c
RM
14 In addition to the permissions in the GNU Lesser General Public
15 License, the Free Software Foundation gives you unlimited
16 permission to link the compiled version of this file into
17 combinations with other programs, and to distribute those
18 combinations without any restriction coming from the use of this
19 file. (The Lesser General Public License restrictions do apply in
20 other respects; for example, they cover modification of the file,
21 and distribution when not linked into a combine executable.)
22
d876f532
UD
23 The GNU C Library is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 26 Lesser General Public License for more details.
d876f532 27
41bdb6e2 28 You should have received a copy of the GNU Lesser General Public
59ba27a6 29 License along with the GNU C Library; if not, see
5a82c748 30 <https://www.gnu.org/licenses/>. */
d876f532 31
a2f8be9c
JM
32#ifndef SOFT_FP_OP_COMMON_H
33#define SOFT_FP_OP_COMMON_H 1
34
b838844b
JM
35#define _FP_DECL(wc, X) \
36 _FP_I_TYPE X##_c __attribute__ ((unused)) _FP_ZERO_INIT; \
37 _FP_I_TYPE X##_s __attribute__ ((unused)) _FP_ZERO_INIT; \
38 _FP_I_TYPE X##_e __attribute__ ((unused)) _FP_ZERO_INIT; \
51ca9e29 39 _FP_FRAC_DECL_##wc (X)
d876f532 40
2848b105 41/* Test whether the qNaN bit denotes a signaling NaN. */
51ca9e29
JM
42#define _FP_FRAC_SNANP(fs, X) \
43 ((_FP_QNANNEGATEDP) \
44 ? (_FP_FRAC_HIGH_RAW_##fs (X) & _FP_QNANBIT_##fs) \
45 : !(_FP_FRAC_HIGH_RAW_##fs (X) & _FP_QNANBIT_##fs))
46#define _FP_FRAC_SNANP_SEMIRAW(fs, X) \
47 ((_FP_QNANNEGATEDP) \
48 ? (_FP_FRAC_HIGH_##fs (X) & _FP_QNANBIT_SH_##fs) \
49 : !(_FP_FRAC_HIGH_##fs (X) & _FP_QNANBIT_SH_##fs))
2848b105 50
c4fe3ea7
JM
51/* Finish truly unpacking a native fp value by classifying the kind
52 of fp value and normalizing both the exponent and the fraction. */
d876f532 53
1e145589
JM
54#define _FP_UNPACK_CANONICAL(fs, wc, X) \
55 do \
56 { \
57 switch (X##_e) \
58 { \
59 default: \
51ca9e29
JM
60 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
61 _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
1e145589
JM
62 X##_e -= _FP_EXPBIAS_##fs; \
63 X##_c = FP_CLS_NORMAL; \
64 break; \
65 \
66 case 0: \
51ca9e29 67 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589 68 X##_c = FP_CLS_ZERO; \
454ac701
JM
69 else if (FP_DENORM_ZERO) \
70 { \
71 X##_c = FP_CLS_ZERO; \
72 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
73 FP_SET_EXCEPTION (FP_EX_DENORM); \
74 } \
1e145589
JM
75 else \
76 { \
c4fe3ea7 77 /* A denormalized number. */ \
9c37ec0b
JM
78 _FP_I_TYPE _FP_UNPACK_CANONICAL_shift; \
79 _FP_FRAC_CLZ_##wc (_FP_UNPACK_CANONICAL_shift, \
80 X); \
81 _FP_UNPACK_CANONICAL_shift -= _FP_FRACXBITS_##fs; \
82 _FP_FRAC_SLL_##wc (X, (_FP_UNPACK_CANONICAL_shift \
83 + _FP_WORKBITS)); \
84 X##_e -= (_FP_EXPBIAS_##fs - 1 \
85 + _FP_UNPACK_CANONICAL_shift); \
1e145589 86 X##_c = FP_CLS_NORMAL; \
51ca9e29 87 FP_SET_EXCEPTION (FP_EX_DENORM); \
1e145589
JM
88 } \
89 break; \
90 \
91 case _FP_EXPMAX_##fs: \
51ca9e29 92 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589
JM
93 X##_c = FP_CLS_INF; \
94 else \
95 { \
96 X##_c = FP_CLS_NAN; \
c4fe3ea7 97 /* Check for signaling NaN. */ \
51ca9e29 98 if (_FP_FRAC_SNANP (fs, X)) \
ff12c11f
JM
99 FP_SET_EXCEPTION (FP_EX_INVALID \
100 | FP_EX_INVALID_SNAN); \
1e145589
JM
101 } \
102 break; \
103 } \
104 } \
105 while (0)
d876f532 106
fe0b1e85
RM
107/* Finish unpacking an fp value in semi-raw mode: the mantissa is
108 shifted by _FP_WORKBITS but the implicit MSB is not inserted and
109 other classification is not done. */
51ca9e29 110#define _FP_UNPACK_SEMIRAW(fs, wc, X) _FP_FRAC_SLL_##wc (X, _FP_WORKBITS)
fe0b1e85 111
454ac701
JM
112/* Check whether a raw or semi-raw input value should be flushed to
113 zero, and flush it to zero if so. */
114#define _FP_CHECK_FLUSH_ZERO(fs, wc, X) \
115 do \
116 { \
117 if (FP_DENORM_ZERO \
118 && X##_e == 0 \
119 && !_FP_FRAC_ZEROP_##wc (X)) \
120 { \
121 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
122 FP_SET_EXCEPTION (FP_EX_DENORM); \
123 } \
124 } \
125 while (0)
126
fe0b1e85
RM
127/* A semi-raw value has overflowed to infinity. Adjust the mantissa
128 and exponent appropriately. */
129#define _FP_OVERFLOW_SEMIRAW(fs, wc, X) \
1e145589 130 do \
fe0b1e85 131 { \
1e145589
JM
132 if (FP_ROUNDMODE == FP_RND_NEAREST \
133 || (FP_ROUNDMODE == FP_RND_PINF && !X##_s) \
134 || (FP_ROUNDMODE == FP_RND_MINF && X##_s)) \
135 { \
136 X##_e = _FP_EXPMAX_##fs; \
51ca9e29 137 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1e145589
JM
138 } \
139 else \
140 { \
141 X##_e = _FP_EXPMAX_##fs - 1; \
51ca9e29 142 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
1e145589 143 } \
51ca9e29
JM
144 FP_SET_EXCEPTION (FP_EX_INEXACT); \
145 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
fe0b1e85 146 } \
1e145589 147 while (0)
fe0b1e85
RM
148
149/* Check for a semi-raw value being a signaling NaN and raise the
150 invalid exception if so. */
ff12c11f
JM
151#define _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X) \
152 do \
153 { \
154 if (X##_e == _FP_EXPMAX_##fs \
155 && !_FP_FRAC_ZEROP_##wc (X) \
156 && _FP_FRAC_SNANP_SEMIRAW (fs, X)) \
157 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SNAN); \
158 } \
1e145589 159 while (0)
fe0b1e85
RM
160
161/* Choose a NaN result from an operation on two semi-raw NaN
162 values. */
163#define _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP) \
1e145589
JM
164 do \
165 { \
166 /* _FP_CHOOSENAN expects raw values, so shift as required. */ \
51ca9e29
JM
167 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
168 _FP_FRAC_SRL_##wc (Y, _FP_WORKBITS); \
169 _FP_CHOOSENAN (fs, wc, R, X, Y, OP); \
170 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
1e145589
JM
171 } \
172 while (0)
fe0b1e85 173
2848b105
MR
174/* Make the fractional part a quiet NaN, preserving the payload
175 if possible, otherwise make it the canonical quiet NaN and set
176 the sign bit accordingly. */
1e145589
JM
177#define _FP_SETQNAN(fs, wc, X) \
178 do \
179 { \
180 if (_FP_QNANNEGATEDP) \
181 { \
51ca9e29
JM
182 _FP_FRAC_HIGH_RAW_##fs (X) &= _FP_QNANBIT_##fs - 1; \
183 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589
JM
184 { \
185 X##_s = _FP_NANSIGN_##fs; \
51ca9e29 186 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
1e145589
JM
187 } \
188 } \
189 else \
51ca9e29 190 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_QNANBIT_##fs; \
1e145589
JM
191 } \
192 while (0)
193#define _FP_SETQNAN_SEMIRAW(fs, wc, X) \
194 do \
195 { \
196 if (_FP_QNANNEGATEDP) \
197 { \
51ca9e29
JM
198 _FP_FRAC_HIGH_##fs (X) &= _FP_QNANBIT_SH_##fs - 1; \
199 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589
JM
200 { \
201 X##_s = _FP_NANSIGN_##fs; \
51ca9e29
JM
202 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
203 _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
1e145589
JM
204 } \
205 } \
206 else \
51ca9e29 207 _FP_FRAC_HIGH_##fs (X) |= _FP_QNANBIT_SH_##fs; \
1e145589
JM
208 } \
209 while (0)
2848b105 210
fe0b1e85
RM
211/* Test whether a biased exponent is normal (not zero or maximum). */
212#define _FP_EXP_NORMAL(fs, wc, X) (((X##_e + 1) & _FP_EXPMAX_##fs) > 1)
213
214/* Prepare to pack an fp value in semi-raw mode: the mantissa is
215 rounded and shifted right, with the rounding possibly increasing
216 the exponent (including changing a finite value to infinity). */
217#define _FP_PACK_SEMIRAW(fs, wc, X) \
1e145589
JM
218 do \
219 { \
ace614b8
JM
220 int _FP_PACK_SEMIRAW_is_tiny \
221 = X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X); \
222 if (_FP_TININESS_AFTER_ROUNDING \
223 && _FP_PACK_SEMIRAW_is_tiny) \
224 { \
225 FP_DECL_##fs (_FP_PACK_SEMIRAW_T); \
226 _FP_FRAC_COPY_##wc (_FP_PACK_SEMIRAW_T, X); \
227 _FP_PACK_SEMIRAW_T##_s = X##_s; \
228 _FP_PACK_SEMIRAW_T##_e = X##_e; \
229 _FP_FRAC_SLL_##wc (_FP_PACK_SEMIRAW_T, 1); \
230 _FP_ROUND (wc, _FP_PACK_SEMIRAW_T); \
231 if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_SEMIRAW_T)) \
232 _FP_PACK_SEMIRAW_is_tiny = 0; \
233 } \
51ca9e29 234 _FP_ROUND (wc, X); \
ace614b8 235 if (_FP_PACK_SEMIRAW_is_tiny) \
1e145589 236 { \
47594329
MS
237 if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
238 || (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
51ca9e29 239 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
1e145589 240 } \
51ca9e29 241 if (_FP_FRAC_HIGH_##fs (X) \
1e145589 242 & (_FP_OVERFLOW_##fs >> 1)) \
ae251b0b 243 { \
51ca9e29 244 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_OVERFLOW_##fs >> 1); \
1e145589
JM
245 X##_e++; \
246 if (X##_e == _FP_EXPMAX_##fs) \
51ca9e29 247 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
1e145589 248 } \
51ca9e29
JM
249 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
250 if (X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1e145589
JM
251 { \
252 if (!_FP_KEEPNANFRACP) \
253 { \
51ca9e29 254 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
1e145589
JM
255 X##_s = _FP_NANSIGN_##fs; \
256 } \
257 else \
51ca9e29 258 _FP_SETQNAN (fs, wc, X); \
ae251b0b 259 } \
fe0b1e85 260 } \
1e145589 261 while (0)
fe0b1e85 262
c4fe3ea7
JM
263/* Before packing the bits back into the native fp result, take care
264 of such mundane things as rounding and overflow. Also, for some
265 kinds of fp values, the original parts may not have been fully
266 extracted -- but that is ok, we can regenerate them now. */
d876f532 267
1e145589
JM
268#define _FP_PACK_CANONICAL(fs, wc, X) \
269 do \
270 { \
271 switch (X##_c) \
272 { \
273 case FP_CLS_NORMAL: \
274 X##_e += _FP_EXPBIAS_##fs; \
275 if (X##_e > 0) \
276 { \
51ca9e29
JM
277 _FP_ROUND (wc, X); \
278 if (_FP_FRAC_OVERP_##wc (fs, X)) \
1e145589 279 { \
51ca9e29 280 _FP_FRAC_CLEAR_OVERP_##wc (fs, X); \
1e145589
JM
281 X##_e++; \
282 } \
51ca9e29 283 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
1e145589
JM
284 if (X##_e >= _FP_EXPMAX_##fs) \
285 { \
c4fe3ea7 286 /* Overflow. */ \
1e145589
JM
287 switch (FP_ROUNDMODE) \
288 { \
289 case FP_RND_NEAREST: \
290 X##_c = FP_CLS_INF; \
291 break; \
292 case FP_RND_PINF: \
293 if (!X##_s) \
294 X##_c = FP_CLS_INF; \
295 break; \
296 case FP_RND_MINF: \
297 if (X##_s) \
298 X##_c = FP_CLS_INF; \
299 break; \
300 } \
301 if (X##_c == FP_CLS_INF) \
302 { \
c4fe3ea7 303 /* Overflow to infinity. */ \
1e145589 304 X##_e = _FP_EXPMAX_##fs; \
51ca9e29 305 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1e145589
JM
306 } \
307 else \
308 { \
c4fe3ea7 309 /* Overflow to maximum normal. */ \
1e145589 310 X##_e = _FP_EXPMAX_##fs - 1; \
51ca9e29 311 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
1e145589 312 } \
51ca9e29
JM
313 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
314 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1e145589
JM
315 } \
316 } \
317 else \
318 { \
c4fe3ea7 319 /* We've got a denormalized number. */ \
ace614b8
JM
320 int _FP_PACK_CANONICAL_is_tiny = 1; \
321 if (_FP_TININESS_AFTER_ROUNDING && X##_e == 0) \
322 { \
323 FP_DECL_##fs (_FP_PACK_CANONICAL_T); \
324 _FP_FRAC_COPY_##wc (_FP_PACK_CANONICAL_T, X); \
325 _FP_PACK_CANONICAL_T##_s = X##_s; \
326 _FP_PACK_CANONICAL_T##_e = X##_e; \
327 _FP_ROUND (wc, _FP_PACK_CANONICAL_T); \
328 if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_CANONICAL_T)) \
329 _FP_PACK_CANONICAL_is_tiny = 0; \
330 } \
1e145589
JM
331 X##_e = -X##_e + 1; \
332 if (X##_e <= _FP_WFRACBITS_##fs) \
333 { \
51ca9e29
JM
334 _FP_FRAC_SRS_##wc (X, X##_e, _FP_WFRACBITS_##fs); \
335 _FP_ROUND (wc, X); \
336 if (_FP_FRAC_HIGH_##fs (X) \
1e145589
JM
337 & (_FP_OVERFLOW_##fs >> 1)) \
338 { \
339 X##_e = 1; \
51ca9e29
JM
340 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
341 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1e145589
JM
342 } \
343 else \
344 { \
345 X##_e = 0; \
51ca9e29 346 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
1e145589 347 } \
ace614b8
JM
348 if (_FP_PACK_CANONICAL_is_tiny \
349 && ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
350 || (FP_TRAPPING_EXCEPTIONS \
351 & FP_EX_UNDERFLOW))) \
51ca9e29 352 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
1e145589
JM
353 } \
354 else \
355 { \
c4fe3ea7 356 /* Underflow to zero. */ \
1e145589 357 X##_e = 0; \
51ca9e29 358 if (!_FP_FRAC_ZEROP_##wc (X)) \
1e145589 359 { \
51ca9e29
JM
360 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
361 _FP_ROUND (wc, X); \
362 _FP_FRAC_LOW_##wc (X) >>= (_FP_WORKBITS); \
1e145589 363 } \
51ca9e29 364 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
1e145589
JM
365 } \
366 } \
367 break; \
368 \
369 case FP_CLS_ZERO: \
370 X##_e = 0; \
51ca9e29 371 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1e145589
JM
372 break; \
373 \
374 case FP_CLS_INF: \
375 X##_e = _FP_EXPMAX_##fs; \
51ca9e29 376 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1e145589
JM
377 break; \
378 \
379 case FP_CLS_NAN: \
380 X##_e = _FP_EXPMAX_##fs; \
381 if (!_FP_KEEPNANFRACP) \
382 { \
51ca9e29 383 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
1e145589
JM
384 X##_s = _FP_NANSIGN_##fs; \
385 } \
386 else \
51ca9e29 387 _FP_SETQNAN (fs, wc, X); \
1e145589
JM
388 break; \
389 } \
390 } \
391 while (0)
d876f532
UD
392
393/* This one accepts raw argument and not cooked, returns
c4fe3ea7 394 1 if X is a signaling NaN. */
1e145589
JM
395#define _FP_ISSIGNAN(fs, wc, X) \
396 ({ \
9c37ec0b 397 int _FP_ISSIGNAN_ret = 0; \
1e145589
JM
398 if (X##_e == _FP_EXPMAX_##fs) \
399 { \
51ca9e29
JM
400 if (!_FP_FRAC_ZEROP_##wc (X) \
401 && _FP_FRAC_SNANP (fs, X)) \
9c37ec0b 402 _FP_ISSIGNAN_ret = 1; \
1e145589 403 } \
9c37ec0b 404 _FP_ISSIGNAN_ret; \
1e145589 405 })
d876f532
UD
406
407
408
409
410
fe0b1e85 411/* Addition on semi-raw values. */
1e145589
JM
412#define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
413 do \
414 { \
454ac701
JM
415 _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
416 _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
1e145589
JM
417 if (X##_s == Y##_s) \
418 { \
419 /* Addition. */ \
d7423856 420 __label__ add1, add2, add3, add_done; \
1e145589 421 R##_s = X##_s; \
9c37ec0b
JM
422 int _FP_ADD_INTERNAL_ediff = X##_e - Y##_e; \
423 if (_FP_ADD_INTERNAL_ediff > 0) \
1e145589
JM
424 { \
425 R##_e = X##_e; \
426 if (Y##_e == 0) \
427 { \
428 /* Y is zero or denormalized. */ \
51ca9e29 429 if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589 430 { \
51ca9e29
JM
431 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
432 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
433 goto add_done; \
434 } \
435 else \
436 { \
51ca9e29 437 FP_SET_EXCEPTION (FP_EX_DENORM); \
9c37ec0b
JM
438 _FP_ADD_INTERNAL_ediff--; \
439 if (_FP_ADD_INTERNAL_ediff == 0) \
1e145589 440 { \
51ca9e29 441 _FP_FRAC_ADD_##wc (R, X, Y); \
1e145589
JM
442 goto add3; \
443 } \
444 if (X##_e == _FP_EXPMAX_##fs) \
445 { \
51ca9e29
JM
446 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
447 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
448 goto add_done; \
449 } \
450 goto add1; \
451 } \
452 } \
453 else if (X##_e == _FP_EXPMAX_##fs) \
454 { \
455 /* X is NaN or Inf, Y is normal. */ \
51ca9e29
JM
456 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
457 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
458 goto add_done; \
459 } \
460 \
461 /* Insert implicit MSB of Y. */ \
51ca9e29 462 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
1e145589
JM
463 \
464 add1: \
9c37ec0b
JM
465 /* Shift the mantissa of Y to the right \
466 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
467 later for the implicit MSB of X. */ \
468 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
469 _FP_FRAC_SRS_##wc (Y, _FP_ADD_INTERNAL_ediff, \
470 _FP_WFRACBITS_##fs); \
51ca9e29
JM
471 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
472 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
473 _FP_FRAC_ADD_##wc (R, X, Y); \
1e145589 474 } \
9c37ec0b 475 else if (_FP_ADD_INTERNAL_ediff < 0) \
1e145589 476 { \
9c37ec0b 477 _FP_ADD_INTERNAL_ediff = -_FP_ADD_INTERNAL_ediff; \
1e145589
JM
478 R##_e = Y##_e; \
479 if (X##_e == 0) \
480 { \
481 /* X is zero or denormalized. */ \
51ca9e29 482 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589 483 { \
51ca9e29
JM
484 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
485 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
486 goto add_done; \
487 } \
488 else \
489 { \
51ca9e29 490 FP_SET_EXCEPTION (FP_EX_DENORM); \
9c37ec0b
JM
491 _FP_ADD_INTERNAL_ediff--; \
492 if (_FP_ADD_INTERNAL_ediff == 0) \
1e145589 493 { \
51ca9e29 494 _FP_FRAC_ADD_##wc (R, Y, X); \
1e145589
JM
495 goto add3; \
496 } \
497 if (Y##_e == _FP_EXPMAX_##fs) \
498 { \
51ca9e29
JM
499 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
500 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
501 goto add_done; \
502 } \
503 goto add2; \
504 } \
505 } \
506 else if (Y##_e == _FP_EXPMAX_##fs) \
507 { \
508 /* Y is NaN or Inf, X is normal. */ \
51ca9e29
JM
509 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
510 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
511 goto add_done; \
512 } \
513 \
514 /* Insert implicit MSB of X. */ \
51ca9e29 515 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
1e145589
JM
516 \
517 add2: \
9c37ec0b
JM
518 /* Shift the mantissa of X to the right \
519 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
520 later for the implicit MSB of Y. */ \
521 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
522 _FP_FRAC_SRS_##wc (X, _FP_ADD_INTERNAL_ediff, \
523 _FP_WFRACBITS_##fs); \
51ca9e29
JM
524 else if (!_FP_FRAC_ZEROP_##wc (X)) \
525 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
526 _FP_FRAC_ADD_##wc (R, Y, X); \
1e145589
JM
527 } \
528 else \
529 { \
9c37ec0b 530 /* _FP_ADD_INTERNAL_ediff == 0. */ \
51ca9e29 531 if (!_FP_EXP_NORMAL (fs, wc, X)) \
1e145589
JM
532 { \
533 if (X##_e == 0) \
534 { \
535 /* X and Y are zero or denormalized. */ \
536 R##_e = 0; \
51ca9e29 537 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589 538 { \
51ca9e29
JM
539 if (!_FP_FRAC_ZEROP_##wc (Y)) \
540 FP_SET_EXCEPTION (FP_EX_DENORM); \
541 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
542 goto add_done; \
543 } \
51ca9e29 544 else if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589 545 { \
51ca9e29
JM
546 FP_SET_EXCEPTION (FP_EX_DENORM); \
547 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
548 goto add_done; \
549 } \
550 else \
551 { \
51ca9e29
JM
552 FP_SET_EXCEPTION (FP_EX_DENORM); \
553 _FP_FRAC_ADD_##wc (R, X, Y); \
554 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
1e145589
JM
555 { \
556 /* Normalized result. */ \
51ca9e29
JM
557 _FP_FRAC_HIGH_##fs (R) \
558 &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1e145589
JM
559 R##_e = 1; \
560 } \
561 goto add_done; \
562 } \
563 } \
564 else \
565 { \
566 /* X and Y are NaN or Inf. */ \
51ca9e29
JM
567 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
568 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
1e145589 569 R##_e = _FP_EXPMAX_##fs; \
51ca9e29
JM
570 if (_FP_FRAC_ZEROP_##wc (X)) \
571 _FP_FRAC_COPY_##wc (R, Y); \
572 else if (_FP_FRAC_ZEROP_##wc (Y)) \
573 _FP_FRAC_COPY_##wc (R, X); \
1e145589 574 else \
51ca9e29 575 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
1e145589
JM
576 goto add_done; \
577 } \
578 } \
579 /* The exponents of X and Y, both normal, are equal. The \
580 implicit MSBs will always add to increase the \
581 exponent. */ \
51ca9e29 582 _FP_FRAC_ADD_##wc (R, X, Y); \
1e145589 583 R##_e = X##_e + 1; \
51ca9e29 584 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
1e145589
JM
585 if (R##_e == _FP_EXPMAX_##fs) \
586 /* Overflow to infinity (depending on rounding mode). */ \
51ca9e29 587 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
1e145589
JM
588 goto add_done; \
589 } \
590 add3: \
51ca9e29 591 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
1e145589
JM
592 { \
593 /* Overflow. */ \
51ca9e29 594 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1e145589 595 R##_e++; \
51ca9e29 596 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
1e145589
JM
597 if (R##_e == _FP_EXPMAX_##fs) \
598 /* Overflow to infinity (depending on rounding mode). */ \
51ca9e29 599 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
1e145589
JM
600 } \
601 add_done: ; \
602 } \
603 else \
604 { \
605 /* Subtraction. */ \
d7423856 606 __label__ sub1, sub2, sub3, norm, sub_done; \
9c37ec0b
JM
607 int _FP_ADD_INTERNAL_ediff = X##_e - Y##_e; \
608 if (_FP_ADD_INTERNAL_ediff > 0) \
1e145589
JM
609 { \
610 R##_e = X##_e; \
611 R##_s = X##_s; \
612 if (Y##_e == 0) \
613 { \
614 /* Y is zero or denormalized. */ \
51ca9e29 615 if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589 616 { \
51ca9e29
JM
617 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
618 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
619 goto sub_done; \
620 } \
621 else \
622 { \
51ca9e29 623 FP_SET_EXCEPTION (FP_EX_DENORM); \
9c37ec0b
JM
624 _FP_ADD_INTERNAL_ediff--; \
625 if (_FP_ADD_INTERNAL_ediff == 0) \
1e145589 626 { \
51ca9e29 627 _FP_FRAC_SUB_##wc (R, X, Y); \
1e145589
JM
628 goto sub3; \
629 } \
630 if (X##_e == _FP_EXPMAX_##fs) \
631 { \
51ca9e29
JM
632 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
633 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
634 goto sub_done; \
635 } \
636 goto sub1; \
637 } \
638 } \
639 else if (X##_e == _FP_EXPMAX_##fs) \
640 { \
641 /* X is NaN or Inf, Y is normal. */ \
51ca9e29
JM
642 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
643 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
644 goto sub_done; \
645 } \
646 \
647 /* Insert implicit MSB of Y. */ \
51ca9e29 648 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
1e145589
JM
649 \
650 sub1: \
9c37ec0b
JM
651 /* Shift the mantissa of Y to the right \
652 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
653 later for the implicit MSB of X. */ \
654 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
655 _FP_FRAC_SRS_##wc (Y, _FP_ADD_INTERNAL_ediff, \
656 _FP_WFRACBITS_##fs); \
51ca9e29
JM
657 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
658 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
659 _FP_FRAC_SUB_##wc (R, X, Y); \
1e145589 660 } \
9c37ec0b 661 else if (_FP_ADD_INTERNAL_ediff < 0) \
1e145589 662 { \
9c37ec0b 663 _FP_ADD_INTERNAL_ediff = -_FP_ADD_INTERNAL_ediff; \
1e145589
JM
664 R##_e = Y##_e; \
665 R##_s = Y##_s; \
666 if (X##_e == 0) \
667 { \
668 /* X is zero or denormalized. */ \
51ca9e29 669 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589 670 { \
51ca9e29
JM
671 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
672 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
673 goto sub_done; \
674 } \
675 else \
676 { \
51ca9e29 677 FP_SET_EXCEPTION (FP_EX_DENORM); \
9c37ec0b
JM
678 _FP_ADD_INTERNAL_ediff--; \
679 if (_FP_ADD_INTERNAL_ediff == 0) \
1e145589 680 { \
51ca9e29 681 _FP_FRAC_SUB_##wc (R, Y, X); \
1e145589
JM
682 goto sub3; \
683 } \
684 if (Y##_e == _FP_EXPMAX_##fs) \
685 { \
51ca9e29
JM
686 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
687 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
688 goto sub_done; \
689 } \
690 goto sub2; \
691 } \
692 } \
693 else if (Y##_e == _FP_EXPMAX_##fs) \
694 { \
695 /* Y is NaN or Inf, X is normal. */ \
51ca9e29
JM
696 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
697 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
698 goto sub_done; \
699 } \
700 \
701 /* Insert implicit MSB of X. */ \
51ca9e29 702 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
1e145589
JM
703 \
704 sub2: \
9c37ec0b
JM
705 /* Shift the mantissa of X to the right \
706 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
707 later for the implicit MSB of Y. */ \
708 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
709 _FP_FRAC_SRS_##wc (X, _FP_ADD_INTERNAL_ediff, \
710 _FP_WFRACBITS_##fs); \
51ca9e29
JM
711 else if (!_FP_FRAC_ZEROP_##wc (X)) \
712 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
713 _FP_FRAC_SUB_##wc (R, Y, X); \
1e145589
JM
714 } \
715 else \
716 { \
717 /* ediff == 0. */ \
51ca9e29 718 if (!_FP_EXP_NORMAL (fs, wc, X)) \
1e145589
JM
719 { \
720 if (X##_e == 0) \
721 { \
722 /* X and Y are zero or denormalized. */ \
723 R##_e = 0; \
51ca9e29 724 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589 725 { \
51ca9e29
JM
726 _FP_FRAC_COPY_##wc (R, Y); \
727 if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589
JM
728 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
729 else \
730 { \
51ca9e29 731 FP_SET_EXCEPTION (FP_EX_DENORM); \
1e145589
JM
732 R##_s = Y##_s; \
733 } \
734 goto sub_done; \
735 } \
51ca9e29 736 else if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589 737 { \
51ca9e29
JM
738 FP_SET_EXCEPTION (FP_EX_DENORM); \
739 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
740 R##_s = X##_s; \
741 goto sub_done; \
742 } \
743 else \
744 { \
51ca9e29
JM
745 FP_SET_EXCEPTION (FP_EX_DENORM); \
746 _FP_FRAC_SUB_##wc (R, X, Y); \
1e145589 747 R##_s = X##_s; \
51ca9e29 748 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
1e145589
JM
749 { \
750 /* |X| < |Y|, negate result. */ \
51ca9e29 751 _FP_FRAC_SUB_##wc (R, Y, X); \
1e145589
JM
752 R##_s = Y##_s; \
753 } \
51ca9e29 754 else if (_FP_FRAC_ZEROP_##wc (R)) \
1e145589
JM
755 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
756 goto sub_done; \
757 } \
758 } \
759 else \
760 { \
761 /* X and Y are NaN or Inf, of opposite signs. */ \
51ca9e29
JM
762 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
763 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
1e145589 764 R##_e = _FP_EXPMAX_##fs; \
51ca9e29 765 if (_FP_FRAC_ZEROP_##wc (X)) \
1e145589 766 { \
51ca9e29 767 if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589
JM
768 { \
769 /* Inf - Inf. */ \
770 R##_s = _FP_NANSIGN_##fs; \
51ca9e29
JM
771 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
772 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
ff12c11f
JM
773 FP_SET_EXCEPTION (FP_EX_INVALID \
774 | FP_EX_INVALID_ISI); \
1e145589
JM
775 } \
776 else \
777 { \
778 /* Inf - NaN. */ \
779 R##_s = Y##_s; \
51ca9e29 780 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
781 } \
782 } \
783 else \
784 { \
51ca9e29 785 if (_FP_FRAC_ZEROP_##wc (Y)) \
1e145589
JM
786 { \
787 /* NaN - Inf. */ \
788 R##_s = X##_s; \
51ca9e29 789 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
790 } \
791 else \
792 { \
793 /* NaN - NaN. */ \
51ca9e29 794 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
1e145589
JM
795 } \
796 } \
797 goto sub_done; \
798 } \
799 } \
800 /* The exponents of X and Y, both normal, are equal. The \
801 implicit MSBs cancel. */ \
802 R##_e = X##_e; \
51ca9e29 803 _FP_FRAC_SUB_##wc (R, X, Y); \
1e145589 804 R##_s = X##_s; \
51ca9e29 805 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
1e145589
JM
806 { \
807 /* |X| < |Y|, negate result. */ \
51ca9e29 808 _FP_FRAC_SUB_##wc (R, Y, X); \
1e145589
JM
809 R##_s = Y##_s; \
810 } \
51ca9e29 811 else if (_FP_FRAC_ZEROP_##wc (R)) \
1e145589
JM
812 { \
813 R##_e = 0; \
814 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
815 goto sub_done; \
816 } \
817 goto norm; \
818 } \
819 sub3: \
51ca9e29 820 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
1e145589 821 { \
9c37ec0b 822 int _FP_ADD_INTERNAL_diff; \
1e145589
JM
823 /* Carry into most significant bit of larger one of X and Y, \
824 canceling it; renormalize. */ \
51ca9e29 825 _FP_FRAC_HIGH_##fs (R) &= _FP_IMPLBIT_SH_##fs - 1; \
1e145589 826 norm: \
9c37ec0b
JM
827 _FP_FRAC_CLZ_##wc (_FP_ADD_INTERNAL_diff, R); \
828 _FP_ADD_INTERNAL_diff -= _FP_WFRACXBITS_##fs; \
829 _FP_FRAC_SLL_##wc (R, _FP_ADD_INTERNAL_diff); \
830 if (R##_e <= _FP_ADD_INTERNAL_diff) \
1e145589
JM
831 { \
832 /* R is denormalized. */ \
9c37ec0b
JM
833 _FP_ADD_INTERNAL_diff \
834 = _FP_ADD_INTERNAL_diff - R##_e + 1; \
835 _FP_FRAC_SRS_##wc (R, _FP_ADD_INTERNAL_diff, \
836 _FP_WFRACBITS_##fs); \
1e145589
JM
837 R##_e = 0; \
838 } \
839 else \
840 { \
9c37ec0b 841 R##_e -= _FP_ADD_INTERNAL_diff; \
51ca9e29 842 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1e145589
JM
843 } \
844 } \
845 sub_done: ; \
846 } \
847 } \
848 while (0)
d876f532 849
51ca9e29 850#define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL (fs, wc, R, X, Y, '+')
1e145589
JM
851#define _FP_SUB(fs, wc, R, X, Y) \
852 do \
853 { \
51ca9e29 854 if (!(Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1e145589 855 Y##_s ^= 1; \
51ca9e29 856 _FP_ADD_INTERNAL (fs, wc, R, X, Y, '-'); \
1e145589
JM
857 } \
858 while (0)
d876f532
UD
859
860
c4fe3ea7 861/* Main negation routine. The input value is raw. */
d876f532 862
1e145589
JM
863#define _FP_NEG(fs, wc, R, X) \
864 do \
865 { \
51ca9e29 866 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
867 R##_e = X##_e; \
868 R##_s = 1 ^ X##_s; \
869 } \
870 while (0)
d876f532
UD
871
872
c4fe3ea7 873/* Main multiplication routine. The input values should be cooked. */
d876f532 874
1e145589
JM
875#define _FP_MUL(fs, wc, R, X, Y) \
876 do \
877 { \
878 R##_s = X##_s ^ Y##_s; \
879 R##_e = X##_e + Y##_e + 1; \
51ca9e29 880 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
1e145589 881 { \
51ca9e29 882 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
1e145589 883 R##_c = FP_CLS_NORMAL; \
77f01ab5 884 \
51ca9e29 885 _FP_MUL_MEAT_##fs (R, X, Y); \
77f01ab5 886 \
51ca9e29
JM
887 if (_FP_FRAC_OVERP_##wc (fs, R)) \
888 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
1e145589
JM
889 else \
890 R##_e--; \
891 break; \
77f01ab5 892 \
51ca9e29
JM
893 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
894 _FP_CHOOSENAN (fs, wc, R, X, Y, '*'); \
1e145589 895 break; \
77f01ab5 896 \
51ca9e29
JM
897 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
898 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
899 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1e145589 900 R##_s = X##_s; \
af1a265d 901 /* FALLTHRU */ \
77f01ab5 902 \
51ca9e29
JM
903 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
904 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
905 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
906 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
907 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
908 R##_c = X##_c; \
909 break; \
77f01ab5 910 \
51ca9e29
JM
911 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
912 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
913 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1e145589 914 R##_s = Y##_s; \
af1a265d 915 /* FALLTHRU */ \
77f01ab5 916 \
51ca9e29
JM
917 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
918 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
919 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
920 R##_c = Y##_c; \
921 break; \
77f01ab5 922 \
51ca9e29
JM
923 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
924 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1e145589
JM
925 R##_s = _FP_NANSIGN_##fs; \
926 R##_c = FP_CLS_NAN; \
51ca9e29 927 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
ff12c11f 928 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_IMZ); \
1e145589 929 break; \
77f01ab5 930 \
1e145589 931 default: \
068a6274 932 _FP_UNREACHABLE; \
1e145589
JM
933 } \
934 } \
935 while (0)
936
937
938/* Fused multiply-add. The input values should be cooked. */
939
940#define _FP_FMA(fs, wc, dwc, R, X, Y, Z) \
941 do \
942 { \
d7423856 943 __label__ done_fma; \
9c37ec0b
JM
944 FP_DECL_##fs (_FP_FMA_T); \
945 _FP_FMA_T##_s = X##_s ^ Y##_s; \
946 _FP_FMA_T##_e = X##_e + Y##_e + 1; \
51ca9e29 947 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
1e145589 948 { \
51ca9e29 949 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
1e145589
JM
950 switch (Z##_c) \
951 { \
952 case FP_CLS_INF: \
953 case FP_CLS_NAN: \
954 R##_s = Z##_s; \
51ca9e29 955 _FP_FRAC_COPY_##wc (R, Z); \
1e145589
JM
956 R##_c = Z##_c; \
957 break; \
958 \
959 case FP_CLS_ZERO: \
960 R##_c = FP_CLS_NORMAL; \
9c37ec0b
JM
961 R##_s = _FP_FMA_T##_s; \
962 R##_e = _FP_FMA_T##_e; \
1e145589 963 \
51ca9e29 964 _FP_MUL_MEAT_##fs (R, X, Y); \
1e145589 965 \
51ca9e29
JM
966 if (_FP_FRAC_OVERP_##wc (fs, R)) \
967 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
1e145589
JM
968 else \
969 R##_e--; \
970 break; \
971 \
972 case FP_CLS_NORMAL:; \
9c37ec0b
JM
973 _FP_FRAC_DECL_##dwc (_FP_FMA_TD); \
974 _FP_FRAC_DECL_##dwc (_FP_FMA_ZD); \
975 _FP_FRAC_DECL_##dwc (_FP_FMA_RD); \
976 _FP_MUL_MEAT_DW_##fs (_FP_FMA_TD, X, Y); \
977 R##_e = _FP_FMA_T##_e; \
978 int _FP_FMA_tsh \
979 = _FP_FRAC_HIGHBIT_DW_##dwc (fs, _FP_FMA_TD) == 0; \
980 _FP_FMA_T##_e -= _FP_FMA_tsh; \
981 int _FP_FMA_ediff = _FP_FMA_T##_e - Z##_e; \
982 if (_FP_FMA_ediff >= 0) \
1e145589 983 { \
9c37ec0b
JM
984 int _FP_FMA_shift \
985 = _FP_WFRACBITS_##fs - _FP_FMA_tsh - _FP_FMA_ediff; \
986 if (_FP_FMA_shift <= -_FP_WFRACBITS_##fs) \
987 _FP_FRAC_SET_##dwc (_FP_FMA_ZD, _FP_MINFRAC_##dwc); \
1e145589
JM
988 else \
989 { \
9c37ec0b
JM
990 _FP_FRAC_COPY_##dwc##_##wc (_FP_FMA_ZD, Z); \
991 if (_FP_FMA_shift < 0) \
992 _FP_FRAC_SRS_##dwc (_FP_FMA_ZD, -_FP_FMA_shift, \
51ca9e29 993 _FP_WFRACBITS_DW_##fs); \
9c37ec0b
JM
994 else if (_FP_FMA_shift > 0) \
995 _FP_FRAC_SLL_##dwc (_FP_FMA_ZD, _FP_FMA_shift); \
1e145589 996 } \
9c37ec0b
JM
997 R##_s = _FP_FMA_T##_s; \
998 if (_FP_FMA_T##_s == Z##_s) \
999 _FP_FRAC_ADD_##dwc (_FP_FMA_RD, _FP_FMA_TD, \
1000 _FP_FMA_ZD); \
1e145589
JM
1001 else \
1002 { \
9c37ec0b
JM
1003 _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_TD, \
1004 _FP_FMA_ZD); \
1005 if (_FP_FRAC_NEGP_##dwc (_FP_FMA_RD)) \
1e145589
JM
1006 { \
1007 R##_s = Z##_s; \
9c37ec0b
JM
1008 _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
1009 _FP_FMA_TD); \
1e145589
JM
1010 } \
1011 } \
1012 } \
1013 else \
1014 { \
1015 R##_e = Z##_e; \
1016 R##_s = Z##_s; \
9c37ec0b
JM
1017 _FP_FRAC_COPY_##dwc##_##wc (_FP_FMA_ZD, Z); \
1018 _FP_FRAC_SLL_##dwc (_FP_FMA_ZD, _FP_WFRACBITS_##fs); \
1019 int _FP_FMA_shift = -_FP_FMA_ediff - _FP_FMA_tsh; \
1020 if (_FP_FMA_shift >= _FP_WFRACBITS_DW_##fs) \
1021 _FP_FRAC_SET_##dwc (_FP_FMA_TD, _FP_MINFRAC_##dwc); \
1022 else if (_FP_FMA_shift > 0) \
1023 _FP_FRAC_SRS_##dwc (_FP_FMA_TD, _FP_FMA_shift, \
51ca9e29 1024 _FP_WFRACBITS_DW_##fs); \
9c37ec0b
JM
1025 if (Z##_s == _FP_FMA_T##_s) \
1026 _FP_FRAC_ADD_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
1027 _FP_FMA_TD); \
1e145589 1028 else \
9c37ec0b
JM
1029 _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
1030 _FP_FMA_TD); \
1e145589 1031 } \
9c37ec0b 1032 if (_FP_FRAC_ZEROP_##dwc (_FP_FMA_RD)) \
1e145589 1033 { \
9c37ec0b 1034 if (_FP_FMA_T##_s == Z##_s) \
1e145589
JM
1035 R##_s = Z##_s; \
1036 else \
1037 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
51ca9e29 1038 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1e145589
JM
1039 R##_c = FP_CLS_ZERO; \
1040 } \
1041 else \
1042 { \
9c37ec0b
JM
1043 int _FP_FMA_rlz; \
1044 _FP_FRAC_CLZ_##dwc (_FP_FMA_rlz, _FP_FMA_RD); \
1045 _FP_FMA_rlz -= _FP_WFRACXBITS_DW_##fs; \
1046 R##_e -= _FP_FMA_rlz; \
1047 int _FP_FMA_shift = _FP_WFRACBITS_##fs - _FP_FMA_rlz; \
1048 if (_FP_FMA_shift > 0) \
1049 _FP_FRAC_SRS_##dwc (_FP_FMA_RD, _FP_FMA_shift, \
51ca9e29 1050 _FP_WFRACBITS_DW_##fs); \
9c37ec0b
JM
1051 else if (_FP_FMA_shift < 0) \
1052 _FP_FRAC_SLL_##dwc (_FP_FMA_RD, -_FP_FMA_shift); \
1053 _FP_FRAC_COPY_##wc##_##dwc (R, _FP_FMA_RD); \
1e145589
JM
1054 R##_c = FP_CLS_NORMAL; \
1055 } \
1056 break; \
1057 } \
1058 goto done_fma; \
1059 \
51ca9e29 1060 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
9c37ec0b 1061 _FP_CHOOSENAN (fs, wc, _FP_FMA_T, X, Y, '*'); \
1e145589
JM
1062 break; \
1063 \
51ca9e29
JM
1064 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1065 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1066 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
9c37ec0b 1067 _FP_FMA_T##_s = X##_s; \
af1a265d 1068 /* FALLTHRU */ \
1e145589 1069 \
51ca9e29
JM
1070 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1071 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1072 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1073 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
9c37ec0b
JM
1074 _FP_FRAC_COPY_##wc (_FP_FMA_T, X); \
1075 _FP_FMA_T##_c = X##_c; \
1e145589
JM
1076 break; \
1077 \
51ca9e29
JM
1078 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1079 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1080 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
9c37ec0b 1081 _FP_FMA_T##_s = Y##_s; \
af1a265d 1082 /* FALLTHRU */ \
1e145589 1083 \
51ca9e29
JM
1084 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1085 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
9c37ec0b
JM
1086 _FP_FRAC_COPY_##wc (_FP_FMA_T, Y); \
1087 _FP_FMA_T##_c = Y##_c; \
1e145589
JM
1088 break; \
1089 \
51ca9e29
JM
1090 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1091 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
9c37ec0b
JM
1092 _FP_FMA_T##_s = _FP_NANSIGN_##fs; \
1093 _FP_FMA_T##_c = FP_CLS_NAN; \
1094 _FP_FRAC_SET_##wc (_FP_FMA_T, _FP_NANFRAC_##fs); \
ff12c11f 1095 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_IMZ_FMA); \
1e145589
JM
1096 break; \
1097 \
1098 default: \
068a6274 1099 _FP_UNREACHABLE; \
1e145589
JM
1100 } \
1101 \
1102 /* T = X * Y is zero, infinity or NaN. */ \
9c37ec0b 1103 switch (_FP_CLS_COMBINE (_FP_FMA_T##_c, Z##_c)) \
1e145589 1104 { \
51ca9e29 1105 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
9c37ec0b 1106 _FP_CHOOSENAN (fs, wc, R, _FP_FMA_T, Z, '+'); \
1e145589
JM
1107 break; \
1108 \
51ca9e29
JM
1109 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1110 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1111 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1112 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1113 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
9c37ec0b
JM
1114 R##_s = _FP_FMA_T##_s; \
1115 _FP_FRAC_COPY_##wc (R, _FP_FMA_T); \
1116 R##_c = _FP_FMA_T##_c; \
1e145589
JM
1117 break; \
1118 \
51ca9e29
JM
1119 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1120 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1121 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1122 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1e145589 1123 R##_s = Z##_s; \
51ca9e29 1124 _FP_FRAC_COPY_##wc (R, Z); \
1e145589 1125 R##_c = Z##_c; \
1c7a4a51 1126 R##_e = Z##_e; \
1e145589
JM
1127 break; \
1128 \
51ca9e29 1129 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
9c37ec0b 1130 if (_FP_FMA_T##_s == Z##_s) \
1e145589
JM
1131 { \
1132 R##_s = Z##_s; \
51ca9e29 1133 _FP_FRAC_COPY_##wc (R, Z); \
1e145589
JM
1134 R##_c = Z##_c; \
1135 } \
1136 else \
1137 { \
1138 R##_s = _FP_NANSIGN_##fs; \
1139 R##_c = FP_CLS_NAN; \
51ca9e29 1140 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
ff12c11f 1141 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_ISI); \
1e145589
JM
1142 } \
1143 break; \
1144 \
51ca9e29 1145 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
9c37ec0b 1146 if (_FP_FMA_T##_s == Z##_s) \
1e145589
JM
1147 R##_s = Z##_s; \
1148 else \
1149 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
51ca9e29 1150 _FP_FRAC_COPY_##wc (R, Z); \
1e145589
JM
1151 R##_c = Z##_c; \
1152 break; \
1153 \
1154 default: \
068a6274 1155 _FP_UNREACHABLE; \
1e145589
JM
1156 } \
1157 done_fma: ; \
1158 } \
1159 while (0)
1160
1161
c4fe3ea7 1162/* Main division routine. The input values should be cooked. */
1e145589
JM
1163
1164#define _FP_DIV(fs, wc, R, X, Y) \
1165 do \
1166 { \
1167 R##_s = X##_s ^ Y##_s; \
1168 R##_e = X##_e - Y##_e; \
51ca9e29 1169 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
1e145589 1170 { \
51ca9e29 1171 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
1e145589 1172 R##_c = FP_CLS_NORMAL; \
77f01ab5 1173 \
51ca9e29 1174 _FP_DIV_MEAT_##fs (R, X, Y); \
1e145589 1175 break; \
77f01ab5 1176 \
51ca9e29
JM
1177 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1178 _FP_CHOOSENAN (fs, wc, R, X, Y, '/'); \
1e145589 1179 break; \
77f01ab5 1180 \
51ca9e29
JM
1181 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1182 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1183 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1e145589 1184 R##_s = X##_s; \
51ca9e29 1185 _FP_FRAC_COPY_##wc (R, X); \
1e145589
JM
1186 R##_c = X##_c; \
1187 break; \
77f01ab5 1188 \
51ca9e29
JM
1189 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1190 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1191 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1e145589 1192 R##_s = Y##_s; \
51ca9e29 1193 _FP_FRAC_COPY_##wc (R, Y); \
1e145589
JM
1194 R##_c = Y##_c; \
1195 break; \
77f01ab5 1196 \
51ca9e29
JM
1197 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1198 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1199 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1e145589
JM
1200 R##_c = FP_CLS_ZERO; \
1201 break; \
77f01ab5 1202 \
51ca9e29
JM
1203 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1204 FP_SET_EXCEPTION (FP_EX_DIVZERO); \
af1a265d 1205 /* FALLTHRU */ \
51ca9e29
JM
1206 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1207 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1e145589
JM
1208 R##_c = FP_CLS_INF; \
1209 break; \
77f01ab5 1210 \
51ca9e29
JM
1211 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1212 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1e145589
JM
1213 R##_s = _FP_NANSIGN_##fs; \
1214 R##_c = FP_CLS_NAN; \
51ca9e29 1215 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
ff12c11f
JM
1216 FP_SET_EXCEPTION (FP_EX_INVALID \
1217 | (X##_c == FP_CLS_INF \
1218 ? FP_EX_INVALID_IDI \
1219 : FP_EX_INVALID_ZDZ)); \
1e145589 1220 break; \
77f01ab5 1221 \
1e145589 1222 default: \
068a6274 1223 _FP_UNREACHABLE; \
1e145589
JM
1224 } \
1225 } \
1226 while (0)
d876f532
UD
1227
1228
73e28d9c
JM
1229/* Helper for comparisons. EX is 0 not to raise exceptions, 1 to
1230 raise exceptions for signaling NaN operands, 2 to raise exceptions
ff12c11f
JM
1231 for all NaN operands. Conditionals are organized to allow the
1232 compiler to optimize away code based on the value of EX. */
73e28d9c 1233
ff12c11f
JM
1234#define _FP_CMP_CHECK_NAN(fs, wc, X, Y, ex) \
1235 do \
1236 { \
1237 /* The arguments are unordered, which may or may not result in \
1238 an exception. */ \
1239 if (ex) \
1240 { \
1241 /* At least some cases of unordered arguments result in \
1242 exceptions; check whether this is one. */ \
1243 if (FP_EX_INVALID_SNAN || FP_EX_INVALID_VC) \
1244 { \
1245 /* Check separately for each case of "invalid" \
1246 exceptions. */ \
1247 if ((ex) == 2) \
1248 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_VC); \
1249 if (_FP_ISSIGNAN (fs, wc, X) \
1250 || _FP_ISSIGNAN (fs, wc, Y)) \
1251 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SNAN); \
1252 } \
1253 /* Otherwise, we only need to check whether to raise an \
1254 exception, not which case or cases it is. */ \
1255 else if ((ex) == 2 \
1256 || _FP_ISSIGNAN (fs, wc, X) \
1257 || _FP_ISSIGNAN (fs, wc, Y)) \
1258 FP_SET_EXCEPTION (FP_EX_INVALID); \
1259 } \
1260 } \
73e28d9c
JM
1261 while (0)
1262
61f006c1
JM
1263/* Helper for comparisons. If denormal operands would raise an
1264 exception, check for them, and flush to zero as appropriate
1265 (otherwise, we need only check and flush to zero if it might affect
1266 the result, which is done later with _FP_CMP_CHECK_FLUSH_ZERO). */
1267#define _FP_CMP_CHECK_DENORM(fs, wc, X, Y) \
1268 do \
1269 { \
1270 if (FP_EX_DENORM != 0) \
1271 { \
1272 /* We must ensure the correct exceptions are raised for \
1273 denormal operands, even though this may not affect the \
1274 result of the comparison. */ \
1275 if (FP_DENORM_ZERO) \
1276 { \
1277 _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
1278 _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
1279 } \
1280 else \
1281 { \
1282 if ((X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X)) \
1283 || (Y##_e == 0 && !_FP_FRAC_ZEROP_##wc (Y))) \
1284 FP_SET_EXCEPTION (FP_EX_DENORM); \
1285 } \
1286 } \
1287 } \
1288 while (0)
1289
1290/* Helper for comparisons. Check for flushing denormals for zero if
1291 we didn't need to check earlier for any denormal operands. */
1292#define _FP_CMP_CHECK_FLUSH_ZERO(fs, wc, X, Y) \
1293 do \
1294 { \
1295 if (FP_EX_DENORM == 0) \
1296 { \
1297 _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
1298 _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
1299 } \
1300 } \
1301 while (0)
1302
c4fe3ea7 1303/* Main differential comparison routine. The inputs should be raw not
73e28d9c
JM
1304 cooked. The return is -1, 0, 1 for normal values, UN
1305 otherwise. */
d876f532 1306
73e28d9c 1307#define _FP_CMP(fs, wc, ret, X, Y, un, ex) \
1e145589
JM
1308 do \
1309 { \
61f006c1 1310 _FP_CMP_CHECK_DENORM (fs, wc, X, Y); \
c4fe3ea7 1311 /* NANs are unordered. */ \
51ca9e29
JM
1312 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1313 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1e145589 1314 { \
5c0508a3
JM
1315 (ret) = (un); \
1316 _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
1e145589
JM
1317 } \
1318 else \
1319 { \
9c37ec0b
JM
1320 int _FP_CMP_is_zero_x; \
1321 int _FP_CMP_is_zero_y; \
d876f532 1322 \
61f006c1 1323 _FP_CMP_CHECK_FLUSH_ZERO (fs, wc, X, Y); \
454ac701 1324 \
9c37ec0b
JM
1325 _FP_CMP_is_zero_x \
1326 = (!X##_e && _FP_FRAC_ZEROP_##wc (X)) ? 1 : 0; \
1327 _FP_CMP_is_zero_y \
1328 = (!Y##_e && _FP_FRAC_ZEROP_##wc (Y)) ? 1 : 0; \
d876f532 1329 \
9c37ec0b 1330 if (_FP_CMP_is_zero_x && _FP_CMP_is_zero_y) \
5c0508a3 1331 (ret) = 0; \
9c37ec0b 1332 else if (_FP_CMP_is_zero_x) \
5c0508a3 1333 (ret) = Y##_s ? 1 : -1; \
9c37ec0b 1334 else if (_FP_CMP_is_zero_y) \
5c0508a3 1335 (ret) = X##_s ? -1 : 1; \
1e145589 1336 else if (X##_s != Y##_s) \
5c0508a3 1337 (ret) = X##_s ? -1 : 1; \
1e145589 1338 else if (X##_e > Y##_e) \
5c0508a3 1339 (ret) = X##_s ? -1 : 1; \
1e145589 1340 else if (X##_e < Y##_e) \
5c0508a3 1341 (ret) = X##_s ? 1 : -1; \
51ca9e29 1342 else if (_FP_FRAC_GT_##wc (X, Y)) \
5c0508a3 1343 (ret) = X##_s ? -1 : 1; \
51ca9e29 1344 else if (_FP_FRAC_GT_##wc (Y, X)) \
5c0508a3 1345 (ret) = X##_s ? 1 : -1; \
1e145589 1346 else \
5c0508a3 1347 (ret) = 0; \
1e145589
JM
1348 } \
1349 } \
1350 while (0)
d876f532
UD
1351
1352
1353/* Simplification for strict equality. */
1354
73e28d9c 1355#define _FP_CMP_EQ(fs, wc, ret, X, Y, ex) \
1e145589
JM
1356 do \
1357 { \
61f006c1 1358 _FP_CMP_CHECK_DENORM (fs, wc, X, Y); \
c4fe3ea7 1359 /* NANs are unordered. */ \
51ca9e29
JM
1360 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1361 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1e145589 1362 { \
5c0508a3
JM
1363 (ret) = 1; \
1364 _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
1e145589
JM
1365 } \
1366 else \
1367 { \
61f006c1 1368 _FP_CMP_CHECK_FLUSH_ZERO (fs, wc, X, Y); \
454ac701 1369 \
5c0508a3
JM
1370 (ret) = !(X##_e == Y##_e \
1371 && _FP_FRAC_EQ_##wc (X, Y) \
1372 && (X##_s == Y##_s \
1373 || (!X##_e && _FP_FRAC_ZEROP_##wc (X)))); \
1e145589
JM
1374 } \
1375 } \
1376 while (0)
d876f532 1377
e7b8c7bc
RM
1378/* Version to test unordered. */
1379
73e28d9c 1380#define _FP_CMP_UNORD(fs, wc, ret, X, Y, ex) \
1e145589
JM
1381 do \
1382 { \
61f006c1 1383 _FP_CMP_CHECK_DENORM (fs, wc, X, Y); \
5c0508a3
JM
1384 (ret) = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1385 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))); \
73e28d9c 1386 if (ret) \
5c0508a3 1387 _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
1e145589
JM
1388 } \
1389 while (0)
e7b8c7bc 1390
c4fe3ea7 1391/* Main square root routine. The input value should be cooked. */
d876f532 1392
ff12c11f
JM
1393#define _FP_SQRT(fs, wc, R, X) \
1394 do \
1395 { \
1396 _FP_FRAC_DECL_##wc (_FP_SQRT_T); \
1397 _FP_FRAC_DECL_##wc (_FP_SQRT_S); \
1398 _FP_W_TYPE _FP_SQRT_q; \
1399 switch (X##_c) \
1400 { \
1401 case FP_CLS_NAN: \
1402 _FP_FRAC_COPY_##wc (R, X); \
1403 R##_s = X##_s; \
1404 R##_c = FP_CLS_NAN; \
1405 break; \
1406 case FP_CLS_INF: \
1407 if (X##_s) \
1408 { \
1409 R##_s = _FP_NANSIGN_##fs; \
1410 R##_c = FP_CLS_NAN; /* NAN */ \
1411 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1412 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
1413 } \
1414 else \
1415 { \
1416 R##_s = 0; \
1417 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
1418 } \
1419 break; \
1420 case FP_CLS_ZERO: \
1421 R##_s = X##_s; \
1422 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
1423 break; \
1424 case FP_CLS_NORMAL: \
1425 R##_s = 0; \
1426 if (X##_s) \
1427 { \
1428 R##_c = FP_CLS_NAN; /* NAN */ \
1429 R##_s = _FP_NANSIGN_##fs; \
1430 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1431 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
1432 break; \
1433 } \
1434 R##_c = FP_CLS_NORMAL; \
1435 if (X##_e & 1) \
1436 _FP_FRAC_SLL_##wc (X, 1); \
1437 R##_e = X##_e >> 1; \
1438 _FP_FRAC_SET_##wc (_FP_SQRT_S, _FP_ZEROFRAC_##wc); \
1439 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1440 _FP_SQRT_q = _FP_OVERFLOW_##fs >> 1; \
1441 _FP_SQRT_MEAT_##wc (R, _FP_SQRT_S, _FP_SQRT_T, X, \
1442 _FP_SQRT_q); \
1443 } \
1444 } \
1e145589 1445 while (0)
d876f532 1446
c4fe3ea7 1447/* Convert from FP to integer. Input is raw. */
d876f532
UD
1448
1449/* RSIGNED can have following values:
c4fe3ea7
JM
1450 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
1451 the result is either 0 or (2^rsize)-1 depending on the sign in such
1452 case.
1453 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1454 NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1455 depending on the sign in such case.
a736ec37
JM
1456 2: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1457 NV is set plus the result is reduced modulo 2^rsize.
c4fe3ea7
JM
1458 -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
1459 set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1460 depending on the sign in such case. */
fe0b1e85 1461#define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
1e145589 1462 do \
fe0b1e85 1463 { \
1e145589 1464 if (X##_e < _FP_EXPBIAS_##fs) \
fe0b1e85 1465 { \
5c0508a3 1466 (r) = 0; \
1e145589 1467 if (X##_e == 0) \
fe0b1e85 1468 { \
51ca9e29 1469 if (!_FP_FRAC_ZEROP_##wc (X)) \
1e145589 1470 { \
454ac701
JM
1471 if (!FP_DENORM_ZERO) \
1472 FP_SET_EXCEPTION (FP_EX_INEXACT); \
51ca9e29 1473 FP_SET_EXCEPTION (FP_EX_DENORM); \
1e145589 1474 } \
fe0b1e85 1475 } \
1e145589 1476 else \
51ca9e29 1477 FP_SET_EXCEPTION (FP_EX_INEXACT); \
fe0b1e85 1478 } \
5c0508a3 1479 else if ((rsigned) == 2 \
a736ec37
JM
1480 && (X##_e \
1481 >= ((_FP_EXPMAX_##fs \
5c0508a3 1482 < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1) \
a736ec37 1483 ? _FP_EXPMAX_##fs \
5c0508a3 1484 : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1))) \
a736ec37
JM
1485 { \
1486 /* Overflow resulting in 0. */ \
5c0508a3 1487 (r) = 0; \
a736ec37
JM
1488 FP_SET_EXCEPTION (FP_EX_INVALID \
1489 | FP_EX_INVALID_CVI \
1490 | ((FP_EX_INVALID_SNAN \
1491 && _FP_ISSIGNAN (fs, wc, X)) \
1492 ? FP_EX_INVALID_SNAN \
1493 : 0)); \
1494 } \
5c0508a3
JM
1495 else if ((rsigned) != 2 \
1496 && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + (rsize) \
a736ec37 1497 ? _FP_EXPMAX_##fs \
5c0508a3
JM
1498 : (_FP_EXPBIAS_##fs + (rsize) \
1499 - ((rsigned) > 0 || X##_s))) \
1500 || (!(rsigned) && X##_s))) \
fe0b1e85 1501 { \
1e145589
JM
1502 /* Overflow or converting to the most negative integer. */ \
1503 if (rsigned) \
1504 { \
5c0508a3
JM
1505 (r) = 1; \
1506 (r) <<= (rsize) - 1; \
1507 (r) -= 1 - X##_s; \
4e8afe69
JM
1508 } \
1509 else \
1510 { \
5c0508a3 1511 (r) = 0; \
4e8afe69 1512 if (!X##_s) \
5c0508a3 1513 (r) = ~(r); \
4e8afe69 1514 } \
a334319f 1515 \
5c0508a3
JM
1516 if (_FP_EXPBIAS_##fs + (rsize) - 1 < _FP_EXPMAX_##fs \
1517 && (rsigned) \
0022e688 1518 && X##_s \
5c0508a3 1519 && X##_e == _FP_EXPBIAS_##fs + (rsize) - 1) \
1e145589
JM
1520 { \
1521 /* Possibly converting to most negative integer; check the \
1522 mantissa. */ \
9c37ec0b 1523 int _FP_TO_INT_inexact = 0; \
5c0508a3 1524 (void) ((_FP_FRACBITS_##fs > (rsize)) \
51ca9e29 1525 ? ({ \
9c37ec0b 1526 _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
5c0508a3 1527 _FP_FRACBITS_##fs - (rsize), \
51ca9e29
JM
1528 _FP_FRACBITS_##fs); \
1529 0; \
1530 }) \
1531 : 0); \
1532 if (!_FP_FRAC_ZEROP_##wc (X)) \
ff12c11f 1533 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
9c37ec0b 1534 else if (_FP_TO_INT_inexact) \
51ca9e29 1535 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1e145589
JM
1536 } \
1537 else \
ff12c11f
JM
1538 FP_SET_EXCEPTION (FP_EX_INVALID \
1539 | FP_EX_INVALID_CVI \
1540 | ((FP_EX_INVALID_SNAN \
1541 && _FP_ISSIGNAN (fs, wc, X)) \
1542 ? FP_EX_INVALID_SNAN \
1543 : 0)); \
fe0b1e85
RM
1544 } \
1545 else \
1e145589 1546 { \
a736ec37 1547 int _FP_TO_INT_inexact = 0; \
51ca9e29 1548 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1e145589
JM
1549 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1550 { \
5c0508a3
JM
1551 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1552 (r) <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1e145589
JM
1553 } \
1554 else \
1555 { \
9c37ec0b 1556 _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
51ca9e29
JM
1557 (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
1558 - X##_e), \
1559 _FP_FRACBITS_##fs); \
5c0508a3 1560 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1e145589 1561 } \
5c0508a3
JM
1562 if ((rsigned) && X##_s) \
1563 (r) = -(r); \
1564 if ((rsigned) == 2 && X##_e >= _FP_EXPBIAS_##fs + (rsize) - 1) \
a736ec37
JM
1565 { \
1566 /* Overflow or converting to the most negative integer. */ \
5c0508a3 1567 if (X##_e > _FP_EXPBIAS_##fs + (rsize) - 1 \
a736ec37 1568 || !X##_s \
5c0508a3 1569 || (r) != (((typeof (r)) 1) << ((rsize) - 1))) \
a736ec37
JM
1570 { \
1571 _FP_TO_INT_inexact = 0; \
1572 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1573 } \
1574 } \
1575 if (_FP_TO_INT_inexact) \
1576 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1e145589 1577 } \
fe0b1e85 1578 } \
1e145589
JM
1579 while (0)
1580
2004e7fb
JM
1581/* Convert from floating point to integer, rounding according to the
1582 current rounding direction. Input is raw. RSIGNED is as for
1583 _FP_TO_INT. */
1584#define _FP_TO_INT_ROUND(fs, wc, r, X, rsize, rsigned) \
1585 do \
1586 { \
d7423856 1587 __label__ _FP_TO_INT_ROUND_done; \
2004e7fb
JM
1588 if (X##_e < _FP_EXPBIAS_##fs) \
1589 { \
1590 int _FP_TO_INT_ROUND_rounds_away = 0; \
1591 if (X##_e == 0) \
1592 { \
1593 if (_FP_FRAC_ZEROP_##wc (X)) \
1594 { \
1595 (r) = 0; \
1596 goto _FP_TO_INT_ROUND_done; \
1597 } \
1598 else \
1599 { \
1600 FP_SET_EXCEPTION (FP_EX_DENORM); \
1601 if (FP_DENORM_ZERO) \
1602 { \
1603 (r) = 0; \
1604 goto _FP_TO_INT_ROUND_done; \
1605 } \
1606 } \
1607 } \
1608 /* The result is 0, 1 or -1 depending on the rounding mode; \
1609 -1 may cause overflow in the unsigned case. */ \
1610 switch (FP_ROUNDMODE) \
1611 { \
1612 case FP_RND_NEAREST: \
1613 _FP_TO_INT_ROUND_rounds_away \
1614 = (X##_e == _FP_EXPBIAS_##fs - 1 \
1615 && !_FP_FRAC_ZEROP_##wc (X)); \
1616 break; \
1617 case FP_RND_ZERO: \
1618 /* _FP_TO_INT_ROUND_rounds_away is already 0. */ \
1619 break; \
1620 case FP_RND_PINF: \
1621 _FP_TO_INT_ROUND_rounds_away = !X##_s; \
1622 break; \
1623 case FP_RND_MINF: \
1624 _FP_TO_INT_ROUND_rounds_away = X##_s; \
1625 break; \
1626 } \
1627 if ((rsigned) == 0 && _FP_TO_INT_ROUND_rounds_away && X##_s) \
1628 { \
1629 /* Result of -1 for an unsigned conversion. */ \
1630 (r) = 0; \
1631 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1632 } \
1633 else if ((rsize) == 1 && (rsigned) > 0 \
1634 && _FP_TO_INT_ROUND_rounds_away && !X##_s) \
1635 { \
1636 /* Converting to a 1-bit signed bit-field, which cannot \
1637 represent +1. */ \
1638 (r) = ((rsigned) == 2 ? -1 : 0); \
1639 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1640 } \
1641 else \
1642 { \
1643 (r) = (_FP_TO_INT_ROUND_rounds_away \
1644 ? (X##_s ? -1 : 1) \
1645 : 0); \
1646 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1647 } \
1648 } \
1649 else if ((rsigned) == 2 \
1650 && (X##_e \
1651 >= ((_FP_EXPMAX_##fs \
1652 < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1) \
1653 ? _FP_EXPMAX_##fs \
1654 : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1))) \
1655 { \
1656 /* Overflow resulting in 0. */ \
1657 (r) = 0; \
1658 FP_SET_EXCEPTION (FP_EX_INVALID \
1659 | FP_EX_INVALID_CVI \
1660 | ((FP_EX_INVALID_SNAN \
1661 && _FP_ISSIGNAN (fs, wc, X)) \
1662 ? FP_EX_INVALID_SNAN \
1663 : 0)); \
1664 } \
1665 else if ((rsigned) != 2 \
1666 && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + (rsize) \
1667 ? _FP_EXPMAX_##fs \
1668 : (_FP_EXPBIAS_##fs + (rsize) \
1669 - ((rsigned) > 0 && !X##_s))) \
1670 || ((rsigned) == 0 && X##_s))) \
1671 { \
1672 /* Definite overflow (does not require rounding to tell). */ \
1673 if ((rsigned) != 0) \
1674 { \
1675 (r) = 1; \
1676 (r) <<= (rsize) - 1; \
1677 (r) -= 1 - X##_s; \
1678 } \
1679 else \
1680 { \
1681 (r) = 0; \
1682 if (!X##_s) \
1683 (r) = ~(r); \
1684 } \
1685 \
1686 FP_SET_EXCEPTION (FP_EX_INVALID \
1687 | FP_EX_INVALID_CVI \
1688 | ((FP_EX_INVALID_SNAN \
1689 && _FP_ISSIGNAN (fs, wc, X)) \
1690 ? FP_EX_INVALID_SNAN \
1691 : 0)); \
1692 } \
1693 else \
1694 { \
1695 /* The value is finite, with magnitude at least 1. If \
1696 the conversion is unsigned, the value is positive. \
1697 If RSIGNED is not 2, the value does not definitely \
1698 overflow by virtue of its exponent, but may still turn \
1699 out to overflow after rounding; if RSIGNED is 2, the \
1700 exponent may be such that the value definitely overflows, \
1701 but at least one mantissa bit will not be shifted out. */ \
1702 int _FP_TO_INT_ROUND_inexact = 0; \
1703 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1704 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1705 { \
1706 /* The value is an integer, no rounding needed. */ \
1707 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1708 (r) <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1709 } \
1710 else \
1711 { \
1712 /* May need to shift in order to round (unless there \
1713 are exactly _FP_WORKBITS fractional bits already). */ \
1714 int _FP_TO_INT_ROUND_rshift \
1715 = (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs \
1716 - 1 - _FP_WORKBITS - X##_e); \
1717 if (_FP_TO_INT_ROUND_rshift > 0) \
1718 _FP_FRAC_SRS_##wc (X, _FP_TO_INT_ROUND_rshift, \
1719 _FP_WFRACBITS_##fs); \
1720 else if (_FP_TO_INT_ROUND_rshift < 0) \
1721 _FP_FRAC_SLL_##wc (X, -_FP_TO_INT_ROUND_rshift); \
1722 /* Round like _FP_ROUND, but setting \
1723 _FP_TO_INT_ROUND_inexact instead of directly setting \
1724 the "inexact" exception, since it may turn out we \
1725 should set "invalid" instead. */ \
1726 if (_FP_FRAC_LOW_##wc (X) & 7) \
1727 { \
1728 _FP_TO_INT_ROUND_inexact = 1; \
1729 switch (FP_ROUNDMODE) \
1730 { \
1731 case FP_RND_NEAREST: \
1732 _FP_ROUND_NEAREST (wc, X); \
1733 break; \
1734 case FP_RND_ZERO: \
1735 _FP_ROUND_ZERO (wc, X); \
1736 break; \
1737 case FP_RND_PINF: \
1738 _FP_ROUND_PINF (wc, X); \
1739 break; \
1740 case FP_RND_MINF: \
1741 _FP_ROUND_MINF (wc, X); \
1742 break; \
1743 } \
1744 } \
1745 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
1746 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1747 } \
1748 if ((rsigned) != 0 && X##_s) \
1749 (r) = -(r); \
1750 /* An exponent of RSIZE - 1 always needs testing for \
1751 overflow (either directly overflowing, or overflowing \
1752 when rounding up results in 2^RSIZE). An exponent of \
1753 RSIZE - 2 can overflow for positive values when rounding \
1754 up to 2^(RSIZE-1), but cannot overflow for negative \
1755 values. Smaller exponents cannot overflow. */ \
1756 if (X##_e >= (_FP_EXPBIAS_##fs + (rsize) - 1 \
1757 - ((rsigned) > 0 && !X##_s))) \
1758 { \
1759 if (X##_e > _FP_EXPBIAS_##fs + (rsize) - 1 \
1760 || (X##_e == _FP_EXPBIAS_##fs + (rsize) - 1 \
1761 && (X##_s \
1762 ? (r) != (((typeof (r)) 1) << ((rsize) - 1)) \
1763 : ((rsigned) > 0 || (r) == 0))) \
1764 || ((rsigned) > 0 \
1765 && !X##_s \
1766 && X##_e == _FP_EXPBIAS_##fs + (rsize) - 2 \
1767 && (r) == (((typeof (r)) 1) << ((rsize) - 1)))) \
1768 { \
1769 if ((rsigned) != 2) \
1770 { \
1771 if ((rsigned) != 0) \
1772 { \
1773 (r) = 1; \
1774 (r) <<= (rsize) - 1; \
1775 (r) -= 1 - X##_s; \
1776 } \
1777 else \
1778 { \
1779 (r) = 0; \
1780 (r) = ~(r); \
1781 } \
1782 } \
1783 _FP_TO_INT_ROUND_inexact = 0; \
1784 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1785 } \
1786 } \
1787 if (_FP_TO_INT_ROUND_inexact) \
1788 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1789 } \
1790 _FP_TO_INT_ROUND_done: ; \
1791 } \
1792 while (0)
1793
1e145589
JM
1794/* Convert integer to fp. Output is raw. RTYPE is unsigned even if
1795 input is signed. */
1796#define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
1797 do \
fe0b1e85 1798 { \
d7423856 1799 __label__ pack_semiraw; \
1e145589 1800 if (r) \
fe0b1e85 1801 { \
85bae5a1 1802 rtype _FP_FROM_INT_ur = (r); \
1e145589 1803 \
5c0508a3 1804 if ((X##_s = ((r) < 0))) \
85bae5a1 1805 _FP_FROM_INT_ur = -_FP_FROM_INT_ur; \
1e145589 1806 \
7d67a196
JM
1807 _FP_STATIC_ASSERT ((rsize) <= 2 * _FP_W_TYPE_SIZE, \
1808 "rsize too large"); \
5c0508a3 1809 (void) (((rsize) <= _FP_W_TYPE_SIZE) \
51ca9e29 1810 ? ({ \
9c37ec0b
JM
1811 int _FP_FROM_INT_lz; \
1812 __FP_CLZ (_FP_FROM_INT_lz, \
1813 (_FP_W_TYPE) _FP_FROM_INT_ur); \
1814 X##_e = (_FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 \
1815 - _FP_FROM_INT_lz); \
51ca9e29 1816 }) \
7d67a196
JM
1817 : ({ \
1818 int _FP_FROM_INT_lz; \
1819 __FP_CLZ_2 (_FP_FROM_INT_lz, \
1820 (_FP_W_TYPE) (_FP_FROM_INT_ur \
1821 >> _FP_W_TYPE_SIZE), \
1822 (_FP_W_TYPE) _FP_FROM_INT_ur); \
1823 X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
1824 - _FP_FROM_INT_lz); \
1825 })); \
1e145589 1826 \
5c0508a3 1827 if ((rsize) - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
1e145589
JM
1828 && X##_e >= _FP_EXPMAX_##fs) \
1829 { \
1830 /* Exponent too big; overflow to infinity. (May also \
1831 happen after rounding below.) */ \
51ca9e29 1832 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
1e145589
JM
1833 goto pack_semiraw; \
1834 } \
1835 \
5c0508a3 1836 if ((rsize) <= _FP_FRACBITS_##fs \
1e145589
JM
1837 || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
1838 { \
1839 /* Exactly representable; shift left. */ \
5c0508a3 1840 _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, (rsize)); \
1e145589 1841 if (_FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1 - X##_e > 0) \
51ca9e29
JM
1842 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1843 + _FP_FRACBITS_##fs - 1 - X##_e)); \
1e145589
JM
1844 } \
1845 else \
1846 { \
1847 /* More bits in integer than in floating type; need to \
1848 round. */ \
1849 if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
9c37ec0b
JM
1850 _FP_FROM_INT_ur \
1851 = ((_FP_FROM_INT_ur >> (X##_e - _FP_EXPBIAS_##fs \
1852 - _FP_WFRACBITS_##fs + 1)) \
1853 | ((_FP_FROM_INT_ur \
5c0508a3
JM
1854 << ((rsize) - (X##_e - _FP_EXPBIAS_##fs \
1855 - _FP_WFRACBITS_##fs + 1))) \
9c37ec0b 1856 != 0)); \
5c0508a3 1857 _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, (rsize)); \
1e145589 1858 if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
51ca9e29
JM
1859 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1860 + _FP_WFRACBITS_##fs - 1 - X##_e)); \
1861 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1e145589 1862 pack_semiraw: \
51ca9e29 1863 _FP_PACK_SEMIRAW (fs, wc, X); \
1e145589 1864 } \
fe0b1e85
RM
1865 } \
1866 else \
1867 { \
1e145589
JM
1868 X##_s = 0; \
1869 X##_e = 0; \
51ca9e29 1870 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
fe0b1e85 1871 } \
fe0b1e85 1872 } \
1e145589 1873 while (0)
d876f532 1874
0ecb606c 1875
fe0b1e85 1876/* Extend from a narrower floating-point format to a wider one. Input
639e42eb
JM
1877 and output are raw. If CHECK_NAN, then signaling NaNs are
1878 converted to quiet with the "invalid" exception raised; otherwise
1879 signaling NaNs remain signaling with no exception. */
1880#define _FP_EXTEND_CNAN(dfs, sfs, dwc, swc, D, S, check_nan) \
1e145589
JM
1881 do \
1882 { \
7d67a196
JM
1883 _FP_STATIC_ASSERT (_FP_FRACBITS_##dfs >= _FP_FRACBITS_##sfs, \
1884 "destination mantissa narrower than source"); \
1885 _FP_STATIC_ASSERT ((_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
1886 >= _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs), \
1887 "destination max exponent smaller" \
1888 " than source"); \
1889 _FP_STATIC_ASSERT (((_FP_EXPBIAS_##dfs \
1890 >= (_FP_EXPBIAS_##sfs \
1891 + _FP_FRACBITS_##sfs - 1)) \
1892 || (_FP_EXPBIAS_##dfs == _FP_EXPBIAS_##sfs)), \
1893 "source subnormals do not all become normal," \
1894 " but bias not the same"); \
1e145589 1895 D##_s = S##_s; \
51ca9e29
JM
1896 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1897 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1e145589
JM
1898 { \
1899 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
51ca9e29 1900 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
1e145589
JM
1901 } \
1902 else \
1903 { \
1904 if (S##_e == 0) \
1905 { \
454ac701 1906 _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
51ca9e29 1907 if (_FP_FRAC_ZEROP_##swc (S)) \
1e145589
JM
1908 D##_e = 0; \
1909 else if (_FP_EXPBIAS_##dfs \
1910 < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
1911 { \
51ca9e29
JM
1912 FP_SET_EXCEPTION (FP_EX_DENORM); \
1913 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1914 - _FP_FRACBITS_##sfs)); \
1e145589 1915 D##_e = 0; \
43059f42
JM
1916 if (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW) \
1917 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
1e145589
JM
1918 } \
1919 else \
1920 { \
9c37ec0b 1921 int FP_EXTEND_lz; \
51ca9e29 1922 FP_SET_EXCEPTION (FP_EX_DENORM); \
9c37ec0b 1923 _FP_FRAC_CLZ_##swc (FP_EXTEND_lz, S); \
51ca9e29 1924 _FP_FRAC_SLL_##dwc (D, \
9c37ec0b 1925 FP_EXTEND_lz + _FP_FRACBITS_##dfs \
51ca9e29 1926 - _FP_FRACTBITS_##sfs); \
1e145589 1927 D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
9c37ec0b 1928 + _FP_FRACXBITS_##sfs - FP_EXTEND_lz); \
1e145589
JM
1929 } \
1930 } \
1931 else \
1932 { \
1933 D##_e = _FP_EXPMAX_##dfs; \
51ca9e29 1934 if (!_FP_FRAC_ZEROP_##swc (S)) \
1e145589 1935 { \
639e42eb 1936 if (check_nan && _FP_FRAC_SNANP (sfs, S)) \
ff12c11f
JM
1937 FP_SET_EXCEPTION (FP_EX_INVALID \
1938 | FP_EX_INVALID_SNAN); \
51ca9e29
JM
1939 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1940 - _FP_FRACBITS_##sfs)); \
639e42eb
JM
1941 if (check_nan) \
1942 _FP_SETQNAN (dfs, dwc, D); \
1e145589
JM
1943 } \
1944 } \
1945 } \
1946 } \
1947 while (0)
fe0b1e85 1948
639e42eb
JM
1949#define FP_EXTEND(dfs, sfs, dwc, swc, D, S) \
1950 _FP_EXTEND_CNAN (dfs, sfs, dwc, swc, D, S, 1)
1951
fe0b1e85
RM
1952/* Truncate from a wider floating-point format to a narrower one.
1953 Input and output are semi-raw. */
51ca9e29 1954#define FP_TRUNC(dfs, sfs, dwc, swc, D, S) \
1e145589
JM
1955 do \
1956 { \
7d67a196
JM
1957 _FP_STATIC_ASSERT (_FP_FRACBITS_##sfs >= _FP_FRACBITS_##dfs, \
1958 "destination mantissa wider than source"); \
1959 _FP_STATIC_ASSERT (((_FP_EXPBIAS_##sfs \
1960 >= (_FP_EXPBIAS_##dfs \
1961 + _FP_FRACBITS_##dfs - 1)) \
1962 || _FP_EXPBIAS_##sfs == _FP_EXPBIAS_##dfs), \
1963 "source subnormals do not all become same," \
1964 " but bias not the same"); \
1e145589 1965 D##_s = S##_s; \
51ca9e29 1966 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1e145589
JM
1967 { \
1968 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1969 if (D##_e >= _FP_EXPMAX_##dfs) \
51ca9e29 1970 _FP_OVERFLOW_SEMIRAW (dfs, dwc, D); \
1e145589
JM
1971 else \
1972 { \
1973 if (D##_e <= 0) \
1974 { \
1975 if (D##_e < 1 - _FP_FRACBITS_##dfs) \
1976 { \
51ca9e29
JM
1977 _FP_FRAC_SET_##swc (S, _FP_ZEROFRAC_##swc); \
1978 _FP_FRAC_LOW_##swc (S) |= 1; \
1e145589
JM
1979 } \
1980 else \
1981 { \
51ca9e29
JM
1982 _FP_FRAC_HIGH_##sfs (S) |= _FP_IMPLBIT_SH_##sfs; \
1983 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1984 - _FP_WFRACBITS_##dfs \
1985 + 1 - D##_e), \
1986 _FP_WFRACBITS_##sfs); \
1e145589
JM
1987 } \
1988 D##_e = 0; \
1989 } \
1990 else \
51ca9e29
JM
1991 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1992 - _FP_WFRACBITS_##dfs), \
1993 _FP_WFRACBITS_##sfs); \
1994 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1e145589
JM
1995 } \
1996 } \
1997 else \
1998 { \
1999 if (S##_e == 0) \
2000 { \
454ac701 2001 _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
1e145589 2002 D##_e = 0; \
51ca9e29
JM
2003 if (_FP_FRAC_ZEROP_##swc (S)) \
2004 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1e145589
JM
2005 else \
2006 { \
51ca9e29 2007 FP_SET_EXCEPTION (FP_EX_DENORM); \
1e145589
JM
2008 if (_FP_EXPBIAS_##sfs \
2009 < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
2010 { \
51ca9e29
JM
2011 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
2012 - _FP_WFRACBITS_##dfs), \
2013 _FP_WFRACBITS_##sfs); \
2014 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1e145589
JM
2015 } \
2016 else \
2017 { \
51ca9e29
JM
2018 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
2019 _FP_FRAC_LOW_##dwc (D) |= 1; \
1e145589
JM
2020 } \
2021 } \
2022 } \
2023 else \
2024 { \
2025 D##_e = _FP_EXPMAX_##dfs; \
51ca9e29
JM
2026 if (_FP_FRAC_ZEROP_##swc (S)) \
2027 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1e145589
JM
2028 else \
2029 { \
51ca9e29
JM
2030 _FP_CHECK_SIGNAN_SEMIRAW (sfs, swc, S); \
2031 _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
2032 - _FP_WFRACBITS_##dfs)); \
2033 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1e145589 2034 /* Semi-raw NaN must have all workbits cleared. */ \
51ca9e29 2035 _FP_FRAC_LOW_##dwc (D) \
1e145589 2036 &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
51ca9e29 2037 _FP_SETQNAN_SEMIRAW (dfs, dwc, D); \
1e145589
JM
2038 } \
2039 } \
2040 } \
2041 } \
2042 while (0)
d876f532 2043
69a01461
JM
2044/* Truncate from a wider floating-point format to a narrower one.
2045 Input and output are cooked. */
2046#define FP_TRUNC_COOKED(dfs, sfs, dwc, swc, D, S) \
2047 do \
2048 { \
2049 _FP_STATIC_ASSERT (_FP_FRACBITS_##sfs >= _FP_FRACBITS_##dfs, \
2050 "destination mantissa wider than source"); \
2051 if (S##_c == FP_CLS_NAN) \
2052 _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
2053 - _FP_WFRACBITS_##dfs)); \
2054 else \
2055 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
2056 - _FP_WFRACBITS_##dfs), \
2057 _FP_WFRACBITS_##sfs); \
2058 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
2059 D##_e = S##_e; \
2060 D##_c = S##_c; \
2061 D##_s = S##_s; \
2062 } \
2063 while (0)
2064
c4fe3ea7 2065/* Helper primitives. */
d876f532
UD
2066
2067/* Count leading zeros in a word. */
2068
2069#ifndef __FP_CLZ
0d86378f 2070/* GCC 3.4 and later provide the builtins for us. */
1e145589
JM
2071# define __FP_CLZ(r, x) \
2072 do \
2073 { \
7d67a196
JM
2074 _FP_STATIC_ASSERT ((sizeof (_FP_W_TYPE) == sizeof (unsigned int) \
2075 || (sizeof (_FP_W_TYPE) \
2076 == sizeof (unsigned long)) \
2077 || (sizeof (_FP_W_TYPE) \
2078 == sizeof (unsigned long long))), \
2079 "_FP_W_TYPE size unsupported for clz"); \
1e145589 2080 if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
5c0508a3 2081 (r) = __builtin_clz (x); \
1e145589 2082 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
5c0508a3 2083 (r) = __builtin_clzl (x); \
7d67a196 2084 else /* sizeof (_FP_W_TYPE) == sizeof (unsigned long long). */ \
5c0508a3 2085 (r) = __builtin_clzll (x); \
1e145589
JM
2086 } \
2087 while (0)
d876f532
UD
2088#endif /* ndef __FP_CLZ */
2089
2090#define _FP_DIV_HELP_imm(q, r, n, d) \
1e145589
JM
2091 do \
2092 { \
5c0508a3 2093 (q) = (n) / (d), (r) = (n) % (d); \
1e145589
JM
2094 } \
2095 while (0)
d876f532 2096
1a8aaf91
UD
2097
2098/* A restoring bit-by-bit division primitive. */
2099
2100#define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
1e145589
JM
2101 do \
2102 { \
9c37ec0b
JM
2103 int _FP_DIV_MEAT_N_loop_count = _FP_WFRACBITS_##fs; \
2104 _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_u); \
2105 _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_v); \
2106 _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_u, X); \
2107 _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_v, Y); \
1e145589 2108 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
9c37ec0b
JM
2109 /* Normalize _FP_DIV_MEAT_N_LOOP_U and _FP_DIV_MEAT_N_LOOP_V. */ \
2110 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, _FP_WFRACXBITS_##fs); \
2111 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_v, _FP_WFRACXBITS_##fs); \
1e145589
JM
2112 /* First round. Since the operands are normalized, either the \
2113 first or second bit will be set in the fraction. Produce a \
2114 normalized result by checking which and adjusting the loop \
2115 count and exponent accordingly. */ \
9c37ec0b 2116 if (_FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, _FP_DIV_MEAT_N_loop_v)) \
1a8aaf91 2117 { \
9c37ec0b
JM
2118 _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
2119 _FP_DIV_MEAT_N_loop_u, \
2120 _FP_DIV_MEAT_N_loop_v); \
1a8aaf91 2121 _FP_FRAC_LOW_##wc (R) |= 1; \
9c37ec0b 2122 _FP_DIV_MEAT_N_loop_count--; \
1a8aaf91 2123 } \
1e145589
JM
2124 else \
2125 R##_e--; \
2126 /* Subsequent rounds. */ \
2127 do \
2128 { \
9c37ec0b
JM
2129 int _FP_DIV_MEAT_N_loop_msb \
2130 = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (_FP_DIV_MEAT_N_loop_u) < 0; \
2131 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, 1); \
1e145589 2132 _FP_FRAC_SLL_##wc (R, 1); \
9c37ec0b
JM
2133 if (_FP_DIV_MEAT_N_loop_msb \
2134 || _FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, \
2135 _FP_DIV_MEAT_N_loop_v)) \
1e145589 2136 { \
9c37ec0b
JM
2137 _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
2138 _FP_DIV_MEAT_N_loop_u, \
2139 _FP_DIV_MEAT_N_loop_v); \
1e145589
JM
2140 _FP_FRAC_LOW_##wc (R) |= 1; \
2141 } \
2142 } \
9c37ec0b
JM
2143 while (--_FP_DIV_MEAT_N_loop_count > 0); \
2144 /* If there's anything left in _FP_DIV_MEAT_N_LOOP_U, the result \
2145 is inexact. */ \
2146 _FP_FRAC_LOW_##wc (R) \
2147 |= !_FP_FRAC_ZEROP_##wc (_FP_DIV_MEAT_N_loop_u); \
1e145589
JM
2148 } \
2149 while (0)
1a8aaf91
UD
2150
2151#define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
2152#define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
2153#define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)
a2f8be9c
JM
2154
2155#endif /* !SOFT_FP_OP_COMMON_H */