Statistics
| Branch: | Revision:

root / target-arm / op_helper.c @ 37f9ba46

History | View | Annotate | Download (12.3 kB)

1 b7bcbe95 bellard
/*
2 b7bcbe95 bellard
 *  ARM helper routines
3 5fafdf24 ths
 *
4 9ee6e8bb pbrook
 *  Copyright (c) 2005-2007 CodeSourcery, LLC
5 b7bcbe95 bellard
 *
6 b7bcbe95 bellard
 * This library is free software; you can redistribute it and/or
7 b7bcbe95 bellard
 * modify it under the terms of the GNU Lesser General Public
8 b7bcbe95 bellard
 * License as published by the Free Software Foundation; either
9 b7bcbe95 bellard
 * version 2 of the License, or (at your option) any later version.
10 b7bcbe95 bellard
 *
11 b7bcbe95 bellard
 * This library is distributed in the hope that it will be useful,
12 b7bcbe95 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 b7bcbe95 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b7bcbe95 bellard
 * Lesser General Public License for more details.
15 b7bcbe95 bellard
 *
16 b7bcbe95 bellard
 * You should have received a copy of the GNU Lesser General Public
17 b7bcbe95 bellard
 * License along with this library; if not, write to the Free Software
18 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19 b7bcbe95 bellard
 */
20 b7bcbe95 bellard
#include "exec.h"
21 1497c961 pbrook
#include "helpers.h"
22 b7bcbe95 bellard
23 ad69471c pbrook
#define SIGNBIT (uint32_t)0x80000000
24 ad69471c pbrook
#define SIGNBIT64 ((uint64_t)1 << 63)
25 ad69471c pbrook
26 b7bcbe95 bellard
void raise_exception(int tt)
27 b7bcbe95 bellard
{
28 b7bcbe95 bellard
    env->exception_index = tt;
29 b7bcbe95 bellard
    cpu_loop_exit();
30 b7bcbe95 bellard
}
31 b7bcbe95 bellard
32 b7bcbe95 bellard
/* thread support */
33 b7bcbe95 bellard
34 b1d8e52e blueswir1
static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
35 b7bcbe95 bellard
36 b7bcbe95 bellard
void cpu_lock(void)
37 b7bcbe95 bellard
{
38 b7bcbe95 bellard
    spin_lock(&global_cpu_lock);
39 b7bcbe95 bellard
}
40 b7bcbe95 bellard
41 b7bcbe95 bellard
void cpu_unlock(void)
42 b7bcbe95 bellard
{
43 b7bcbe95 bellard
    spin_unlock(&global_cpu_lock);
44 b7bcbe95 bellard
}
45 b7bcbe95 bellard
46 8f8e3aa4 pbrook
uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def,
47 8f8e3aa4 pbrook
                          uint32_t rn, uint32_t maxindex)
48 9ee6e8bb pbrook
{
49 9ee6e8bb pbrook
    uint32_t val;
50 9ee6e8bb pbrook
    uint32_t tmp;
51 9ee6e8bb pbrook
    int index;
52 9ee6e8bb pbrook
    int shift;
53 9ee6e8bb pbrook
    uint64_t *table;
54 9ee6e8bb pbrook
    table = (uint64_t *)&env->vfp.regs[rn];
55 9ee6e8bb pbrook
    val = 0;
56 9ee6e8bb pbrook
    for (shift = 0; shift < 32; shift += 8) {
57 8f8e3aa4 pbrook
        index = (ireg >> shift) & 0xff;
58 8f8e3aa4 pbrook
        if (index < maxindex) {
59 3018f259 pbrook
            tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
60 9ee6e8bb pbrook
            val |= tmp << shift;
61 9ee6e8bb pbrook
        } else {
62 8f8e3aa4 pbrook
            val |= def & (0xff << shift);
63 9ee6e8bb pbrook
        }
64 9ee6e8bb pbrook
    }
65 8f8e3aa4 pbrook
    return val;
66 9ee6e8bb pbrook
}
67 9ee6e8bb pbrook
68 b5ff1b31 bellard
#if !defined(CONFIG_USER_ONLY)
69 b5ff1b31 bellard
70 b5ff1b31 bellard
#define MMUSUFFIX _mmu
71 b5ff1b31 bellard
72 b5ff1b31 bellard
#define SHIFT 0
73 b5ff1b31 bellard
#include "softmmu_template.h"
74 b5ff1b31 bellard
75 b5ff1b31 bellard
#define SHIFT 1
76 b5ff1b31 bellard
#include "softmmu_template.h"
77 b5ff1b31 bellard
78 b5ff1b31 bellard
#define SHIFT 2
79 b5ff1b31 bellard
#include "softmmu_template.h"
80 b5ff1b31 bellard
81 b5ff1b31 bellard
#define SHIFT 3
82 b5ff1b31 bellard
#include "softmmu_template.h"
83 b5ff1b31 bellard
84 b5ff1b31 bellard
/* try to fill the TLB and return an exception if error. If retaddr is
85 b5ff1b31 bellard
   NULL, it means that the function was called in C code (i.e. not
86 b5ff1b31 bellard
   from generated code or from helper.c) */
