2 * Helpers for HPPA instructions.
4 * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "exec/cpu_ldst.h"
26 void QEMU_NORETURN
HELPER(excp
)(CPUHPPAState
*env
, int excp
)
28 HPPACPU
*cpu
= hppa_env_get_cpu(env
);
29 CPUState
*cs
= CPU(cpu
);
31 cs
->exception_index
= excp
;
35 static void QEMU_NORETURN
dynexcp(CPUHPPAState
*env
, int excp
, uintptr_t ra
)
37 HPPACPU
*cpu
= hppa_env_get_cpu(env
);
38 CPUState
*cs
= CPU(cpu
);
40 cs
->exception_index
= excp
;
41 cpu_loop_exit_restore(cs
, ra
);
44 void HELPER(tsv
)(CPUHPPAState
*env
, target_ulong cond
)
46 if (unlikely((target_long
)cond
< 0)) {
47 dynexcp(env
, EXCP_SIGFPE
, GETPC());
51 void HELPER(tcond
)(CPUHPPAState
*env
, target_ulong cond
)
54 dynexcp(env
, EXCP_SIGFPE
, GETPC());
58 static void atomic_store_3(CPUHPPAState
*env
, target_ulong addr
, uint32_t val
,
59 uint32_t mask
, uintptr_t ra
)
61 uint32_t old
, new, cmp
;
63 #ifdef CONFIG_USER_ONLY
64 uint32_t *haddr
= g2h(addr
- 1);
67 new = (old
& ~mask
) | (val
& mask
);
68 cmp
= atomic_cmpxchg(haddr
, old
, new);
75 #error "Not implemented."
79 void HELPER(stby_b
)(CPUHPPAState
*env
, target_ulong addr
, target_ulong val
)
81 uintptr_t ra
= GETPC();
85 cpu_stb_data_ra(env
, addr
, val
, ra
);
88 cpu_stw_data_ra(env
, addr
, val
, ra
);
91 /* The 3 byte store must appear atomic. */
93 atomic_store_3(env
, addr
, val
, 0x00ffffffu
, ra
);
95 cpu_stb_data_ra(env
, addr
, val
>> 16, ra
);
96 cpu_stw_data_ra(env
, addr
+ 1, val
, ra
);
100 cpu_stl_data_ra(env
, addr
, val
, ra
);
105 void HELPER(stby_e
)(CPUHPPAState
*env
, target_ulong addr
, target_ulong val
)
107 uintptr_t ra
= GETPC();
111 /* The 3 byte store must appear atomic. */
113 atomic_store_3(env
, addr
- 3, val
, 0xffffff00u
, ra
);
115 cpu_stw_data_ra(env
, addr
- 3, val
>> 16, ra
);
116 cpu_stb_data_ra(env
, addr
- 1, val
>> 8, ra
);
120 cpu_stw_data_ra(env
, addr
- 2, val
>> 16, ra
);
123 cpu_stb_data_ra(env
, addr
- 1, val
>> 24, ra
);
126 /* Nothing is stored, but protection is checked and the
127 cacheline is marked dirty. */
128 #ifndef CONFIG_USER_ONLY
129 probe_write(env
, addr
, cpu_mmu_index(env
, 0), ra
);
135 target_ulong
HELPER(probe_r
)(target_ulong addr
)
137 return page_check_range(addr
, 1, PAGE_READ
);
140 target_ulong
HELPER(probe_w
)(target_ulong addr
)
142 return page_check_range(addr
, 1, PAGE_WRITE
);
145 void HELPER(loaded_fr0
)(CPUHPPAState
*env
)
147 uint32_t shadow
= env
->fr
[0] >> 32;
150 env
->fr0_shadow
= shadow
;
152 switch (extract32(shadow
, 9, 2)) {
154 rm
= float_round_nearest_even
;
157 rm
= float_round_to_zero
;
163 rm
= float_round_down
;
166 set_float_rounding_mode(rm
, &env
->fp_status
);
168 d
= extract32(shadow
, 5, 1);
169 set_flush_to_zero(d
, &env
->fp_status
);
170 set_flush_inputs_to_zero(d
, &env
->fp_status
);
173 void cpu_hppa_loaded_fr0(CPUHPPAState
*env
)
175 helper_loaded_fr0(env
);
178 #define CONVERT_BIT(X, SRC, DST) \
180 ? (X) / ((SRC) / (DST)) & (DST) \
181 : ((X) & (SRC)) * ((DST) / (SRC)))
183 static void update_fr0_op(CPUHPPAState
*env
, uintptr_t ra
)
185 uint32_t soft_exp
= get_float_exception_flags(&env
->fp_status
);
186 uint32_t hard_exp
= 0;
187 uint32_t shadow
= env
->fr0_shadow
;
189 if (likely(soft_exp
== 0)) {
190 env
->fr
[0] = (uint64_t)shadow
<< 32;
193 set_float_exception_flags(0, &env
->fp_status
);
195 hard_exp
|= CONVERT_BIT(soft_exp
, float_flag_inexact
, 1u << 0);
196 hard_exp
|= CONVERT_BIT(soft_exp
, float_flag_underflow
, 1u << 1);
197 hard_exp
|= CONVERT_BIT(soft_exp
, float_flag_overflow
, 1u << 2);
198 hard_exp
|= CONVERT_BIT(soft_exp
, float_flag_divbyzero
, 1u << 3);
199 hard_exp
|= CONVERT_BIT(soft_exp
, float_flag_invalid
, 1u << 4);
200 shadow
|= hard_exp
<< (32 - 5);
201 env
->fr0_shadow
= shadow
;
202 env
->fr
[0] = (uint64_t)shadow
<< 32;
204 if (hard_exp
& shadow
) {
205 dynexcp(env
, EXCP_SIGFPE
, ra
);
209 float32
HELPER(fsqrt_s
)(CPUHPPAState
*env
, float32 arg
)
211 float32 ret
= float32_sqrt(arg
, &env
->fp_status
);
212 update_fr0_op(env
, GETPC());
216 float32
HELPER(frnd_s
)(CPUHPPAState
*env
, float32 arg
)
218 float32 ret
= float32_round_to_int(arg
, &env
->fp_status
);
219 update_fr0_op(env
, GETPC());
223 float32
HELPER(fadd_s
)(CPUHPPAState
*env
, float32 a
, float32 b
)
225 float32 ret
= float32_add(a
, b
, &env
->fp_status
);
226 update_fr0_op(env
, GETPC());
230 float32
HELPER(fsub_s
)(CPUHPPAState
*env
, float32 a
, float32 b
)
232 float32 ret
= float32_sub(a
, b
, &env
->fp_status
);
233 update_fr0_op(env
, GETPC());
237 float32
HELPER(fmpy_s
)(CPUHPPAState
*env
, float32 a
, float32 b
)
239 float32 ret
= float32_mul(a
, b
, &env
->fp_status
);
240 update_fr0_op(env
, GETPC());
244 float32
HELPER(fdiv_s
)(CPUHPPAState
*env
, float32 a
, float32 b
)
246 float32 ret
= float32_div(a
, b
, &env
->fp_status
);
247 update_fr0_op(env
, GETPC());
251 float64
HELPER(fsqrt_d
)(CPUHPPAState
*env
, float64 arg
)
253 float64 ret
= float64_sqrt(arg
, &env
->fp_status
);
254 update_fr0_op(env
, GETPC());
258 float64
HELPER(frnd_d
)(CPUHPPAState
*env
, float64 arg
)
260 float64 ret
= float64_round_to_int(arg
, &env
->fp_status
);
261 update_fr0_op(env
, GETPC());
265 float64
HELPER(fadd_d
)(CPUHPPAState
*env
, float64 a
, float64 b
)
267 float64 ret
= float64_add(a
, b
, &env
->fp_status
);
268 update_fr0_op(env
, GETPC());
272 float64
HELPER(fsub_d
)(CPUHPPAState
*env
, float64 a
, float64 b
)
274 float64 ret
= float64_sub(a
, b
, &env
->fp_status
);
275 update_fr0_op(env
, GETPC());
279 float64
HELPER(fmpy_d
)(CPUHPPAState
*env
, float64 a
, float64 b
)
281 float64 ret
= float64_mul(a
, b
, &env
->fp_status
);
282 update_fr0_op(env
, GETPC());
286 float64
HELPER(fdiv_d
)(CPUHPPAState
*env
, float64 a
, float64 b
)
288 float64 ret
= float64_div(a
, b
, &env
->fp_status
);
289 update_fr0_op(env
, GETPC());
293 float64
HELPER(fcnv_s_d
)(CPUHPPAState
*env
, float32 arg
)
295 float64 ret
= float32_to_float64(arg
, &env
->fp_status
);
296 ret
= float64_maybe_silence_nan(ret
, &env
->fp_status
);
297 update_fr0_op(env
, GETPC());
301 float32
HELPER(fcnv_d_s
)(CPUHPPAState
*env
, float64 arg
)
303 float32 ret
= float64_to_float32(arg
, &env
->fp_status
);
304 ret
= float32_maybe_silence_nan(ret
, &env
->fp_status
);
305 update_fr0_op(env
, GETPC());
309 float32
HELPER(fcnv_w_s
)(CPUHPPAState
*env
, int32_t arg
)
311 float32 ret
= int32_to_float32(arg
, &env
->fp_status
);
312 update_fr0_op(env
, GETPC());
316 float32
HELPER(fcnv_dw_s
)(CPUHPPAState
*env
, int64_t arg
)
318 float32 ret
= int64_to_float32(arg
, &env
->fp_status
);
319 update_fr0_op(env
, GETPC());
323 float64
HELPER(fcnv_w_d
)(CPUHPPAState
*env
, int32_t arg
)
325 float64 ret
= int32_to_float64(arg
, &env
->fp_status
);
326 update_fr0_op(env
, GETPC());
330 float64
HELPER(fcnv_dw_d
)(CPUHPPAState
*env
, int64_t arg
)
332 float64 ret
= int64_to_float64(arg
, &env
->fp_status
);
333 update_fr0_op(env
, GETPC());
337 int32_t HELPER(fcnv_s_w
)(CPUHPPAState
*env
, float32 arg
)
339 int32_t ret
= float32_to_int32(arg
, &env
->fp_status
);
340 update_fr0_op(env
, GETPC());
344 int32_t HELPER(fcnv_d_w
)(CPUHPPAState
*env
, float64 arg
)
346 int32_t ret
= float64_to_int32(arg
, &env
->fp_status
);
347 update_fr0_op(env
, GETPC());
351 int64_t HELPER(fcnv_s_dw
)(CPUHPPAState
*env
, float32 arg
)
353 int64_t ret
= float32_to_int64(arg
, &env
->fp_status
);
354 update_fr0_op(env
, GETPC());
358 int64_t HELPER(fcnv_d_dw
)(CPUHPPAState
*env
, float64 arg
)
360 int64_t ret
= float64_to_int64(arg
, &env
->fp_status
);
361 update_fr0_op(env
, GETPC());
365 int32_t HELPER(fcnv_t_s_w
)(CPUHPPAState
*env
, float32 arg
)
367 int32_t ret
= float32_to_int32_round_to_zero(arg
, &env
->fp_status
);
368 update_fr0_op(env
, GETPC());
372 int32_t HELPER(fcnv_t_d_w
)(CPUHPPAState
*env
, float64 arg
)
374 int32_t ret
= float64_to_int32_round_to_zero(arg
, &env
->fp_status
);
375 update_fr0_op(env
, GETPC());
379 int64_t HELPER(fcnv_t_s_dw
)(CPUHPPAState
*env
, float32 arg
)
381 int64_t ret
= float32_to_int64_round_to_zero(arg
, &env
->fp_status
);
382 update_fr0_op(env
, GETPC());
386 int64_t HELPER(fcnv_t_d_dw
)(CPUHPPAState
*env
, float64 arg
)
388 int64_t ret
= float64_to_int64_round_to_zero(arg
, &env
->fp_status
);
389 update_fr0_op(env
, GETPC());
393 float32
HELPER(fcnv_uw_s
)(CPUHPPAState
*env
, uint32_t arg
)
395 float32 ret
= uint32_to_float32(arg
, &env
->fp_status
);
396 update_fr0_op(env
, GETPC());
400 float32
HELPER(fcnv_udw_s
)(CPUHPPAState
*env
, uint64_t arg
)
402 float32 ret
= uint64_to_float32(arg
, &env
->fp_status
);
403 update_fr0_op(env
, GETPC());
407 float64
HELPER(fcnv_uw_d
)(CPUHPPAState
*env
, uint32_t arg
)
409 float64 ret
= uint32_to_float64(arg
, &env
->fp_status
);
410 update_fr0_op(env
, GETPC());
414 float64
HELPER(fcnv_udw_d
)(CPUHPPAState
*env
, uint64_t arg
)
416 float64 ret
= uint64_to_float64(arg
, &env
->fp_status
);
417 update_fr0_op(env
, GETPC());
421 uint32_t HELPER(fcnv_s_uw
)(CPUHPPAState
*env
, float32 arg
)
423 uint32_t ret
= float32_to_uint32(arg
, &env
->fp_status
);
424 update_fr0_op(env
, GETPC());
428 uint32_t HELPER(fcnv_d_uw
)(CPUHPPAState
*env
, float64 arg
)
430 uint32_t ret
= float64_to_uint32(arg
, &env
->fp_status
);
431 update_fr0_op(env
, GETPC());
435 uint64_t HELPER(fcnv_s_udw
)(CPUHPPAState
*env
, float32 arg
)
437 uint64_t ret
= float32_to_uint64(arg
, &env
->fp_status
);
438 update_fr0_op(env
, GETPC());
442 uint64_t HELPER(fcnv_d_udw
)(CPUHPPAState
*env
, float64 arg
)
444 uint64_t ret
= float64_to_uint64(arg
, &env
->fp_status
);
445 update_fr0_op(env
, GETPC());
449 uint32_t HELPER(fcnv_t_s_uw
)(CPUHPPAState
*env
, float32 arg
)
451 uint32_t ret
= float32_to_uint32_round_to_zero(arg
, &env
->fp_status
);
452 update_fr0_op(env
, GETPC());
456 uint32_t HELPER(fcnv_t_d_uw
)(CPUHPPAState
*env
, float64 arg
)
458 uint32_t ret
= float64_to_uint32_round_to_zero(arg
, &env
->fp_status
);
459 update_fr0_op(env
, GETPC());
463 uint64_t HELPER(fcnv_t_s_udw
)(CPUHPPAState
*env
, float32 arg
)
465 uint64_t ret
= float32_to_uint64_round_to_zero(arg
, &env
->fp_status
);
466 update_fr0_op(env
, GETPC());
470 uint64_t HELPER(fcnv_t_d_udw
)(CPUHPPAState
*env
, float64 arg
)
472 uint64_t ret
= float64_to_uint64_round_to_zero(arg
, &env
->fp_status
);
473 update_fr0_op(env
, GETPC());
477 static void update_fr0_cmp(CPUHPPAState
*env
, uint32_t y
, uint32_t c
, int r
)
479 uint32_t shadow
= env
->fr0_shadow
;
482 case float_relation_greater
:
483 c
= extract32(c
, 4, 1);
485 case float_relation_less
:
486 c
= extract32(c
, 3, 1);
488 case float_relation_equal
:
489 c
= extract32(c
, 2, 1);
491 case float_relation_unordered
:
492 c
= extract32(c
, 1, 1);
495 g_assert_not_reached();
499 /* targeted comparison */
500 /* set fpsr[ca[y - 1]] to current compare */
501 shadow
= deposit32(shadow
, 21 - (y
- 1), 1, c
);
503 /* queued comparison */
504 /* shift cq right by one place */
505 shadow
= deposit32(shadow
, 11, 10, extract32(shadow
, 12, 10));
506 /* move fpsr[c] to fpsr[cq[0]] */
507 shadow
= deposit32(shadow
, 21, 1, extract32(shadow
, 26, 1));
508 /* set fpsr[c] to current compare */
509 shadow
= deposit32(shadow
, 26, 1, c
);
512 env
->fr0_shadow
= shadow
;
513 env
->fr
[0] = (uint64_t)shadow
<< 32;
516 void HELPER(fcmp_s
)(CPUHPPAState
*env
, float32 a
, float32 b
,
517 uint32_t y
, uint32_t c
)
521 r
= float32_compare(a
, b
, &env
->fp_status
);
523 r
= float32_compare_quiet(a
, b
, &env
->fp_status
);
525 update_fr0_op(env
, GETPC());
526 update_fr0_cmp(env
, y
, c
, r
);
529 void HELPER(fcmp_d
)(CPUHPPAState
*env
, float64 a
, float64 b
,
530 uint32_t y
, uint32_t c
)
534 r
= float64_compare(a
, b
, &env
->fp_status
);
536 r
= float64_compare_quiet(a
, b
, &env
->fp_status
);
538 update_fr0_op(env
, GETPC());
539 update_fr0_cmp(env
, y
, c
, r
);
542 float32
HELPER(fmpyfadd_s
)(CPUHPPAState
*env
, float32 a
, float32 b
, float32 c
)
544 float32 ret
= float32_muladd(a
, b
, c
, 0, &env
->fp_status
);
545 update_fr0_op(env
, GETPC());
549 float32
HELPER(fmpynfadd_s
)(CPUHPPAState
*env
, float32 a
, float32 b
, float32 c
)
551 float32 ret
= float32_muladd(a
, b
, c
, float_muladd_negate_product
,
553 update_fr0_op(env
, GETPC());
557 float64
HELPER(fmpyfadd_d
)(CPUHPPAState
*env
, float64 a
, float64 b
, float64 c
)
559 float64 ret
= float64_muladd(a
, b
, c
, 0, &env
->fp_status
);
560 update_fr0_op(env
, GETPC());
564 float64
HELPER(fmpynfadd_d
)(CPUHPPAState
*env
, float64 a
, float64 b
, float64 c
)
566 float64 ret
= float64_muladd(a
, b
, c
, float_muladd_negate_product
,
568 update_fr0_op(env
, GETPC());