target-tricore: Add instructions of RRR1 opcode format, which have 0xe3 as first...
[qemu/ar7.git] / target-tricore / op_helper.c
blob76d791123567f7f71b135beb8ca7a18c0bc53b03
1 /*
2 * Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 #include <stdlib.h>
18 #include "cpu.h"
19 #include "qemu/host-utils.h"
20 #include "exec/helper-proto.h"
21 #include "exec/cpu_ldst.h"
23 /* Addressing mode helper */
25 static uint16_t reverse16(uint16_t val)
27 uint8_t high = (uint8_t)(val >> 8);
28 uint8_t low = (uint8_t)(val & 0xff);
30 uint16_t rh, rl;
32 rl = (uint16_t)((high * 0x0202020202ULL & 0x010884422010ULL) % 1023);
33 rh = (uint16_t)((low * 0x0202020202ULL & 0x010884422010ULL) % 1023);
35 return (rh << 8) | rl;
38 uint32_t helper_br_update(uint32_t reg)
40 uint32_t index = reg & 0xffff;
41 uint32_t incr = reg >> 16;
42 uint32_t new_index = reverse16(reverse16(index) + reverse16(incr));
43 return reg - index + new_index;
46 uint32_t helper_circ_update(uint32_t reg, uint32_t off)
48 uint32_t index = reg & 0xffff;
49 uint32_t length = reg >> 16;
50 int32_t new_index = index + off;
51 if (new_index < 0) {
52 new_index += length;
53 } else {
54 new_index %= length;
56 return reg - index + new_index;
59 static uint32_t ssov32(CPUTriCoreState *env, int64_t arg)
61 uint32_t ret;
62 int64_t max_pos = INT32_MAX;
63 int64_t max_neg = INT32_MIN;
64 if (arg > max_pos) {
65 env->PSW_USB_V = (1 << 31);
66 env->PSW_USB_SV = (1 << 31);
67 ret = (target_ulong)max_pos;
68 } else {
69 if (arg < max_neg) {
70 env->PSW_USB_V = (1 << 31);
71 env->PSW_USB_SV = (1 << 31);
72 ret = (target_ulong)max_neg;
73 } else {
74 env->PSW_USB_V = 0;
75 ret = (target_ulong)arg;
78 env->PSW_USB_AV = arg ^ arg * 2u;
79 env->PSW_USB_SAV |= env->PSW_USB_AV;
80 return ret;
83 static uint32_t suov32_pos(CPUTriCoreState *env, uint64_t arg)
85 uint32_t ret;
86 uint64_t max_pos = UINT32_MAX;
87 if (arg > max_pos) {
88 env->PSW_USB_V = (1 << 31);
89 env->PSW_USB_SV = (1 << 31);
90 ret = (target_ulong)max_pos;
91 } else {
92 env->PSW_USB_V = 0;
93 ret = (target_ulong)arg;
95 env->PSW_USB_AV = arg ^ arg * 2u;
96 env->PSW_USB_SAV |= env->PSW_USB_AV;
97 return ret;
100 static uint32_t suov32_neg(CPUTriCoreState *env, int64_t arg)
102 uint32_t ret;
104 if (arg < 0) {
105 env->PSW_USB_V = (1 << 31);
106 env->PSW_USB_SV = (1 << 31);
107 ret = 0;
108 } else {
109 env->PSW_USB_V = 0;
110 ret = (target_ulong)arg;
112 env->PSW_USB_AV = arg ^ arg * 2u;
113 env->PSW_USB_SAV |= env->PSW_USB_AV;
114 return ret;
117 static uint32_t ssov16(CPUTriCoreState *env, int32_t hw0, int32_t hw1)
119 int32_t max_pos = INT16_MAX;
120 int32_t max_neg = INT16_MIN;
121 int32_t av0, av1;
123 env->PSW_USB_V = 0;
124 av0 = hw0 ^ hw0 * 2u;
125 if (hw0 > max_pos) {
126 env->PSW_USB_V = (1 << 31);
127 hw0 = max_pos;
128 } else if (hw0 < max_neg) {
129 env->PSW_USB_V = (1 << 31);
130 hw0 = max_neg;
133 av1 = hw1 ^ hw1 * 2u;
134 if (hw1 > max_pos) {
135 env->PSW_USB_V = (1 << 31);
136 hw1 = max_pos;
137 } else if (hw1 < max_neg) {
138 env->PSW_USB_V = (1 << 31);
139 hw1 = max_neg;
142 env->PSW_USB_SV |= env->PSW_USB_V;
143 env->PSW_USB_AV = (av0 | av1) << 16;
144 env->PSW_USB_SAV |= env->PSW_USB_AV;
145 return (hw0 & 0xffff) | (hw1 << 16);
148 static uint32_t suov16(CPUTriCoreState *env, int32_t hw0, int32_t hw1)
150 int32_t max_pos = UINT16_MAX;
151 int32_t av0, av1;
153 env->PSW_USB_V = 0;
154 av0 = hw0 ^ hw0 * 2u;
155 if (hw0 > max_pos) {
156 env->PSW_USB_V = (1 << 31);
157 hw0 = max_pos;
158 } else if (hw0 < 0) {
159 env->PSW_USB_V = (1 << 31);
160 hw0 = 0;
163 av1 = hw1 ^ hw1 * 2u;
164 if (hw1 > max_pos) {
165 env->PSW_USB_V = (1 << 31);
166 hw1 = max_pos;
167 } else if (hw1 < 0) {
168 env->PSW_USB_V = (1 << 31);
169 hw1 = 0;
172 env->PSW_USB_SV |= env->PSW_USB_V;
173 env->PSW_USB_AV = (av0 | av1) << 16;
174 env->PSW_USB_SAV |= env->PSW_USB_AV;
175 return (hw0 & 0xffff) | (hw1 << 16);
178 target_ulong helper_add_ssov(CPUTriCoreState *env, target_ulong r1,
179 target_ulong r2)
181 int64_t t1 = sextract64(r1, 0, 32);
182 int64_t t2 = sextract64(r2, 0, 32);
183 int64_t result = t1 + t2;
184 return ssov32(env, result);
187 uint64_t helper_add64_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2)
189 uint64_t result;
190 int64_t ovf;
192 result = r1 + r2;
193 ovf = (result ^ r1) & ~(r1 ^ r2);
194 env->PSW_USB_AV = (result ^ result * 2u) >> 32;
195 env->PSW_USB_SAV |= env->PSW_USB_AV;
196 if (ovf < 0) {
197 env->PSW_USB_V = (1 << 31);
198 env->PSW_USB_SV = (1 << 31);
199 /* ext_ret > MAX_INT */
200 if ((int64_t)r1 >= 0) {
201 result = INT64_MAX;
202 /* ext_ret < MIN_INT */
203 } else {
204 result = INT64_MIN;
206 } else {
207 env->PSW_USB_V = 0;
209 return result;
212 target_ulong helper_add_h_ssov(CPUTriCoreState *env, target_ulong r1,
213 target_ulong r2)
215 int32_t ret_hw0, ret_hw1;
217 ret_hw0 = sextract32(r1, 0, 16) + sextract32(r2, 0, 16);
218 ret_hw1 = sextract32(r1, 16, 16) + sextract32(r2, 16, 16);
219 return ssov16(env, ret_hw0, ret_hw1);
222 uint32_t helper_addr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
223 uint32_t r2_h)
225 int64_t mul_res0 = sextract64(r1, 0, 32);
226 int64_t mul_res1 = sextract64(r1, 32, 32);
227 int64_t r2_low = sextract64(r2_l, 0, 32);
228 int64_t r2_high = sextract64(r2_h, 0, 32);
229 int64_t result0, result1;
230 uint32_t ovf0, ovf1;
231 uint32_t avf0, avf1;
233 ovf0 = ovf1 = 0;
235 result0 = r2_low + mul_res0 + 0x8000;
236 result1 = r2_high + mul_res1 + 0x8000;
238 avf0 = result0 * 2u;
239 avf0 = result0 ^ avf0;
240 avf1 = result1 * 2u;
241 avf1 = result1 ^ avf1;
243 if (result0 > INT32_MAX) {
244 ovf0 = (1 << 31);
245 result0 = INT32_MAX;
246 } else if (result0 < INT32_MIN) {
247 ovf0 = (1 << 31);
248 result0 = INT32_MIN;
251 if (result1 > INT32_MAX) {
252 ovf1 = (1 << 31);
253 result1 = INT32_MAX;
254 } else if (result1 < INT32_MIN) {
255 ovf1 = (1 << 31);
256 result1 = INT32_MIN;
259 env->PSW_USB_V = ovf0 | ovf1;
260 env->PSW_USB_SV |= env->PSW_USB_V;
262 env->PSW_USB_AV = avf0 | avf1;
263 env->PSW_USB_SAV |= env->PSW_USB_AV;
265 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
268 uint32_t helper_addsur_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
269 uint32_t r2_h)
271 int64_t mul_res0 = sextract64(r1, 0, 32);
272 int64_t mul_res1 = sextract64(r1, 32, 32);
273 int64_t r2_low = sextract64(r2_l, 0, 32);
274 int64_t r2_high = sextract64(r2_h, 0, 32);
275 int64_t result0, result1;
276 uint32_t ovf0, ovf1;
277 uint32_t avf0, avf1;
279 ovf0 = ovf1 = 0;
281 result0 = r2_low - mul_res0 + 0x8000;
282 result1 = r2_high + mul_res1 + 0x8000;
284 avf0 = result0 * 2u;
285 avf0 = result0 ^ avf0;
286 avf1 = result1 * 2u;
287 avf1 = result1 ^ avf1;
289 if (result0 > INT32_MAX) {
290 ovf0 = (1 << 31);
291 result0 = INT32_MAX;
292 } else if (result0 < INT32_MIN) {
293 ovf0 = (1 << 31);
294 result0 = INT32_MIN;
297 if (result1 > INT32_MAX) {
298 ovf1 = (1 << 31);
299 result1 = INT32_MAX;
300 } else if (result1 < INT32_MIN) {
301 ovf1 = (1 << 31);
302 result1 = INT32_MIN;
305 env->PSW_USB_V = ovf0 | ovf1;
306 env->PSW_USB_SV |= env->PSW_USB_V;
308 env->PSW_USB_AV = avf0 | avf1;
309 env->PSW_USB_SAV |= env->PSW_USB_AV;
311 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
315 target_ulong helper_add_suov(CPUTriCoreState *env, target_ulong r1,
316 target_ulong r2)
318 int64_t t1 = extract64(r1, 0, 32);
319 int64_t t2 = extract64(r2, 0, 32);
320 int64_t result = t1 + t2;
321 return suov32_pos(env, result);
324 target_ulong helper_add_h_suov(CPUTriCoreState *env, target_ulong r1,
325 target_ulong r2)
327 int32_t ret_hw0, ret_hw1;
329 ret_hw0 = extract32(r1, 0, 16) + extract32(r2, 0, 16);
330 ret_hw1 = extract32(r1, 16, 16) + extract32(r2, 16, 16);
331 return suov16(env, ret_hw0, ret_hw1);
334 target_ulong helper_sub_ssov(CPUTriCoreState *env, target_ulong r1,
335 target_ulong r2)
337 int64_t t1 = sextract64(r1, 0, 32);
338 int64_t t2 = sextract64(r2, 0, 32);
339 int64_t result = t1 - t2;
340 return ssov32(env, result);
343 uint64_t helper_sub64_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2)
345 uint64_t result;
346 int64_t ovf;
348 result = r1 - r2;
349 ovf = (result ^ r1) & (r1 ^ r2);
350 env->PSW_USB_AV = (result ^ result * 2u) >> 32;
351 env->PSW_USB_SAV |= env->PSW_USB_AV;
352 if (ovf < 0) {
353 env->PSW_USB_V = (1 << 31);
354 env->PSW_USB_SV = (1 << 31);
355 /* ext_ret > MAX_INT */
356 if ((int64_t)r1 >= 0) {
357 result = INT64_MAX;
358 /* ext_ret < MIN_INT */
359 } else {
360 result = INT64_MIN;
362 } else {
363 env->PSW_USB_V = 0;
365 return result;
368 target_ulong helper_sub_h_ssov(CPUTriCoreState *env, target_ulong r1,
369 target_ulong r2)
371 int32_t ret_hw0, ret_hw1;
373 ret_hw0 = sextract32(r1, 0, 16) - sextract32(r2, 0, 16);
374 ret_hw1 = sextract32(r1, 16, 16) - sextract32(r2, 16, 16);
375 return ssov16(env, ret_hw0, ret_hw1);
378 uint32_t helper_subr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
379 uint32_t r2_h)
381 int64_t mul_res0 = sextract64(r1, 0, 32);
382 int64_t mul_res1 = sextract64(r1, 32, 32);
383 int64_t r2_low = sextract64(r2_l, 0, 32);
384 int64_t r2_high = sextract64(r2_h, 0, 32);
385 int64_t result0, result1;
386 uint32_t ovf0, ovf1;
387 uint32_t avf0, avf1;
389 ovf0 = ovf1 = 0;
391 result0 = r2_low - mul_res0 + 0x8000;
392 result1 = r2_high - mul_res1 + 0x8000;
394 avf0 = result0 * 2u;
395 avf0 = result0 ^ avf0;
396 avf1 = result1 * 2u;
397 avf1 = result1 ^ avf1;
399 if (result0 > INT32_MAX) {
400 ovf0 = (1 << 31);
401 result0 = INT32_MAX;
402 } else if (result0 < INT32_MIN) {
403 ovf0 = (1 << 31);
404 result0 = INT32_MIN;
407 if (result1 > INT32_MAX) {
408 ovf1 = (1 << 31);
409 result1 = INT32_MAX;
410 } else if (result1 < INT32_MIN) {
411 ovf1 = (1 << 31);
412 result1 = INT32_MIN;
415 env->PSW_USB_V = ovf0 | ovf1;
416 env->PSW_USB_SV |= env->PSW_USB_V;
418 env->PSW_USB_AV = avf0 | avf1;
419 env->PSW_USB_SAV |= env->PSW_USB_AV;
421 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
424 uint32_t helper_subadr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
425 uint32_t r2_h)
427 int64_t mul_res0 = sextract64(r1, 0, 32);
428 int64_t mul_res1 = sextract64(r1, 32, 32);
429 int64_t r2_low = sextract64(r2_l, 0, 32);
430 int64_t r2_high = sextract64(r2_h, 0, 32);
431 int64_t result0, result1;
432 uint32_t ovf0, ovf1;
433 uint32_t avf0, avf1;
435 ovf0 = ovf1 = 0;
437 result0 = r2_low + mul_res0 + 0x8000;
438 result1 = r2_high - mul_res1 + 0x8000;
440 avf0 = result0 * 2u;
441 avf0 = result0 ^ avf0;
442 avf1 = result1 * 2u;
443 avf1 = result1 ^ avf1;
445 if (result0 > INT32_MAX) {
446 ovf0 = (1 << 31);
447 result0 = INT32_MAX;
448 } else if (result0 < INT32_MIN) {
449 ovf0 = (1 << 31);
450 result0 = INT32_MIN;
453 if (result1 > INT32_MAX) {
454 ovf1 = (1 << 31);
455 result1 = INT32_MAX;
456 } else if (result1 < INT32_MIN) {
457 ovf1 = (1 << 31);
458 result1 = INT32_MIN;
461 env->PSW_USB_V = ovf0 | ovf1;
462 env->PSW_USB_SV |= env->PSW_USB_V;
464 env->PSW_USB_AV = avf0 | avf1;
465 env->PSW_USB_SAV |= env->PSW_USB_AV;
467 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
470 target_ulong helper_sub_suov(CPUTriCoreState *env, target_ulong r1,
471 target_ulong r2)
473 int64_t t1 = extract64(r1, 0, 32);
474 int64_t t2 = extract64(r2, 0, 32);
475 int64_t result = t1 - t2;
476 return suov32_neg(env, result);
479 target_ulong helper_sub_h_suov(CPUTriCoreState *env, target_ulong r1,
480 target_ulong r2)
482 int32_t ret_hw0, ret_hw1;
484 ret_hw0 = extract32(r1, 0, 16) - extract32(r2, 0, 16);
485 ret_hw1 = extract32(r1, 16, 16) - extract32(r2, 16, 16);
486 return suov16(env, ret_hw0, ret_hw1);
489 target_ulong helper_mul_ssov(CPUTriCoreState *env, target_ulong r1,
490 target_ulong r2)
492 int64_t t1 = sextract64(r1, 0, 32);
493 int64_t t2 = sextract64(r2, 0, 32);
494 int64_t result = t1 * t2;
495 return ssov32(env, result);
498 target_ulong helper_mul_suov(CPUTriCoreState *env, target_ulong r1,
499 target_ulong r2)
501 int64_t t1 = extract64(r1, 0, 32);
502 int64_t t2 = extract64(r2, 0, 32);
503 int64_t result = t1 * t2;
505 return suov32_pos(env, result);
508 target_ulong helper_sha_ssov(CPUTriCoreState *env, target_ulong r1,
509 target_ulong r2)
511 int64_t t1 = sextract64(r1, 0, 32);
512 int32_t t2 = sextract64(r2, 0, 6);
513 int64_t result;
514 if (t2 == 0) {
515 result = t1;
516 } else if (t2 > 0) {
517 result = t1 << t2;
518 } else {
519 result = t1 >> -t2;
521 return ssov32(env, result);
524 uint32_t helper_abs_ssov(CPUTriCoreState *env, target_ulong r1)
526 target_ulong result;
527 result = ((int32_t)r1 >= 0) ? r1 : (0 - r1);
528 return ssov32(env, result);
531 uint32_t helper_abs_h_ssov(CPUTriCoreState *env, target_ulong r1)
533 int32_t ret_h0, ret_h1;
535 ret_h0 = sextract32(r1, 0, 16);
536 ret_h0 = (ret_h0 >= 0) ? ret_h0 : (0 - ret_h0);
538 ret_h1 = sextract32(r1, 16, 16);
539 ret_h1 = (ret_h1 >= 0) ? ret_h1 : (0 - ret_h1);
541 return ssov16(env, ret_h0, ret_h1);
544 target_ulong helper_absdif_ssov(CPUTriCoreState *env, target_ulong r1,
545 target_ulong r2)
547 int64_t t1 = sextract64(r1, 0, 32);
548 int64_t t2 = sextract64(r2, 0, 32);
549 int64_t result;
551 if (t1 > t2) {
552 result = t1 - t2;
553 } else {
554 result = t2 - t1;
556 return ssov32(env, result);
559 uint32_t helper_absdif_h_ssov(CPUTriCoreState *env, target_ulong r1,
560 target_ulong r2)
562 int32_t t1, t2;
563 int32_t ret_h0, ret_h1;
565 t1 = sextract32(r1, 0, 16);
566 t2 = sextract32(r2, 0, 16);
567 if (t1 > t2) {
568 ret_h0 = t1 - t2;
569 } else {
570 ret_h0 = t2 - t1;
573 t1 = sextract32(r1, 16, 16);
574 t2 = sextract32(r2, 16, 16);
575 if (t1 > t2) {
576 ret_h1 = t1 - t2;
577 } else {
578 ret_h1 = t2 - t1;
581 return ssov16(env, ret_h0, ret_h1);
584 target_ulong helper_madd32_ssov(CPUTriCoreState *env, target_ulong r1,
585 target_ulong r2, target_ulong r3)
587 int64_t t1 = sextract64(r1, 0, 32);
588 int64_t t2 = sextract64(r2, 0, 32);
589 int64_t t3 = sextract64(r3, 0, 32);
590 int64_t result;
592 result = t2 + (t1 * t3);
593 return ssov32(env, result);
596 target_ulong helper_madd32_suov(CPUTriCoreState *env, target_ulong r1,
597 target_ulong r2, target_ulong r3)
599 uint64_t t1 = extract64(r1, 0, 32);
600 uint64_t t2 = extract64(r2, 0, 32);
601 uint64_t t3 = extract64(r3, 0, 32);
602 int64_t result;
604 result = t2 + (t1 * t3);
605 return suov32_pos(env, result);
608 uint64_t helper_madd64_ssov(CPUTriCoreState *env, target_ulong r1,
609 uint64_t r2, target_ulong r3)
611 uint64_t ret, ovf;
612 int64_t t1 = sextract64(r1, 0, 32);
613 int64_t t3 = sextract64(r3, 0, 32);
614 int64_t mul;
616 mul = t1 * t3;
617 ret = mul + r2;
618 ovf = (ret ^ mul) & ~(mul ^ r2);
620 t1 = ret >> 32;
621 env->PSW_USB_AV = t1 ^ t1 * 2u;
622 env->PSW_USB_SAV |= env->PSW_USB_AV;
624 if ((int64_t)ovf < 0) {
625 env->PSW_USB_V = (1 << 31);
626 env->PSW_USB_SV = (1 << 31);
627 /* ext_ret > MAX_INT */
628 if (mul >= 0) {
629 ret = INT64_MAX;
630 /* ext_ret < MIN_INT */
631 } else {
632 ret = INT64_MIN;
634 } else {
635 env->PSW_USB_V = 0;
638 return ret;
641 uint32_t
642 helper_madd32_q_add_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2)
644 int64_t result;
646 result = (r1 + r2);
648 env->PSW_USB_AV = (result ^ result * 2u);
649 env->PSW_USB_SAV |= env->PSW_USB_AV;
651 /* we do the saturation by hand, since we produce an overflow on the host
652 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
653 case, we flip the saturated value. */
654 if (r2 == 0x8000000000000000LL) {
655 if (result > 0x7fffffffLL) {
656 env->PSW_USB_V = (1 << 31);
657 env->PSW_USB_SV = (1 << 31);
658 result = INT32_MIN;
659 } else if (result < -0x80000000LL) {
660 env->PSW_USB_V = (1 << 31);
661 env->PSW_USB_SV = (1 << 31);
662 result = INT32_MAX;
663 } else {
664 env->PSW_USB_V = 0;
666 } else {
667 if (result > 0x7fffffffLL) {
668 env->PSW_USB_V = (1 << 31);
669 env->PSW_USB_SV = (1 << 31);
670 result = INT32_MAX;
671 } else if (result < -0x80000000LL) {
672 env->PSW_USB_V = (1 << 31);
673 env->PSW_USB_SV = (1 << 31);
674 result = INT32_MIN;
675 } else {
676 env->PSW_USB_V = 0;
679 return (uint32_t)result;
682 uint64_t helper_madd64_q_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2,
683 uint32_t r3, uint32_t n)
685 int64_t t1 = (int64_t)r1;
686 int64_t t2 = sextract64(r2, 0, 32);
687 int64_t t3 = sextract64(r3, 0, 32);
688 int64_t result, mul;
689 int64_t ovf;
691 mul = (t2 * t3) << n;
692 result = mul + t1;
694 env->PSW_USB_AV = (result ^ result * 2u) >> 32;
695 env->PSW_USB_SAV |= env->PSW_USB_AV;
697 ovf = (result ^ mul) & ~(mul ^ t1);
698 /* we do the saturation by hand, since we produce an overflow on the host
699 if the mul was (0x80000000 * 0x80000000) << 1). If this is the
700 case, we flip the saturated value. */
701 if ((r2 == 0x80000000) && (r3 == 0x80000000) && (n == 1)) {
702 if (ovf >= 0) {
703 env->PSW_USB_V = (1 << 31);
704 env->PSW_USB_SV = (1 << 31);
705 /* ext_ret > MAX_INT */
706 if (mul < 0) {
707 result = INT64_MAX;
708 /* ext_ret < MIN_INT */
709 } else {
710 result = INT64_MIN;
712 } else {
713 env->PSW_USB_V = 0;
715 } else {
716 if (ovf < 0) {
717 env->PSW_USB_V = (1 << 31);
718 env->PSW_USB_SV = (1 << 31);
719 /* ext_ret > MAX_INT */
720 if (mul >= 0) {
721 result = INT64_MAX;
722 /* ext_ret < MIN_INT */
723 } else {
724 result = INT64_MIN;
726 } else {
727 env->PSW_USB_V = 0;
730 return (uint64_t)result;
733 uint32_t helper_maddr_q_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2,
734 uint32_t r3, uint32_t n)
736 int64_t t1 = sextract64(r1, 0, 32);
737 int64_t t2 = sextract64(r2, 0, 32);
738 int64_t t3 = sextract64(r3, 0, 32);
739 int64_t mul, ret;
741 if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) {
742 mul = 0x7fffffff;
743 } else {
744 mul = (t2 * t3) << n;
747 ret = t1 + mul + 0x8000;
749 env->PSW_USB_AV = ret ^ ret * 2u;
750 env->PSW_USB_SAV |= env->PSW_USB_AV;
752 if (ret > 0x7fffffffll) {
753 env->PSW_USB_V = (1 << 31);
754 env->PSW_USB_SV |= env->PSW_USB_V;
755 ret = INT32_MAX;
756 } else if (ret < -0x80000000ll) {
757 env->PSW_USB_V = (1 << 31);
758 env->PSW_USB_SV |= env->PSW_USB_V;
759 ret = INT32_MIN;
760 } else {
761 env->PSW_USB_V = 0;
763 return ret & 0xffff0000ll;
766 uint64_t helper_madd64_suov(CPUTriCoreState *env, target_ulong r1,
767 uint64_t r2, target_ulong r3)
769 uint64_t ret, mul;
770 uint64_t t1 = extract64(r1, 0, 32);
771 uint64_t t3 = extract64(r3, 0, 32);
773 mul = t1 * t3;
774 ret = mul + r2;
776 t1 = ret >> 32;
777 env->PSW_USB_AV = t1 ^ t1 * 2u;
778 env->PSW_USB_SAV |= env->PSW_USB_AV;
780 if (ret < r2) {
781 env->PSW_USB_V = (1 << 31);
782 env->PSW_USB_SV = (1 << 31);
783 /* saturate */
784 ret = UINT64_MAX;
785 } else {
786 env->PSW_USB_V = 0;
788 return ret;
791 target_ulong helper_msub32_ssov(CPUTriCoreState *env, target_ulong r1,
792 target_ulong r2, target_ulong r3)
794 int64_t t1 = sextract64(r1, 0, 32);
795 int64_t t2 = sextract64(r2, 0, 32);
796 int64_t t3 = sextract64(r3, 0, 32);
797 int64_t result;
799 result = t2 - (t1 * t3);
800 return ssov32(env, result);
803 target_ulong helper_msub32_suov(CPUTriCoreState *env, target_ulong r1,
804 target_ulong r2, target_ulong r3)
806 uint64_t t1 = extract64(r1, 0, 32);
807 uint64_t t2 = extract64(r2, 0, 32);
808 uint64_t t3 = extract64(r3, 0, 32);
809 uint64_t result;
810 uint64_t mul;
812 mul = (t1 * t3);
813 result = t2 - mul;
815 env->PSW_USB_AV = result ^ result * 2u;
816 env->PSW_USB_SAV |= env->PSW_USB_AV;
817 /* we calculate ovf by hand here, because the multiplication can overflow on
818 the host, which would give false results if we compare to less than
819 zero */
820 if (mul > t2) {
821 env->PSW_USB_V = (1 << 31);
822 env->PSW_USB_SV = (1 << 31);
823 result = 0;
824 } else {
825 env->PSW_USB_V = 0;
827 return result;
830 uint64_t helper_msub64_ssov(CPUTriCoreState *env, target_ulong r1,
831 uint64_t r2, target_ulong r3)
833 uint64_t ret, ovf;
834 int64_t t1 = sextract64(r1, 0, 32);
835 int64_t t3 = sextract64(r3, 0, 32);
836 int64_t mul;
838 mul = t1 * t3;
839 ret = r2 - mul;
840 ovf = (ret ^ r2) & (mul ^ r2);
842 t1 = ret >> 32;
843 env->PSW_USB_AV = t1 ^ t1 * 2u;
844 env->PSW_USB_SAV |= env->PSW_USB_AV;
846 if ((int64_t)ovf < 0) {
847 env->PSW_USB_V = (1 << 31);
848 env->PSW_USB_SV = (1 << 31);
849 /* ext_ret > MAX_INT */
850 if (mul < 0) {
851 ret = INT64_MAX;
852 /* ext_ret < MIN_INT */
853 } else {
854 ret = INT64_MIN;
856 } else {
857 env->PSW_USB_V = 0;
859 return ret;
862 uint64_t helper_msub64_suov(CPUTriCoreState *env, target_ulong r1,
863 uint64_t r2, target_ulong r3)
865 uint64_t ret, mul;
866 uint64_t t1 = extract64(r1, 0, 32);
867 uint64_t t3 = extract64(r3, 0, 32);
869 mul = t1 * t3;
870 ret = r2 - mul;
872 t1 = ret >> 32;
873 env->PSW_USB_AV = t1 ^ t1 * 2u;
874 env->PSW_USB_SAV |= env->PSW_USB_AV;
876 if (ret > r2) {
877 env->PSW_USB_V = (1 << 31);
878 env->PSW_USB_SV = (1 << 31);
879 /* saturate */
880 ret = 0;
881 } else {
882 env->PSW_USB_V = 0;
884 return ret;
887 uint32_t
888 helper_msub32_q_sub_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2)
890 int64_t result;
891 int64_t t1 = (int64_t)r1;
892 int64_t t2 = (int64_t)r2;
894 result = t1 - t2;
896 env->PSW_USB_AV = (result ^ result * 2u);
897 env->PSW_USB_SAV |= env->PSW_USB_AV;
899 /* we do the saturation by hand, since we produce an overflow on the host
900 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
901 case, we flip the saturated value. */
902 if (r2 == 0x8000000000000000LL) {
903 if (result > 0x7fffffffLL) {
904 env->PSW_USB_V = (1 << 31);
905 env->PSW_USB_SV = (1 << 31);
906 result = INT32_MIN;
907 } else if (result < -0x80000000LL) {
908 env->PSW_USB_V = (1 << 31);
909 env->PSW_USB_SV = (1 << 31);
910 result = INT32_MAX;
911 } else {
912 env->PSW_USB_V = 0;
914 } else {
915 if (result > 0x7fffffffLL) {
916 env->PSW_USB_V = (1 << 31);
917 env->PSW_USB_SV = (1 << 31);
918 result = INT32_MAX;
919 } else if (result < -0x80000000LL) {
920 env->PSW_USB_V = (1 << 31);
921 env->PSW_USB_SV = (1 << 31);
922 result = INT32_MIN;
923 } else {
924 env->PSW_USB_V = 0;
927 return (uint32_t)result;
930 uint64_t helper_msub64_q_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2,
931 uint32_t r3, uint32_t n)
933 int64_t t1 = (int64_t)r1;
934 int64_t t2 = sextract64(r2, 0, 32);
935 int64_t t3 = sextract64(r3, 0, 32);
936 int64_t result, mul;
937 int64_t ovf;
939 mul = (t2 * t3) << n;
940 result = t1 - mul;
942 env->PSW_USB_AV = (result ^ result * 2u) >> 32;
943 env->PSW_USB_SAV |= env->PSW_USB_AV;
945 ovf = (result ^ t1) & (t1 ^ mul);
946 /* we do the saturation by hand, since we produce an overflow on the host
947 if the mul before was (0x80000000 * 0x80000000) << 1). If this is the
948 case, we flip the saturated value. */
949 if (mul == 0x8000000000000000LL) {
950 if (ovf >= 0) {
951 env->PSW_USB_V = (1 << 31);
952 env->PSW_USB_SV = (1 << 31);
953 /* ext_ret > MAX_INT */
954 if (mul >= 0) {
955 result = INT64_MAX;
956 /* ext_ret < MIN_INT */
957 } else {
958 result = INT64_MIN;
961 } else {
962 if (ovf < 0) {
963 env->PSW_USB_V = (1 << 31);
964 env->PSW_USB_SV = (1 << 31);
965 /* ext_ret > MAX_INT */
966 if (mul < 0) {
967 result = INT64_MAX;
968 /* ext_ret < MIN_INT */
969 } else {
970 result = INT64_MIN;
972 } else {
973 env->PSW_USB_V = 0;
977 return (uint64_t)result;
980 uint32_t helper_msubr_q_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2,
981 uint32_t r3, uint32_t n)
983 int64_t t1 = sextract64(r1, 0, 32);
984 int64_t t2 = sextract64(r2, 0, 32);
985 int64_t t3 = sextract64(r3, 0, 32);
986 int64_t mul, ret;
988 if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) {
989 mul = 0x7fffffff;
990 } else {
991 mul = (t2 * t3) << n;
994 ret = t1 - mul + 0x8000;
996 env->PSW_USB_AV = ret ^ ret * 2u;
997 env->PSW_USB_SAV |= env->PSW_USB_AV;
999 if (ret > 0x7fffffffll) {
1000 env->PSW_USB_V = (1 << 31);
1001 env->PSW_USB_SV |= env->PSW_USB_V;
1002 ret = INT32_MAX;
1003 } else if (ret < -0x80000000ll) {
1004 env->PSW_USB_V = (1 << 31);
1005 env->PSW_USB_SV |= env->PSW_USB_V;
1006 ret = INT32_MIN;
1007 } else {
1008 env->PSW_USB_V = 0;
1010 return ret & 0xffff0000ll;
1013 uint32_t helper_abs_b(CPUTriCoreState *env, target_ulong arg)
1015 int32_t b, i;
1016 int32_t ovf = 0;
1017 int32_t avf = 0;
1018 int32_t ret = 0;
1020 for (i = 0; i < 4; i++) {
1021 b = sextract32(arg, i * 8, 8);
1022 b = (b >= 0) ? b : (0 - b);
1023 ovf |= (b > 0x7F) || (b < -0x80);
1024 avf |= b ^ b * 2u;
1025 ret |= (b & 0xff) << (i * 8);
1028 env->PSW_USB_V = ovf << 31;
1029 env->PSW_USB_SV |= env->PSW_USB_V;
1030 env->PSW_USB_AV = avf << 24;
1031 env->PSW_USB_SAV |= env->PSW_USB_AV;
1033 return ret;
1036 uint32_t helper_abs_h(CPUTriCoreState *env, target_ulong arg)
1038 int32_t h, i;
1039 int32_t ovf = 0;
1040 int32_t avf = 0;
1041 int32_t ret = 0;
1043 for (i = 0; i < 2; i++) {
1044 h = sextract32(arg, i * 16, 16);
1045 h = (h >= 0) ? h : (0 - h);
1046 ovf |= (h > 0x7FFF) || (h < -0x8000);
1047 avf |= h ^ h * 2u;
1048 ret |= (h & 0xffff) << (i * 16);
1051 env->PSW_USB_V = ovf << 31;
1052 env->PSW_USB_SV |= env->PSW_USB_V;
1053 env->PSW_USB_AV = avf << 16;
1054 env->PSW_USB_SAV |= env->PSW_USB_AV;
1056 return ret;
1059 uint32_t helper_absdif_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1061 int32_t b, i;
1062 int32_t extr_r2;
1063 int32_t ovf = 0;
1064 int32_t avf = 0;
1065 int32_t ret = 0;
1067 for (i = 0; i < 4; i++) {
1068 extr_r2 = sextract32(r2, i * 8, 8);
1069 b = sextract32(r1, i * 8, 8);
1070 b = (b > extr_r2) ? (b - extr_r2) : (extr_r2 - b);
1071 ovf |= (b > 0x7F) || (b < -0x80);
1072 avf |= b ^ b * 2u;
1073 ret |= (b & 0xff) << (i * 8);
1076 env->PSW_USB_V = ovf << 31;
1077 env->PSW_USB_SV |= env->PSW_USB_V;
1078 env->PSW_USB_AV = avf << 24;
1079 env->PSW_USB_SAV |= env->PSW_USB_AV;
1080 return ret;
1083 uint32_t helper_absdif_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1085 int32_t h, i;
1086 int32_t extr_r2;
1087 int32_t ovf = 0;
1088 int32_t avf = 0;
1089 int32_t ret = 0;
1091 for (i = 0; i < 2; i++) {
1092 extr_r2 = sextract32(r2, i * 16, 16);
1093 h = sextract32(r1, i * 16, 16);
1094 h = (h > extr_r2) ? (h - extr_r2) : (extr_r2 - h);
1095 ovf |= (h > 0x7FFF) || (h < -0x8000);
1096 avf |= h ^ h * 2u;
1097 ret |= (h & 0xffff) << (i * 16);
1100 env->PSW_USB_V = ovf << 31;
1101 env->PSW_USB_SV |= env->PSW_USB_V;
1102 env->PSW_USB_AV = avf << 16;
1103 env->PSW_USB_SAV |= env->PSW_USB_AV;
1105 return ret;
1108 uint32_t helper_addr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
1109 uint32_t r2_h)
1111 int64_t mul_res0 = sextract64(r1, 0, 32);
1112 int64_t mul_res1 = sextract64(r1, 32, 32);
1113 int64_t r2_low = sextract64(r2_l, 0, 32);
1114 int64_t r2_high = sextract64(r2_h, 0, 32);
1115 int64_t result0, result1;
1116 uint32_t ovf0, ovf1;
1117 uint32_t avf0, avf1;
1119 ovf0 = ovf1 = 0;
1121 result0 = r2_low + mul_res0 + 0x8000;
1122 result1 = r2_high + mul_res1 + 0x8000;
1124 if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) {
1125 ovf0 = (1 << 31);
1128 if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) {
1129 ovf1 = (1 << 31);
1132 env->PSW_USB_V = ovf0 | ovf1;
1133 env->PSW_USB_SV |= env->PSW_USB_V;
1135 avf0 = result0 * 2u;
1136 avf0 = result0 ^ avf0;
1137 avf1 = result1 * 2u;
1138 avf1 = result1 ^ avf1;
1140 env->PSW_USB_AV = avf0 | avf1;
1141 env->PSW_USB_SAV |= env->PSW_USB_AV;
1143 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
1146 uint32_t helper_addsur_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
1147 uint32_t r2_h)
1149 int64_t mul_res0 = sextract64(r1, 0, 32);
1150 int64_t mul_res1 = sextract64(r1, 32, 32);
1151 int64_t r2_low = sextract64(r2_l, 0, 32);
1152 int64_t r2_high = sextract64(r2_h, 0, 32);
1153 int64_t result0, result1;
1154 uint32_t ovf0, ovf1;
1155 uint32_t avf0, avf1;
1157 ovf0 = ovf1 = 0;
1159 result0 = r2_low - mul_res0 + 0x8000;
1160 result1 = r2_high + mul_res1 + 0x8000;
1162 if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) {
1163 ovf0 = (1 << 31);
1166 if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) {
1167 ovf1 = (1 << 31);
1170 env->PSW_USB_V = ovf0 | ovf1;
1171 env->PSW_USB_SV |= env->PSW_USB_V;
1173 avf0 = result0 * 2u;
1174 avf0 = result0 ^ avf0;
1175 avf1 = result1 * 2u;
1176 avf1 = result1 ^ avf1;
1178 env->PSW_USB_AV = avf0 | avf1;
1179 env->PSW_USB_SAV |= env->PSW_USB_AV;
1181 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
1184 uint32_t helper_maddr_q(CPUTriCoreState *env, uint32_t r1, uint32_t r2,
1185 uint32_t r3, uint32_t n)
1187 int64_t t1 = sextract64(r1, 0, 32);
1188 int64_t t2 = sextract64(r2, 0, 32);
1189 int64_t t3 = sextract64(r3, 0, 32);
1190 int64_t mul, ret;
1192 if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) {
1193 mul = 0x7fffffff;
1194 } else {
1195 mul = (t2 * t3) << n;
1198 ret = t1 + mul + 0x8000;
1200 if ((ret > 0x7fffffffll) || (ret < -0x80000000ll)) {
1201 env->PSW_USB_V = (1 << 31);
1202 env->PSW_USB_SV |= env->PSW_USB_V;
1203 } else {
1204 env->PSW_USB_V = 0;
1206 env->PSW_USB_AV = ret ^ ret * 2u;
1207 env->PSW_USB_SAV |= env->PSW_USB_AV;
1209 return ret & 0xffff0000ll;
1212 uint32_t helper_add_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1214 int32_t b, i;
1215 int32_t extr_r1, extr_r2;
1216 int32_t ovf = 0;
1217 int32_t avf = 0;
1218 uint32_t ret = 0;
1220 for (i = 0; i < 4; i++) {
1221 extr_r1 = sextract32(r1, i * 8, 8);
1222 extr_r2 = sextract32(r2, i * 8, 8);
1224 b = extr_r1 + extr_r2;
1225 ovf |= ((b > 0x7f) || (b < -0x80));
1226 avf |= b ^ b * 2u;
1227 ret |= ((b & 0xff) << (i*8));
1230 env->PSW_USB_V = (ovf << 31);
1231 env->PSW_USB_SV |= env->PSW_USB_V;
1232 env->PSW_USB_AV = avf << 24;
1233 env->PSW_USB_SAV |= env->PSW_USB_AV;
1235 return ret;
1238 uint32_t helper_add_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1240 int32_t h, i;
1241 int32_t extr_r1, extr_r2;
1242 int32_t ovf = 0;
1243 int32_t avf = 0;
1244 int32_t ret = 0;
1246 for (i = 0; i < 2; i++) {
1247 extr_r1 = sextract32(r1, i * 16, 16);
1248 extr_r2 = sextract32(r2, i * 16, 16);
1249 h = extr_r1 + extr_r2;
1250 ovf |= ((h > 0x7fff) || (h < -0x8000));
1251 avf |= h ^ h * 2u;
1252 ret |= (h & 0xffff) << (i * 16);
1255 env->PSW_USB_V = (ovf << 31);
1256 env->PSW_USB_SV |= env->PSW_USB_V;
1257 env->PSW_USB_AV = (avf << 16);
1258 env->PSW_USB_SAV |= env->PSW_USB_AV;
1260 return ret;
1263 uint32_t helper_subr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
1264 uint32_t r2_h)
1266 int64_t mul_res0 = sextract64(r1, 0, 32);
1267 int64_t mul_res1 = sextract64(r1, 32, 32);
1268 int64_t r2_low = sextract64(r2_l, 0, 32);
1269 int64_t r2_high = sextract64(r2_h, 0, 32);
1270 int64_t result0, result1;
1271 uint32_t ovf0, ovf1;
1272 uint32_t avf0, avf1;
1274 ovf0 = ovf1 = 0;
1276 result0 = r2_low - mul_res0 + 0x8000;
1277 result1 = r2_high - mul_res1 + 0x8000;
1279 if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) {
1280 ovf0 = (1 << 31);
1283 if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) {
1284 ovf1 = (1 << 31);
1287 env->PSW_USB_V = ovf0 | ovf1;
1288 env->PSW_USB_SV |= env->PSW_USB_V;
1290 avf0 = result0 * 2u;
1291 avf0 = result0 ^ avf0;
1292 avf1 = result1 * 2u;
1293 avf1 = result1 ^ avf1;
1295 env->PSW_USB_AV = avf0 | avf1;
1296 env->PSW_USB_SAV |= env->PSW_USB_AV;
1298 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
1301 uint32_t helper_subadr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l,
1302 uint32_t r2_h)
1304 int64_t mul_res0 = sextract64(r1, 0, 32);
1305 int64_t mul_res1 = sextract64(r1, 32, 32);
1306 int64_t r2_low = sextract64(r2_l, 0, 32);
1307 int64_t r2_high = sextract64(r2_h, 0, 32);
1308 int64_t result0, result1;
1309 uint32_t ovf0, ovf1;
1310 uint32_t avf0, avf1;
1312 ovf0 = ovf1 = 0;
1314 result0 = r2_low + mul_res0 + 0x8000;
1315 result1 = r2_high - mul_res1 + 0x8000;
1317 if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) {
1318 ovf0 = (1 << 31);
1321 if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) {
1322 ovf1 = (1 << 31);
1325 env->PSW_USB_V = ovf0 | ovf1;
1326 env->PSW_USB_SV |= env->PSW_USB_V;
1328 avf0 = result0 * 2u;
1329 avf0 = result0 ^ avf0;
1330 avf1 = result1 * 2u;
1331 avf1 = result1 ^ avf1;
1333 env->PSW_USB_AV = avf0 | avf1;
1334 env->PSW_USB_SAV |= env->PSW_USB_AV;
1336 return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL);
1339 uint32_t helper_msubr_q(CPUTriCoreState *env, uint32_t r1, uint32_t r2,
1340 uint32_t r3, uint32_t n)
1342 int64_t t1 = sextract64(r1, 0, 32);
1343 int64_t t2 = sextract64(r2, 0, 32);
1344 int64_t t3 = sextract64(r3, 0, 32);
1345 int64_t mul, ret;
1347 if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) {
1348 mul = 0x7fffffff;
1349 } else {
1350 mul = (t2 * t3) << n;
1353 ret = t1 - mul + 0x8000;
1355 if ((ret > 0x7fffffffll) || (ret < -0x80000000ll)) {
1356 env->PSW_USB_V = (1 << 31);
1357 env->PSW_USB_SV |= env->PSW_USB_V;
1358 } else {
1359 env->PSW_USB_V = 0;
1361 env->PSW_USB_AV = ret ^ ret * 2u;
1362 env->PSW_USB_SAV |= env->PSW_USB_AV;
1364 return ret & 0xffff0000ll;
1367 uint32_t helper_sub_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1369 int32_t b, i;
1370 int32_t extr_r1, extr_r2;
1371 int32_t ovf = 0;
1372 int32_t avf = 0;
1373 uint32_t ret = 0;
1375 for (i = 0; i < 4; i++) {
1376 extr_r1 = sextract32(r1, i * 8, 8);
1377 extr_r2 = sextract32(r2, i * 8, 8);
1379 b = extr_r1 - extr_r2;
1380 ovf |= ((b > 0x7f) || (b < -0x80));
1381 avf |= b ^ b * 2u;
1382 ret |= ((b & 0xff) << (i*8));
1385 env->PSW_USB_V = (ovf << 31);
1386 env->PSW_USB_SV |= env->PSW_USB_V;
1387 env->PSW_USB_AV = avf << 24;
1388 env->PSW_USB_SAV |= env->PSW_USB_AV;
1390 return ret;
1393 uint32_t helper_sub_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1395 int32_t h, i;
1396 int32_t extr_r1, extr_r2;
1397 int32_t ovf = 0;
1398 int32_t avf = 0;
1399 int32_t ret = 0;
1401 for (i = 0; i < 2; i++) {
1402 extr_r1 = sextract32(r1, i * 16, 16);
1403 extr_r2 = sextract32(r2, i * 16, 16);
1404 h = extr_r1 - extr_r2;
1405 ovf |= ((h > 0x7fff) || (h < -0x8000));
1406 avf |= h ^ h * 2u;
1407 ret |= (h & 0xffff) << (i * 16);
1410 env->PSW_USB_V = (ovf << 31);
1411 env->PSW_USB_SV |= env->PSW_USB_V;
1412 env->PSW_USB_AV = avf << 16;
1413 env->PSW_USB_SAV |= env->PSW_USB_AV;
1415 return ret;
1418 uint32_t helper_eq_b(target_ulong r1, target_ulong r2)
1420 int32_t ret;
1421 int32_t i, msk;
1423 ret = 0;
1424 msk = 0xff;
1425 for (i = 0; i < 4; i++) {
1426 if ((r1 & msk) == (r2 & msk)) {
1427 ret |= msk;
1429 msk = msk << 8;
1432 return ret;
1435 uint32_t helper_eq_h(target_ulong r1, target_ulong r2)
1437 int32_t ret = 0;
1439 if ((r1 & 0xffff) == (r2 & 0xffff)) {
1440 ret = 0xffff;
1443 if ((r1 & 0xffff0000) == (r2 & 0xffff0000)) {
1444 ret |= 0xffff0000;
1447 return ret;
1450 uint32_t helper_eqany_b(target_ulong r1, target_ulong r2)
1452 int32_t i;
1453 uint32_t ret = 0;
1455 for (i = 0; i < 4; i++) {
1456 ret |= (sextract32(r1, i * 8, 8) == sextract32(r2, i * 8, 8));
1459 return ret;
1462 uint32_t helper_eqany_h(target_ulong r1, target_ulong r2)
1464 uint32_t ret;
1466 ret = (sextract32(r1, 0, 16) == sextract32(r2, 0, 16));
1467 ret |= (sextract32(r1, 16, 16) == sextract32(r2, 16, 16));
1469 return ret;
1472 uint32_t helper_lt_b(target_ulong r1, target_ulong r2)
1474 int32_t i;
1475 uint32_t ret = 0;
1477 for (i = 0; i < 4; i++) {
1478 if (sextract32(r1, i * 8, 8) < sextract32(r2, i * 8, 8)) {
1479 ret |= (0xff << (i * 8));
1483 return ret;
1486 uint32_t helper_lt_bu(target_ulong r1, target_ulong r2)
1488 int32_t i;
1489 uint32_t ret = 0;
1491 for (i = 0; i < 4; i++) {
1492 if (extract32(r1, i * 8, 8) < extract32(r2, i * 8, 8)) {
1493 ret |= (0xff << (i * 8));
1497 return ret;
1500 uint32_t helper_lt_h(target_ulong r1, target_ulong r2)
1502 uint32_t ret = 0;
1504 if (sextract32(r1, 0, 16) < sextract32(r2, 0, 16)) {
1505 ret |= 0xffff;
1508 if (sextract32(r1, 16, 16) < sextract32(r2, 16, 16)) {
1509 ret |= 0xffff0000;
1512 return ret;
1515 uint32_t helper_lt_hu(target_ulong r1, target_ulong r2)
1517 uint32_t ret = 0;
1519 if (extract32(r1, 0, 16) < extract32(r2, 0, 16)) {
1520 ret |= 0xffff;
1523 if (extract32(r1, 16, 16) < extract32(r2, 16, 16)) {
1524 ret |= 0xffff0000;
1527 return ret;
1530 #define EXTREMA_H_B(name, op) \
1531 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
1533 int32_t i, extr_r1, extr_r2; \
1534 uint32_t ret = 0; \
1536 for (i = 0; i < 4; i++) { \
1537 extr_r1 = sextract32(r1, i * 8, 8); \
1538 extr_r2 = sextract32(r2, i * 8, 8); \
1539 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1540 ret |= (extr_r1 & 0xff) << (i * 8); \
1542 return ret; \
1545 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
1547 int32_t i; \
1548 uint32_t extr_r1, extr_r2; \
1549 uint32_t ret = 0; \
1551 for (i = 0; i < 4; i++) { \
1552 extr_r1 = extract32(r1, i * 8, 8); \
1553 extr_r2 = extract32(r2, i * 8, 8); \
1554 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1555 ret |= (extr_r1 & 0xff) << (i * 8); \
1557 return ret; \
1560 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
1562 int32_t extr_r1, extr_r2; \
1563 uint32_t ret = 0; \
1565 extr_r1 = sextract32(r1, 0, 16); \
1566 extr_r2 = sextract32(r2, 0, 16); \
1567 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1568 ret = ret & 0xffff; \
1570 extr_r1 = sextract32(r1, 16, 16); \
1571 extr_r2 = sextract32(r2, 16, 16); \
1572 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1573 ret |= extr_r1 << 16; \
1575 return ret; \
1578 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
1580 uint32_t extr_r1, extr_r2; \
1581 uint32_t ret = 0; \
1583 extr_r1 = extract32(r1, 0, 16); \
1584 extr_r2 = extract32(r2, 0, 16); \
1585 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1586 ret = ret & 0xffff; \
1588 extr_r1 = extract32(r1, 16, 16); \
1589 extr_r2 = extract32(r2, 16, 16); \
1590 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
1591 ret |= extr_r1 << (16); \
1593 return ret; \
1596 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
1598 int64_t r2l, r2h, r1hl; \
1599 uint64_t ret = 0; \
1601 ret = ((r1 + 2) & 0xffff); \
1602 r2l = sextract64(r2, 0, 16); \
1603 r2h = sextract64(r2, 16, 16); \
1604 r1hl = sextract64(r1, 32, 16); \
1606 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1607 ret |= (r2l & 0xffff) << 32; \
1608 ret |= extract64(r1, 0, 16) << 16; \
1609 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1610 ret |= extract64(r2, 16, 16) << 32; \
1611 ret |= extract64(r1 + 1, 0, 16) << 16; \
1612 } else { \
1613 ret |= r1 & 0xffffffff0000ull; \
1615 return ret; \
1618 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
1620 int64_t r2l, r2h, r1hl; \
1621 uint64_t ret = 0; \
1623 ret = ((r1 + 2) & 0xffff); \
1624 r2l = extract64(r2, 0, 16); \
1625 r2h = extract64(r2, 16, 16); \
1626 r1hl = extract64(r1, 32, 16); \
1628 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
1629 ret |= (r2l & 0xffff) << 32; \
1630 ret |= extract64(r1, 0, 16) << 16; \
1631 } else if ((r2h op r2l) && (r2h op r1hl)) { \
1632 ret |= extract64(r2, 16, 16) << 32; \
1633 ret |= extract64(r1 + 1, 0, 16) << 16; \
1634 } else { \
1635 ret |= r1 & 0xffffffff0000ull; \
1637 return ret; \
1640 EXTREMA_H_B(max, >)
1641 EXTREMA_H_B(min, <)
1643 #undef EXTREMA_H_B
1645 uint32_t helper_clo(target_ulong r1)
1647 return clo32(r1);
1650 uint32_t helper_clo_h(target_ulong r1)
1652 uint32_t ret_hw0 = extract32(r1, 0, 16);
1653 uint32_t ret_hw1 = extract32(r1, 16, 16);
1655 ret_hw0 = clo32(ret_hw0 << 16);
1656 ret_hw1 = clo32(ret_hw1 << 16);
1658 if (ret_hw0 > 16) {
1659 ret_hw0 = 16;
1661 if (ret_hw1 > 16) {
1662 ret_hw1 = 16;
1665 return ret_hw0 | (ret_hw1 << 16);
1668 uint32_t helper_clz(target_ulong r1)
1670 return clz32(r1);
1673 uint32_t helper_clz_h(target_ulong r1)
1675 uint32_t ret_hw0 = extract32(r1, 0, 16);
1676 uint32_t ret_hw1 = extract32(r1, 16, 16);
1678 ret_hw0 = clz32(ret_hw0 << 16);
1679 ret_hw1 = clz32(ret_hw1 << 16);
1681 if (ret_hw0 > 16) {
1682 ret_hw0 = 16;
1684 if (ret_hw1 > 16) {
1685 ret_hw1 = 16;
1688 return ret_hw0 | (ret_hw1 << 16);
1691 uint32_t helper_cls(target_ulong r1)
1693 return clrsb32(r1);
1696 uint32_t helper_cls_h(target_ulong r1)
1698 uint32_t ret_hw0 = extract32(r1, 0, 16);
1699 uint32_t ret_hw1 = extract32(r1, 16, 16);
1701 ret_hw0 = clrsb32(ret_hw0 << 16);
1702 ret_hw1 = clrsb32(ret_hw1 << 16);
1704 if (ret_hw0 > 15) {
1705 ret_hw0 = 15;
1707 if (ret_hw1 > 15) {
1708 ret_hw1 = 15;
1711 return ret_hw0 | (ret_hw1 << 16);
1714 uint32_t helper_sh(target_ulong r1, target_ulong r2)
1716 int32_t shift_count = sextract32(r2, 0, 6);
1718 if (shift_count == -32) {
1719 return 0;
1720 } else if (shift_count < 0) {
1721 return r1 >> -shift_count;
1722 } else {
1723 return r1 << shift_count;
1727 uint32_t helper_sh_h(target_ulong r1, target_ulong r2)
1729 int32_t ret_hw0, ret_hw1;
1730 int32_t shift_count;
1732 shift_count = sextract32(r2, 0, 5);
1734 if (shift_count == -16) {
1735 return 0;
1736 } else if (shift_count < 0) {
1737 ret_hw0 = extract32(r1, 0, 16) >> -shift_count;
1738 ret_hw1 = extract32(r1, 16, 16) >> -shift_count;
1739 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1740 } else {
1741 ret_hw0 = extract32(r1, 0, 16) << shift_count;
1742 ret_hw1 = extract32(r1, 16, 16) << shift_count;
1743 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1747 uint32_t helper_sha(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1749 int32_t shift_count;
1750 int64_t result, t1;
1751 uint32_t ret;
1753 shift_count = sextract32(r2, 0, 6);
1754 t1 = sextract32(r1, 0, 32);
1756 if (shift_count == 0) {
1757 env->PSW_USB_C = env->PSW_USB_V = 0;
1758 ret = r1;
1759 } else if (shift_count == -32) {
1760 env->PSW_USB_C = r1;
1761 env->PSW_USB_V = 0;
1762 ret = t1 >> 31;
1763 } else if (shift_count > 0) {
1764 result = t1 << shift_count;
1765 /* calc carry */
1766 env->PSW_USB_C = ((result & 0xffffffff00000000ULL) != 0);
1767 /* calc v */
1768 env->PSW_USB_V = (((result > 0x7fffffffLL) ||
1769 (result < -0x80000000LL)) << 31);
1770 /* calc sv */
1771 env->PSW_USB_SV |= env->PSW_USB_V;
1772 ret = (uint32_t)result;
1773 } else {
1774 env->PSW_USB_V = 0;
1775 env->PSW_USB_C = (r1 & ((1 << -shift_count) - 1));
1776 ret = t1 >> -shift_count;
1779 env->PSW_USB_AV = ret ^ ret * 2u;
1780 env->PSW_USB_SAV |= env->PSW_USB_AV;
1782 return ret;
1785 uint32_t helper_sha_h(target_ulong r1, target_ulong r2)
1787 int32_t shift_count;
1788 int32_t ret_hw0, ret_hw1;
1790 shift_count = sextract32(r2, 0, 5);
1792 if (shift_count == 0) {
1793 return r1;
1794 } else if (shift_count < 0) {
1795 ret_hw0 = sextract32(r1, 0, 16) >> -shift_count;
1796 ret_hw1 = sextract32(r1, 16, 16) >> -shift_count;
1797 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1798 } else {
1799 ret_hw0 = sextract32(r1, 0, 16) << shift_count;
1800 ret_hw1 = sextract32(r1, 16, 16) << shift_count;
1801 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1805 uint32_t helper_bmerge(target_ulong r1, target_ulong r2)
1807 uint32_t i, ret;
1809 ret = 0;
1810 for (i = 0; i < 16; i++) {
1811 ret |= (r1 & 1) << (2 * i + 1);
1812 ret |= (r2 & 1) << (2 * i);
1813 r1 = r1 >> 1;
1814 r2 = r2 >> 1;
1816 return ret;
1819 uint64_t helper_bsplit(uint32_t r1)
1821 int32_t i;
1822 uint64_t ret;
1824 ret = 0;
1825 for (i = 0; i < 32; i = i + 2) {
1826 /* even */
1827 ret |= (r1 & 1) << (i/2);
1828 r1 = r1 >> 1;
1829 /* odd */
1830 ret |= (uint64_t)(r1 & 1) << (i/2 + 32);
1831 r1 = r1 >> 1;
1833 return ret;
1836 uint32_t helper_parity(target_ulong r1)
1838 uint32_t ret;
1839 uint32_t nOnes, i;
1841 ret = 0;
1842 nOnes = 0;
1843 for (i = 0; i < 8; i++) {
1844 ret ^= (r1 & 1);
1845 r1 = r1 >> 1;
1847 /* second byte */
1848 nOnes = 0;
1849 for (i = 0; i < 8; i++) {
1850 nOnes ^= (r1 & 1);
1851 r1 = r1 >> 1;
1853 ret |= nOnes << 8;
1854 /* third byte */
1855 nOnes = 0;
1856 for (i = 0; i < 8; i++) {
1857 nOnes ^= (r1 & 1);
1858 r1 = r1 >> 1;
1860 ret |= nOnes << 16;
1861 /* fourth byte */
1862 nOnes = 0;
1863 for (i = 0; i < 8; i++) {
1864 nOnes ^= (r1 & 1);
1865 r1 = r1 >> 1;
1867 ret |= nOnes << 24;
1869 return ret;
1872 uint32_t helper_pack(uint32_t carry, uint32_t r1_low, uint32_t r1_high,
1873 target_ulong r2)
1875 uint32_t ret;
1876 int32_t fp_exp, fp_frac, temp_exp, fp_exp_frac;
1877 int32_t int_exp = r1_high;
1878 int32_t int_mant = r1_low;
1879 uint32_t flag_rnd = (int_mant & (1 << 7)) && (
1880 (int_mant & (1 << 8)) ||
1881 (int_mant & 0x7f) ||
1882 (carry != 0));
1883 if (((int_mant & (1<<31)) == 0) && (int_exp == 255)) {
1884 fp_exp = 255;
1885 fp_frac = extract32(int_mant, 8, 23);
1886 } else if ((int_mant & (1<<31)) && (int_exp >= 127)) {
1887 fp_exp = 255;
1888 fp_frac = 0;
1889 } else if ((int_mant & (1<<31)) && (int_exp <= -128)) {
1890 fp_exp = 0;
1891 fp_frac = 0;
1892 } else if (int_mant == 0) {
1893 fp_exp = 0;
1894 fp_frac = 0;
1895 } else {
1896 if (((int_mant & (1 << 31)) == 0)) {
1897 temp_exp = 0;
1898 } else {
1899 temp_exp = int_exp + 128;
1901 fp_exp_frac = (((temp_exp & 0xff) << 23) |
1902 extract32(int_mant, 8, 23))
1903 + flag_rnd;
1904 fp_exp = extract32(fp_exp_frac, 23, 8);
1905 fp_frac = extract32(fp_exp_frac, 0, 23);
1907 ret = r2 & (1 << 31);
1908 ret = ret + (fp_exp << 23);
1909 ret = ret + (fp_frac & 0x7fffff);
1911 return ret;
1914 uint64_t helper_unpack(target_ulong arg1)
1916 int32_t fp_exp = extract32(arg1, 23, 8);
1917 int32_t fp_frac = extract32(arg1, 0, 23);
1918 uint64_t ret;
1919 int32_t int_exp, int_mant;
1921 if (fp_exp == 255) {
1922 int_exp = 255;
1923 int_mant = (fp_frac << 7);
1924 } else if ((fp_exp == 0) && (fp_frac == 0)) {
1925 int_exp = -127;
1926 int_mant = 0;
1927 } else if ((fp_exp == 0) && (fp_frac != 0)) {
1928 int_exp = -126;
1929 int_mant = (fp_frac << 7);
1930 } else {
1931 int_exp = fp_exp - 127;
1932 int_mant = (fp_frac << 7);
1933 int_mant |= (1 << 30);
1935 ret = int_exp;
1936 ret = ret << 32;
1937 ret |= int_mant;
1939 return ret;
1942 uint64_t helper_dvinit_b_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1944 uint64_t ret;
1945 int32_t abs_sig_dividend, abs_base_dividend, abs_divisor;
1946 int32_t quotient_sign;
1948 ret = sextract32(r1, 0, 32);
1949 ret = ret << 24;
1950 quotient_sign = 0;
1951 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1952 ret |= 0xffffff;
1953 quotient_sign = 1;
1956 abs_sig_dividend = abs(r1) >> 7;
1957 abs_base_dividend = abs(r1) & 0x7f;
1958 abs_divisor = abs(r1);
1959 /* calc overflow */
1960 env->PSW_USB_V = 0;
1961 if ((quotient_sign) && (abs_divisor)) {
1962 env->PSW_USB_V = (((abs_sig_dividend == abs_divisor) &&
1963 (abs_base_dividend >= abs_divisor)) ||
1964 (abs_sig_dividend > abs_divisor));
1965 } else {
1966 env->PSW_USB_V = (abs_sig_dividend >= abs_divisor);
1968 env->PSW_USB_V = env->PSW_USB_V << 31;
1969 env->PSW_USB_SV |= env->PSW_USB_V;
1970 env->PSW_USB_AV = 0;
1972 return ret;
1975 uint64_t helper_dvinit_b_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1977 uint64_t ret = sextract32(r1, 0, 32);
1979 ret = ret << 24;
1980 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1981 ret |= 0xffffff;
1983 /* calc overflow */
1984 env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffffff80)));
1985 env->PSW_USB_V = env->PSW_USB_V << 31;
1986 env->PSW_USB_SV |= env->PSW_USB_V;
1987 env->PSW_USB_AV = 0;
1989 return ret;
1992 uint64_t helper_dvinit_h_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1994 uint64_t ret;
1995 int32_t abs_sig_dividend, abs_base_dividend, abs_divisor;
1996 int32_t quotient_sign;
1998 ret = sextract32(r1, 0, 32);
1999 ret = ret << 16;
2000 quotient_sign = 0;
2001 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
2002 ret |= 0xffff;
2003 quotient_sign = 1;
2006 abs_sig_dividend = abs(r1) >> 7;
2007 abs_base_dividend = abs(r1) & 0x7f;
2008 abs_divisor = abs(r1);
2009 /* calc overflow */
2010 env->PSW_USB_V = 0;
2011 if ((quotient_sign) && (abs_divisor)) {
2012 env->PSW_USB_V = (((abs_sig_dividend == abs_divisor) &&
2013 (abs_base_dividend >= abs_divisor)) ||
2014 (abs_sig_dividend > abs_divisor));
2015 } else {
2016 env->PSW_USB_V = (abs_sig_dividend >= abs_divisor);
2018 env->PSW_USB_V = env->PSW_USB_V << 31;
2019 env->PSW_USB_SV |= env->PSW_USB_V;
2020 env->PSW_USB_AV = 0;
2022 return ret;
2025 uint64_t helper_dvinit_h_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
2027 uint64_t ret = sextract32(r1, 0, 32);
2029 ret = ret << 16;
2030 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
2031 ret |= 0xffff;
2033 /* calc overflow */
2034 env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffff8000)));
2035 env->PSW_USB_V = env->PSW_USB_V << 31;
2036 env->PSW_USB_SV |= env->PSW_USB_V;
2037 env->PSW_USB_AV = 0;
2039 return ret;
2042 uint64_t helper_dvadj(uint64_t r1, uint32_t r2)
2044 int32_t x_sign = (r1 >> 63);
2045 int32_t q_sign = x_sign ^ (r2 >> 31);
2046 int32_t eq_pos = x_sign & ((r1 >> 32) == r2);
2047 int32_t eq_neg = x_sign & ((r1 >> 32) == -r2);
2048 uint32_t quotient;
2049 uint64_t ret, remainder;
2051 if ((q_sign & ~eq_neg) | eq_pos) {
2052 quotient = (r1 + 1) & 0xffffffff;
2053 } else {
2054 quotient = r1 & 0xffffffff;
2057 if (eq_pos | eq_neg) {
2058 remainder = 0;
2059 } else {
2060 remainder = (r1 & 0xffffffff00000000ull);
2062 ret = remainder|quotient;
2063 return ret;
2066 uint64_t helper_dvstep(uint64_t r1, uint32_t r2)
2068 int32_t dividend_sign = extract64(r1, 63, 1);
2069 int32_t divisor_sign = extract32(r2, 31, 1);
2070 int32_t quotient_sign = (dividend_sign != divisor_sign);
2071 int32_t addend, dividend_quotient, remainder;
2072 int32_t i, temp;
2074 if (quotient_sign) {
2075 addend = r2;
2076 } else {
2077 addend = -r2;
2079 dividend_quotient = (int32_t)r1;
2080 remainder = (int32_t)(r1 >> 32);
2082 for (i = 0; i < 8; i++) {
2083 remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1);
2084 dividend_quotient <<= 1;
2085 temp = remainder + addend;
2086 if ((temp < 0) == dividend_sign) {
2087 remainder = temp;
2089 if (((temp < 0) == dividend_sign)) {
2090 dividend_quotient = dividend_quotient | !quotient_sign;
2091 } else {
2092 dividend_quotient = dividend_quotient | quotient_sign;
2095 return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient;
2098 uint64_t helper_dvstep_u(uint64_t r1, uint32_t r2)
2100 int32_t dividend_quotient = extract64(r1, 0, 32);
2101 int64_t remainder = extract64(r1, 32, 32);
2102 int32_t i;
2103 int64_t temp;
2104 for (i = 0; i < 8; i++) {
2105 remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1);
2106 dividend_quotient <<= 1;
2107 temp = (remainder & 0xffffffff) - r2;
2108 if (temp >= 0) {
2109 remainder = temp;
2111 dividend_quotient = dividend_quotient | !(temp < 0);
2113 return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient;
2116 uint64_t helper_mul_h(uint32_t arg00, uint32_t arg01,
2117 uint32_t arg10, uint32_t arg11, uint32_t n)
2119 uint64_t ret;
2120 uint32_t result0, result1;
2122 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
2123 ((arg10 & 0xffff) == 0x8000) && (n == 1);
2124 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
2125 ((arg11 & 0xffff) == 0x8000) && (n == 1);
2126 if (sc1) {
2127 result1 = 0x7fffffff;
2128 } else {
2129 result1 = (((uint32_t)(arg00 * arg10)) << n);
2131 if (sc0) {
2132 result0 = 0x7fffffff;
2133 } else {
2134 result0 = (((uint32_t)(arg01 * arg11)) << n);
2136 ret = (((uint64_t)result1 << 32)) | result0;
2137 return ret;
2140 uint64_t helper_mulm_h(uint32_t arg00, uint32_t arg01,
2141 uint32_t arg10, uint32_t arg11, uint32_t n)
2143 uint64_t ret;
2144 int64_t result0, result1;
2146 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
2147 ((arg10 & 0xffff) == 0x8000) && (n == 1);
2148 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
2149 ((arg11 & 0xffff) == 0x8000) && (n == 1);
2151 if (sc1) {
2152 result1 = 0x7fffffff;
2153 } else {
2154 result1 = (((int32_t)arg00 * (int32_t)arg10) << n);
2156 if (sc0) {
2157 result0 = 0x7fffffff;
2158 } else {
2159 result0 = (((int32_t)arg01 * (int32_t)arg11) << n);
2161 ret = (result1 + result0);
2162 ret = ret << 16;
2163 return ret;
2165 uint32_t helper_mulr_h(uint32_t arg00, uint32_t arg01,
2166 uint32_t arg10, uint32_t arg11, uint32_t n)
2168 uint32_t result0, result1;
2170 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
2171 ((arg10 & 0xffff) == 0x8000) && (n == 1);
2172 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
2173 ((arg11 & 0xffff) == 0x8000) && (n == 1);
2175 if (sc1) {
2176 result1 = 0x7fffffff;
2177 } else {
2178 result1 = ((arg00 * arg10) << n) + 0x8000;
2180 if (sc0) {
2181 result0 = 0x7fffffff;
2182 } else {
2183 result0 = ((arg01 * arg11) << n) + 0x8000;
2185 return (result1 & 0xffff0000) | (result0 >> 16);
2188 /* context save area (CSA) related helpers */
2190 static int cdc_increment(target_ulong *psw)
2192 if ((*psw & MASK_PSW_CDC) == 0x7f) {
2193 return 0;
2196 (*psw)++;
2197 /* check for overflow */
2198 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
2199 int mask = (1u << (7 - lo)) - 1;
2200 int count = *psw & mask;
2201 if (count == 0) {
2202 (*psw)--;
2203 return 1;
2205 return 0;
2208 static int cdc_decrement(target_ulong *psw)
2210 if ((*psw & MASK_PSW_CDC) == 0x7f) {
2211 return 0;
2213 /* check for underflow */
2214 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
2215 int mask = (1u << (7 - lo)) - 1;
2216 int count = *psw & mask;
2217 if (count == 0) {
2218 return 1;
2220 (*psw)--;
2221 return 0;
2224 static bool cdc_zero(target_ulong *psw)
2226 int cdc = *psw & MASK_PSW_CDC;
2227 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
2228 7'b1111111, otherwise returns FALSE. */
2229 if (cdc == 0x7f) {
2230 return true;
2232 /* find CDC.COUNT */
2233 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
2234 int mask = (1u << (7 - lo)) - 1;
2235 int count = *psw & mask;
2236 return count == 0;
2239 static void save_context_upper(CPUTriCoreState *env, int ea)
2241 cpu_stl_data(env, ea, env->PCXI);
2242 cpu_stl_data(env, ea+4, env->PSW);
2243 cpu_stl_data(env, ea+8, env->gpr_a[10]);
2244 cpu_stl_data(env, ea+12, env->gpr_a[11]);
2245 cpu_stl_data(env, ea+16, env->gpr_d[8]);
2246 cpu_stl_data(env, ea+20, env->gpr_d[9]);
2247 cpu_stl_data(env, ea+24, env->gpr_d[10]);
2248 cpu_stl_data(env, ea+28, env->gpr_d[11]);
2249 cpu_stl_data(env, ea+32, env->gpr_a[12]);
2250 cpu_stl_data(env, ea+36, env->gpr_a[13]);
2251 cpu_stl_data(env, ea+40, env->gpr_a[14]);
2252 cpu_stl_data(env, ea+44, env->gpr_a[15]);
2253 cpu_stl_data(env, ea+48, env->gpr_d[12]);
2254 cpu_stl_data(env, ea+52, env->gpr_d[13]);
2255 cpu_stl_data(env, ea+56, env->gpr_d[14]);
2256 cpu_stl_data(env, ea+60, env->gpr_d[15]);
2259 static void save_context_lower(CPUTriCoreState *env, int ea)
2261 cpu_stl_data(env, ea, env->PCXI);
2262 cpu_stl_data(env, ea+4, env->gpr_a[11]);
2263 cpu_stl_data(env, ea+8, env->gpr_a[2]);
2264 cpu_stl_data(env, ea+12, env->gpr_a[3]);
2265 cpu_stl_data(env, ea+16, env->gpr_d[0]);
2266 cpu_stl_data(env, ea+20, env->gpr_d[1]);
2267 cpu_stl_data(env, ea+24, env->gpr_d[2]);
2268 cpu_stl_data(env, ea+28, env->gpr_d[3]);
2269 cpu_stl_data(env, ea+32, env->gpr_a[4]);
2270 cpu_stl_data(env, ea+36, env->gpr_a[5]);
2271 cpu_stl_data(env, ea+40, env->gpr_a[6]);
2272 cpu_stl_data(env, ea+44, env->gpr_a[7]);
2273 cpu_stl_data(env, ea+48, env->gpr_d[4]);
2274 cpu_stl_data(env, ea+52, env->gpr_d[5]);
2275 cpu_stl_data(env, ea+56, env->gpr_d[6]);
2276 cpu_stl_data(env, ea+60, env->gpr_d[7]);
2279 static void restore_context_upper(CPUTriCoreState *env, int ea,
2280 target_ulong *new_PCXI, target_ulong *new_PSW)
2282 *new_PCXI = cpu_ldl_data(env, ea);
2283 *new_PSW = cpu_ldl_data(env, ea+4);
2284 env->gpr_a[10] = cpu_ldl_data(env, ea+8);
2285 env->gpr_a[11] = cpu_ldl_data(env, ea+12);
2286 env->gpr_d[8] = cpu_ldl_data(env, ea+16);
2287 env->gpr_d[9] = cpu_ldl_data(env, ea+20);
2288 env->gpr_d[10] = cpu_ldl_data(env, ea+24);
2289 env->gpr_d[11] = cpu_ldl_data(env, ea+28);
2290 env->gpr_a[12] = cpu_ldl_data(env, ea+32);
2291 env->gpr_a[13] = cpu_ldl_data(env, ea+36);
2292 env->gpr_a[14] = cpu_ldl_data(env, ea+40);
2293 env->gpr_a[15] = cpu_ldl_data(env, ea+44);
2294 env->gpr_d[12] = cpu_ldl_data(env, ea+48);
2295 env->gpr_d[13] = cpu_ldl_data(env, ea+52);
2296 env->gpr_d[14] = cpu_ldl_data(env, ea+56);
2297 env->gpr_d[15] = cpu_ldl_data(env, ea+60);
2300 static void restore_context_lower(CPUTriCoreState *env, int ea,
2301 target_ulong *ra, target_ulong *pcxi)
2303 *pcxi = cpu_ldl_data(env, ea);
2304 *ra = cpu_ldl_data(env, ea+4);
2305 env->gpr_a[2] = cpu_ldl_data(env, ea+8);
2306 env->gpr_a[3] = cpu_ldl_data(env, ea+12);
2307 env->gpr_d[0] = cpu_ldl_data(env, ea+16);
2308 env->gpr_d[1] = cpu_ldl_data(env, ea+20);
2309 env->gpr_d[2] = cpu_ldl_data(env, ea+24);
2310 env->gpr_d[3] = cpu_ldl_data(env, ea+28);
2311 env->gpr_a[4] = cpu_ldl_data(env, ea+32);
2312 env->gpr_a[5] = cpu_ldl_data(env, ea+36);
2313 env->gpr_a[6] = cpu_ldl_data(env, ea+40);
2314 env->gpr_a[7] = cpu_ldl_data(env, ea+44);
2315 env->gpr_d[4] = cpu_ldl_data(env, ea+48);
2316 env->gpr_d[5] = cpu_ldl_data(env, ea+52);
2317 env->gpr_d[6] = cpu_ldl_data(env, ea+56);
2318 env->gpr_d[7] = cpu_ldl_data(env, ea+60);
2321 void helper_call(CPUTriCoreState *env, uint32_t next_pc)
2323 target_ulong tmp_FCX;
2324 target_ulong ea;
2325 target_ulong new_FCX;
2326 target_ulong psw;
2328 psw = psw_read(env);
2329 /* if (FCX == 0) trap(FCU); */
2330 if (env->FCX == 0) {
2331 /* FCU trap */
2333 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
2334 if (psw & MASK_PSW_CDE) {
2335 if (cdc_increment(&psw)) {
2336 /* CDO trap */
2339 /* PSW.CDE = 1;*/
2340 psw |= MASK_PSW_CDE;
2341 /* tmp_FCX = FCX; */
2342 tmp_FCX = env->FCX;
2343 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
2344 ea = ((env->FCX & MASK_FCX_FCXS) << 12) +
2345 ((env->FCX & MASK_FCX_FCXO) << 6);
2346 /* new_FCX = M(EA, word); */
2347 new_FCX = cpu_ldl_data(env, ea);
2348 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
2349 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
2350 D[15]}; */
2351 save_context_upper(env, ea);
2353 /* PCXI.PCPN = ICR.CCPN; */
2354 env->PCXI = (env->PCXI & 0xffffff) +
2355 ((env->ICR & MASK_ICR_CCPN) << 24);
2356 /* PCXI.PIE = ICR.IE; */
2357 env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) +
2358 ((env->ICR & MASK_ICR_IE) << 15));
2359 /* PCXI.UL = 1; */
2360 env->PCXI |= MASK_PCXI_UL;
2362 /* PCXI[19: 0] = FCX[19: 0]; */
2363 env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff);
2364 /* FCX[19: 0] = new_FCX[19: 0]; */
2365 env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff);
2366 /* A[11] = next_pc[31: 0]; */
2367 env->gpr_a[11] = next_pc;
2369 /* if (tmp_FCX == LCX) trap(FCD);*/
2370 if (tmp_FCX == env->LCX) {
2371 /* FCD trap */
2373 psw_write(env, psw);
2376 void helper_ret(CPUTriCoreState *env)
2378 target_ulong ea;
2379 target_ulong new_PCXI;
2380 target_ulong new_PSW, psw;
2382 psw = psw_read(env);
2383 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
2384 if (env->PSW & MASK_PSW_CDE) {
2385 if (cdc_decrement(&(env->PSW))) {
2386 /* CDU trap */
2389 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2390 if ((env->PCXI & 0xfffff) == 0) {
2391 /* CSU trap */
2393 /* if (PCXI.UL == 0) then trap(CTYP); */
2394 if ((env->PCXI & MASK_PCXI_UL) == 0) {
2395 /* CTYP trap */
2397 /* PC = {A11 [31: 1], 1’b0}; */
2398 env->PC = env->gpr_a[11] & 0xfffffffe;
2400 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
2401 ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) +
2402 ((env->PCXI & MASK_PCXI_PCXO) << 6);
2403 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2404 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2405 restore_context_upper(env, ea, &new_PCXI, &new_PSW);
2406 /* M(EA, word) = FCX; */
2407 cpu_stl_data(env, ea, env->FCX);
2408 /* FCX[19: 0] = PCXI[19: 0]; */
2409 env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff);
2410 /* PCXI = new_PCXI; */
2411 env->PCXI = new_PCXI;
2413 if (tricore_feature(env, TRICORE_FEATURE_13)) {
2414 /* PSW = new_PSW */
2415 psw_write(env, new_PSW);
2416 } else {
2417 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
2418 psw_write(env, (new_PSW & ~(0x3000000)) + (psw & (0x3000000)));
2422 void helper_bisr(CPUTriCoreState *env, uint32_t const9)
2424 target_ulong tmp_FCX;
2425 target_ulong ea;
2426 target_ulong new_FCX;
2428 if (env->FCX == 0) {
2429 /* FCU trap */
2432 tmp_FCX = env->FCX;
2433 ea = ((env->FCX & 0xf0000) << 12) + ((env->FCX & 0xffff) << 6);
2435 /* new_FCX = M(EA, word); */
2436 new_FCX = cpu_ldl_data(env, ea);
2437 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
2438 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
2439 save_context_lower(env, ea);
2442 /* PCXI.PCPN = ICR.CCPN */
2443 env->PCXI = (env->PCXI & 0xffffff) +
2444 ((env->ICR & MASK_ICR_CCPN) << 24);
2445 /* PCXI.PIE = ICR.IE */
2446 env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) +
2447 ((env->ICR & MASK_ICR_IE) << 15));
2448 /* PCXI.UL = 0 */
2449 env->PCXI &= ~(MASK_PCXI_UL);
2450 /* PCXI[19: 0] = FCX[19: 0] */
2451 env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff);
2452 /* FXC[19: 0] = new_FCX[19: 0] */
2453 env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff);
2454 /* ICR.IE = 1 */
2455 env->ICR |= MASK_ICR_IE;
2457 env->ICR |= const9; /* ICR.CCPN = const9[7: 0];*/
2459 if (tmp_FCX == env->LCX) {
2460 /* FCD trap */
2464 void helper_rfe(CPUTriCoreState *env)
2466 target_ulong ea;
2467 target_ulong new_PCXI;
2468 target_ulong new_PSW;
2469 /* if (PCXI[19: 0] == 0) then trap(CSU); */
2470 if ((env->PCXI & 0xfffff) == 0) {
2471 /* raise csu trap */
2473 /* if (PCXI.UL == 0) then trap(CTYP); */
2474 if ((env->PCXI & MASK_PCXI_UL) == 0) {
2475 /* raise CTYP trap */
2477 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
2478 if (!cdc_zero(&(env->PSW)) && (env->PSW & MASK_PSW_CDE)) {
2479 /* raise MNG trap */
2481 /* ICR.IE = PCXI.PIE; */
2482 env->ICR = (env->ICR & ~MASK_ICR_IE) + ((env->PCXI & MASK_PCXI_PIE) >> 15);
2483 /* ICR.CCPN = PCXI.PCPN; */
2484 env->ICR = (env->ICR & ~MASK_ICR_CCPN) +
2485 ((env->PCXI & MASK_PCXI_PCPN) >> 24);
2486 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
2487 ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) +
2488 ((env->PCXI & MASK_PCXI_PCXO) << 6);
2489 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
2490 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
2491 restore_context_upper(env, ea, &new_PCXI, &new_PSW);
2492 /* M(EA, word) = FCX;*/
2493 cpu_stl_data(env, ea, env->FCX);
2494 /* FCX[19: 0] = PCXI[19: 0]; */
2495 env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff);
2496 /* PCXI = new_PCXI; */
2497 env->PCXI = new_PCXI;
2498 /* write psw */
2499 psw_write(env, new_PSW);
2502 void helper_ldlcx(CPUTriCoreState *env, uint32_t ea)
2504 uint32_t dummy;
2505 /* insn doesn't load PCXI and RA */
2506 restore_context_lower(env, ea, &dummy, &dummy);
2509 void helper_lducx(CPUTriCoreState *env, uint32_t ea)
2511 uint32_t dummy;
2512 /* insn doesn't load PCXI and PSW */
2513 restore_context_upper(env, ea, &dummy, &dummy);
2516 void helper_stlcx(CPUTriCoreState *env, uint32_t ea)
2518 save_context_lower(env, ea);
2521 void helper_stucx(CPUTriCoreState *env, uint32_t ea)
2523 save_context_upper(env, ea);
2526 void helper_psw_write(CPUTriCoreState *env, uint32_t arg)
2528 psw_write(env, arg);
2531 uint32_t helper_psw_read(CPUTriCoreState *env)
2533 return psw_read(env);
2537 static inline void QEMU_NORETURN do_raise_exception_err(CPUTriCoreState *env,
2538 uint32_t exception,
2539 int error_code,
2540 uintptr_t pc)
2542 CPUState *cs = CPU(tricore_env_get_cpu(env));
2543 cs->exception_index = exception;
2544 env->error_code = error_code;
2546 if (pc) {
2547 /* now we have a real cpu fault */
2548 cpu_restore_state(cs, pc);
2551 cpu_loop_exit(cs);
2554 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
2555 uintptr_t retaddr)
2557 int ret;
2558 ret = cpu_tricore_handle_mmu_fault(cs, addr, is_write, mmu_idx);
2559 if (ret) {
2560 TriCoreCPU *cpu = TRICORE_CPU(cs);
2561 CPUTriCoreState *env = &cpu->env;
2562 do_raise_exception_err(env, cs->exception_index,
2563 env->error_code, retaddr);