Merge remote-tracking branch 'remotes/stefanha/tags/net-pull-request' into staging
[qemu.git] / target-tricore / op_helper.c
blobed26b302b062e4961031bc664acfdc9752f473ee
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 target_ulong helper_add_h_ssov(CPUTriCoreState *env, target_ulong r1,
188 target_ulong r2)
190 int32_t ret_hw0, ret_hw1;
192 ret_hw0 = sextract32(r1, 0, 16) + sextract32(r2, 0, 16);
193 ret_hw1 = sextract32(r1, 16, 16) + sextract32(r2, 16, 16);
194 return ssov16(env, ret_hw0, ret_hw1);
197 target_ulong helper_add_suov(CPUTriCoreState *env, target_ulong r1,
198 target_ulong r2)
200 int64_t t1 = extract64(r1, 0, 32);
201 int64_t t2 = extract64(r2, 0, 32);
202 int64_t result = t1 + t2;
203 return suov32_pos(env, result);
206 target_ulong helper_add_h_suov(CPUTriCoreState *env, target_ulong r1,
207 target_ulong r2)
209 int32_t ret_hw0, ret_hw1;
211 ret_hw0 = extract32(r1, 0, 16) + extract32(r2, 0, 16);
212 ret_hw1 = extract32(r1, 16, 16) + extract32(r2, 16, 16);
213 return suov16(env, ret_hw0, ret_hw1);
216 target_ulong helper_sub_ssov(CPUTriCoreState *env, target_ulong r1,
217 target_ulong r2)
219 int64_t t1 = sextract64(r1, 0, 32);
220 int64_t t2 = sextract64(r2, 0, 32);
221 int64_t result = t1 - t2;
222 return ssov32(env, result);
225 target_ulong helper_sub_h_ssov(CPUTriCoreState *env, target_ulong r1,
226 target_ulong r2)
228 int32_t ret_hw0, ret_hw1;
230 ret_hw0 = sextract32(r1, 0, 16) - sextract32(r2, 0, 16);
231 ret_hw1 = sextract32(r1, 16, 16) - sextract32(r2, 16, 16);
232 return ssov16(env, ret_hw0, ret_hw1);
235 target_ulong helper_sub_suov(CPUTriCoreState *env, target_ulong r1,
236 target_ulong r2)
238 int64_t t1 = extract64(r1, 0, 32);
239 int64_t t2 = extract64(r2, 0, 32);
240 int64_t result = t1 - t2;
241 return suov32_neg(env, result);
244 target_ulong helper_sub_h_suov(CPUTriCoreState *env, target_ulong r1,
245 target_ulong r2)
247 int32_t ret_hw0, ret_hw1;
249 ret_hw0 = extract32(r1, 0, 16) - extract32(r2, 0, 16);
250 ret_hw1 = extract32(r1, 16, 16) - extract32(r2, 16, 16);
251 return suov16(env, ret_hw0, ret_hw1);
254 target_ulong helper_mul_ssov(CPUTriCoreState *env, target_ulong r1,
255 target_ulong r2)
257 int64_t t1 = sextract64(r1, 0, 32);
258 int64_t t2 = sextract64(r2, 0, 32);
259 int64_t result = t1 * t2;
260 return ssov32(env, result);
263 target_ulong helper_mul_suov(CPUTriCoreState *env, target_ulong r1,
264 target_ulong r2)
266 int64_t t1 = extract64(r1, 0, 32);
267 int64_t t2 = extract64(r2, 0, 32);
268 int64_t result = t1 * t2;
270 return suov32_pos(env, result);
273 target_ulong helper_sha_ssov(CPUTriCoreState *env, target_ulong r1,
274 target_ulong r2)
276 int64_t t1 = sextract64(r1, 0, 32);
277 int32_t t2 = sextract64(r2, 0, 6);
278 int64_t result;
279 if (t2 == 0) {
280 result = t1;
281 } else if (t2 > 0) {
282 result = t1 << t2;
283 } else {
284 result = t1 >> -t2;
286 return ssov32(env, result);
289 uint32_t helper_abs_ssov(CPUTriCoreState *env, target_ulong r1)
291 target_ulong result;
292 result = ((int32_t)r1 >= 0) ? r1 : (0 - r1);
293 return ssov32(env, result);
296 uint32_t helper_abs_h_ssov(CPUTriCoreState *env, target_ulong r1)
298 int32_t ret_h0, ret_h1;
300 ret_h0 = sextract32(r1, 0, 16);
301 ret_h0 = (ret_h0 >= 0) ? ret_h0 : (0 - ret_h0);
303 ret_h1 = sextract32(r1, 16, 16);
304 ret_h1 = (ret_h1 >= 0) ? ret_h1 : (0 - ret_h1);
306 return ssov16(env, ret_h0, ret_h1);
309 target_ulong helper_absdif_ssov(CPUTriCoreState *env, target_ulong r1,
310 target_ulong r2)
312 int64_t t1 = sextract64(r1, 0, 32);
313 int64_t t2 = sextract64(r2, 0, 32);
314 int64_t result;
316 if (t1 > t2) {
317 result = t1 - t2;
318 } else {
319 result = t2 - t1;
321 return ssov32(env, result);
324 uint32_t helper_absdif_h_ssov(CPUTriCoreState *env, target_ulong r1,
325 target_ulong r2)
327 int32_t t1, t2;
328 int32_t ret_h0, ret_h1;
330 t1 = sextract32(r1, 0, 16);
331 t2 = sextract32(r2, 0, 16);
332 if (t1 > t2) {
333 ret_h0 = t1 - t2;
334 } else {
335 ret_h0 = t2 - t1;
338 t1 = sextract32(r1, 16, 16);
339 t2 = sextract32(r2, 16, 16);
340 if (t1 > t2) {
341 ret_h1 = t1 - t2;
342 } else {
343 ret_h1 = t2 - t1;
346 return ssov16(env, ret_h0, ret_h1);
349 target_ulong helper_madd32_ssov(CPUTriCoreState *env, target_ulong r1,
350 target_ulong r2, target_ulong r3)
352 int64_t t1 = sextract64(r1, 0, 32);
353 int64_t t2 = sextract64(r2, 0, 32);
354 int64_t t3 = sextract64(r3, 0, 32);
355 int64_t result;
357 result = t2 + (t1 * t3);
358 return ssov32(env, result);
361 target_ulong helper_madd32_suov(CPUTriCoreState *env, target_ulong r1,
362 target_ulong r2, target_ulong r3)
364 uint64_t t1 = extract64(r1, 0, 32);
365 uint64_t t2 = extract64(r2, 0, 32);
366 uint64_t t3 = extract64(r3, 0, 32);
367 int64_t result;
369 result = t2 + (t1 * t3);
370 return suov32_pos(env, result);
373 uint64_t helper_madd64_ssov(CPUTriCoreState *env, target_ulong r1,
374 uint64_t r2, target_ulong r3)
376 uint64_t ret, ovf;
377 int64_t t1 = sextract64(r1, 0, 32);
378 int64_t t3 = sextract64(r3, 0, 32);
379 int64_t mul;
381 mul = t1 * t3;
382 ret = mul + r2;
383 ovf = (ret ^ mul) & ~(mul ^ r2);
385 t1 = ret >> 32;
386 env->PSW_USB_AV = t1 ^ t1 * 2u;
387 env->PSW_USB_SAV |= env->PSW_USB_AV;
389 if ((int64_t)ovf < 0) {
390 env->PSW_USB_V = (1 << 31);
391 env->PSW_USB_SV = (1 << 31);
392 /* ext_ret > MAX_INT */
393 if (mul >= 0) {
394 ret = INT64_MAX;
395 /* ext_ret < MIN_INT */
396 } else {
397 ret = INT64_MIN;
399 } else {
400 env->PSW_USB_V = 0;
403 return ret;
406 uint64_t helper_madd64_suov(CPUTriCoreState *env, target_ulong r1,
407 uint64_t r2, target_ulong r3)
409 uint64_t ret, mul;
410 uint64_t t1 = extract64(r1, 0, 32);
411 uint64_t t3 = extract64(r3, 0, 32);
413 mul = t1 * t3;
414 ret = mul + r2;
416 t1 = ret >> 32;
417 env->PSW_USB_AV = t1 ^ t1 * 2u;
418 env->PSW_USB_SAV |= env->PSW_USB_AV;
420 if (ret < r2) {
421 env->PSW_USB_V = (1 << 31);
422 env->PSW_USB_SV = (1 << 31);
423 /* saturate */
424 ret = UINT64_MAX;
425 } else {
426 env->PSW_USB_V = 0;
428 return ret;
431 target_ulong helper_msub32_ssov(CPUTriCoreState *env, target_ulong r1,
432 target_ulong r2, target_ulong r3)
434 int64_t t1 = sextract64(r1, 0, 32);
435 int64_t t2 = sextract64(r2, 0, 32);
436 int64_t t3 = sextract64(r3, 0, 32);
437 int64_t result;
439 result = t2 - (t1 * t3);
440 return ssov32(env, result);
443 target_ulong helper_msub32_suov(CPUTriCoreState *env, target_ulong r1,
444 target_ulong r2, target_ulong r3)
446 int64_t t1 = extract64(r1, 0, 32);
447 int64_t t2 = extract64(r2, 0, 32);
448 int64_t t3 = extract64(r3, 0, 32);
449 int64_t result;
451 result = t2 - (t1 * t3);
452 return suov32_neg(env, result);
455 uint64_t helper_msub64_ssov(CPUTriCoreState *env, target_ulong r1,
456 uint64_t r2, target_ulong r3)
458 uint64_t ret, ovf;
459 int64_t t1 = sextract64(r1, 0, 32);
460 int64_t t3 = sextract64(r3, 0, 32);
461 int64_t mul;
463 mul = t1 * t3;
464 ret = r2 - mul;
465 ovf = (ret ^ r2) & (mul ^ r2);
467 t1 = ret >> 32;
468 env->PSW_USB_AV = t1 ^ t1 * 2u;
469 env->PSW_USB_SAV |= env->PSW_USB_AV;
471 if ((int64_t)ovf < 0) {
472 env->PSW_USB_V = (1 << 31);
473 env->PSW_USB_SV = (1 << 31);
474 /* ext_ret > MAX_INT */
475 if (mul < 0) {
476 ret = INT64_MAX;
477 /* ext_ret < MIN_INT */
478 } else {
479 ret = INT64_MIN;
481 } else {
482 env->PSW_USB_V = 0;
484 return ret;
487 uint64_t helper_msub64_suov(CPUTriCoreState *env, target_ulong r1,
488 uint64_t r2, target_ulong r3)
490 uint64_t ret, mul;
491 uint64_t t1 = extract64(r1, 0, 32);
492 uint64_t t3 = extract64(r3, 0, 32);
494 mul = t1 * t3;
495 ret = r2 - mul;
497 t1 = ret >> 32;
498 env->PSW_USB_AV = t1 ^ t1 * 2u;
499 env->PSW_USB_SAV |= env->PSW_USB_AV;
501 if (ret > r2) {
502 env->PSW_USB_V = (1 << 31);
503 env->PSW_USB_SV = (1 << 31);
504 /* saturate */
505 ret = 0;
506 } else {
507 env->PSW_USB_V = 0;
509 return ret;
512 uint32_t helper_abs_b(CPUTriCoreState *env, target_ulong arg)
514 int32_t b, i;
515 int32_t ovf = 0;
516 int32_t avf = 0;
517 int32_t ret = 0;
519 for (i = 0; i < 4; i++) {
520 b = sextract32(arg, i * 8, 8);
521 b = (b >= 0) ? b : (0 - b);
522 ovf |= (b > 0x7F) || (b < -0x80);
523 avf |= b ^ b * 2u;
524 ret |= (b & 0xff) << (i * 8);
527 env->PSW_USB_V = ovf << 31;
528 env->PSW_USB_SV |= env->PSW_USB_V;
529 env->PSW_USB_AV = avf << 24;
530 env->PSW_USB_SAV |= env->PSW_USB_AV;
532 return ret;
535 uint32_t helper_abs_h(CPUTriCoreState *env, target_ulong arg)
537 int32_t h, i;
538 int32_t ovf = 0;
539 int32_t avf = 0;
540 int32_t ret = 0;
542 for (i = 0; i < 2; i++) {
543 h = sextract32(arg, i * 16, 16);
544 h = (h >= 0) ? h : (0 - h);
545 ovf |= (h > 0x7FFF) || (h < -0x8000);
546 avf |= h ^ h * 2u;
547 ret |= (h & 0xffff) << (i * 16);
550 env->PSW_USB_V = ovf << 31;
551 env->PSW_USB_SV |= env->PSW_USB_V;
552 env->PSW_USB_AV = avf << 16;
553 env->PSW_USB_SAV |= env->PSW_USB_AV;
555 return ret;
558 uint32_t helper_absdif_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
560 int32_t b, i;
561 int32_t extr_r2;
562 int32_t ovf = 0;
563 int32_t avf = 0;
564 int32_t ret = 0;
566 for (i = 0; i < 4; i++) {
567 extr_r2 = sextract32(r2, i * 8, 8);
568 b = sextract32(r1, i * 8, 8);
569 b = (b > extr_r2) ? (b - extr_r2) : (extr_r2 - b);
570 ovf |= (b > 0x7F) || (b < -0x80);
571 avf |= b ^ b * 2u;
572 ret |= (b & 0xff) << (i * 8);
575 env->PSW_USB_V = ovf << 31;
576 env->PSW_USB_SV |= env->PSW_USB_V;
577 env->PSW_USB_AV = avf << 24;
578 env->PSW_USB_SAV |= env->PSW_USB_AV;
579 return ret;
582 uint32_t helper_absdif_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
584 int32_t h, i;
585 int32_t extr_r2;
586 int32_t ovf = 0;
587 int32_t avf = 0;
588 int32_t ret = 0;
590 for (i = 0; i < 2; i++) {
591 extr_r2 = sextract32(r2, i * 16, 16);
592 h = sextract32(r1, i * 16, 16);
593 h = (h > extr_r2) ? (h - extr_r2) : (extr_r2 - h);
594 ovf |= (h > 0x7FFF) || (h < -0x8000);
595 avf |= h ^ h * 2u;
596 ret |= (h & 0xffff) << (i * 16);
599 env->PSW_USB_V = ovf << 31;
600 env->PSW_USB_SV |= env->PSW_USB_V;
601 env->PSW_USB_AV = avf << 16;
602 env->PSW_USB_SAV |= env->PSW_USB_AV;
604 return ret;
607 uint32_t helper_add_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
609 int32_t b, i;
610 int32_t extr_r1, extr_r2;
611 int32_t ovf = 0;
612 int32_t avf = 0;
613 uint32_t ret = 0;
615 for (i = 0; i < 4; i++) {
616 extr_r1 = sextract32(r1, i * 8, 8);
617 extr_r2 = sextract32(r2, i * 8, 8);
619 b = extr_r1 + extr_r2;
620 ovf |= ((b > 0x7f) || (b < -0x80));
621 avf |= b ^ b * 2u;
622 ret |= ((b & 0xff) << (i*8));
625 env->PSW_USB_V = (ovf << 31);
626 env->PSW_USB_SV |= env->PSW_USB_V;
627 env->PSW_USB_AV = avf << 24;
628 env->PSW_USB_SAV |= env->PSW_USB_AV;
630 return ret;
633 uint32_t helper_add_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
635 int32_t h, i;
636 int32_t extr_r1, extr_r2;
637 int32_t ovf = 0;
638 int32_t avf = 0;
639 int32_t ret = 0;
641 for (i = 0; i < 2; i++) {
642 extr_r1 = sextract32(r1, i * 16, 16);
643 extr_r2 = sextract32(r2, i * 16, 16);
644 h = extr_r1 + extr_r2;
645 ovf |= ((h > 0x7fff) || (h < -0x8000));
646 avf |= h ^ h * 2u;
647 ret |= (h & 0xffff) << (i * 16);
650 env->PSW_USB_V = (ovf << 31);
651 env->PSW_USB_SV |= env->PSW_USB_V;
652 env->PSW_USB_AV = (avf << 16);
653 env->PSW_USB_SAV |= env->PSW_USB_AV;
655 return ret;
658 uint32_t helper_sub_b(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
660 int32_t b, i;
661 int32_t extr_r1, extr_r2;
662 int32_t ovf = 0;
663 int32_t avf = 0;
664 uint32_t ret = 0;
666 for (i = 0; i < 4; i++) {
667 extr_r1 = sextract32(r1, i * 8, 8);
668 extr_r2 = sextract32(r2, i * 8, 8);
670 b = extr_r1 - extr_r2;
671 ovf |= ((b > 0x7f) || (b < -0x80));
672 avf |= b ^ b * 2u;
673 ret |= ((b & 0xff) << (i*8));
676 env->PSW_USB_V = (ovf << 31);
677 env->PSW_USB_SV |= env->PSW_USB_V;
678 env->PSW_USB_AV = avf << 24;
679 env->PSW_USB_SAV |= env->PSW_USB_AV;
681 return ret;
684 uint32_t helper_sub_h(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
686 int32_t h, i;
687 int32_t extr_r1, extr_r2;
688 int32_t ovf = 0;
689 int32_t avf = 0;
690 int32_t ret = 0;
692 for (i = 0; i < 2; i++) {
693 extr_r1 = sextract32(r1, i * 16, 16);
694 extr_r2 = sextract32(r2, i * 16, 16);
695 h = extr_r1 - extr_r2;
696 ovf |= ((h > 0x7fff) || (h < -0x8000));
697 avf |= h ^ h * 2u;
698 ret |= (h & 0xffff) << (i * 16);
701 env->PSW_USB_V = (ovf << 31);
702 env->PSW_USB_SV |= env->PSW_USB_V;
703 env->PSW_USB_AV = avf << 16;
704 env->PSW_USB_SAV |= env->PSW_USB_AV;
706 return ret;
709 uint32_t helper_eq_b(target_ulong r1, target_ulong r2)
711 int32_t ret;
712 int32_t i, msk;
714 ret = 0;
715 msk = 0xff;
716 for (i = 0; i < 4; i++) {
717 if ((r1 & msk) == (r2 & msk)) {
718 ret |= msk;
720 msk = msk << 8;
723 return ret;
726 uint32_t helper_eq_h(target_ulong r1, target_ulong r2)
728 int32_t ret = 0;
730 if ((r1 & 0xffff) == (r2 & 0xffff)) {
731 ret = 0xffff;
734 if ((r1 & 0xffff0000) == (r2 & 0xffff0000)) {
735 ret |= 0xffff0000;
738 return ret;
741 uint32_t helper_eqany_b(target_ulong r1, target_ulong r2)
743 int32_t i;
744 uint32_t ret = 0;
746 for (i = 0; i < 4; i++) {
747 ret |= (sextract32(r1, i * 8, 8) == sextract32(r2, i * 8, 8));
750 return ret;
753 uint32_t helper_eqany_h(target_ulong r1, target_ulong r2)
755 uint32_t ret;
757 ret = (sextract32(r1, 0, 16) == sextract32(r2, 0, 16));
758 ret |= (sextract32(r1, 16, 16) == sextract32(r2, 16, 16));
760 return ret;
763 uint32_t helper_lt_b(target_ulong r1, target_ulong r2)
765 int32_t i;
766 uint32_t ret = 0;
768 for (i = 0; i < 4; i++) {
769 if (sextract32(r1, i * 8, 8) < sextract32(r2, i * 8, 8)) {
770 ret |= (0xff << (i * 8));
774 return ret;
777 uint32_t helper_lt_bu(target_ulong r1, target_ulong r2)
779 int32_t i;
780 uint32_t ret = 0;
782 for (i = 0; i < 4; i++) {
783 if (extract32(r1, i * 8, 8) < extract32(r2, i * 8, 8)) {
784 ret |= (0xff << (i * 8));
788 return ret;
791 uint32_t helper_lt_h(target_ulong r1, target_ulong r2)
793 uint32_t ret = 0;
795 if (sextract32(r1, 0, 16) < sextract32(r2, 0, 16)) {
796 ret |= 0xffff;
799 if (sextract32(r1, 16, 16) < sextract32(r2, 16, 16)) {
800 ret |= 0xffff0000;
803 return ret;
806 uint32_t helper_lt_hu(target_ulong r1, target_ulong r2)
808 uint32_t ret = 0;
810 if (extract32(r1, 0, 16) < extract32(r2, 0, 16)) {
811 ret |= 0xffff;
814 if (extract32(r1, 16, 16) < extract32(r2, 16, 16)) {
815 ret |= 0xffff0000;
818 return ret;
821 #define EXTREMA_H_B(name, op) \
822 uint32_t helper_##name ##_b(target_ulong r1, target_ulong r2) \
824 int32_t i, extr_r1, extr_r2; \
825 uint32_t ret = 0; \
827 for (i = 0; i < 4; i++) { \
828 extr_r1 = sextract32(r1, i * 8, 8); \
829 extr_r2 = sextract32(r2, i * 8, 8); \
830 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
831 ret |= (extr_r1 & 0xff) << (i * 8); \
833 return ret; \
836 uint32_t helper_##name ##_bu(target_ulong r1, target_ulong r2)\
838 int32_t i; \
839 uint32_t extr_r1, extr_r2; \
840 uint32_t ret = 0; \
842 for (i = 0; i < 4; i++) { \
843 extr_r1 = extract32(r1, i * 8, 8); \
844 extr_r2 = extract32(r2, i * 8, 8); \
845 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
846 ret |= (extr_r1 & 0xff) << (i * 8); \
848 return ret; \
851 uint32_t helper_##name ##_h(target_ulong r1, target_ulong r2) \
853 int32_t extr_r1, extr_r2; \
854 uint32_t ret = 0; \
856 extr_r1 = sextract32(r1, 0, 16); \
857 extr_r2 = sextract32(r2, 0, 16); \
858 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
859 ret = ret & 0xffff; \
861 extr_r1 = sextract32(r1, 16, 16); \
862 extr_r2 = sextract32(r2, 16, 16); \
863 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
864 ret |= extr_r1 << 16; \
866 return ret; \
869 uint32_t helper_##name ##_hu(target_ulong r1, target_ulong r2)\
871 uint32_t extr_r1, extr_r2; \
872 uint32_t ret = 0; \
874 extr_r1 = extract32(r1, 0, 16); \
875 extr_r2 = extract32(r2, 0, 16); \
876 ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
877 ret = ret & 0xffff; \
879 extr_r1 = extract32(r1, 16, 16); \
880 extr_r2 = extract32(r2, 16, 16); \
881 extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \
882 ret |= extr_r1 << (16); \
884 return ret; \
887 uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \
889 int64_t r2l, r2h, r1hl; \
890 uint64_t ret = 0; \
892 ret = ((r1 + 2) & 0xffff); \
893 r2l = sextract64(r2, 0, 16); \
894 r2h = sextract64(r2, 16, 16); \
895 r1hl = sextract64(r1, 32, 16); \
897 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
898 ret |= (r2l & 0xffff) << 32; \
899 ret |= extract64(r1, 0, 16) << 16; \
900 } else if ((r2h op r2l) && (r2h op r1hl)) { \
901 ret |= extract64(r2, 16, 16) << 32; \
902 ret |= extract64(r1 + 1, 0, 16) << 16; \
903 } else { \
904 ret |= r1 & 0xffffffff0000ull; \
906 return ret; \
909 uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \
911 int64_t r2l, r2h, r1hl; \
912 uint64_t ret = 0; \
914 ret = ((r1 + 2) & 0xffff); \
915 r2l = extract64(r2, 0, 16); \
916 r2h = extract64(r2, 16, 16); \
917 r1hl = extract64(r1, 32, 16); \
919 if ((r2l op ## = r2h) && (r2l op r1hl)) { \
920 ret |= (r2l & 0xffff) << 32; \
921 ret |= extract64(r1, 0, 16) << 16; \
922 } else if ((r2h op r2l) && (r2h op r1hl)) { \
923 ret |= extract64(r2, 16, 16) << 32; \
924 ret |= extract64(r1 + 1, 0, 16) << 16; \
925 } else { \
926 ret |= r1 & 0xffffffff0000ull; \
928 return ret; \
931 EXTREMA_H_B(max, >)
932 EXTREMA_H_B(min, <)
934 #undef EXTREMA_H_B
936 uint32_t helper_clo(target_ulong r1)
938 return clo32(r1);
941 uint32_t helper_clo_h(target_ulong r1)
943 uint32_t ret_hw0 = extract32(r1, 0, 16);
944 uint32_t ret_hw1 = extract32(r1, 16, 16);
946 ret_hw0 = clo32(ret_hw0 << 16);
947 ret_hw1 = clo32(ret_hw1 << 16);
949 if (ret_hw0 > 16) {
950 ret_hw0 = 16;
952 if (ret_hw1 > 16) {
953 ret_hw1 = 16;
956 return ret_hw0 | (ret_hw1 << 16);
959 uint32_t helper_clz(target_ulong r1)
961 return clz32(r1);
964 uint32_t helper_clz_h(target_ulong r1)
966 uint32_t ret_hw0 = extract32(r1, 0, 16);
967 uint32_t ret_hw1 = extract32(r1, 16, 16);
969 ret_hw0 = clz32(ret_hw0 << 16);
970 ret_hw1 = clz32(ret_hw1 << 16);
972 if (ret_hw0 > 16) {
973 ret_hw0 = 16;
975 if (ret_hw1 > 16) {
976 ret_hw1 = 16;
979 return ret_hw0 | (ret_hw1 << 16);
982 uint32_t helper_cls(target_ulong r1)
984 return clrsb32(r1);
987 uint32_t helper_cls_h(target_ulong r1)
989 uint32_t ret_hw0 = extract32(r1, 0, 16);
990 uint32_t ret_hw1 = extract32(r1, 16, 16);
992 ret_hw0 = clrsb32(ret_hw0 << 16);
993 ret_hw1 = clrsb32(ret_hw1 << 16);
995 if (ret_hw0 > 15) {
996 ret_hw0 = 15;
998 if (ret_hw1 > 15) {
999 ret_hw1 = 15;
1002 return ret_hw0 | (ret_hw1 << 16);
1005 uint32_t helper_sh(target_ulong r1, target_ulong r2)
1007 int32_t shift_count = sextract32(r2, 0, 6);
1009 if (shift_count == -32) {
1010 return 0;
1011 } else if (shift_count < 0) {
1012 return r1 >> -shift_count;
1013 } else {
1014 return r1 << shift_count;
1018 uint32_t helper_sh_h(target_ulong r1, target_ulong r2)
1020 int32_t ret_hw0, ret_hw1;
1021 int32_t shift_count;
1023 shift_count = sextract32(r2, 0, 5);
1025 if (shift_count == -16) {
1026 return 0;
1027 } else if (shift_count < 0) {
1028 ret_hw0 = extract32(r1, 0, 16) >> -shift_count;
1029 ret_hw1 = extract32(r1, 16, 16) >> -shift_count;
1030 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1031 } else {
1032 ret_hw0 = extract32(r1, 0, 16) << shift_count;
1033 ret_hw1 = extract32(r1, 16, 16) << shift_count;
1034 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1038 uint32_t helper_sha(CPUTriCoreState *env, target_ulong r1, target_ulong r2)
1040 int32_t shift_count;
1041 int64_t result, t1;
1042 uint32_t ret;
1044 shift_count = sextract32(r2, 0, 6);
1045 t1 = sextract32(r1, 0, 32);
1047 if (shift_count == 0) {
1048 env->PSW_USB_C = env->PSW_USB_V = 0;
1049 ret = r1;
1050 } else if (shift_count == -32) {
1051 env->PSW_USB_C = r1;
1052 env->PSW_USB_V = 0;
1053 ret = t1 >> 31;
1054 } else if (shift_count > 0) {
1055 result = t1 << shift_count;
1056 /* calc carry */
1057 env->PSW_USB_C = ((result & 0xffffffff00000000ULL) != 0);
1058 /* calc v */
1059 env->PSW_USB_V = (((result > 0x7fffffffLL) ||
1060 (result < -0x80000000LL)) << 31);
1061 /* calc sv */
1062 env->PSW_USB_SV |= env->PSW_USB_V;
1063 ret = (uint32_t)result;
1064 } else {
1065 env->PSW_USB_V = 0;
1066 env->PSW_USB_C = (r1 & ((1 << -shift_count) - 1));
1067 ret = t1 >> -shift_count;
1070 env->PSW_USB_AV = ret ^ ret * 2u;
1071 env->PSW_USB_SAV |= env->PSW_USB_AV;
1073 return ret;
1076 uint32_t helper_sha_h(target_ulong r1, target_ulong r2)
1078 int32_t shift_count;
1079 int32_t ret_hw0, ret_hw1;
1081 shift_count = sextract32(r2, 0, 5);
1083 if (shift_count == 0) {
1084 return r1;
1085 } else if (shift_count < 0) {
1086 ret_hw0 = sextract32(r1, 0, 16) >> -shift_count;
1087 ret_hw1 = sextract32(r1, 16, 16) >> -shift_count;
1088 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1089 } else {
1090 ret_hw0 = sextract32(r1, 0, 16) << shift_count;
1091 ret_hw1 = sextract32(r1, 16, 16) << shift_count;
1092 return (ret_hw0 & 0xffff) | (ret_hw1 << 16);
1096 uint32_t helper_bmerge(target_ulong r1, target_ulong r2)
1098 uint32_t i, ret;
1100 ret = 0;
1101 for (i = 0; i < 16; i++) {
1102 ret |= (r1 & 1) << (2 * i + 1);
1103 ret |= (r2 & 1) << (2 * i);
1104 r1 = r1 >> 1;
1105 r2 = r2 >> 1;
1107 return ret;
1110 uint64_t helper_bsplit(uint32_t r1)
1112 int32_t i;
1113 uint64_t ret;
1115 ret = 0;
1116 for (i = 0; i < 32; i = i + 2) {
1117 /* even */
1118 ret |= (r1 & 1) << (i/2);
1119 r1 = r1 >> 1;
1120 /* odd */
1121 ret |= (uint64_t)(r1 & 1) << (i/2 + 32);
1122 r1 = r1 >> 1;
1124 return ret;
1127 uint32_t helper_parity(target_ulong r1)
1129 uint32_t ret;
1130 uint32_t nOnes, i;
1132 ret = 0;
1133 nOnes = 0;
1134 for (i = 0; i < 8; i++) {
1135 ret ^= (r1 & 1);
1136 r1 = r1 >> 1;
1138 /* second byte */
1139 nOnes = 0;
1140 for (i = 0; i < 8; i++) {
1141 nOnes ^= (r1 & 1);
1142 r1 = r1 >> 1;
1144 ret |= nOnes << 8;
1145 /* third byte */
1146 nOnes = 0;
1147 for (i = 0; i < 8; i++) {
1148 nOnes ^= (r1 & 1);
1149 r1 = r1 >> 1;
1151 ret |= nOnes << 16;
1152 /* fourth byte */
1153 nOnes = 0;
1154 for (i = 0; i < 8; i++) {
1155 nOnes ^= (r1 & 1);
1156 r1 = r1 >> 1;
1158 ret |= nOnes << 24;
1160 return ret;
1163 uint32_t helper_pack(uint32_t carry, uint32_t r1_low, uint32_t r1_high,
1164 target_ulong r2)
1166 uint32_t ret;
1167 int32_t fp_exp, fp_frac, temp_exp, fp_exp_frac;
1168 int32_t int_exp = r1_high;
1169 int32_t int_mant = r1_low;
1170 uint32_t flag_rnd = (int_mant & (1 << 7)) && (
1171 (int_mant & (1 << 8)) ||
1172 (int_mant & 0x7f) ||
1173 (carry != 0));
1174 if (((int_mant & (1<<31)) == 0) && (int_exp == 255)) {
1175 fp_exp = 255;
1176 fp_frac = extract32(int_mant, 8, 23);
1177 } else if ((int_mant & (1<<31)) && (int_exp >= 127)) {
1178 fp_exp = 255;
1179 fp_frac = 0;
1180 } else if ((int_mant & (1<<31)) && (int_exp <= -128)) {
1181 fp_exp = 0;
1182 fp_frac = 0;
1183 } else if (int_mant == 0) {
1184 fp_exp = 0;
1185 fp_frac = 0;
1186 } else {
1187 if (((int_mant & (1 << 31)) == 0)) {
1188 temp_exp = 0;
1189 } else {
1190 temp_exp = int_exp + 128;
1192 fp_exp_frac = (((temp_exp & 0xff) << 23) |
1193 extract32(int_mant, 8, 23))
1194 + flag_rnd;
1195 fp_exp = extract32(fp_exp_frac, 23, 8);
1196 fp_frac = extract32(fp_exp_frac, 0, 23);
1198 ret = r2 & (1 << 31);
1199 ret = ret + (fp_exp << 23);
1200 ret = ret + (fp_frac & 0x7fffff);
1202 return ret;
1205 uint64_t helper_unpack(target_ulong arg1)
1207 int32_t fp_exp = extract32(arg1, 23, 8);
1208 int32_t fp_frac = extract32(arg1, 0, 23);
1209 uint64_t ret;
1210 int32_t int_exp, int_mant;
1212 if (fp_exp == 255) {
1213 int_exp = 255;
1214 int_mant = (fp_frac << 7);
1215 } else if ((fp_exp == 0) && (fp_frac == 0)) {
1216 int_exp = -127;
1217 int_mant = 0;
1218 } else if ((fp_exp == 0) && (fp_frac != 0)) {
1219 int_exp = -126;
1220 int_mant = (fp_frac << 7);
1221 } else {
1222 int_exp = fp_exp - 127;
1223 int_mant = (fp_frac << 7);
1224 int_mant |= (1 << 30);
1226 ret = int_exp;
1227 ret = ret << 32;
1228 ret |= int_mant;
1230 return ret;
1233 uint64_t helper_dvinit_b_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1235 uint64_t ret;
1236 int32_t abs_sig_dividend, abs_base_dividend, abs_divisor;
1237 int32_t quotient_sign;
1239 ret = sextract32(r1, 0, 32);
1240 ret = ret << 24;
1241 quotient_sign = 0;
1242 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1243 ret |= 0xffffff;
1244 quotient_sign = 1;
1247 abs_sig_dividend = abs(r1) >> 7;
1248 abs_base_dividend = abs(r1) & 0x7f;
1249 abs_divisor = abs(r1);
1250 /* calc overflow */
1251 env->PSW_USB_V = 0;
1252 if ((quotient_sign) && (abs_divisor)) {
1253 env->PSW_USB_V = (((abs_sig_dividend == abs_divisor) &&
1254 (abs_base_dividend >= abs_divisor)) ||
1255 (abs_sig_dividend > abs_divisor));
1256 } else {
1257 env->PSW_USB_V = (abs_sig_dividend >= abs_divisor);
1259 env->PSW_USB_V = env->PSW_USB_V << 31;
1260 env->PSW_USB_SV |= env->PSW_USB_V;
1261 env->PSW_USB_AV = 0;
1263 return ret;
1266 uint64_t helper_dvinit_b_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1268 uint64_t ret = sextract32(r1, 0, 32);
1270 ret = ret << 24;
1271 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1272 ret |= 0xffffff;
1274 /* calc overflow */
1275 env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffffff80)));
1276 env->PSW_USB_V = env->PSW_USB_V << 31;
1277 env->PSW_USB_SV |= env->PSW_USB_V;
1278 env->PSW_USB_AV = 0;
1280 return ret;
1283 uint64_t helper_dvinit_h_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1285 uint64_t ret;
1286 int32_t abs_sig_dividend, abs_base_dividend, abs_divisor;
1287 int32_t quotient_sign;
1289 ret = sextract32(r1, 0, 32);
1290 ret = ret << 16;
1291 quotient_sign = 0;
1292 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1293 ret |= 0xffff;
1294 quotient_sign = 1;
1297 abs_sig_dividend = abs(r1) >> 7;
1298 abs_base_dividend = abs(r1) & 0x7f;
1299 abs_divisor = abs(r1);
1300 /* calc overflow */
1301 env->PSW_USB_V = 0;
1302 if ((quotient_sign) && (abs_divisor)) {
1303 env->PSW_USB_V = (((abs_sig_dividend == abs_divisor) &&
1304 (abs_base_dividend >= abs_divisor)) ||
1305 (abs_sig_dividend > abs_divisor));
1306 } else {
1307 env->PSW_USB_V = (abs_sig_dividend >= abs_divisor);
1309 env->PSW_USB_V = env->PSW_USB_V << 31;
1310 env->PSW_USB_SV |= env->PSW_USB_V;
1311 env->PSW_USB_AV = 0;
1313 return ret;
1316 uint64_t helper_dvinit_h_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2)
1318 uint64_t ret = sextract32(r1, 0, 32);
1320 ret = ret << 16;
1321 if (!((r1 & 0x80000000) == (r2 & 0x80000000))) {
1322 ret |= 0xffff;
1324 /* calc overflow */
1325 env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffff8000)));
1326 env->PSW_USB_V = env->PSW_USB_V << 31;
1327 env->PSW_USB_SV |= env->PSW_USB_V;
1328 env->PSW_USB_AV = 0;
1330 return ret;
1333 uint64_t helper_dvadj(uint64_t r1, uint32_t r2)
1335 int32_t x_sign = (r1 >> 63);
1336 int32_t q_sign = x_sign ^ (r2 >> 31);
1337 int32_t eq_pos = x_sign & ((r1 >> 32) == r2);
1338 int32_t eq_neg = x_sign & ((r1 >> 32) == -r2);
1339 uint32_t quotient;
1340 uint64_t ret, remainder;
1342 if ((q_sign & ~eq_neg) | eq_pos) {
1343 quotient = (r1 + 1) & 0xffffffff;
1344 } else {
1345 quotient = r1 & 0xffffffff;
1348 if (eq_pos | eq_neg) {
1349 remainder = 0;
1350 } else {
1351 remainder = (r1 & 0xffffffff00000000ull);
1353 ret = remainder|quotient;
1354 return ret;
1357 uint64_t helper_dvstep(uint64_t r1, uint32_t r2)
1359 int32_t dividend_sign = extract64(r1, 63, 1);
1360 int32_t divisor_sign = extract32(r2, 31, 1);
1361 int32_t quotient_sign = (dividend_sign != divisor_sign);
1362 int32_t addend, dividend_quotient, remainder;
1363 int32_t i, temp;
1365 if (quotient_sign) {
1366 addend = r2;
1367 } else {
1368 addend = -r2;
1370 dividend_quotient = (int32_t)r1;
1371 remainder = (int32_t)(r1 >> 32);
1373 for (i = 0; i < 8; i++) {
1374 remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1);
1375 dividend_quotient <<= 1;
1376 temp = remainder + addend;
1377 if ((temp < 0) == dividend_sign) {
1378 remainder = temp;
1380 if (((temp < 0) == dividend_sign)) {
1381 dividend_quotient = dividend_quotient | !quotient_sign;
1382 } else {
1383 dividend_quotient = dividend_quotient | quotient_sign;
1386 return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient;
1389 uint64_t helper_dvstep_u(uint64_t r1, uint32_t r2)
1391 int32_t dividend_quotient = extract64(r1, 0, 32);
1392 int64_t remainder = extract64(r1, 32, 32);
1393 int32_t i;
1394 int64_t temp;
1395 for (i = 0; i < 8; i++) {
1396 remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1);
1397 dividend_quotient <<= 1;
1398 temp = (remainder & 0xffffffff) - r2;
1399 if (temp >= 0) {
1400 remainder = temp;
1402 dividend_quotient = dividend_quotient | !(temp < 0);
1404 return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient;
1407 uint64_t helper_mul_h(uint32_t arg00, uint32_t arg01,
1408 uint32_t arg10, uint32_t arg11, uint32_t n)
1410 uint64_t ret;
1411 uint32_t result0, result1;
1413 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
1414 ((arg10 & 0xffff) == 0x8000) && (n == 1);
1415 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
1416 ((arg11 & 0xffff) == 0x8000) && (n == 1);
1417 if (sc1) {
1418 result1 = 0x7fffffff;
1419 } else {
1420 result1 = (((uint32_t)(arg00 * arg10)) << n);
1422 if (sc0) {
1423 result0 = 0x7fffffff;
1424 } else {
1425 result0 = (((uint32_t)(arg01 * arg11)) << n);
1427 ret = (((uint64_t)result1 << 32)) | result0;
1428 return ret;
1431 uint64_t helper_mulm_h(uint32_t arg00, uint32_t arg01,
1432 uint32_t arg10, uint32_t arg11, uint32_t n)
1434 uint64_t ret;
1435 int64_t result0, result1;
1437 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
1438 ((arg10 & 0xffff) == 0x8000) && (n == 1);
1439 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
1440 ((arg11 & 0xffff) == 0x8000) && (n == 1);
1442 if (sc1) {
1443 result1 = 0x7fffffff;
1444 } else {
1445 result1 = (((int32_t)arg00 * (int32_t)arg10) << n);
1447 if (sc0) {
1448 result0 = 0x7fffffff;
1449 } else {
1450 result0 = (((int32_t)arg01 * (int32_t)arg11) << n);
1452 ret = (result1 + result0);
1453 ret = ret << 16;
1454 return ret;
1456 uint32_t helper_mulr_h(uint32_t arg00, uint32_t arg01,
1457 uint32_t arg10, uint32_t arg11, uint32_t n)
1459 uint32_t result0, result1;
1461 int32_t sc1 = ((arg00 & 0xffff) == 0x8000) &&
1462 ((arg10 & 0xffff) == 0x8000) && (n == 1);
1463 int32_t sc0 = ((arg01 & 0xffff) == 0x8000) &&
1464 ((arg11 & 0xffff) == 0x8000) && (n == 1);
1466 if (sc1) {
1467 result1 = 0x7fffffff;
1468 } else {
1469 result1 = ((arg00 * arg10) << n) + 0x8000;
1471 if (sc0) {
1472 result0 = 0x7fffffff;
1473 } else {
1474 result0 = ((arg01 * arg11) << n) + 0x8000;
1476 return (result1 & 0xffff0000) | (result0 >> 16);
1479 /* context save area (CSA) related helpers */
1481 static int cdc_increment(target_ulong *psw)
1483 if ((*psw & MASK_PSW_CDC) == 0x7f) {
1484 return 0;
1487 (*psw)++;
1488 /* check for overflow */
1489 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
1490 int mask = (1u << (7 - lo)) - 1;
1491 int count = *psw & mask;
1492 if (count == 0) {
1493 (*psw)--;
1494 return 1;
1496 return 0;
1499 static int cdc_decrement(target_ulong *psw)
1501 if ((*psw & MASK_PSW_CDC) == 0x7f) {
1502 return 0;
1504 /* check for underflow */
1505 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
1506 int mask = (1u << (7 - lo)) - 1;
1507 int count = *psw & mask;
1508 if (count == 0) {
1509 return 1;
1511 (*psw)--;
1512 return 0;
1515 static bool cdc_zero(target_ulong *psw)
1517 int cdc = *psw & MASK_PSW_CDC;
1518 /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC ==
1519 7'b1111111, otherwise returns FALSE. */
1520 if (cdc == 0x7f) {
1521 return true;
1523 /* find CDC.COUNT */
1524 int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7));
1525 int mask = (1u << (7 - lo)) - 1;
1526 int count = *psw & mask;
1527 return count == 0;
1530 static void save_context_upper(CPUTriCoreState *env, int ea)
1532 cpu_stl_data(env, ea, env->PCXI);
1533 cpu_stl_data(env, ea+4, env->PSW);
1534 cpu_stl_data(env, ea+8, env->gpr_a[10]);
1535 cpu_stl_data(env, ea+12, env->gpr_a[11]);
1536 cpu_stl_data(env, ea+16, env->gpr_d[8]);
1537 cpu_stl_data(env, ea+20, env->gpr_d[9]);
1538 cpu_stl_data(env, ea+24, env->gpr_d[10]);
1539 cpu_stl_data(env, ea+28, env->gpr_d[11]);
1540 cpu_stl_data(env, ea+32, env->gpr_a[12]);
1541 cpu_stl_data(env, ea+36, env->gpr_a[13]);
1542 cpu_stl_data(env, ea+40, env->gpr_a[14]);
1543 cpu_stl_data(env, ea+44, env->gpr_a[15]);
1544 cpu_stl_data(env, ea+48, env->gpr_d[12]);
1545 cpu_stl_data(env, ea+52, env->gpr_d[13]);
1546 cpu_stl_data(env, ea+56, env->gpr_d[14]);
1547 cpu_stl_data(env, ea+60, env->gpr_d[15]);
1550 static void save_context_lower(CPUTriCoreState *env, int ea)
1552 cpu_stl_data(env, ea, env->PCXI);
1553 cpu_stl_data(env, ea+4, env->gpr_a[11]);
1554 cpu_stl_data(env, ea+8, env->gpr_a[2]);
1555 cpu_stl_data(env, ea+12, env->gpr_a[3]);
1556 cpu_stl_data(env, ea+16, env->gpr_d[0]);
1557 cpu_stl_data(env, ea+20, env->gpr_d[1]);
1558 cpu_stl_data(env, ea+24, env->gpr_d[2]);
1559 cpu_stl_data(env, ea+28, env->gpr_d[3]);
1560 cpu_stl_data(env, ea+32, env->gpr_a[4]);
1561 cpu_stl_data(env, ea+36, env->gpr_a[5]);
1562 cpu_stl_data(env, ea+40, env->gpr_a[6]);
1563 cpu_stl_data(env, ea+44, env->gpr_a[7]);
1564 cpu_stl_data(env, ea+48, env->gpr_d[4]);
1565 cpu_stl_data(env, ea+52, env->gpr_d[5]);
1566 cpu_stl_data(env, ea+56, env->gpr_d[6]);
1567 cpu_stl_data(env, ea+60, env->gpr_d[7]);
1570 static void restore_context_upper(CPUTriCoreState *env, int ea,
1571 target_ulong *new_PCXI, target_ulong *new_PSW)
1573 *new_PCXI = cpu_ldl_data(env, ea);
1574 *new_PSW = cpu_ldl_data(env, ea+4);
1575 env->gpr_a[10] = cpu_ldl_data(env, ea+8);
1576 env->gpr_a[11] = cpu_ldl_data(env, ea+12);
1577 env->gpr_d[8] = cpu_ldl_data(env, ea+16);
1578 env->gpr_d[9] = cpu_ldl_data(env, ea+20);
1579 env->gpr_d[10] = cpu_ldl_data(env, ea+24);
1580 env->gpr_d[11] = cpu_ldl_data(env, ea+28);
1581 env->gpr_a[12] = cpu_ldl_data(env, ea+32);
1582 env->gpr_a[13] = cpu_ldl_data(env, ea+36);
1583 env->gpr_a[14] = cpu_ldl_data(env, ea+40);
1584 env->gpr_a[15] = cpu_ldl_data(env, ea+44);
1585 env->gpr_d[12] = cpu_ldl_data(env, ea+48);
1586 env->gpr_d[13] = cpu_ldl_data(env, ea+52);
1587 env->gpr_d[14] = cpu_ldl_data(env, ea+56);
1588 env->gpr_d[15] = cpu_ldl_data(env, ea+60);
1591 static void restore_context_lower(CPUTriCoreState *env, int ea,
1592 target_ulong *ra, target_ulong *pcxi)
1594 *pcxi = cpu_ldl_data(env, ea);
1595 *ra = cpu_ldl_data(env, ea+4);
1596 env->gpr_a[2] = cpu_ldl_data(env, ea+8);
1597 env->gpr_a[3] = cpu_ldl_data(env, ea+12);
1598 env->gpr_d[0] = cpu_ldl_data(env, ea+16);
1599 env->gpr_d[1] = cpu_ldl_data(env, ea+20);
1600 env->gpr_d[2] = cpu_ldl_data(env, ea+24);
1601 env->gpr_d[3] = cpu_ldl_data(env, ea+28);
1602 env->gpr_a[4] = cpu_ldl_data(env, ea+32);
1603 env->gpr_a[5] = cpu_ldl_data(env, ea+36);
1604 env->gpr_a[6] = cpu_ldl_data(env, ea+40);
1605 env->gpr_a[7] = cpu_ldl_data(env, ea+44);
1606 env->gpr_d[4] = cpu_ldl_data(env, ea+48);
1607 env->gpr_d[5] = cpu_ldl_data(env, ea+52);
1608 env->gpr_d[6] = cpu_ldl_data(env, ea+56);
1609 env->gpr_d[7] = cpu_ldl_data(env, ea+60);
1612 void helper_call(CPUTriCoreState *env, uint32_t next_pc)
1614 target_ulong tmp_FCX;
1615 target_ulong ea;
1616 target_ulong new_FCX;
1617 target_ulong psw;
1619 psw = psw_read(env);
1620 /* if (FCX == 0) trap(FCU); */
1621 if (env->FCX == 0) {
1622 /* FCU trap */
1624 /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */
1625 if (psw & MASK_PSW_CDE) {
1626 if (cdc_increment(&psw)) {
1627 /* CDO trap */
1630 /* PSW.CDE = 1;*/
1631 psw |= MASK_PSW_CDE;
1632 /* tmp_FCX = FCX; */
1633 tmp_FCX = env->FCX;
1634 /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */
1635 ea = ((env->FCX & MASK_FCX_FCXS) << 12) +
1636 ((env->FCX & MASK_FCX_FCXO) << 6);
1637 /* new_FCX = M(EA, word); */
1638 new_FCX = cpu_ldl_data(env, ea);
1639 /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11],
1640 A[12], A[13], A[14], A[15], D[12], D[13], D[14],
1641 D[15]}; */
1642 save_context_upper(env, ea);
1644 /* PCXI.PCPN = ICR.CCPN; */
1645 env->PCXI = (env->PCXI & 0xffffff) +
1646 ((env->ICR & MASK_ICR_CCPN) << 24);
1647 /* PCXI.PIE = ICR.IE; */
1648 env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) +
1649 ((env->ICR & MASK_ICR_IE) << 15));
1650 /* PCXI.UL = 1; */
1651 env->PCXI |= MASK_PCXI_UL;
1653 /* PCXI[19: 0] = FCX[19: 0]; */
1654 env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff);
1655 /* FCX[19: 0] = new_FCX[19: 0]; */
1656 env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff);
1657 /* A[11] = next_pc[31: 0]; */
1658 env->gpr_a[11] = next_pc;
1660 /* if (tmp_FCX == LCX) trap(FCD);*/
1661 if (tmp_FCX == env->LCX) {
1662 /* FCD trap */
1664 psw_write(env, psw);
1667 void helper_ret(CPUTriCoreState *env)
1669 target_ulong ea;
1670 target_ulong new_PCXI;
1671 target_ulong new_PSW, psw;
1673 psw = psw_read(env);
1674 /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/
1675 if (env->PSW & MASK_PSW_CDE) {
1676 if (cdc_decrement(&(env->PSW))) {
1677 /* CDU trap */
1680 /* if (PCXI[19: 0] == 0) then trap(CSU); */
1681 if ((env->PCXI & 0xfffff) == 0) {
1682 /* CSU trap */
1684 /* if (PCXI.UL == 0) then trap(CTYP); */
1685 if ((env->PCXI & MASK_PCXI_UL) == 0) {
1686 /* CTYP trap */
1688 /* PC = {A11 [31: 1], 1’b0}; */
1689 env->PC = env->gpr_a[11] & 0xfffffffe;
1691 /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */
1692 ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) +
1693 ((env->PCXI & MASK_PCXI_PCXO) << 6);
1694 /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
1695 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
1696 restore_context_upper(env, ea, &new_PCXI, &new_PSW);
1697 /* M(EA, word) = FCX; */
1698 cpu_stl_data(env, ea, env->FCX);
1699 /* FCX[19: 0] = PCXI[19: 0]; */
1700 env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff);
1701 /* PCXI = new_PCXI; */
1702 env->PCXI = new_PCXI;
1704 if (tricore_feature(env, TRICORE_FEATURE_13)) {
1705 /* PSW = new_PSW */
1706 psw_write(env, new_PSW);
1707 } else {
1708 /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */
1709 psw_write(env, (new_PSW & ~(0x3000000)) + (psw & (0x3000000)));
1713 void helper_bisr(CPUTriCoreState *env, uint32_t const9)
1715 target_ulong tmp_FCX;
1716 target_ulong ea;
1717 target_ulong new_FCX;
1719 if (env->FCX == 0) {
1720 /* FCU trap */
1723 tmp_FCX = env->FCX;
1724 ea = ((env->FCX & 0xf0000) << 12) + ((env->FCX & 0xffff) << 6);
1726 /* new_FCX = M(EA, word); */
1727 new_FCX = cpu_ldl_data(env, ea);
1728 /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4]
1729 , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */
1730 save_context_lower(env, ea);
1733 /* PCXI.PCPN = ICR.CCPN */
1734 env->PCXI = (env->PCXI & 0xffffff) +
1735 ((env->ICR & MASK_ICR_CCPN) << 24);
1736 /* PCXI.PIE = ICR.IE */
1737 env->PCXI = ((env->PCXI & ~MASK_PCXI_PIE) +
1738 ((env->ICR & MASK_ICR_IE) << 15));
1739 /* PCXI.UL = 0 */
1740 env->PCXI &= ~(MASK_PCXI_UL);
1741 /* PCXI[19: 0] = FCX[19: 0] */
1742 env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff);
1743 /* FXC[19: 0] = new_FCX[19: 0] */
1744 env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff);
1745 /* ICR.IE = 1 */
1746 env->ICR |= MASK_ICR_IE;
1748 env->ICR |= const9; /* ICR.CCPN = const9[7: 0];*/
1750 if (tmp_FCX == env->LCX) {
1751 /* FCD trap */
1755 void helper_rfe(CPUTriCoreState *env)
1757 target_ulong ea;
1758 target_ulong new_PCXI;
1759 target_ulong new_PSW;
1760 /* if (PCXI[19: 0] == 0) then trap(CSU); */
1761 if ((env->PCXI & 0xfffff) == 0) {
1762 /* raise csu trap */
1764 /* if (PCXI.UL == 0) then trap(CTYP); */
1765 if ((env->PCXI & MASK_PCXI_UL) == 0) {
1766 /* raise CTYP trap */
1768 /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */
1769 if (!cdc_zero(&(env->PSW)) && (env->PSW & MASK_PSW_CDE)) {
1770 /* raise MNG trap */
1772 /* ICR.IE = PCXI.PIE; */
1773 env->ICR = (env->ICR & ~MASK_ICR_IE) + ((env->PCXI & MASK_PCXI_PIE) >> 15);
1774 /* ICR.CCPN = PCXI.PCPN; */
1775 env->ICR = (env->ICR & ~MASK_ICR_CCPN) +
1776 ((env->PCXI & MASK_PCXI_PCPN) >> 24);
1777 /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/
1778 ea = ((env->PCXI & MASK_PCXI_PCXS) << 12) +
1779 ((env->PCXI & MASK_PCXI_PCXO) << 6);
1780 /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12],
1781 A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */
1782 restore_context_upper(env, ea, &new_PCXI, &new_PSW);
1783 /* M(EA, word) = FCX;*/
1784 cpu_stl_data(env, ea, env->FCX);
1785 /* FCX[19: 0] = PCXI[19: 0]; */
1786 env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff);
1787 /* PCXI = new_PCXI; */
1788 env->PCXI = new_PCXI;
1789 /* write psw */
1790 psw_write(env, new_PSW);
1793 void helper_ldlcx(CPUTriCoreState *env, uint32_t ea)
1795 uint32_t dummy;
1796 /* insn doesn't load PCXI and RA */
1797 restore_context_lower(env, ea, &dummy, &dummy);
1800 void helper_lducx(CPUTriCoreState *env, uint32_t ea)
1802 uint32_t dummy;
1803 /* insn doesn't load PCXI and PSW */
1804 restore_context_upper(env, ea, &dummy, &dummy);
1807 void helper_stlcx(CPUTriCoreState *env, uint32_t ea)
1809 save_context_lower(env, ea);
1812 void helper_stucx(CPUTriCoreState *env, uint32_t ea)
1814 save_context_upper(env, ea);
1817 void helper_psw_write(CPUTriCoreState *env, uint32_t arg)
1819 psw_write(env, arg);
1822 uint32_t helper_psw_read(CPUTriCoreState *env)
1824 return psw_read(env);
1828 static inline void QEMU_NORETURN do_raise_exception_err(CPUTriCoreState *env,
1829 uint32_t exception,
1830 int error_code,
1831 uintptr_t pc)
1833 CPUState *cs = CPU(tricore_env_get_cpu(env));
1834 cs->exception_index = exception;
1835 env->error_code = error_code;
1837 if (pc) {
1838 /* now we have a real cpu fault */
1839 cpu_restore_state(cs, pc);
1842 cpu_loop_exit(cs);
1845 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
1846 uintptr_t retaddr)
1848 int ret;
1849 ret = cpu_tricore_handle_mmu_fault(cs, addr, is_write, mmu_idx);
1850 if (ret) {
1851 TriCoreCPU *cpu = TRICORE_CPU(cs);
1852 CPUTriCoreState *env = &cpu->env;
1853 do_raise_exception_err(env, cs->exception_index,
1854 env->error_code, retaddr);