xref: /openbmc/qemu/target/hexagon/macros.h (revision 36ebc7db)
1 /*
2  *  Copyright(c) 2019-2022 Qualcomm Innovation Center, Inc. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program 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
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef HEXAGON_MACROS_H
19 #define HEXAGON_MACROS_H
20 
21 #include "cpu.h"
22 #include "hex_regs.h"
23 #include "reg_fields.h"
24 
25 #ifdef QEMU_GENERATE
26 #define READ_REG(dest, NUM)              gen_read_reg(dest, NUM)
27 #else
28 #define READ_REG(NUM)                    (env->gpr[(NUM)])
29 #define READ_PREG(NUM)                   (env->pred[NUM])
30 
31 #define WRITE_RREG(NUM, VAL)             log_reg_write(env, NUM, VAL, slot)
32 #define WRITE_PREG(NUM, VAL)             log_pred_write(env, NUM, VAL)
33 #endif
34 
35 #define PCALIGN 4
36 #define PCALIGN_MASK (PCALIGN - 1)
37 
38 #define GET_FIELD(FIELD, REGIN) \
39     fEXTRACTU_BITS(REGIN, reg_field_info[FIELD].width, \
40                    reg_field_info[FIELD].offset)
41 
42 #ifdef QEMU_GENERATE
43 #define GET_USR_FIELD(FIELD, DST) \
44     tcg_gen_extract_tl(DST, hex_gpr[HEX_REG_USR], \
45                        reg_field_info[FIELD].offset, \
46                        reg_field_info[FIELD].width)
47 
48 #define TYPE_INT(X)          __builtin_types_compatible_p(typeof(X), int)
49 #define TYPE_TCGV(X)         __builtin_types_compatible_p(typeof(X), TCGv)
50 #define TYPE_TCGV_I64(X)     __builtin_types_compatible_p(typeof(X), TCGv_i64)
51 
52 #define SET_USR_FIELD_FUNC(X) \
53     __builtin_choose_expr(TYPE_INT(X), \
54         gen_set_usr_fieldi, \
55         __builtin_choose_expr(TYPE_TCGV(X), \
56             gen_set_usr_field, (void)0))
57 #define SET_USR_FIELD(FIELD, VAL) \
58     SET_USR_FIELD_FUNC(VAL)(FIELD, VAL)
59 #else
60 #define GET_USR_FIELD(FIELD) \
61     fEXTRACTU_BITS(env->gpr[HEX_REG_USR], reg_field_info[FIELD].width, \
62                    reg_field_info[FIELD].offset)
63 
64 #define SET_USR_FIELD(FIELD, VAL) \
65     fINSERT_BITS(env->new_value[HEX_REG_USR], reg_field_info[FIELD].width, \
66                  reg_field_info[FIELD].offset, (VAL))
67 #endif
68 
69 #ifdef QEMU_GENERATE
70 /*
71  * Section 5.5 of the Hexagon V67 Programmer's Reference Manual
72  *
73  * Slot 1 store with slot 0 load
74  * A slot 1 store operation with a slot 0 load operation can appear in a packet.
75  * The packet attribute :mem_noshuf inhibits the instruction reordering that
76  * would otherwise be done by the assembler. For example:
77  *     {
78  *         memw(R5) = R2 // slot 1 store
79  *         R3 = memh(R6) // slot 0 load
80  *     }:mem_noshuf
81  * Unlike most packetized operations, these memory operations are not executed
82  * in parallel (Section 3.3.1). Instead, the store instruction in Slot 1
83  * effectively executes first, followed by the load instruction in Slot 0. If
84  * the addresses of the two operations are overlapping, the load will receive
85  * the newly stored data. This feature is supported in processor versions
86  * V65 or greater.
87  *
88  *
89  * For qemu, we look for a load in slot 0 when there is  a store in slot 1
90  * in the same packet.  When we see this, we call a helper that probes the
91  * load to make sure it doesn't fault.  Then, we process the store ahead of
92  * the actual load.
93 
94  */
95 #define CHECK_NOSHUF(VA, SIZE) \
96     do { \
97         if (insn->slot == 0 && ctx->pkt->pkt_has_store_s1) { \
98             probe_noshuf_load(VA, SIZE, ctx->mem_idx); \
99             process_store(ctx, 1); \
100         } \
101     } while (0)
102 
103 #define CHECK_NOSHUF_PRED(GET_EA, SIZE, PRED) \
104     do { \
105         TCGLabel *label = gen_new_label(); \
106         tcg_gen_brcondi_tl(TCG_COND_EQ, PRED, 0, label); \
107         GET_EA; \
108         if (insn->slot == 0 && ctx->pkt->pkt_has_store_s1) { \
109             probe_noshuf_load(EA, SIZE, ctx->mem_idx); \
110         } \
111         gen_set_label(label); \
112         if (insn->slot == 0 && ctx->pkt->pkt_has_store_s1) { \
113             process_store(ctx, 1); \
114         } \
115     } while (0)
116 
117 #define MEM_LOAD1s(DST, VA) \
118     do { \
119         CHECK_NOSHUF(VA, 1); \
120         tcg_gen_qemu_ld8s(DST, VA, ctx->mem_idx); \
121     } while (0)
122 #define MEM_LOAD1u(DST, VA) \
123     do { \
124         CHECK_NOSHUF(VA, 1); \
125         tcg_gen_qemu_ld8u(DST, VA, ctx->mem_idx); \
126     } while (0)
127 #define MEM_LOAD2s(DST, VA) \
128     do { \
129         CHECK_NOSHUF(VA, 2); \
130         tcg_gen_qemu_ld16s(DST, VA, ctx->mem_idx); \
131     } while (0)
132 #define MEM_LOAD2u(DST, VA) \
133     do { \
134         CHECK_NOSHUF(VA, 2); \
135         tcg_gen_qemu_ld16u(DST, VA, ctx->mem_idx); \
136     } while (0)
137 #define MEM_LOAD4s(DST, VA) \
138     do { \
139         CHECK_NOSHUF(VA, 4); \
140         tcg_gen_qemu_ld32s(DST, VA, ctx->mem_idx); \
141     } while (0)
142 #define MEM_LOAD4u(DST, VA) \
143     do { \
144         CHECK_NOSHUF(VA, 4); \
145         tcg_gen_qemu_ld32s(DST, VA, ctx->mem_idx); \
146     } while (0)
147 #define MEM_LOAD8u(DST, VA) \
148     do { \
149         CHECK_NOSHUF(VA, 8); \
150         tcg_gen_qemu_ld64(DST, VA, ctx->mem_idx); \
151     } while (0)
152 
153 #define MEM_STORE1_FUNC(X) \
154     __builtin_choose_expr(TYPE_INT(X), \
155         gen_store1i, \
156         __builtin_choose_expr(TYPE_TCGV(X), \
157             gen_store1, (void)0))
158 #define MEM_STORE1(VA, DATA, SLOT) \
159     MEM_STORE1_FUNC(DATA)(cpu_env, VA, DATA, SLOT)
160 
161 #define MEM_STORE2_FUNC(X) \
162     __builtin_choose_expr(TYPE_INT(X), \
163         gen_store2i, \
164         __builtin_choose_expr(TYPE_TCGV(X), \
165             gen_store2, (void)0))
166 #define MEM_STORE2(VA, DATA, SLOT) \
167     MEM_STORE2_FUNC(DATA)(cpu_env, VA, DATA, SLOT)
168 
169 #define MEM_STORE4_FUNC(X) \
170     __builtin_choose_expr(TYPE_INT(X), \
171         gen_store4i, \
172         __builtin_choose_expr(TYPE_TCGV(X), \
173             gen_store4, (void)0))
174 #define MEM_STORE4(VA, DATA, SLOT) \
175     MEM_STORE4_FUNC(DATA)(cpu_env, VA, DATA, SLOT)
176 
177 #define MEM_STORE8_FUNC(X) \
178     __builtin_choose_expr(TYPE_INT(X), \
179         gen_store8i, \
180         __builtin_choose_expr(TYPE_TCGV_I64(X), \
181             gen_store8, (void)0))
182 #define MEM_STORE8(VA, DATA, SLOT) \
183     MEM_STORE8_FUNC(DATA)(cpu_env, VA, DATA, SLOT)
184 #else
185 #define MEM_LOAD1s(VA) ((int8_t)mem_load1(env, slot, VA))
186 #define MEM_LOAD1u(VA) ((uint8_t)mem_load1(env, slot, VA))
187 #define MEM_LOAD2s(VA) ((int16_t)mem_load2(env, slot, VA))
188 #define MEM_LOAD2u(VA) ((uint16_t)mem_load2(env, slot, VA))
189 #define MEM_LOAD4s(VA) ((int32_t)mem_load4(env, slot, VA))
190 #define MEM_LOAD4u(VA) ((uint32_t)mem_load4(env, slot, VA))
191 #define MEM_LOAD8s(VA) ((int64_t)mem_load8(env, slot, VA))
192 #define MEM_LOAD8u(VA) ((uint64_t)mem_load8(env, slot, VA))
193 
194 #define MEM_STORE1(VA, DATA, SLOT) log_store32(env, VA, DATA, 1, SLOT)
195 #define MEM_STORE2(VA, DATA, SLOT) log_store32(env, VA, DATA, 2, SLOT)
196 #define MEM_STORE4(VA, DATA, SLOT) log_store32(env, VA, DATA, 4, SLOT)
197 #define MEM_STORE8(VA, DATA, SLOT) log_store64(env, VA, DATA, 8, SLOT)
198 #endif
199 
200 #ifdef QEMU_GENERATE
201 static inline void gen_cancel(uint32_t slot)
202 {
203     tcg_gen_ori_tl(hex_slot_cancelled, hex_slot_cancelled, 1 << slot);
204 }
205 
206 #define CANCEL gen_cancel(slot);
207 #else
208 #define CANCEL cancel_slot(env, slot)
209 #endif
210 
211 #define LOAD_CANCEL(EA) do { CANCEL; } while (0)
212 
213 #ifdef QEMU_GENERATE
214 static inline void gen_pred_cancel(TCGv pred, uint32_t slot_num)
215  {
216     TCGv slot_mask = tcg_temp_new();
217     TCGv tmp = tcg_temp_new();
218     TCGv zero = tcg_constant_tl(0);
219     tcg_gen_ori_tl(slot_mask, hex_slot_cancelled, 1 << slot_num);
220     tcg_gen_andi_tl(tmp, pred, 1);
221     tcg_gen_movcond_tl(TCG_COND_EQ, hex_slot_cancelled, tmp, zero,
222                        slot_mask, hex_slot_cancelled);
223     tcg_temp_free(slot_mask);
224     tcg_temp_free(tmp);
225 }
226 #define PRED_LOAD_CANCEL(PRED, EA) \
227     gen_pred_cancel(PRED, insn->is_endloop ? 4 : insn->slot)
228 #endif
229 
230 #define STORE_CANCEL(EA) { env->slot_cancelled |= (1 << slot); }
231 
232 #define fMAX(A, B) (((A) > (B)) ? (A) : (B))
233 
234 #define fMIN(A, B) (((A) < (B)) ? (A) : (B))
235 
236 #define fABS(A) (((A) < 0) ? (-(A)) : (A))
237 #define fINSERT_BITS(REG, WIDTH, OFFSET, INVAL) \
238     REG = ((WIDTH) ? deposit64(REG, (OFFSET), (WIDTH), (INVAL)) : REG)
239 #define fEXTRACTU_BITS(INREG, WIDTH, OFFSET) \
240     ((WIDTH) ? extract64((INREG), (OFFSET), (WIDTH)) : 0LL)
241 #define fEXTRACTU_BIDIR(INREG, WIDTH, OFFSET) \
242     (fZXTN(WIDTH, 32, fBIDIR_LSHIFTR((INREG), (OFFSET), 4_8)))
243 #define fEXTRACTU_RANGE(INREG, HIBIT, LOWBIT) \
244     (((HIBIT) - (LOWBIT) + 1) ? \
245         extract64((INREG), (LOWBIT), ((HIBIT) - (LOWBIT) + 1)) : \
246         0LL)
247 #define fINSERT_RANGE(INREG, HIBIT, LOWBIT, INVAL) \
248     do { \
249         int width = ((HIBIT) - (LOWBIT) + 1); \
250         INREG = (width >= 0 ? \
251             deposit64((INREG), (LOWBIT), width, (INVAL)) : \
252             INREG); \
253     } while (0)
254 
255 #define f8BITSOF(VAL) ((VAL) ? 0xff : 0x00)
256 
257 #ifdef QEMU_GENERATE
258 #define fLSBOLD(VAL) tcg_gen_andi_tl(LSB, (VAL), 1)
259 #else
260 #define fLSBOLD(VAL)  ((VAL) & 1)
261 #endif
262 
263 #ifdef QEMU_GENERATE
264 #define fLSBNEW(PVAL)   tcg_gen_andi_tl(LSB, (PVAL), 1)
265 #define fLSBNEW0        tcg_gen_andi_tl(LSB, hex_new_pred_value[0], 1)
266 #define fLSBNEW1        tcg_gen_andi_tl(LSB, hex_new_pred_value[1], 1)
267 #else
268 #define fLSBNEW(PVAL)   ((PVAL) & 1)
269 #define fLSBNEW0        (env->new_pred_value[0] & 1)
270 #define fLSBNEW1        (env->new_pred_value[1] & 1)
271 #endif
272 
273 #ifdef QEMU_GENERATE
274 #define fLSBOLDNOT(VAL) \
275     do { \
276         tcg_gen_andi_tl(LSB, (VAL), 1); \
277         tcg_gen_xori_tl(LSB, LSB, 1); \
278     } while (0)
279 #define fLSBNEWNOT(PNUM) \
280     do { \
281         tcg_gen_andi_tl(LSB, (PNUM), 1); \
282         tcg_gen_xori_tl(LSB, LSB, 1); \
283     } while (0)
284 #else
285 #define fLSBNEWNOT(PNUM) (!fLSBNEW(PNUM))
286 #define fLSBOLDNOT(VAL) (!fLSBOLD(VAL))
287 #define fLSBNEW0NOT (!fLSBNEW0)
288 #define fLSBNEW1NOT (!fLSBNEW1)
289 #endif
290 
291 #define fNEWREG(VAL) ((int32_t)(VAL))
292 
293 #define fNEWREG_ST(VAL) (VAL)
294 
295 #define fVSATUVALN(N, VAL) \
296     ({ \
297         (((int64_t)(VAL)) < 0) ? 0 : ((1LL << (N)) - 1); \
298     })
299 #define fSATUVALN(N, VAL) \
300     ({ \
301         fSET_OVERFLOW(); \
302         ((VAL) < 0) ? 0 : ((1LL << (N)) - 1); \
303     })
304 #define fSATVALN(N, VAL) \
305     ({ \
306         fSET_OVERFLOW(); \
307         ((VAL) < 0) ? (-(1LL << ((N) - 1))) : ((1LL << ((N) - 1)) - 1); \
308     })
309 #define fVSATVALN(N, VAL) \
310     ({ \
311         ((VAL) < 0) ? (-(1LL << ((N) - 1))) : ((1LL << ((N) - 1)) - 1); \
312     })
313 #define fZXTN(N, M, VAL) (((N) != 0) ? extract64((VAL), 0, (N)) : 0LL)
314 #define fSXTN(N, M, VAL) (((N) != 0) ? sextract64((VAL), 0, (N)) : 0LL)
315 #define fSATN(N, VAL) \
316     ((fSXTN(N, 64, VAL) == (VAL)) ? (VAL) : fSATVALN(N, VAL))
317 #define fVSATN(N, VAL) \
318     ((fSXTN(N, 64, VAL) == (VAL)) ? (VAL) : fVSATVALN(N, VAL))
319 #define fADDSAT64(DST, A, B) \
320     do { \
321         uint64_t __a = fCAST8u(A); \
322         uint64_t __b = fCAST8u(B); \
323         uint64_t __sum = __a + __b; \
324         uint64_t __xor = __a ^ __b; \
325         const uint64_t __mask = 0x8000000000000000ULL; \
326         if (__xor & __mask) { \
327             DST = __sum; \
328         } \
329         else if ((__a ^ __sum) & __mask) { \
330             if (__sum & __mask) { \
331                 DST = 0x7FFFFFFFFFFFFFFFLL; \
332                 fSET_OVERFLOW(); \
333             } else { \
334                 DST = 0x8000000000000000LL; \
335                 fSET_OVERFLOW(); \
336             } \
337         } else { \
338             DST = __sum; \
339         } \
340     } while (0)
341 #define fVSATUN(N, VAL) \
342     ((fZXTN(N, 64, VAL) == (VAL)) ? (VAL) : fVSATUVALN(N, VAL))
343 #define fSATUN(N, VAL) \
344     ((fZXTN(N, 64, VAL) == (VAL)) ? (VAL) : fSATUVALN(N, VAL))
345 #define fSATH(VAL) (fSATN(16, VAL))
346 #define fSATUH(VAL) (fSATUN(16, VAL))
347 #define fVSATH(VAL) (fVSATN(16, VAL))
348 #define fVSATUH(VAL) (fVSATUN(16, VAL))
349 #define fSATUB(VAL) (fSATUN(8, VAL))
350 #define fSATB(VAL) (fSATN(8, VAL))
351 #define fVSATUB(VAL) (fVSATUN(8, VAL))
352 #define fVSATB(VAL) (fVSATN(8, VAL))
353 #define fIMMEXT(IMM) (IMM = IMM)
354 #define fMUST_IMMEXT(IMM) fIMMEXT(IMM)
355 
356 #define fPCALIGN(IMM) IMM = (IMM & ~PCALIGN_MASK)
357 
358 #ifdef QEMU_GENERATE
359 static inline TCGv gen_read_ireg(TCGv result, TCGv val, int shift)
360 {
361     /*
362      * Section 2.2.4 of the Hexagon V67 Programmer's Reference Manual
363      *
364      *  The "I" value from a modifier register is divided into two pieces
365      *      LSB         bits 23:17
366      *      MSB         bits 31:28
367      * The value is signed
368      *
369      * At the end we shift the result according to the shift argument
370      */
371     TCGv msb = tcg_temp_new();
372     TCGv lsb = tcg_temp_new();
373 
374     tcg_gen_extract_tl(lsb, val, 17, 7);
375     tcg_gen_sari_tl(msb, val, 21);
376     tcg_gen_deposit_tl(result, msb, lsb, 0, 7);
377 
378     tcg_gen_shli_tl(result, result, shift);
379 
380     tcg_temp_free(msb);
381     tcg_temp_free(lsb);
382 
383     return result;
384 }
385 #define fREAD_IREG(VAL, SHIFT) gen_read_ireg(ireg, (VAL), (SHIFT))
386 #else
387 #define fREAD_IREG(VAL) \
388     (fSXTN(11, 64, (((VAL) & 0xf0000000) >> 21) | ((VAL >> 17) & 0x7f)))
389 #endif
390 
391 #define fREAD_LR() (READ_REG(HEX_REG_LR))
392 
393 #define fWRITE_LR(A) WRITE_RREG(HEX_REG_LR, A)
394 #define fWRITE_FP(A) WRITE_RREG(HEX_REG_FP, A)
395 #define fWRITE_SP(A) WRITE_RREG(HEX_REG_SP, A)
396 
397 #define fREAD_SP() (READ_REG(HEX_REG_SP))
398 #define fREAD_LC0 (READ_REG(HEX_REG_LC0))
399 #define fREAD_LC1 (READ_REG(HEX_REG_LC1))
400 #define fREAD_SA0 (READ_REG(HEX_REG_SA0))
401 #define fREAD_SA1 (READ_REG(HEX_REG_SA1))
402 #define fREAD_FP() (READ_REG(HEX_REG_FP))
403 #ifdef FIXME
404 /* Figure out how to get insn->extension_valid to helper */
405 #define fREAD_GP() \
406     (insn->extension_valid ? 0 : READ_REG(HEX_REG_GP))
407 #else
408 #define fREAD_GP() READ_REG(HEX_REG_GP)
409 #endif
410 #define fREAD_PC() (PC)
411 
412 #define fREAD_NPC() (next_PC & (0xfffffffe))
413 
414 #define fREAD_P0() (READ_PREG(0))
415 #define fREAD_P3() (READ_PREG(3))
416 
417 #define fCHECK_PCALIGN(A)
418 
419 #define fWRITE_NPC(A) write_new_pc(env, pkt_has_multi_cof != 0, A)
420 
421 #define fBRANCH(LOC, TYPE)          fWRITE_NPC(LOC)
422 #define fJUMPR(REGNO, TARGET, TYPE) fBRANCH(TARGET, COF_TYPE_JUMPR)
423 #define fHINTJR(TARGET) { /* Not modelled in qemu */}
424 #define fCALL(A) \
425     do { \
426         fWRITE_LR(fREAD_NPC()); \
427         fBRANCH(A, COF_TYPE_CALL); \
428     } while (0)
429 #define fCALLR(A) \
430     do { \
431         fWRITE_LR(fREAD_NPC()); \
432         fBRANCH(A, COF_TYPE_CALLR); \
433     } while (0)
434 #define fWRITE_LOOP_REGS0(START, COUNT) \
435     do { \
436         WRITE_RREG(HEX_REG_LC0, COUNT);  \
437         WRITE_RREG(HEX_REG_SA0, START); \
438     } while (0)
439 #define fWRITE_LOOP_REGS1(START, COUNT) \
440     do { \
441         WRITE_RREG(HEX_REG_LC1, COUNT);  \
442         WRITE_RREG(HEX_REG_SA1, START);\
443     } while (0)
444 #define fWRITE_LC0(VAL) WRITE_RREG(HEX_REG_LC0, VAL)
445 #define fWRITE_LC1(VAL) WRITE_RREG(HEX_REG_LC1, VAL)
446 
447 #define fSET_OVERFLOW() SET_USR_FIELD(USR_OVF, 1)
448 #define fSET_LPCFG(VAL) SET_USR_FIELD(USR_LPCFG, (VAL))
449 #define fGET_LPCFG (GET_USR_FIELD(USR_LPCFG))
450 #define fWRITE_P0(VAL) WRITE_PREG(0, VAL)
451 #define fWRITE_P1(VAL) WRITE_PREG(1, VAL)
452 #define fWRITE_P2(VAL) WRITE_PREG(2, VAL)
453 #define fWRITE_P3(VAL) WRITE_PREG(3, VAL)
454 #define fPART1(WORK) if (part1) { WORK; return; }
455 #define fCAST4u(A) ((uint32_t)(A))
456 #define fCAST4s(A) ((int32_t)(A))
457 #define fCAST8u(A) ((uint64_t)(A))
458 #define fCAST8s(A) ((int64_t)(A))
459 #define fCAST2_2s(A) ((int16_t)(A))
460 #define fCAST2_2u(A) ((uint16_t)(A))
461 #define fCAST4_4s(A) ((int32_t)(A))
462 #define fCAST4_4u(A) ((uint32_t)(A))
463 #define fCAST4_8s(A) ((int64_t)((int32_t)(A)))
464 #define fCAST4_8u(A) ((uint64_t)((uint32_t)(A)))
465 #define fCAST8_8s(A) ((int64_t)(A))
466 #define fCAST8_8u(A) ((uint64_t)(A))
467 #define fCAST2_8s(A) ((int64_t)((int16_t)(A)))
468 #define fCAST2_8u(A) ((uint64_t)((uint16_t)(A)))
469 #define fZE8_16(A) ((int16_t)((uint8_t)(A)))
470 #define fSE8_16(A) ((int16_t)((int8_t)(A)))
471 #define fSE16_32(A) ((int32_t)((int16_t)(A)))
472 #define fZE16_32(A) ((uint32_t)((uint16_t)(A)))
473 #define fSE32_64(A) ((int64_t)((int32_t)(A)))
474 #define fZE32_64(A) ((uint64_t)((uint32_t)(A)))
475 #define fSE8_32(A) ((int32_t)((int8_t)(A)))
476 #define fZE8_32(A) ((int32_t)((uint8_t)(A)))
477 #define fMPY8UU(A, B) (int)(fZE8_16(A) * fZE8_16(B))
478 #define fMPY8US(A, B) (int)(fZE8_16(A) * fSE8_16(B))
479 #define fMPY8SU(A, B) (int)(fSE8_16(A) * fZE8_16(B))
480 #define fMPY8SS(A, B) (int)((short)(A) * (short)(B))
481 #define fMPY16SS(A, B) fSE32_64(fSE16_32(A) * fSE16_32(B))
482 #define fMPY16UU(A, B) fZE32_64(fZE16_32(A) * fZE16_32(B))
483 #define fMPY16SU(A, B) fSE32_64(fSE16_32(A) * fZE16_32(B))
484 #define fMPY16US(A, B) fMPY16SU(B, A)
485 #define fMPY32SS(A, B) (fSE32_64(A) * fSE32_64(B))
486 #define fMPY32UU(A, B) (fZE32_64(A) * fZE32_64(B))
487 #define fMPY32SU(A, B) (fSE32_64(A) * fZE32_64(B))
488 #define fMPY3216SS(A, B) (fSE32_64(A) * fSXTN(16, 64, B))
489 #define fMPY3216SU(A, B) (fSE32_64(A) * fZXTN(16, 64, B))
490 #define fROUND(A) (A + 0x8000)
491 #define fCLIP(DST, SRC, U) \
492     do { \
493         int32_t maxv = (1 << U) - 1; \
494         int32_t minv = -(1 << U); \
495         DST = fMIN(maxv, fMAX(SRC, minv)); \
496     } while (0)
497 #define fCRND(A) ((((A) & 0x3) == 0x3) ? ((A) + 1) : ((A)))
498 #define fRNDN(A, N) ((((N) == 0) ? (A) : (((fSE32_64(A)) + (1 << ((N) - 1))))))
499 #define fCRNDN(A, N) (conv_round(A, N))
500 #define fADD128(A, B) (int128_add(A, B))
501 #define fSUB128(A, B) (int128_sub(A, B))
502 #define fSHIFTR128(A, B) (int128_rshift(A, B))
503 #define fSHIFTL128(A, B) (int128_lshift(A, B))
504 #define fAND128(A, B) (int128_and(A, B))
505 #define fCAST8S_16S(A) (int128_exts64(A))
506 #define fCAST16S_8S(A) (int128_getlo(A))
507 
508 #ifdef QEMU_GENERATE
509 #define fEA_RI(REG, IMM) tcg_gen_addi_tl(EA, REG, IMM)
510 #define fEA_RRs(REG, REG2, SCALE) \
511     do { \
512         TCGv tmp = tcg_temp_new(); \
513         tcg_gen_shli_tl(tmp, REG2, SCALE); \
514         tcg_gen_add_tl(EA, REG, tmp); \
515         tcg_temp_free(tmp); \
516     } while (0)
517 #define fEA_IRs(IMM, REG, SCALE) \
518     do { \
519         tcg_gen_shli_tl(EA, REG, SCALE); \
520         tcg_gen_addi_tl(EA, EA, IMM); \
521     } while (0)
522 #else
523 #define fEA_RI(REG, IMM) \
524     do { \
525         EA = REG + IMM; \
526     } while (0)
527 #define fEA_RRs(REG, REG2, SCALE) \
528     do { \
529         EA = REG + (REG2 << SCALE); \
530     } while (0)
531 #define fEA_IRs(IMM, REG, SCALE) \
532     do { \
533         EA = IMM + (REG << SCALE); \
534     } while (0)
535 #endif
536 
537 #ifdef QEMU_GENERATE
538 #define fEA_IMM(IMM) tcg_gen_movi_tl(EA, IMM)
539 #define fEA_REG(REG) tcg_gen_mov_tl(EA, REG)
540 #define fEA_BREVR(REG)      gen_helper_fbrev(EA, REG)
541 #define fPM_I(REG, IMM)     tcg_gen_addi_tl(REG, REG, IMM)
542 #define fPM_M(REG, MVAL)    tcg_gen_add_tl(REG, REG, MVAL)
543 #define fPM_CIRI(REG, IMM, MVAL) \
544     do { \
545         TCGv tcgv_siV = tcg_constant_tl(siV); \
546         gen_helper_fcircadd(REG, REG, tcgv_siV, MuV, \
547                             hex_gpr[HEX_REG_CS0 + MuN]); \
548     } while (0)
549 #else
550 #define fEA_IMM(IMM)        do { EA = (IMM); } while (0)
551 #define fEA_REG(REG)        do { EA = (REG); } while (0)
552 #define fEA_GPI(IMM)        do { EA = (fREAD_GP() + (IMM)); } while (0)
553 #define fPM_I(REG, IMM)     do { REG = REG + (IMM); } while (0)
554 #define fPM_M(REG, MVAL)    do { REG = REG + (MVAL); } while (0)
555 #endif
556 #define fSCALE(N, A) (((int64_t)(A)) << N)
557 #define fVSATW(A) fVSATN(32, ((long long)A))
558 #define fSATW(A) fSATN(32, ((long long)A))
559 #define fVSAT(A) fVSATN(32, (A))
560 #define fSAT(A) fSATN(32, (A))
561 #define fSAT_ORIG_SHL(A, ORIG_REG) \
562     ((((int32_t)((fSAT(A)) ^ ((int32_t)(ORIG_REG)))) < 0) \
563         ? fSATVALN(32, ((int32_t)(ORIG_REG))) \
564         : ((((ORIG_REG) > 0) && ((A) == 0)) ? fSATVALN(32, (ORIG_REG)) \
565                                             : fSAT(A)))
566 #define fPASS(A) A
567 #define fBIDIR_SHIFTL(SRC, SHAMT, REGSTYPE) \
568     (((SHAMT) < 0) ? ((fCAST##REGSTYPE(SRC) >> ((-(SHAMT)) - 1)) >> 1) \
569                    : (fCAST##REGSTYPE(SRC) << (SHAMT)))
570 #define fBIDIR_ASHIFTL(SRC, SHAMT, REGSTYPE) \
571     fBIDIR_SHIFTL(SRC, SHAMT, REGSTYPE##s)
572 #define fBIDIR_LSHIFTL(SRC, SHAMT, REGSTYPE) \
573     fBIDIR_SHIFTL(SRC, SHAMT, REGSTYPE##u)
574 #define fBIDIR_ASHIFTL_SAT(SRC, SHAMT, REGSTYPE) \
575     (((SHAMT) < 0) ? ((fCAST##REGSTYPE##s(SRC) >> ((-(SHAMT)) - 1)) >> 1) \
576                    : fSAT_ORIG_SHL(fCAST##REGSTYPE##s(SRC) << (SHAMT), (SRC)))
577 #define fBIDIR_SHIFTR(SRC, SHAMT, REGSTYPE) \
578     (((SHAMT) < 0) ? ((fCAST##REGSTYPE(SRC) << ((-(SHAMT)) - 1)) << 1) \
579                    : (fCAST##REGSTYPE(SRC) >> (SHAMT)))
580 #define fBIDIR_ASHIFTR(SRC, SHAMT, REGSTYPE) \
581     fBIDIR_SHIFTR(SRC, SHAMT, REGSTYPE##s)
582 #define fBIDIR_LSHIFTR(SRC, SHAMT, REGSTYPE) \
583     fBIDIR_SHIFTR(SRC, SHAMT, REGSTYPE##u)
584 #define fBIDIR_ASHIFTR_SAT(SRC, SHAMT, REGSTYPE) \
585     (((SHAMT) < 0) ? fSAT_ORIG_SHL((fCAST##REGSTYPE##s(SRC) \
586                         << ((-(SHAMT)) - 1)) << 1, (SRC)) \
587                    : (fCAST##REGSTYPE##s(SRC) >> (SHAMT)))
588 #define fASHIFTR(SRC, SHAMT, REGSTYPE) (fCAST##REGSTYPE##s(SRC) >> (SHAMT))
589 #define fLSHIFTR(SRC, SHAMT, REGSTYPE) \
590     (((SHAMT) >= (sizeof(SRC) * 8)) ? 0 : (fCAST##REGSTYPE##u(SRC) >> (SHAMT)))
591 #define fROTL(SRC, SHAMT, REGSTYPE) \
592     (((SHAMT) == 0) ? (SRC) : ((fCAST##REGSTYPE##u(SRC) << (SHAMT)) | \
593                               ((fCAST##REGSTYPE##u(SRC) >> \
594                                  ((sizeof(SRC) * 8) - (SHAMT))))))
595 #define fROTR(SRC, SHAMT, REGSTYPE) \
596     (((SHAMT) == 0) ? (SRC) : ((fCAST##REGSTYPE##u(SRC) >> (SHAMT)) | \
597                               ((fCAST##REGSTYPE##u(SRC) << \
598                                  ((sizeof(SRC) * 8) - (SHAMT))))))
599 #define fASHIFTL(SRC, SHAMT, REGSTYPE) \
600     (((SHAMT) >= (sizeof(SRC) * 8)) ? 0 : (fCAST##REGSTYPE##s(SRC) << (SHAMT)))
601 
602 #ifdef QEMU_GENERATE
603 #define fLOAD(NUM, SIZE, SIGN, EA, DST) MEM_LOAD##SIZE##SIGN(DST, EA)
604 #else
605 #define fLOAD(NUM, SIZE, SIGN, EA, DST) \
606     DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE##SIGN(EA)
607 #endif
608 
609 #define fMEMOP(NUM, SIZE, SIGN, EA, FNTYPE, VALUE)
610 
611 #define fGET_FRAMEKEY() READ_REG(HEX_REG_FRAMEKEY)
612 #define fFRAME_SCRAMBLE(VAL) ((VAL) ^ (fCAST8u(fGET_FRAMEKEY()) << 32))
613 #define fFRAME_UNSCRAMBLE(VAL) fFRAME_SCRAMBLE(VAL)
614 
615 #ifdef CONFIG_USER_ONLY
616 #define fFRAMECHECK(ADDR, EA) do { } while (0) /* Not modelled in linux-user */
617 #else
618 /* System mode not implemented yet */
619 #define fFRAMECHECK(ADDR, EA)  g_assert_not_reached();
620 #endif
621 
622 #ifdef QEMU_GENERATE
623 #define fLOAD_LOCKED(NUM, SIZE, SIGN, EA, DST) \
624     gen_load_locked##SIZE##SIGN(DST, EA, ctx->mem_idx);
625 #endif
626 
627 #ifdef QEMU_GENERATE
628 #define fSTORE(NUM, SIZE, EA, SRC) MEM_STORE##SIZE(EA, SRC, insn->slot)
629 #else
630 #define fSTORE(NUM, SIZE, EA, SRC) MEM_STORE##SIZE(EA, SRC, slot)
631 #endif
632 
633 #ifdef QEMU_GENERATE
634 #define fSTORE_LOCKED(NUM, SIZE, EA, SRC, PRED) \
635     gen_store_conditional##SIZE(ctx, PRED, EA, SRC);
636 #endif
637 
638 #ifdef QEMU_GENERATE
639 #define GETBYTE_FUNC(X) \
640     __builtin_choose_expr(TYPE_TCGV(X), \
641         gen_get_byte, \
642         __builtin_choose_expr(TYPE_TCGV_I64(X), \
643             gen_get_byte_i64, (void)0))
644 #define fGETBYTE(N, SRC) GETBYTE_FUNC(SRC)(BYTE, N, SRC, true)
645 #define fGETUBYTE(N, SRC) GETBYTE_FUNC(SRC)(BYTE, N, SRC, false)
646 #else
647 #define fGETBYTE(N, SRC) ((int8_t)((SRC >> ((N) * 8)) & 0xff))
648 #define fGETUBYTE(N, SRC) ((uint8_t)((SRC >> ((N) * 8)) & 0xff))
649 #endif
650 
651 #define fSETBYTE(N, DST, VAL) \
652     do { \
653         DST = (DST & ~(0x0ffLL << ((N) * 8))) | \
654         (((uint64_t)((VAL) & 0x0ffLL)) << ((N) * 8)); \
655     } while (0)
656 
657 #ifdef QEMU_GENERATE
658 #define fGETHALF(N, SRC)  gen_get_half(HALF, N, SRC, true)
659 #define fGETUHALF(N, SRC) gen_get_half(HALF, N, SRC, false)
660 #else
661 #define fGETHALF(N, SRC) ((int16_t)((SRC >> ((N) * 16)) & 0xffff))
662 #define fGETUHALF(N, SRC) ((uint16_t)((SRC >> ((N) * 16)) & 0xffff))
663 #endif
664 #define fSETHALF(N, DST, VAL) \
665     do { \
666         DST = (DST & ~(0x0ffffLL << ((N) * 16))) | \
667         (((uint64_t)((VAL) & 0x0ffff)) << ((N) * 16)); \
668     } while (0)
669 #define fSETHALFw fSETHALF
670 #define fSETHALFd fSETHALF
671 
672 #define fGETWORD(N, SRC) \
673     ((int64_t)((int32_t)((SRC >> ((N) * 32)) & 0x0ffffffffLL)))
674 #define fGETUWORD(N, SRC) \
675     ((uint64_t)((uint32_t)((SRC >> ((N) * 32)) & 0x0ffffffffLL)))
676 
677 #define fSETWORD(N, DST, VAL) \
678     do { \
679         DST = (DST & ~(0x0ffffffffLL << ((N) * 32))) | \
680               (((VAL) & 0x0ffffffffLL) << ((N) * 32)); \
681     } while (0)
682 
683 #define fSETBIT(N, DST, VAL) \
684     do { \
685         DST = (DST & ~(1ULL << (N))) | (((uint64_t)(VAL)) << (N)); \
686     } while (0)
687 
688 #define fGETBIT(N, SRC) (((SRC) >> N) & 1)
689 #define fSETBITS(HI, LO, DST, VAL) \
690     do { \
691         int j; \
692         for (j = LO; j <= HI; j++) { \
693             fSETBIT(j, DST, VAL); \
694         } \
695     } while (0)
696 #define fCOUNTONES_2(VAL) ctpop16(VAL)
697 #define fCOUNTONES_4(VAL) ctpop32(VAL)
698 #define fCOUNTONES_8(VAL) ctpop64(VAL)
699 #define fBREV_8(VAL) revbit64(VAL)
700 #define fBREV_4(VAL) revbit32(VAL)
701 #define fCL1_8(VAL) clo64(VAL)
702 #define fCL1_4(VAL) clo32(VAL)
703 #define fCL1_2(VAL) (clz32(~(uint16_t)(VAL) & 0xffff) - 16)
704 #define fINTERLEAVE(ODD, EVEN) interleave(ODD, EVEN)
705 #define fDEINTERLEAVE(MIXED) deinterleave(MIXED)
706 #define fHIDE(A) A
707 #define fCONSTLL(A) A##LL
708 #define fECHO(A) (A)
709 
710 #define fTRAP(TRAPTYPE, IMM) helper_raise_exception(env, HEX_EXCP_TRAP0)
711 #define fPAUSE(IMM)
712 
713 #define fALIGN_REG_FIELD_VALUE(FIELD, VAL) \
714     ((VAL) << reg_field_info[FIELD].offset)
715 #define fGET_REG_FIELD_MASK(FIELD) \
716     (((1 << reg_field_info[FIELD].width) - 1) << reg_field_info[FIELD].offset)
717 #define fREAD_REG_FIELD(REG, FIELD) \
718     fEXTRACTU_BITS(env->gpr[HEX_REG_##REG], \
719                    reg_field_info[FIELD].width, \
720                    reg_field_info[FIELD].offset)
721 #define fGET_FIELD(VAL, FIELD)
722 #define fSET_FIELD(VAL, FIELD, NEWVAL)
723 #define fBARRIER()
724 #define fSYNCH()
725 #define fISYNC()
726 #define fDCFETCH(REG) \
727     do { (void)REG; } while (0) /* Nothing to do in qemu */
728 #define fICINVA(REG) \
729     do { (void)REG; } while (0) /* Nothing to do in qemu */
730 #define fL2FETCH(ADDR, HEIGHT, WIDTH, STRIDE, FLAGS)
731 #define fDCCLEANA(REG) \
732     do { (void)REG; } while (0) /* Nothing to do in qemu */
733 #define fDCCLEANINVA(REG) \
734     do { (void)REG; } while (0) /* Nothing to do in qemu */
735 
736 #define fDCZEROA(REG) do { env->dczero_addr = (REG); } while (0)
737 
738 #define fBRANCH_SPECULATE_STALL(DOTNEWVAL, JUMP_COND, SPEC_DIR, HINTBITNUM, \
739                                 STRBITNUM) /* Nothing */
740 
741 
742 #endif
743