87 b5ff1b31 bellard
/* XXX: fix it to restore all registers */
88 6ebbf390 j_mayer
void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
89 b5ff1b31 bellard
{
90 b5ff1b31 bellard
    TranslationBlock *tb;
91 b5ff1b31 bellard
    CPUState *saved_env;
92 44f8625d bellard
    unsigned long pc;
93 b5ff1b31 bellard
    int ret;
94 b5ff1b31 bellard
95 b5ff1b31 bellard
    /* XXX: hack to restore env in all cases, even if not called from
96 b5ff1b31 bellard
       generated code */
97 b5ff1b31 bellard
    saved_env = env;
98 b5ff1b31 bellard
    env = cpu_single_env;
99 6ebbf390 j_mayer
    ret = cpu_arm_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
100 551bd27f ths
    if (unlikely(ret)) {
101 b5ff1b31 bellard
        if (retaddr) {
102 b5ff1b31 bellard
            /* now we have a real cpu fault */
103 44f8625d bellard
            pc = (unsigned long)retaddr;
104 b5ff1b31 bellard
            tb = tb_find_pc(pc);
105 b5ff1b31 bellard
            if (tb) {
106 b5ff1b31 bellard
                /* the PC is inside the translated code. It means that we have
107 b5ff1b31 bellard
                   a virtual CPU fault */
108 b5ff1b31 bellard
                cpu_restore_state(tb, env, pc, NULL);
109 b5ff1b31 bellard
            }
110 b5ff1b31 bellard
        }
111 b5ff1b31 bellard
        raise_exception(env->exception_index);
112 b5ff1b31 bellard
    }
113 b5ff1b31 bellard
    env = saved_env;
114 b5ff1b31 bellard
}
115 b5ff1b31 bellard
#endif
116 1497c961 pbrook
117 ad69471c pbrook
/* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
118 ad69471c pbrook
   instructions into helper.c  */
119 1497c961 pbrook
uint32_t HELPER(add_setq)(uint32_t a, uint32_t b)
120 1497c961 pbrook
{
121 1497c961 pbrook
    uint32_t res = a + b;
122 1497c961 pbrook
    if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
123 1497c961 pbrook
        env->QF = 1;
124 1497c961 pbrook
    return res;
125 1497c961 pbrook
}
126 1497c961 pbrook
127 1497c961 pbrook
uint32_t HELPER(add_saturate)(uint32_t a, uint32_t b)
128 1497c961 pbrook
{
129 1497c961 pbrook
    uint32_t res = a + b;
130 1497c961 pbrook
    if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
131 1497c961 pbrook
        env->QF = 1;
132 1497c961 pbrook
        res = ~(((int32_t)a >> 31) ^ SIGNBIT);
133 1497c961 pbrook
    }
134 1497c961 pbrook
    return res;
135 1497c961 pbrook
}
136 1497c961 pbrook
137 1497c961 pbrook
uint32_t HELPER(sub_saturate)(uint32_t a, uint32_t b)
138 1497c961 pbrook
{
139 1497c961 pbrook
    uint32_t res = a - b;
140 1497c961 pbrook
    if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
141 1497c961 pbrook
        env->QF = 1;
142 1497c961 pbrook
        res = ~(((int32_t)a >> 31) ^ SIGNBIT);
143 1497c961 pbrook
    }
