pc: fw_cfg: move ioport base constant to pc.h
[qemu.git] / target-tilegx / helper.c
blob616c5c7cfa98a6f1626a46803d57b0251e2f03e8
1 /*
2 * QEMU TILE-Gx helpers
4 * Copyright (c) 2015 Chen Gang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "qemu-common.h"
24 #include "exec/helper-proto.h"
25 #include <zlib.h> /* For crc32 */
26 #include "syscall_defs.h"
28 void helper_exception(CPUTLGState *env, uint32_t excp)
30 CPUState *cs = CPU(tilegx_env_get_cpu(env));
32 cs->exception_index = excp;
33 cpu_loop_exit(cs);
36 void helper_ext01_ics(CPUTLGState *env)
38 uint64_t val = env->spregs[TILEGX_SPR_EX_CONTEXT_0_1];
40 switch (val) {
41 case 0:
42 case 1:
43 env->spregs[TILEGX_SPR_CRITICAL_SEC] = val;
44 break;
45 default:
46 #if defined(CONFIG_USER_ONLY)
47 env->signo = TARGET_SIGILL;
48 env->sigcode = TARGET_ILL_ILLOPC;
49 helper_exception(env, TILEGX_EXCP_SIGNAL);
50 #else
51 helper_exception(env, TILEGX_EXCP_OPCODE_UNIMPLEMENTED);
52 #endif
53 break;
57 uint64_t helper_cntlz(uint64_t arg)
59 return clz64(arg);
62 uint64_t helper_cnttz(uint64_t arg)
64 return ctz64(arg);
67 uint64_t helper_pcnt(uint64_t arg)
69 return ctpop64(arg);
72 uint64_t helper_revbits(uint64_t arg)
74 return revbit64(arg);
78 * Functional Description
79 * uint64_t a = rf[SrcA];
80 * uint64_t b = rf[SrcB];
81 * uint64_t d = rf[Dest];
82 * uint64_t output = 0;
83 * unsigned int counter;
84 * for (counter = 0; counter < (WORD_SIZE / BYTE_SIZE); counter++)
85 * {
86 * int sel = getByte (b, counter) & 0xf;
87 * uint8_t byte = (sel < 8) ? getByte (d, sel) : getByte (a, (sel - 8));
88 * output = setByte (output, counter, byte);
89 * }
90 * rf[Dest] = output;
92 uint64_t helper_shufflebytes(uint64_t dest, uint64_t srca, uint64_t srcb)
94 uint64_t vdst = 0;
95 int count;
97 for (count = 0; count < 64; count += 8) {
98 uint64_t sel = srcb >> count;
99 uint64_t src = (sel & 8) ? srca : dest;
100 vdst |= extract64(src, (sel & 7) * 8, 8) << count;
103 return vdst;
106 uint64_t helper_crc32_8(uint64_t accum, uint64_t input)
108 uint8_t buf = input;
110 /* zlib crc32 converts the accumulator and output to one's complement. */
111 return crc32(accum ^ 0xffffffff, &buf, 1) ^ 0xffffffff;
114 uint64_t helper_crc32_32(uint64_t accum, uint64_t input)
116 uint8_t buf[4];
118 stl_le_p(buf, input);
120 /* zlib crc32 converts the accumulator and output to one's complement. */
121 return crc32(accum ^ 0xffffffff, buf, 4) ^ 0xffffffff;
124 uint64_t helper_cmula(uint64_t srcd, uint64_t srca, uint64_t srcb)
126 uint32_t reala = (int16_t)srca;
127 uint32_t imaga = (int16_t)(srca >> 16);
128 uint32_t realb = (int16_t)srcb;
129 uint32_t imagb = (int16_t)(srcb >> 16);
130 uint32_t reald = srcd;
131 uint32_t imagd = srcd >> 32;
132 uint32_t realr = reala * realb - imaga * imagb + reald;
133 uint32_t imagr = reala * imagb + imaga * realb + imagd;
135 return deposit64(realr, 32, 32, imagr);
138 uint64_t helper_cmulaf(uint64_t srcd, uint64_t srca, uint64_t srcb)
140 uint32_t reala = (int16_t)srca;
141 uint32_t imaga = (int16_t)(srca >> 16);
142 uint32_t realb = (int16_t)srcb;
143 uint32_t imagb = (int16_t)(srcb >> 16);
144 uint32_t reald = (int16_t)srcd;
145 uint32_t imagd = (int16_t)(srcd >> 16);
146 int32_t realr = reala * realb - imaga * imagb;
147 int32_t imagr = reala * imagb + imaga * realb;
149 return deposit32((realr >> 15) + reald, 16, 16, (imagr >> 15) + imagd);
152 uint64_t helper_cmul2(uint64_t srca, uint64_t srcb, int shift, int round)
154 uint32_t reala = (int16_t)srca;
155 uint32_t imaga = (int16_t)(srca >> 16);
156 uint32_t realb = (int16_t)srcb;
157 uint32_t imagb = (int16_t)(srcb >> 16);
158 int32_t realr = reala * realb - imaga * imagb + round;
159 int32_t imagr = reala * imagb + imaga * realb + round;
161 return deposit32(realr >> shift, 16, 16, imagr >> shift);