Statistics
| Branch: | Revision:

root / target-arm / op_helper.c @ f8ed7070

History | View | Annotate | Download (12.4 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 b7bcbe95 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 b7bcbe95 bellard
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 9ee6e8bb pbrook
            tmp = (table[index >> 3] >> (index & 7)) & 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 b5ff1b31 bellard
    if (__builtin_expect(ret, 0)) {
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
    shift = PARAM1;
189 6ddbc6e4 pbrook
    top = val >> shift;
190 6ddbc6e4 pbrook
    mask = (1u << shift) - 1;
191 6ddbc6e4 pbrook
    if (top > 0) {
192 6ddbc6e4 pbrook
        env->QF = 1;
193 6ddbc6e4 pbrook
        return mask;
194 6ddbc6e4 pbrook
    } else if (top < -1) {
195 6ddbc6e4 pbrook
        env->QF = 1;
196 6ddbc6e4 pbrook
        return ~mask;
197 6ddbc6e4 pbrook
    }
198 6ddbc6e4 pbrook
    return val;
199 6ddbc6e4 pbrook
}
200 6ddbc6e4 pbrook
201 6ddbc6e4 pbrook
/* Unsigned saturation.  */
202 6ddbc6e4 pbrook
static inline uint32_t do_usat(int32_t val, int shift)
203 6ddbc6e4 pbrook
{
204 6ddbc6e4 pbrook
    uint32_t max;
205 6ddbc6e4 pbrook
206 6ddbc6e4 pbrook
    shift = PARAM1;
207 6ddbc6e4 pbrook
    max = (1u << shift) - 1;
208 6ddbc6e4 pbrook
    if (val < 0) {
209 6ddbc6e4 pbrook
        env->QF = 1;
210 6ddbc6e4 pbrook
        return 0;
211 6ddbc6e4 pbrook
    } else if (val > max) {
212 6ddbc6e4 pbrook
        env->QF = 1;
213 6ddbc6e4 pbrook
        return max;
214 6ddbc6e4 pbrook
    }
215 6ddbc6e4 pbrook
    return val;
216 6ddbc6e4 pbrook
}
217 6ddbc6e4 pbrook
218 6ddbc6e4 pbrook
/* Signed saturate.  */
219 6ddbc6e4 pbrook
uint32_t HELPER(ssat)(uint32_t x, uint32_t shift)
220 6ddbc6e4 pbrook
{
221 6ddbc6e4 pbrook
    return do_ssat(x, shift);
222 6ddbc6e4 pbrook
}
223 6ddbc6e4 pbrook
224 6ddbc6e4 pbrook
/* Dual halfword signed saturate.  */
225 6ddbc6e4 pbrook
uint32_t HELPER(ssat16)(uint32_t x, uint32_t shift)
226 6ddbc6e4 pbrook
{
227 6ddbc6e4 pbrook
    uint32_t res;
228 6ddbc6e4 pbrook
229 6ddbc6e4 pbrook
    res = (uint16_t)do_ssat((int16_t)x, shift);
230 6ddbc6e4 pbrook
    res |= do_ssat(((int32_t)x) >> 16, shift) << 16;
231 6ddbc6e4 pbrook
    return res;
232 6ddbc6e4 pbrook
}
233 6ddbc6e4 pbrook
234 6ddbc6e4 pbrook
/* Unsigned saturate.  */
235 6ddbc6e4 pbrook
uint32_t HELPER(usat)(uint32_t x, uint32_t shift)
236 6ddbc6e4 pbrook
{
237 6ddbc6e4 pbrook
    return do_usat(x, shift);
238 6ddbc6e4 pbrook
}
239 6ddbc6e4 pbrook
240 6ddbc6e4 pbrook
/* Dual halfword unsigned saturate.  */
241 6ddbc6e4 pbrook
uint32_t HELPER(usat16)(uint32_t x, uint32_t shift)
242 6ddbc6e4 pbrook
{
243 6ddbc6e4 pbrook
    uint32_t res;
244 6ddbc6e4 pbrook
245 6ddbc6e4 pbrook
    res = (uint16_t)do_usat((int16_t)x, shift);
246 6ddbc6e4 pbrook
    res |= do_usat(((int32_t)x) >> 16, shift) << 16;
247 6ddbc6e4 pbrook
    return res;
248 6ddbc6e4 pbrook
}
249 d9ba4830 pbrook
250 d9ba4830 pbrook
void HELPER(wfi)(void)
251 d9ba4830 pbrook
{
252 d9ba4830 pbrook
    env->exception_index = EXCP_HLT;
253 d9ba4830 pbrook
    env->halted = 1;
254 d9ba4830 pbrook
    cpu_loop_exit();
255 d9ba4830 pbrook
}
256 d9ba4830 pbrook
257 d9ba4830 pbrook
void HELPER(exception)(uint32_t excp)
258 d9ba4830 pbrook
{
259 d9ba4830 pbrook
    env->exception_index = excp;
260 d9ba4830 pbrook
    cpu_loop_exit();
261 d9ba4830 pbrook
}
262 d9ba4830 pbrook
263 d9ba4830 pbrook
uint32_t HELPER(cpsr_read)(void)
264 d9ba4830 pbrook
{
265 d9ba4830 pbrook
    return cpsr_read(env) & ~CPSR_EXEC;
266 d9ba4830 pbrook
}
267 d9ba4830 pbrook
268 d9ba4830 pbrook
void HELPER(cpsr_write)(uint32_t val, uint32_t mask)
269 d9ba4830 pbrook
{
270 d9ba4830 pbrook
    cpsr_write(env, val, mask);
271 d9ba4830 pbrook
}
272 b0109805 pbrook
273 b0109805 pbrook
/* Access to user mode registers from privileged modes.  */
274 b0109805 pbrook
uint32_t HELPER(get_user_reg)(uint32_t regno)
275 b0109805 pbrook
{
276 b0109805 pbrook
    uint32_t val;
277 b0109805 pbrook
278 b0109805 pbrook
    if (regno == 13) {
279 b0109805 pbrook
        val = env->banked_r13[0];
280 b0109805 pbrook
    } else if (regno == 14) {
281 b0109805 pbrook
        val = env->banked_r14[0];
282 b0109805 pbrook
    } else if (regno >= 8
283 b0109805 pbrook
               && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
284 b0109805 pbrook
        val = env->usr_regs[regno - 8];
285 b0109805 pbrook
    } else {
286 b0109805 pbrook
        val = env->regs[regno];
287 b0109805 pbrook
    }
288 b0109805 pbrook
    return val;
289 b0109805 pbrook
}
290 b0109805 pbrook
291 b0109805 pbrook
void HELPER(set_user_reg)(uint32_t regno, uint32_t val)
292 b0109805 pbrook
{
293 b0109805 pbrook
    if (regno == 13) {
294 b0109805 pbrook
        env->banked_r13[0] = val;
295 b0109805 pbrook
    } else if (regno == 14) {
296 b0109805 pbrook
        env->banked_r14[0] = val;
297 b0109805 pbrook
    } else if (regno >= 8
298 b0109805 pbrook
               && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
299 b0109805 pbrook
        env->usr_regs[regno - 8] = val;
300 b0109805 pbrook
    } else {
301 b0109805 pbrook
        env->regs[regno] = val;
302 b0109805 pbrook
    }
303 b0109805 pbrook
}
304 b0109805 pbrook
305 8984bd2e pbrook
/* ??? Flag setting arithmetic is awkward because we need to do comparisons.
306 8984bd2e pbrook
   The only way to do that in TCG is a conditional branch, which clobbers
307 8984bd2e pbrook
   all our temporaries.  For now implement these as helper functions.  */
308 8984bd2e pbrook
309 8984bd2e pbrook
uint32_t HELPER (add_cc)(uint32_t a, uint32_t b)
310 8984bd2e pbrook
{
311 8984bd2e pbrook
    uint32_t result;
312 8984bd2e pbrook
    result = T0 + T1;
313 6fbe23d5 pbrook
    env->NF = env->ZF = result;
314 8984bd2e pbrook
    env->CF = result < a;
315 8984bd2e pbrook
    env->VF = (a ^ b ^ -1) & (a ^ result);
316 8984bd2e pbrook
    return result;
317 8984bd2e pbrook
}
318 8984bd2e pbrook
319 8984bd2e pbrook
uint32_t HELPER(adc_cc)(uint32_t a, uint32_t b)
320 8984bd2e pbrook
{
321 8984bd2e pbrook
    uint32_t result;
322 8984bd2e pbrook
    if (!env->CF) {
323 8984bd2e pbrook
        result = a + b;
324 8984bd2e pbrook
        env->CF = result < a;
325 8984bd2e pbrook
    } else {
326 8984bd2e pbrook
        result = a + b + 1;
327 8984bd2e pbrook
        env->CF = result <= a;
328 8984bd2e pbrook
    }
329 8984bd2e pbrook
    env->VF = (a ^ b ^ -1) & (a ^ result);
330 6fbe23d5 pbrook
    env->NF = env->ZF = result;
331 8984bd2e pbrook
    return result;
332 8984bd2e pbrook
}
333 8984bd2e pbrook
334 8984bd2e pbrook
uint32_t HELPER(sub_cc)(uint32_t a, uint32_t b)
335 8984bd2e pbrook
{
336 8984bd2e pbrook
    uint32_t result;
337 8984bd2e pbrook
    result = a - b;
338 6fbe23d5 pbrook
    env->NF = env->ZF = result;
339 8984bd2e pbrook
    env->CF = a >= b;
340 8984bd2e pbrook
    env->VF = (a ^ b) & (a ^ result);
341 8984bd2e pbrook
    return result;
342 8984bd2e pbrook
}
343 8984bd2e pbrook
344 8984bd2e pbrook
uint32_t HELPER(sbc_cc)(uint32_t a, uint32_t b)
345 8984bd2e pbrook
{
346 8984bd2e pbrook
    uint32_t result;
347 8984bd2e pbrook
    if (!env->CF) {
348 8984bd2e pbrook
        result = a - b - 1;
349 8984bd2e pbrook
        env->CF = a > b;
350 8984bd2e pbrook
    } else {
351 8984bd2e pbrook
        result = a - b;
352 8984bd2e pbrook
        env->CF = a >= b;
353 8984bd2e pbrook
    }
354 8984bd2e pbrook
    env->VF = (a ^ b) & (a ^ result);
355 6fbe23d5 pbrook
    env->NF = env->ZF = result;
356 8984bd2e pbrook
    return result;
357 8984bd2e pbrook
}
358 8984bd2e pbrook
359 8984bd2e pbrook
/* Similarly for variable shift instructions.  */
360 8984bd2e pbrook
361 8984bd2e pbrook
uint32_t HELPER(shl)(uint32_t x, uint32_t i)
362 8984bd2e pbrook
{
363 8984bd2e pbrook
    int shift = i & 0xff;
364 8984bd2e pbrook
    if (shift >= 32)
365 8984bd2e pbrook
        return 0;
366 8984bd2e pbrook
    return x << shift;
367 8984bd2e pbrook
}
368 8984bd2e pbrook
369 8984bd2e pbrook
uint32_t HELPER(shr)(uint32_t x, uint32_t i)
370 8984bd2e pbrook
{
371 8984bd2e pbrook
    int shift = i & 0xff;
372 8984bd2e pbrook
    if (shift >= 32)
373 8984bd2e pbrook
        return 0;
374 8984bd2e pbrook
    return (uint32_t)x >> shift;
375 8984bd2e pbrook
}
376 8984bd2e pbrook
377 8984bd2e pbrook
uint32_t HELPER(sar)(uint32_t x, uint32_t i)
378 8984bd2e pbrook
{
379 8984bd2e pbrook
    int shift = i & 0xff;
380 8984bd2e pbrook
    if (shift >= 32)
381 8984bd2e pbrook
        shift = 31;
382 8984bd2e pbrook
    return (int32_t)x >> shift;
383 8984bd2e pbrook
}
384 8984bd2e pbrook
385 8984bd2e pbrook
uint32_t HELPER(ror)(uint32_t x, uint32_t i)
386 8984bd2e pbrook
{
387 8984bd2e pbrook
    int shift = i & 0xff;
388 8984bd2e pbrook
    if (shift == 0)
389 8984bd2e pbrook
        return x;
390 8984bd2e pbrook
    return (x >> shift) | (x << (32 - shift));
391 8984bd2e pbrook
}
392 8984bd2e pbrook
393 8984bd2e pbrook
uint32_t HELPER(shl_cc)(uint32_t x, uint32_t i)
394 8984bd2e pbrook
{
395 8984bd2e pbrook
    int shift = i & 0xff;
396 8984bd2e pbrook
    if (shift >= 32) {
397 8984bd2e pbrook
        if (shift == 32)
398 8984bd2e pbrook
            env->CF = x & 1;
399 8984bd2e pbrook
        else
400 8984bd2e pbrook
            env->CF = 0;
401 8984bd2e pbrook
        return 0;
402 8984bd2e pbrook
    } else if (shift != 0) {
403 8984bd2e pbrook
        env->CF = (x >> (32 - shift)) & 1;
404 8984bd2e pbrook
        return x << shift;
405 8984bd2e pbrook
    }
406 8984bd2e pbrook
    return x;
407 8984bd2e pbrook
}
408 8984bd2e pbrook
409 8984bd2e pbrook
uint32_t HELPER(shr_cc)(uint32_t x, uint32_t i)
410 8984bd2e pbrook
{
411 8984bd2e pbrook
    int shift = i & 0xff;
412 8984bd2e pbrook
    if (shift >= 32) {
413 8984bd2e pbrook
        if (shift == 32)
414 8984bd2e pbrook
            env->CF = (x >> 31) & 1;
415 8984bd2e pbrook
        else
416 8984bd2e pbrook
            env->CF = 0;
417 8984bd2e pbrook
        return 0;
418 8984bd2e pbrook
    } else if (shift != 0) {
419 8984bd2e pbrook
        env->CF = (x >> (shift - 1)) & 1;
420 8984bd2e pbrook
        return x >> shift;
421 8984bd2e pbrook
    }
422 8984bd2e pbrook
    return x;
423 8984bd2e pbrook
}
424 8984bd2e pbrook
425 8984bd2e pbrook
uint32_t HELPER(sar_cc)(uint32_t x, uint32_t i)
426 8984bd2e pbrook
{
427 8984bd2e pbrook
    int shift = i & 0xff;
428 8984bd2e pbrook
    if (shift >= 32) {
429 8984bd2e pbrook
        env->CF = (x >> 31) & 1;
430 8984bd2e pbrook
        return (int32_t)x >> 31;
431 8984bd2e pbrook
    } else if (shift != 0) {
432 8984bd2e pbrook
        env->CF = (x >> (shift - 1)) & 1;
433 8984bd2e pbrook
        return (int32_t)x >> shift;
434 8984bd2e pbrook
    }
435 8984bd2e pbrook
    return x;
436 8984bd2e pbrook
}
437 8984bd2e pbrook
438 8984bd2e pbrook
uint32_t HELPER(ror_cc)(uint32_t x, uint32_t i)
439 8984bd2e pbrook
{
440 8984bd2e pbrook
    int shift1, shift;
441 8984bd2e pbrook
    shift1 = i & 0xff;
442 8984bd2e pbrook
    shift = shift1 & 0x1f;
443 8984bd2e pbrook
    if (shift == 0) {
444 8984bd2e pbrook
        if (shift1 != 0)
445 8984bd2e pbrook
            env->CF = (x >> 31) & 1;
446 8984bd2e pbrook
        return x;
447 8984bd2e pbrook
    } else {
448 8984bd2e pbrook
        env->CF = (x >> (shift - 1)) & 1;
449 8984bd2e pbrook
        return ((uint32_t)x >> shift) | (x << (32 - shift));
450 8984bd2e pbrook
    }
451 8984bd2e pbrook
}
452 8984bd2e pbrook
453 ad69471c pbrook
uint64_t HELPER(neon_add_saturate_s64)(uint64_t src1, uint64_t src2)
454 ad69471c pbrook
{
455 ad69471c pbrook
    uint64_t res;
456 ad69471c pbrook
457 ad69471c pbrook
    res = src1 + src2;
458 ad69471c pbrook
    if (((res ^ src1) & SIGNBIT64) && !((src1 ^ src2) & SIGNBIT64)) {
459 ad69471c pbrook
        env->QF = 1;
460 ad69471c pbrook
        res = ((int64_t)src1 >> 63) ^ ~SIGNBIT64;
461 ad69471c pbrook
    }
462 ad69471c pbrook
    return res;
463 ad69471c pbrook
}
464 ad69471c pbrook
465 ad69471c pbrook
uint64_t HELPER(neon_add_saturate_u64)(uint64_t src1, uint64_t src2)
466 ad69471c pbrook
{
467 ad69471c pbrook
    uint64_t res;
468 ad69471c pbrook
469 ad69471c pbrook
    res = src1 + src2;
470 ad69471c pbrook
    if (res < src1) {
471 ad69471c pbrook
        env->QF = 1;
472 ad69471c pbrook
        res = ~(uint64_t)0;
473 ad69471c pbrook
    }
474 ad69471c pbrook
    return res;
475 ad69471c pbrook
}
476 ad69471c pbrook
477 ad69471c pbrook
uint64_t HELPER(neon_sub_saturate_s64)(uint64_t src1, uint64_t src2)
478 ad69471c pbrook
{
479 ad69471c pbrook
    uint64_t res;
480 ad69471c pbrook
481 ad69471c pbrook
    res = src1 - src2;
482 ad69471c pbrook
    if (((res ^ src1) & SIGNBIT64) && ((src1 ^ src2) & SIGNBIT64)) {
483 ad69471c pbrook
        env->QF = 1;
484 ad69471c pbrook
        res = ((int64_t)src1 >> 63) ^ ~SIGNBIT64;
485 ad69471c pbrook
    }
486 ad69471c pbrook
    return res;
487 ad69471c pbrook
}
488 ad69471c pbrook
489 ad69471c pbrook
uint64_t HELPER(neon_sub_saturate_u64)(uint64_t src1, uint64_t src2)
490 ad69471c pbrook
{
491 ad69471c pbrook
    uint64_t res;
492 ad69471c pbrook
493 ad69471c pbrook
    if (src1 < src2) {
494 ad69471c pbrook
        env->QF = 1;
495 ad69471c pbrook
        res = 0;
496 ad69471c pbrook
    } else {
497 ad69471c pbrook
        res = src1 - src2;
498 ad69471c pbrook
    }
499 ad69471c pbrook
    return res;
500 ad69471c pbrook
}
501 ad69471c pbrook
502 ad69471c pbrook
/* These need to return a pair of value, so still use T0/T1.  */
503 ad69471c pbrook
/* Transpose.  Argument order is rather strange to avoid special casing
504 ad69471c pbrook
   the tranlation code.
505 ad69471c pbrook
   On input T0 = rm, T1 = rd.  On output T0 = rd, T1 = rm  */
506 ad69471c pbrook
void HELPER(neon_trn_u8)(void)
507 ad69471c pbrook
{
508 ad69471c pbrook
    uint32_t rd;
509 ad69471c pbrook
    uint32_t rm;
510 ad69471c pbrook
    rd = ((T0 & 0x00ff00ff) << 8) | (T1 & 0x00ff00ff);
511 ad69471c pbrook
    rm = ((T1 & 0xff00ff00) >> 8) | (T0 & 0xff00ff00);
512 ad69471c pbrook
    T0 = rd;
513 ad69471c pbrook
    T1 = rm;
514 ad69471c pbrook
    FORCE_RET();
515 ad69471c pbrook
}
516 ad69471c pbrook
517 ad69471c pbrook
void HELPER(neon_trn_u16)(void)
518 ad69471c pbrook
{
519 ad69471c pbrook
    uint32_t rd;
520 ad69471c pbrook
    uint32_t rm;
521 ad69471c pbrook
    rd = (T0 << 16) | (T1 & 0xffff);
522 ad69471c pbrook
    rm = (T1 >> 16) | (T0 & 0xffff0000);
523 ad69471c pbrook
    T0 = rd;
524 ad69471c pbrook
    T1 = rm;
525 ad69471c pbrook
    FORCE_RET();
526 ad69471c pbrook
}
527 ad69471c pbrook
528 ad69471c pbrook
/* Worker routines for zip and unzip.  */
529 ad69471c pbrook
void HELPER(neon_unzip_u8)(void)
530 ad69471c pbrook
{
531 ad69471c pbrook
    uint32_t rd;
532 ad69471c pbrook
    uint32_t rm;
533 ad69471c pbrook
    rd = (T0 & 0xff) | ((T0 >> 8) & 0xff00)
534 ad69471c pbrook
         | ((T1 << 16) & 0xff0000) | ((T1 << 8) & 0xff000000);
535 ad69471c pbrook
    rm = ((T0 >> 8) & 0xff) | ((T0 >> 16) & 0xff00)
536 ad69471c pbrook
         | ((T1 << 8) & 0xff0000) | (T1 & 0xff000000);
537 ad69471c pbrook
    T0 = rd;
538 ad69471c pbrook
    T1 = rm;
539 ad69471c pbrook
    FORCE_RET();
540 ad69471c pbrook
}
541 ad69471c pbrook
542 ad69471c pbrook
void HELPER(neon_zip_u8)(void)
543 ad69471c pbrook
{
544 ad69471c pbrook
    uint32_t rd;
545 ad69471c pbrook
    uint32_t rm;
546 ad69471c pbrook
    rd = (T0 & 0xff) | ((T1 << 8) & 0xff00)
547 ad69471c pbrook
         | ((T0 << 16) & 0xff0000) | ((T1 << 24) & 0xff000000);
548 ad69471c pbrook
    rm = ((T0 >> 16) & 0xff) | ((T1 >> 8) & 0xff00)
549 ad69471c pbrook
         | ((T0 >> 8) & 0xff0000) | (T1 & 0xff000000);
550 ad69471c pbrook
    T0 = rd;
551 ad69471c pbrook
    T1 = rm;
552 ad69471c pbrook
    FORCE_RET();
553 ad69471c pbrook
}
554 ad69471c pbrook
555 ad69471c pbrook
void HELPER(neon_zip_u16)(void)
556 ad69471c pbrook
{
557 ad69471c pbrook
    uint32_t tmp;
558 ad69471c pbrook
559 ad69471c pbrook
    tmp = (T0 & 0xffff) | (T1 << 16);
560 ad69471c pbrook
    T1 = (T1 & 0xffff0000) | (T0 >> 16);
561 ad69471c pbrook
    T0 = tmp;
562 ad69471c pbrook
    FORCE_RET();
563 ad69471c pbrook
}