144 1497c961 pbrook
    return res;
145 1497c961 pbrook
}
146 1497c961 pbrook
147 1497c961 pbrook
uint32_t HELPER(double_saturate)(int32_t val)
148 1497c961 pbrook
{
149 1497c961 pbrook
    uint32_t res;
150 1497c961 pbrook
    if (val >= 0x40000000) {
151 1497c961 pbrook
        res = ~SIGNBIT;
152 1497c961 pbrook
        env->QF = 1;
153 1497c961 pbrook
    } else if (val <= (int32_t)0xc0000000) {
154 1497c961 pbrook
        res = SIGNBIT;
155 1497c961 pbrook
        env->QF = 1;
156 1497c961 pbrook
    } else {
157 1497c961 pbrook
        res = val << 1;
158 1497c961 pbrook
    }
159 1497c961 pbrook
    return res;
160 1497c961 pbrook
}
161 1497c961 pbrook
162 1497c961 pbrook
uint32_t HELPER(add_usaturate)(uint32_t a, uint32_t b)
163 1497c961 pbrook
{
164 1497c961 pbrook
    uint32_t res = a + b;
165 1497c961 pbrook
    if (res < a) {
166 1497c961 pbrook
        env->QF = 1;
167 1497c961 pbrook
        res = ~0;
168 1497c961 pbrook
    }
169 1497c961 pbrook
    return res;
170 1497c961 pbrook
}
171 1497c961 pbrook
172 1497c961 pbrook
uint32_t HELPER(sub_usaturate)(uint32_t a, uint32_t b)
173 1497c961 pbrook
{
174 1497c961 pbrook
    uint32_t res = a - b;
175 1497c961 pbrook
    if (res > a) {
176 1497c961 pbrook
        env->QF = 1;
177 1497c961 pbrook
        res = 0;
178 1497c961 pbrook
    }
179 1497c961 pbrook
    return res;
180 1497c961 pbrook
}
181 1497c961 pbrook
182 6ddbc6e4 pbrook
/* Signed saturation.  */
183 6ddbc6e4 pbrook
static inline uint32_t do_ssat(int32_t val, int shift)
184 6ddbc6e4 pbrook
{
185 6ddbc6e4 pbrook
    int32_t top;
186 6ddbc6e4 pbrook
    uint32_t mask;
187 6ddbc6e4 pbrook
188 6ddbc6e4 pbrook
    top = val >> shift;
189 6ddbc6e4 pbrook
    mask = (1u << shift) - 1;
190 6ddbc6e4 pbrook
    if (top > 0) {
191 6ddbc6e4 pbrook
        env->QF = 1;
192 6ddbc6e4 pbrook
        return mask;
193 6ddbc6e4 pbrook
    } else if (top < -1) {
194 6ddbc6e4 pbrook
        env->QF = 1;
195 6ddbc6e4 pbrook
        return ~mask;
196 6ddbc6e4 pbrook
    }
197 6ddbc6e4 pbrook
    return val;
198 6ddbc6e4 pbrook
}
199 6ddbc6e4 pbrook
200 6ddbc6e4 pbrook
/* Unsigned saturation.  */
201 6ddbc6e4 pbrook
static inline uint32_t do_usat(int32_t val, int shift)
202 6ddbc6e4 pbrook
{
203 6ddbc6e4 pbrook
    uint32_t max;
204 6ddbc6e4 pbrook
205 6ddbc6e4 pbrook
    max = (1u << shift) - 1;
206 6ddbc6e4 pbrook
    if (val < 0) {
207 6ddbc6e4 pbrook
        env->QF = 1;
208 6ddbc6e4 pbrook
        return 0;
209 6ddbc6e4 pbrook
    } else if (val > max) {
210 6ddbc6e4 pbrook
        env->QF = 1;
211 6ddbc6e4 pbrook
        return max;
212 6ddbc6e4 pbrook
    }
213 6ddbc6e4 pbrook
    return val;
214 6ddbc6e4 pbrook
}
215 6ddbc6e4 pbrook
216 6ddbc6e4 pbrook
/* Signed saturate.  */
217 6ddbc6e4 pbrook
uint32_t HELPER(ssat)(uint32_t x, uint32_t shift)
218 6ddbc6e4 pbrook
{
219 6ddbc6e4 pbrook
    return do_ssat(x, shift);
220 6ddbc6e4 pbrook
}
221 6ddbc6e4 pbrook
222 6ddbc6e4 pbrook
/* Dual halfword signed saturate.  */
223 6ddbc6e4 pbrook
uint32_t HELPER(ssat16)(uint32_t x, uint32_t shift)
224 6ddbc6e4 pbrook
{
225 6ddbc6e4 pbrook
    uint32_t res;
226 6ddbc6e4 pbrook
227 6ddbc6e4 pbrook
    res = (uint16_t)do_ssat((int16_t)x, shift);
228 6ddbc6e4 pbrook
    res |= do_ssat(((int32_t)x) >> 16, shift) << 16;
229 6ddbc6e4 pbrook
    return res;
230 6ddbc6e4 pbrook
}
231 6ddbc6e4 pbrook
232 6ddbc6e4 pbrook
/* Unsigned saturate.  */
233 6ddbc6e4 pbrook
uint32_t HELPER(usat)(uint32_t x, uint32_t shift)
234 6ddbc6e4 pbrook
{
235 6ddbc6e4 pbrook
    return do_usat(x, shift);
236 6ddbc6e4 pbrook
}
237 6ddbc6e4 pbrook
238 6ddbc6e4 pbrook
/* Dual halfword unsigned saturate.  */
239 6ddbc6e4 pbrook
uint32_t HELPER(usat16)(uint32_t x, uint32_t shift)
240 6ddbc6e4 pbrook
{
241 6ddbc6e4 pbrook
    uint32_t res;
242 6ddbc6e4 pbrook
243 6ddbc6e4 pbrook
    res = (uint16_t)do_usat((int16_t)x, shift);
244 6ddbc6e4 pbrook
    res |= do_usat(((int32_t)x) >> 16, shift) << 16;
245 6ddbc6e4 pbrook
    return res;
246 6ddbc6e4 pbrook
}
247 d9ba4830 pbrook
248 d9ba4830 pbrook
void HELPER(wfi)(void)
249 d9ba4830 pbrook
{
250 d9ba4830 pbrook
    env->exception_index = EXCP_HLT;
251 d9ba4830 pbrook
    env->halted = 1;
252 d9ba4830 pbrook
    cpu_loop_exit();
253 d9ba4830 pbrook
}
254 d9ba4830 pbrook
255 d9ba4830 pbrook
void HELPER(exception)(uint32_t excp)
256 d9ba4830 pbrook
{
257 d9ba4830 pbrook
    env->exception_index = excp;
258 d9ba4830 pbrook
    cpu_loop_exit();
259 d9ba4830 pbrook
}
260 d9ba4830 pbrook
261 d9ba4830 pbrook
uint32_t HELPER(cpsr_read)(void)
262 d9ba4830 pbrook
{
263 d9ba4830 pbrook
    return cpsr_read(env) & ~CPSR_EXEC;
264 d9ba4830 pbrook
}
265 d9ba4830 pbrook
266 d9ba4830 pbrook
void HELPER(cpsr_write)(uint32_t val, uint32_t mask)
267 d9ba4830 pbrook
{
268 d9ba4830 pbrook
    cpsr_write(env, val, mask);
269 d9ba4830 pbrook
}
270 b0109805 pbrook
271 b0109805 pbrook
/* Access to user mode registers from privileged modes.  */
272 b0109805 pbrook
uint32_t HELPER(get_user_reg)(uint32_t regno)
273 b0109805 pbrook
{
274 b0109805 pbrook
    uint32_t val;
275 b0109805 pbrook
276 b0109805 pbrook
    if (regno == 13) {
277 b0109805 pbrook
        val = env->banked_r13[0];
278 b0109805 pbrook
    } else if (regno == 14) {
279 b0109805 pbrook
        val = env->banked_r14[0];
280 b0109805 pbrook
    } else if (regno >= 8
281 b0109805 pbrook
               && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
282 b0109805 pbrook
        val = env->usr_regs[regno - 8];
283 b0109805 pbrook
    } else {
284 b0109805 pbrook
        val = env->regs[regno];
285 b0109805 pbrook
    }
286 b0109805 pbrook
    return val;
287 b0109805 pbrook
}
288 b0109805 pbrook
289 b0109805 pbrook
void HELPER(set_user_reg)(uint32_t regno, uint32_t val)
290 b0109805 pbrook
{
291 b0109805 pbrook
    if (regno == 13) {
292 b0109805 pbrook
        env->banked_r13[0] = val;
293 b0109805 pbrook
    } else if (regno == 14) {
294 b0109805 pbrook
        env->banked_r14[0] = val;
295 b0109805 pbrook
    } else if (regno >= 8
296 b0109805 pbrook
               && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
297 b0109805 pbrook
        env->usr_regs[regno - 8] = val;
298 b0109805 pbrook
    } else {
299 b0109805 pbrook
        env->regs[regno] = val;
300 b0109805 pbrook
    }
301 b0109805 pbrook
}
302 b0109805 pbrook
303 8984bd2e pbrook
/* ??? Flag setting arithmetic is awkward because we need to do comparisons.
304 8984bd2e pbrook
   The only way to do that in TCG is a conditional branch, which clobbers
305 8984bd2e pbrook
   all our temporaries.  For now implement these as helper functions.  */
