Merge remote-tracking branch 'remotes/bonzini/scsi-next' into staging
[sdk/emulator/qemu.git] / target-s390x / fpu_helper.c
1 /*
2  *  S/390 FPU helper routines
3  *
4  *  Copyright (c) 2009 Ulrich Hecht
5  *  Copyright (c) 2009 Alexander Graf
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "cpu.h"
22 #include "helper.h"
23
24 #if !defined(CONFIG_USER_ONLY)
25 #include "exec/softmmu_exec.h"
26 #endif
27
28 /* #define DEBUG_HELPER */
29 #ifdef DEBUG_HELPER
30 #define HELPER_LOG(x...) qemu_log(x)
31 #else
32 #define HELPER_LOG(x...)
33 #endif
34
35 #define RET128(F) (env->retxl = F.low, F.high)
36
37 #define convert_bit(mask, from, to) \
38     (to < from                      \
39      ? (mask / (from / to)) & to    \
40      : (mask & from) * (to / from))
41
42 static void ieee_exception(CPUS390XState *env, uint32_t dxc, uintptr_t retaddr)
43 {
44     /* Install the DXC code.  */
45     env->fpc = (env->fpc & ~0xff00) | (dxc << 8);
46     /* Trap.  */
47     runtime_exception(env, PGM_DATA, retaddr);
48 }
49
50 /* Should be called after any operation that may raise IEEE exceptions.  */
51 static void handle_exceptions(CPUS390XState *env, uintptr_t retaddr)
52 {
53     unsigned s390_exc, qemu_exc;
54
55     /* Get the exceptions raised by the current operation.  Reset the
56        fpu_status contents so that the next operation has a clean slate.  */
57     qemu_exc = env->fpu_status.float_exception_flags;
58     if (qemu_exc == 0) {
59         return;
60     }
61     env->fpu_status.float_exception_flags = 0;
62
63     /* Convert softfloat exception bits to s390 exception bits.  */
64     s390_exc = 0;
65     s390_exc |= convert_bit(qemu_exc, float_flag_invalid, 0x80);
66     s390_exc |= convert_bit(qemu_exc, float_flag_divbyzero, 0x40);
67     s390_exc |= convert_bit(qemu_exc, float_flag_overflow, 0x20);
68     s390_exc |= convert_bit(qemu_exc, float_flag_underflow, 0x10);
69     s390_exc |= convert_bit(qemu_exc, float_flag_inexact, 0x08);
70
71     /* Install the exceptions that we raised.  */
72     env->fpc |= s390_exc << 16;
73
74     /* Send signals for enabled exceptions.  */
75     s390_exc &= env->fpc >> 24;
76     if (s390_exc) {
77         ieee_exception(env, s390_exc, retaddr);
78     }
79 }
80
81 static inline int float_comp_to_cc(CPUS390XState *env, int float_compare)
82 {
83     S390CPU *cpu = s390_env_get_cpu(env);
84
85     switch (float_compare) {
86     case float_relation_equal:
87         return 0;
88     case float_relation_less:
89         return 1;
90     case float_relation_greater:
91         return 2;
92     case float_relation_unordered:
93         return 3;
94     default:
95         cpu_abort(CPU(cpu), "unknown return value for float compare\n");
96     }
97 }
98
99 /* condition codes for unary FP ops */
100 uint32_t set_cc_nz_f32(float32 v)
101 {
102     if (float32_is_any_nan(v)) {
103         return 3;
104     } else if (float32_is_zero(v)) {
105         return 0;
106     } else if (float32_is_neg(v)) {
107         return 1;
108     } else {
109         return 2;
110     }
111 }
112
113 uint32_t set_cc_nz_f64(float64 v)
114 {
115     if (float64_is_any_nan(v)) {
116         return 3;
117     } else if (float64_is_zero(v)) {
118         return 0;
119     } else if (float64_is_neg(v)) {
120         return 1;
121     } else {
122         return 2;
123     }
124 }
125
126 uint32_t set_cc_nz_f128(float128 v)
127 {
128     if (float128_is_any_nan(v)) {
129         return 3;
130     } else if (float128_is_zero(v)) {
131         return 0;
132     } else if (float128_is_neg(v)) {
133         return 1;
134     } else {
135         return 2;
136     }
137 }
138
139 /* 32-bit FP addition */
140 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
141 {
142     float32 ret = float32_add(f1, f2, &env->fpu_status);
143     handle_exceptions(env, GETPC());
144     return ret;
145 }
146
147 /* 64-bit FP addition */
148 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
149 {
150     float64 ret = float64_add(f1, f2, &env->fpu_status);
151     handle_exceptions(env, GETPC());
152     return ret;
153 }
154
155 /* 128-bit FP addition */
156 uint64_t HELPER(axb)(CPUS390XState *env, uint64_t ah, uint64_t al,
157                      uint64_t bh, uint64_t bl)
158 {
159     float128 ret = float128_add(make_float128(ah, al),
160                                 make_float128(bh, bl),
161                                 &env->fpu_status);
162     handle_exceptions(env, GETPC());
163     return RET128(ret);
164 }
165
166 /* 32-bit FP subtraction */
167 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
168 {
169     float32 ret = float32_sub(f1, f2, &env->fpu_status);
170     handle_exceptions(env, GETPC());
171     return ret;
172 }
173
174 /* 64-bit FP subtraction */
175 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
176 {
177     float64 ret = float64_sub(f1, f2, &env->fpu_status);
178     handle_exceptions(env, GETPC());
179     return ret;
180 }
181
182 /* 128-bit FP subtraction */
183 uint64_t HELPER(sxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
184                      uint64_t bh, uint64_t bl)
185 {
186     float128 ret = float128_sub(make_float128(ah, al),
187                                 make_float128(bh, bl),
188                                 &env->fpu_status);
189     handle_exceptions(env, GETPC());
190     return RET128(ret);
191 }
192
193 /* 32-bit FP division */
194 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
195 {
196     float32 ret = float32_div(f1, f2, &env->fpu_status);
197     handle_exceptions(env, GETPC());
198     return ret;
199 }
200
201 /* 64-bit FP division */
202 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
203 {
204     float64 ret = float64_div(f1, f2, &env->fpu_status);
205     handle_exceptions(env, GETPC());
206     return ret;
207 }
208
209 /* 128-bit FP division */
210 uint64_t HELPER(dxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
211                      uint64_t bh, uint64_t bl)
212 {
213     float128 ret = float128_div(make_float128(ah, al),
214                                 make_float128(bh, bl),
215                                 &env->fpu_status);
216     handle_exceptions(env, GETPC());
217     return RET128(ret);
218 }
219
220 /* 32-bit FP multiplication */
221 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
222 {
223     float32 ret = float32_mul(f1, f2, &env->fpu_status);
224     handle_exceptions(env, GETPC());
225     return ret;
226 }
227
228 /* 64-bit FP multiplication */
229 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
230 {
231     float64 ret = float64_mul(f1, f2, &env->fpu_status);
232     handle_exceptions(env, GETPC());
233     return ret;
234 }
235
236 /* 64/32-bit FP multiplication */
237 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
238 {
239     float64 ret = float32_to_float64(f2, &env->fpu_status);
240     ret = float64_mul(f1, ret, &env->fpu_status);
241     handle_exceptions(env, GETPC());
242     return ret;
243 }
244
245 /* 128-bit FP multiplication */
246 uint64_t HELPER(mxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
247                      uint64_t bh, uint64_t bl)
248 {
249     float128 ret = float128_mul(make_float128(ah, al),
250                                 make_float128(bh, bl),
251                                 &env->fpu_status);
252     handle_exceptions(env, GETPC());
253     return RET128(ret);
254 }
255
256 /* 128/64-bit FP multiplication */
257 uint64_t HELPER(mxdb)(CPUS390XState *env, uint64_t ah, uint64_t al,
258                       uint64_t f2)
259 {
260     float128 ret = float64_to_float128(f2, &env->fpu_status);
261     ret = float128_mul(make_float128(ah, al), ret, &env->fpu_status);
262     handle_exceptions(env, GETPC());
263     return RET128(ret);
264 }
265
266 /* convert 32-bit float to 64-bit float */
267 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
268 {
269     float64 ret = float32_to_float64(f2, &env->fpu_status);
270     handle_exceptions(env, GETPC());
271     return ret;
272 }
273
274 /* convert 128-bit float to 64-bit float */
275 uint64_t HELPER(ldxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
276 {
277     float64 ret = float128_to_float64(make_float128(ah, al), &env->fpu_status);
278     handle_exceptions(env, GETPC());
279     return ret;
280 }
281
282 /* convert 64-bit float to 128-bit float */
283 uint64_t HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
284 {
285     float128 ret = float64_to_float128(f2, &env->fpu_status);
286     handle_exceptions(env, GETPC());
287     return RET128(ret);
288 }
289
290 /* convert 32-bit float to 128-bit float */
291 uint64_t HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
292 {
293     float128 ret = float32_to_float128(f2, &env->fpu_status);
294     handle_exceptions(env, GETPC());
295     return RET128(ret);
296 }
297
298 /* convert 64-bit float to 32-bit float */
299 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2)
300 {
301     float32 ret = float64_to_float32(f2, &env->fpu_status);
302     handle_exceptions(env, GETPC());
303     return ret;
304 }
305
306 /* convert 128-bit float to 32-bit float */
307 uint64_t HELPER(lexb)(CPUS390XState *env, uint64_t ah, uint64_t al)
308 {
309     float32 ret = float128_to_float32(make_float128(ah, al), &env->fpu_status);
310     handle_exceptions(env, GETPC());
311     return ret;
312 }
313
314 /* 32-bit FP compare */
315 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
316 {
317     int cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
318     handle_exceptions(env, GETPC());
319     return float_comp_to_cc(env, cmp);
320 }
321
322 /* 64-bit FP compare */
323 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
324 {
325     int cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
326     handle_exceptions(env, GETPC());
327     return float_comp_to_cc(env, cmp);
328 }
329
330 /* 128-bit FP compare */
331 uint32_t HELPER(cxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
332                      uint64_t bh, uint64_t bl)
333 {
334     int cmp = float128_compare_quiet(make_float128(ah, al),
335                                      make_float128(bh, bl),
336                                      &env->fpu_status);
337     handle_exceptions(env, GETPC());
338     return float_comp_to_cc(env, cmp);
339 }
340
341 static int swap_round_mode(CPUS390XState *env, int m3)
342 {
343     int ret = env->fpu_status.float_rounding_mode;
344     switch (m3) {
345     case 0:
346         /* current mode */
347         break;
348     case 1:
349         /* biased round no nearest */
350     case 4:
351         /* round to nearest */
352         set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
353         break;
354     case 5:
355         /* round to zero */
356         set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
357         break;
358     case 6:
359         /* round to +inf */
360         set_float_rounding_mode(float_round_up, &env->fpu_status);
361         break;
362     case 7:
363         /* round to -inf */
364         set_float_rounding_mode(float_round_down, &env->fpu_status);
365         break;
366     }
367     return ret;
368 }
369
370 /* convert 64-bit int to 32-bit float */
371 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m3)
372 {
373     int hold = swap_round_mode(env, m3);
374     float32 ret = int64_to_float32(v2, &env->fpu_status);
375     set_float_rounding_mode(hold, &env->fpu_status);
376     handle_exceptions(env, GETPC());
377     return ret;
378 }
379
380 /* convert 64-bit int to 64-bit float */
381 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m3)
382 {
383     int hold = swap_round_mode(env, m3);
384     float64 ret = int64_to_float64(v2, &env->fpu_status);
385     set_float_rounding_mode(hold, &env->fpu_status);
386     handle_exceptions(env, GETPC());
387     return ret;
388 }
389
390 /* convert 64-bit int to 128-bit float */
391 uint64_t HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m3)
392 {
393     int hold = swap_round_mode(env, m3);
394     float128 ret = int64_to_float128(v2, &env->fpu_status);
395     set_float_rounding_mode(hold, &env->fpu_status);
396     handle_exceptions(env, GETPC());
397     return RET128(ret);
398 }
399
400 /* convert 64-bit uint to 32-bit float */
401 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
402 {
403     int hold = swap_round_mode(env, m3);
404     float32 ret = uint64_to_float32(v2, &env->fpu_status);
405     set_float_rounding_mode(hold, &env->fpu_status);
406     handle_exceptions(env, GETPC());
407     return ret;
408 }
409
410 /* convert 64-bit uint to 64-bit float */
411 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
412 {
413     int hold = swap_round_mode(env, m3);
414     float64 ret = uint64_to_float64(v2, &env->fpu_status);
415     set_float_rounding_mode(hold, &env->fpu_status);
416     handle_exceptions(env, GETPC());
417     return ret;
418 }
419
420 /* convert 64-bit uint to 128-bit float */
421 uint64_t HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
422 {
423     int hold = swap_round_mode(env, m3);
424     float128 ret = uint64_to_float128(v2, &env->fpu_status);
425     set_float_rounding_mode(hold, &env->fpu_status);
426     handle_exceptions(env, GETPC());
427     return RET128(ret);
428 }
429
430 /* convert 32-bit float to 64-bit int */
431 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
432 {
433     int hold = swap_round_mode(env, m3);
434     int64_t ret = float32_to_int64(v2, &env->fpu_status);
435     set_float_rounding_mode(hold, &env->fpu_status);
436     handle_exceptions(env, GETPC());
437     return ret;
438 }
439
440 /* convert 64-bit float to 64-bit int */
441 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
442 {
443     int hold = swap_round_mode(env, m3);
444     int64_t ret = float64_to_int64(v2, &env->fpu_status);
445     set_float_rounding_mode(hold, &env->fpu_status);
446     handle_exceptions(env, GETPC());
447     return ret;
448 }
449
450 /* convert 128-bit float to 64-bit int */
451 uint64_t HELPER(cgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
452 {
453     int hold = swap_round_mode(env, m3);
454     float128 v2 = make_float128(h, l);
455     int64_t ret = float128_to_int64(v2, &env->fpu_status);
456     set_float_rounding_mode(hold, &env->fpu_status);
457     handle_exceptions(env, GETPC());
458     return ret;
459 }
460
461 /* convert 32-bit float to 32-bit int */
462 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
463 {
464     int hold = swap_round_mode(env, m3);
465     int32_t ret = float32_to_int32(v2, &env->fpu_status);
466     set_float_rounding_mode(hold, &env->fpu_status);
467     handle_exceptions(env, GETPC());
468     return ret;
469 }
470
471 /* convert 64-bit float to 32-bit int */
472 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
473 {
474     int hold = swap_round_mode(env, m3);
475     int32_t ret = float64_to_int32(v2, &env->fpu_status);
476     set_float_rounding_mode(hold, &env->fpu_status);
477     handle_exceptions(env, GETPC());
478     return ret;
479 }
480
481 /* convert 128-bit float to 32-bit int */
482 uint64_t HELPER(cfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
483 {
484     int hold = swap_round_mode(env, m3);
485     float128 v2 = make_float128(h, l);
486     int32_t ret = float128_to_int32(v2, &env->fpu_status);
487     set_float_rounding_mode(hold, &env->fpu_status);
488     handle_exceptions(env, GETPC());
489     return ret;
490 }
491
492 /* convert 32-bit float to 64-bit uint */
493 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
494 {
495     int hold = swap_round_mode(env, m3);
496     uint64_t ret;
497     v2 = float32_to_float64(v2, &env->fpu_status);
498     ret = float64_to_uint64(v2, &env->fpu_status);
499     set_float_rounding_mode(hold, &env->fpu_status);
500     handle_exceptions(env, GETPC());
501     return ret;
502 }
503
504 /* convert 64-bit float to 64-bit uint */
505 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
506 {
507     int hold = swap_round_mode(env, m3);
508     uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
509     set_float_rounding_mode(hold, &env->fpu_status);
510     handle_exceptions(env, GETPC());
511     return ret;
512 }
513
514 /* convert 128-bit float to 64-bit uint */
515 uint64_t HELPER(clgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
516 {
517     int hold = swap_round_mode(env, m3);
518     float128 v2 = make_float128(h, l);
519     /* ??? Not 100% correct.  */
520     uint64_t ret = float128_to_int64(v2, &env->fpu_status);
521     set_float_rounding_mode(hold, &env->fpu_status);
522     handle_exceptions(env, GETPC());
523     return ret;
524 }
525
526 /* convert 32-bit float to 32-bit uint */
527 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
528 {
529     int hold = swap_round_mode(env, m3);
530     uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
531     set_float_rounding_mode(hold, &env->fpu_status);
532     handle_exceptions(env, GETPC());
533     return ret;
534 }
535
536 /* convert 64-bit float to 32-bit uint */
537 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
538 {
539     int hold = swap_round_mode(env, m3);
540     uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
541     set_float_rounding_mode(hold, &env->fpu_status);
542     handle_exceptions(env, GETPC());
543     return ret;
544 }
545
546 /* convert 128-bit float to 32-bit uint */
547 uint64_t HELPER(clfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
548 {
549     int hold = swap_round_mode(env, m3);
550     float128 v2 = make_float128(h, l);
551     /* Not 100% correct.  */
552     uint32_t ret = float128_to_int64(v2, &env->fpu_status);
553     set_float_rounding_mode(hold, &env->fpu_status);
554     handle_exceptions(env, GETPC());
555     return ret;
556 }
557
558 /* 32-bit FP multiply and add */
559 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
560                       uint64_t f2, uint64_t f3)
561 {
562     float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
563     handle_exceptions(env, GETPC());
564     return ret;
565 }
566
567 /* 64-bit FP multiply and add */
568 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
569                       uint64_t f2, uint64_t f3)
570 {
571     float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
572     handle_exceptions(env, GETPC());
573     return ret;
574 }
575
576 /* 32-bit FP multiply and subtract */
577 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
578                       uint64_t f2, uint64_t f3)
579 {
580     float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
581                                  &env->fpu_status);
582     handle_exceptions(env, GETPC());
583     return ret;
584 }
585
586 /* 64-bit FP multiply and subtract */
587 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
588                       uint64_t f2, uint64_t f3)
589 {
590     float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
591                                  &env->fpu_status);
592     handle_exceptions(env, GETPC());
593     return ret;
594 }
595
596 /* test data class 32-bit */
597 uint32_t HELPER(tceb)(uint64_t f1, uint64_t m2)
598 {
599     float32 v1 = f1;
600     int neg = float32_is_neg(v1);
601     uint32_t cc = 0;
602
603     if ((float32_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
604         (float32_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
605         (float32_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
606         (float32_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
607         cc = 1;
608     } else if (m2 & (1 << (9-neg))) {
609         /* assume normalized number */
610         cc = 1;
611     }
612     /* FIXME: denormalized? */
613     return cc;
614 }
615
616 /* test data class 64-bit */
617 uint32_t HELPER(tcdb)(uint64_t v1, uint64_t m2)
618 {
619     int neg = float64_is_neg(v1);
620     uint32_t cc = 0;
621
622     if ((float64_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
623         (float64_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
624         (float64_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
625         (float64_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
626         cc = 1;
627     } else if (m2 & (1 << (9-neg))) {
628         /* assume normalized number */
629         cc = 1;
630     }
631     /* FIXME: denormalized? */
632     return cc;
633 }
634
635 /* test data class 128-bit */
636 uint32_t HELPER(tcxb)(uint64_t ah, uint64_t al, uint64_t m2)
637 {
638     float128 v1 = make_float128(ah, al);
639     int neg = float128_is_neg(v1);
640     uint32_t cc = 0;
641
642     if ((float128_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
643         (float128_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
644         (float128_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
645         (float128_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
646         cc = 1;
647     } else if (m2 & (1 << (9-neg))) {
648         /* assume normalized number */
649         cc = 1;
650     }
651     /* FIXME: denormalized? */
652     return cc;
653 }
654
655 /* square root 32-bit */
656 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
657 {
658     float32 ret = float32_sqrt(f2, &env->fpu_status);
659     handle_exceptions(env, GETPC());
660     return ret;
661 }
662
663 /* square root 64-bit */
664 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
665 {
666     float64 ret = float64_sqrt(f2, &env->fpu_status);
667     handle_exceptions(env, GETPC());
668     return ret;
669 }
670
671 /* square root 128-bit */
672 uint64_t HELPER(sqxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
673 {
674     float128 ret = float128_sqrt(make_float128(ah, al), &env->fpu_status);
675     handle_exceptions(env, GETPC());
676     return RET128(ret);
677 }
678
679 static const int fpc_to_rnd[4] = {
680     float_round_nearest_even,
681     float_round_to_zero,
682     float_round_up,
683     float_round_down
684 };
685
686 /* set fpc */
687 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
688 {
689     /* Install everything in the main FPC.  */
690     env->fpc = fpc;
691
692     /* Install the rounding mode in the shadow fpu_status.  */
693     set_float_rounding_mode(fpc_to_rnd[fpc & 3], &env->fpu_status);
694 }
695
696 /* set fpc and signal */
697 void HELPER(sfas)(CPUS390XState *env, uint64_t val)
698 {
699     uint32_t signalling = env->fpc;
700     uint32_t source = val;
701     uint32_t s390_exc;
702
703     /* The contents of the source operand are placed in the FPC register;
704        then the flags in the FPC register are set to the logical OR of the
705        signalling flags and the source flags.  */
706     env->fpc = source | (signalling & 0x00ff0000);
707     set_float_rounding_mode(fpc_to_rnd[source & 3], &env->fpu_status);
708
709     /* If any signalling flag is 1 and the corresponding source mask
710        is also 1, a simulated-iee-exception trap occurs.  */
711     s390_exc = (signalling >> 16) & (source >> 24);
712     if (s390_exc) {
713         ieee_exception(env, s390_exc | 3, GETPC());
714     }
715 }