306 8984bd2e pbrook
307 8984bd2e pbrook
uint32_t HELPER (add_cc)(uint32_t a, uint32_t b)
308 8984bd2e pbrook
{
309 8984bd2e pbrook
    uint32_t result;
310 37f9ba46 aurel32
    result = a + b;
311 6fbe23d5 pbrook
    env->NF = env->ZF = result;
312 8984bd2e pbrook
    env->CF = result < a;
313 8984bd2e pbrook
    env->VF = (a ^ b ^ -1) & (a ^ result);
314 8984bd2e pbrook
    return result;
315 8984bd2e pbrook
}
316 8984bd2e pbrook
317 8984bd2e pbrook
uint32_t HELPER(adc_cc)(uint32_t a, uint32_t b)
318 8984bd2e pbrook
{
319 8984bd2e pbrook
    uint32_t result;
320 8984bd2e pbrook
    if (!env->CF) {
321 8984bd2e pbrook
        result = a + b;
322 8984bd2e pbrook
        env->CF = result < a;
323 8984bd2e pbrook
    } else {
324 8984bd2e pbrook
        result = a + b + 1;
325 8984bd2e pbrook
        env->CF = result <= a;
326 8984bd2e pbrook
    }
327 8984bd2e pbrook
    env->VF = (a ^ b ^ -1) & (a ^ result);
328 6fbe23d5 pbrook
    env->NF = env->ZF = result;
329 8984bd2e pbrook
    return result;
330 8984bd2e pbrook
}
331 8984bd2e pbrook
332 8984bd2e pbrook
uint32_t HELPER(sub_cc)(uint32_t a, uint32_t b)
333 8984bd2e pbrook
{
334 8984bd2e pbrook
    uint32_t result;
335 8984bd2e pbrook
    result = a - b;
336 6fbe23d5 pbrook
    env->NF = env->ZF = result;
337 8984bd2e pbrook
    env->CF = a >= b;
338 8984bd2e pbrook
    env->VF = (a ^ b) & (a ^ result);
339 8984bd2e pbrook
    return result;
340 8984bd2e pbrook
}
341 8984bd2e pbrook
342 8984bd2e pbrook
uint32_t HELPER(sbc_cc)(uint32_t a, uint32_t b)
343 8984bd2e pbrook
{
344 8984bd2e pbrook
    uint32_t result;
345 8984bd2e pbrook
    if (!env->CF) {
346 8984bd2e pbrook
        result = a - b - 1;
347 8984bd2e pbrook
        env->CF = a > b;
348 8984bd2e pbrook
    } else {
349 8984bd2e pbrook
        result = a - b;
350 8984bd2e pbrook
        env->CF = a >= b;
351 8984bd2e pbrook
    }
352 8984bd2e pbrook
    env->VF = (a ^ b) & (a ^ result);
353 6fbe23d5 pbrook
    env->NF = env->ZF = result;
354 8984bd2e pbrook
    return result;
355 8984bd2e pbrook
}
356 8984bd2e pbrook
357 8984bd2e pbrook
/* Similarly for variable shift instructions.  */
358 8984bd2e pbrook
359 8984bd2e pbrook
uint32_t HELPER(shl)(uint32_t x, uint32_t i)
360 8984bd2e pbrook
{
361 8984bd2e pbrook
    int shift = i & 0xff;
362 8984bd2e pbrook
    if (shift >= 32)
363 8984bd2e pbrook
        return 0;
364 8984bd2e pbrook
    return x << shift;
365 8984bd2e pbrook
}
366 8984bd2e pbrook
367 8984bd2e pbrook
uint32_t HELPER(shr)(uint32_t x, uint32_t i)
368 8984bd2e pbrook
{
369 8984bd2e pbrook
    int shift = i & 0xff;
370 8984bd2e pbrook
    if (shift >= 32)
371 8984bd2e pbrook
        return 0;
372 8984bd2e pbrook
    return (uint32_t)x >> shift;
373 8984bd2e pbrook
}
374 8984bd2e pbrook
375 8984bd2e pbrook
uint32_t HELPER(sar)(uint32_t x, uint32_t i)
376 8984bd2e pbrook
{
377 8984bd2e pbrook
    int shift = i & 0xff;
378 8984bd2e pbrook
    if (shift >= 32)
379 8984bd2e pbrook
        shift = 31;
380 8984bd2e pbrook
    return (int32_t)x >> shift;
381 8984bd2e pbrook
}
382 8984bd2e pbrook
383 8984bd2e pbrook
uint32_t HELPER(ror)(uint32_t x, uint32_t i)
384 8984bd2e pbrook
{
385 8984bd2e pbrook
    int shift = i & 0xff;
386 8984bd2e pbrook
    if (shift == 0)
387 8984bd2e pbrook
        return x;
388 8984bd2e pbrook
    return (x >> shift) | (x << (32 - shift));
389 8984bd2e pbrook
}
390 8984bd2e pbrook
391 8984bd2e pbrook
uint32_t HELPER(shl_cc)(uint32_t x, uint32_t i)
392 8984bd2e pbrook
{
393 8984bd2e pbrook
    int shift = i & 0xff;
394 8984bd2e pbrook
    if (shift >= 32) {
395 8984bd2e pbrook
        if (shift == 32)
396 8984bd2e pbrook
            env->CF = x & 1;
397 8984bd2e pbrook
        else
398 8984bd2e pbrook
            env->CF = 0;
399 8984bd2e pbrook
        return 0;
400 8984bd2e pbrook
    } else if (shift != 0) {
401 8984bd2e pbrook
        env->CF = (x >> (32 - shift)) & 1;
402 8984bd2e pbrook
        return x << shift;
403 8984bd2e pbrook
    }
404 8984bd2e pbrook
    return x;
405 8984bd2e pbrook
}
406 8984bd2e pbrook
407 8984bd2e pbrook
uint32_t HELPER(shr_cc)(uint32_t x, uint32_t i)
408 8984bd2e pbrook
{
409 8984bd2e pbrook
    int shift = i & 0xff;
410 8984bd2e pbrook
    if (shift >= 32) {
411 8984bd2e pbrook
        if (shift == 32)
412 8984bd2e pbrook
            env->CF = (x >> 31) & 1;
413 8984bd2e pbrook
        else
414 8984bd2e pbrook
            env->CF = 0;
415 8984bd2e pbrook
        return 0;
416 8984bd2e pbrook
    } else if (shift != 0) {
417 8984bd2e pbrook
        env->CF = (x >> (shift - 1)) & 1;
418 8984bd2e pbrook
        return x >> shift;
419 8984bd2e pbrook
    }
420 8984bd2e pbrook
    return x;
421 8984bd2e pbrook
}
422 8984bd2e pbrook
423 8984bd2e pbrook
uint32_t HELPER(sar_cc)(uint32_t x, uint32_t i)
424 8984bd2e pbrook
{
425 8984bd2e pbrook
    int shift = i & 0xff;
426 8984bd2e pbrook
    if (shift >= 32) {
427 8984bd2e pbrook
        env->CF = (x >> 31) & 1;
428 8984bd2e pbrook
        return (int32_t)x >> 31;
429 8984bd2e pbrook
    } else if (shift != 0) {
430 8984bd2e pbrook
        env->CF = (x >> (shift - 1)) & 1;
431 8984bd2e pbrook
        return (int32_t)x >> shift;
432 8984bd2e pbrook
    }
433 8984bd2e pbrook
    return x;
434 8984bd2e pbrook
}
435 8984bd2e pbrook
436 8984bd2e pbrook
uint32_t HELPER(ror_cc)(uint32_t x, uint32_t i)
437 8984bd2e pbrook
{
438 8984bd2e pbrook
    int shift1, shift;
439 8984bd2e pbrook
    shift1 = i & 0xff;
440 8984bd2e pbrook
    shift = shift1 & 0x1f;
441 8984bd2e pbrook
    if (shift == 0) {
442 8984bd2e pbrook
        if (shift1 != 0)
443 8984bd2e pbrook
            env->CF = (x >> 31) & 1;
444 8984bd2e pbrook
        return x;
445 8984bd2e pbrook
    } else {
446 8984bd2e pbrook
        env->CF = (x >> (shift - 1)) & 1;
447 8984bd2e pbrook
        return ((uint32_t)x >> shift) | (x << (32 - shift));
448 8984bd2e pbrook
    }
449 8984bd2e pbrook
}
450 8984bd2e pbrook
451 ad69471c pbrook
uint64_t HELPER(neon_add_saturate_s64)(uint64_t src1, uint64_t src2)
452 ad69471c pbrook
{
453 ad69471c pbrook
    uint64_t res;
454 ad69471c pbrook
455 ad69471c pbrook
    res = src1 + src2;
456 ad69471c pbrook
    if (((res ^ src1) & SIGNBIT64) && !((src1 ^ src2) & SIGNBIT64)) {
457 ad69471c pbrook
        env->QF = 1;
458 ad69471c pbrook
        res = ((int64_t)src1 >> 63) ^ ~SIGNBIT64;
459 ad69471c pbrook
    }
460 ad69471c pbrook
    return res;
461 ad69471c pbrook
}
462 ad69471c pbrook
463 ad69471c pbrook
uint64_t HELPER(neon_add_saturate_u64)(uint64_t src1, uint64_t src2)
464 ad69471c pbrook
{
465 ad69471c pbrook
    uint64_t res;
466 ad69471c pbrook
467 ad69471c pbrook
    res = src1 + src2;
468 ad69471c pbrook
    if (res < src1) {
469 ad69471c pbrook
        env->QF = 1;
470 ad69471c pbrook
        res = ~(uint64_t)0;
471 ad69471c pbrook
    }
472 ad69471c pbrook
    return res;
473 ad69471c pbrook
}
474 ad69471c pbrook
475 ad69471c pbrook
uint64_t HELPER(neon_sub_saturate_s64)(uint64_t src1, uint64_t src2)
476 ad69471c pbrook
{
477 ad69471c pbrook
    uint64_t res;
478 ad69471c pbrook
479 ad69471c pbrook
    res = src1 - src2;
480 ad69471c pbrook
    if (((res ^ src1) & SIGNBIT64) && ((src1 ^ src2) & SIGNBIT64)) {
481 ad69471c pbrook
        env->QF = 1;
482 ad69471c pbrook
        res = ((int64_t)src1 >> 63) ^ ~SIGNBIT64;
483 ad69471c pbrook
    }
484 ad69471c pbrook
    return res;
485 ad69471c pbrook
}
486 ad69471c pbrook
487 ad69471c pbrook
uint64_t HELPER(neon_sub_saturate_u64)(uint64_t src1, uint64_t src2)
488 ad69471c pbrook
{
489 ad69471c pbrook
    uint64_t res;
490 ad69471c pbrook
491 ad69471c pbrook
    if (src1 < src2) {
492 ad69471c pbrook
        env->QF = 1;
493 ad69471c pbrook
        res = 0;
494 ad69471c pbrook
    } else {
495 ad69471c pbrook
        res = src1 - src2;
496 ad69471c pbrook
    }
497 ad69471c pbrook
    return res;
498 ad69471c pbrook
}
499 ad69471c pbrook
500 ad69471c pbrook
/* These need to return a pair of value, so still use T0/T1.  */
501 ad69471c pbrook
/* Transpose.  Argument order is rather strange to avoid special casing
502 ad69471c pbrook
   the tranlation code.
503 ad69471c pbrook
   On input T0 = rm, T1 = rd.  On output T0 = rd, T1 = rm  */
504 ad69471c pbrook
void HELPER(neon_trn_u8)(void)
505 ad69471c pbrook
{
506 ad69471c pbrook
    uint32_t rd;
507 ad69471c pbrook
    uint32_t rm;
508 ad69471c pbrook
    rd = ((T0 & 0x00ff00ff) << 8) | (T1 & 0x00ff00ff);
509 ad69471c pbrook
    rm = ((T1 & 0xff00ff00) >> 8) | (T0 & 0xff00ff00);
510 ad69471c pbrook
    T0 = rd;
511 ad69471c pbrook
    T1 = rm;
512 ad69471c pbrook
}
513 ad69471c pbrook
514 ad69471c pbrook
void HELPER(neon_trn_u16)(void)
515 ad69471c pbrook
{
516 ad69471c pbrook
    uint32_t rd;
517 ad69471c pbrook
    uint32_t rm;
518 ad69471c pbrook
    rd = (T0 << 16) | (T1 & 0xffff);
519 ad69471c pbrook
    rm = (T1 >> 16) | (T0 & 0xffff0000);
520 ad69471c pbrook
    T0 = rd;
521 ad69471c pbrook
    T1 = rm;
522 ad69471c pbrook
}
523 ad69471c pbrook
524 ad69471c pbrook
/* Worker routines for zip and unzip.  */
525 ad69471c pbrook
void HELPER(neon_unzip_u8)(void)
526 ad69471c pbrook
{
527 ad69471c pbrook
    uint32_t rd;
528 ad69471c pbrook
    uint32_t rm;
529 ad69471c pbrook
    rd = (T0 & 0xff) | ((T0 >> 8) & 0xff00)
530 ad69471c pbrook
         | ((T1 << 16) & 0xff0000) | ((T1 << 8) & 0xff000000);
531 ad69471c pbrook
    rm = ((T0 >> 8) & 0xff) | ((T0 >> 16) & 0xff00)
532 ad69471c pbrook
         | ((T1 << 8) & 0xff0000) | (T1 & 0xff000000);
533 ad69471c pbrook
    T0 = rd;
534 ad69471c pbrook
    T1 = rm;
535 ad69471c pbrook
}
536 ad69471c pbrook
537 ad69471c pbrook
void HELPER(neon_zip_u8)(void)
538 ad69471c pbrook
{
539 ad69471c pbrook
    uint32_t rd;
540 ad69471c pbrook
    uint32_t rm;
541 ad69471c pbrook
    rd = (T0 & 0xff) | ((T1 << 8) & 0xff00)
542 ad69471c pbrook
         | ((T0 << 16) & 0xff0000) | ((T1 << 24) & 0xff000000);
543 ad69471c pbrook
    rm = ((T0 >> 16) & 0xff) | ((T1 >> 8) & 0xff00)
544 ad69471c pbrook
         | ((T0 >> 8) & 0xff0000) | (T1 & 0xff000000);
545 ad69471c pbrook
    T0 = rd;
546 ad69471c pbrook
    T1 = rm;
547 ad69471c pbrook
}
548 ad69471c pbrook
549 ad69471c pbrook
void HELPER(neon_zip_u16)(void)
550 ad69471c pbrook
{
551 ad69471c pbrook
    uint32_t tmp;
552 ad69471c pbrook
553 ad69471c pbrook
    tmp = (T0 & 0xffff) | (T1 << 16);
554 ad69471c pbrook
    T1 = (T1 & 0xffff0000) | (T0 >> 16);
555 ad69471c pbrook
    T0 = tmp;
556 ad69471c pbrook
}