162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * arch/sparc/math-emu/math.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 1998 Peter Maydell (pmaydell@chiark.greenend.org.uk)
662306a36Sopenharmony_ci * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
762306a36Sopenharmony_ci * Copyright (C) 1999 David S. Miller (davem@redhat.com)
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This is a good place to start if you're trying to understand the
1062306a36Sopenharmony_ci * emulation code, because it's pretty simple. What we do is
1162306a36Sopenharmony_ci * essentially analyse the instruction to work out what the operation
1262306a36Sopenharmony_ci * is and which registers are involved. We then execute the appropriate
1362306a36Sopenharmony_ci * FXXXX function. [The floating point queue introduces a minor wrinkle;
1462306a36Sopenharmony_ci * see below...]
1562306a36Sopenharmony_ci * The fxxxxx.c files each emulate a single insn. They look relatively
1662306a36Sopenharmony_ci * simple because the complexity is hidden away in an unholy tangle
1762306a36Sopenharmony_ci * of preprocessor macros.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * The first layer of macros is single.h, double.h, quad.h. Generally
2062306a36Sopenharmony_ci * these files define macros for working with floating point numbers
2162306a36Sopenharmony_ci * of the three IEEE formats. FP_ADD_D(R,A,B) is for adding doubles,
2262306a36Sopenharmony_ci * for instance. These macros are usually defined as calls to more
2362306a36Sopenharmony_ci * generic macros (in this case _FP_ADD(D,2,R,X,Y) where the number
2462306a36Sopenharmony_ci * of machine words required to store the given IEEE format is passed
2562306a36Sopenharmony_ci * as a parameter. [double.h and co check the number of bits in a word
2662306a36Sopenharmony_ci * and define FP_ADD_D & co appropriately].
2762306a36Sopenharmony_ci * The generic macros are defined in op-common.h. This is where all
2862306a36Sopenharmony_ci * the grotty stuff like handling NaNs is coded. To handle the possible
2962306a36Sopenharmony_ci * word sizes macros in op-common.h use macros like _FP_FRAC_SLL_##wc()
3062306a36Sopenharmony_ci * where wc is the 'number of machine words' parameter (here 2).
3162306a36Sopenharmony_ci * These are defined in the third layer of macros: op-1.h, op-2.h
3262306a36Sopenharmony_ci * and op-4.h. These handle operations on floating point numbers composed
3362306a36Sopenharmony_ci * of 1,2 and 4 machine words respectively. [For example, on sparc64
3462306a36Sopenharmony_ci * doubles are one machine word so macros in double.h eventually use
3562306a36Sopenharmony_ci * constructs in op-1.h, but on sparc32 they use op-2.h definitions.]
3662306a36Sopenharmony_ci * soft-fp.h is on the same level as op-common.h, and defines some
3762306a36Sopenharmony_ci * macros which are independent of both word size and FP format.
3862306a36Sopenharmony_ci * Finally, sfp-machine.h is the machine dependent part of the
3962306a36Sopenharmony_ci * code: it defines the word size and what type a word is. It also
4062306a36Sopenharmony_ci * defines how _FP_MUL_MEAT_t() maps to _FP_MUL_MEAT_n_* : op-n.h
4162306a36Sopenharmony_ci * provide several possible flavours of multiply algorithm, most
4262306a36Sopenharmony_ci * of which require that you supply some form of asm or C primitive to
4362306a36Sopenharmony_ci * do the actual multiply. (such asm primitives should be defined
4462306a36Sopenharmony_ci * in sfp-machine.h too). udivmodti4.c is the same sort of thing.
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * There may be some errors here because I'm working from a
4762306a36Sopenharmony_ci * SPARC architecture manual V9, and what I really want is V8...
4862306a36Sopenharmony_ci * Also, the insns which can generate exceptions seem to be a
4962306a36Sopenharmony_ci * greater subset of the FPops than for V9 (for example, FCMPED
5062306a36Sopenharmony_ci * has to be emulated on V8). So I think I'm going to have
5162306a36Sopenharmony_ci * to emulate them all just to be on the safe side...
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * Emulation routines originate from soft-fp package, which is
5462306a36Sopenharmony_ci * part of glibc and has appropriate copyrights in it (allegedly).
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci * NB: on sparc int == long == 4 bytes, long long == 8 bytes.
5762306a36Sopenharmony_ci * Most bits of the kernel seem to go for long rather than int,
5862306a36Sopenharmony_ci * so we follow that practice...
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/* TODO:
6262306a36Sopenharmony_ci * fpsave() saves the FP queue but fpload() doesn't reload it.
6362306a36Sopenharmony_ci * Therefore when we context switch or change FPU ownership
6462306a36Sopenharmony_ci * we have to check to see if the queue had anything in it and
6562306a36Sopenharmony_ci * emulate it if it did. This is going to be a pain.
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#include <linux/types.h>
6962306a36Sopenharmony_ci#include <linux/sched.h>
7062306a36Sopenharmony_ci#include <linux/mm.h>
7162306a36Sopenharmony_ci#include <linux/perf_event.h>
7262306a36Sopenharmony_ci#include <linux/uaccess.h>
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#include "sfp-util_32.h"
7562306a36Sopenharmony_ci#include <math-emu/soft-fp.h>
7662306a36Sopenharmony_ci#include <math-emu/single.h>
7762306a36Sopenharmony_ci#include <math-emu/double.h>
7862306a36Sopenharmony_ci#include <math-emu/quad.h>
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#define FLOATFUNC(x) extern int x(void *,void *,void *)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/* The Vn labels indicate what version of the SPARC architecture gas thinks
8362306a36Sopenharmony_ci * each insn is. This is from the binutils source :->
8462306a36Sopenharmony_ci */
8562306a36Sopenharmony_ci/* quadword instructions */
8662306a36Sopenharmony_ci#define FSQRTQ	0x02b		/* v8 */
8762306a36Sopenharmony_ci#define FADDQ	0x043		/* v8 */
8862306a36Sopenharmony_ci#define FSUBQ	0x047		/* v8 */
8962306a36Sopenharmony_ci#define FMULQ	0x04b		/* v8 */
9062306a36Sopenharmony_ci#define FDIVQ	0x04f		/* v8 */
9162306a36Sopenharmony_ci#define FDMULQ	0x06e		/* v8 */
9262306a36Sopenharmony_ci#define FQTOS	0x0c7		/* v8 */
9362306a36Sopenharmony_ci#define FQTOD	0x0cb		/* v8 */
9462306a36Sopenharmony_ci#define FITOQ	0x0cc		/* v8 */
9562306a36Sopenharmony_ci#define FSTOQ	0x0cd		/* v8 */
9662306a36Sopenharmony_ci#define FDTOQ	0x0ce		/* v8 */
9762306a36Sopenharmony_ci#define FQTOI	0x0d3		/* v8 */
9862306a36Sopenharmony_ci#define FCMPQ	0x053		/* v8 */
9962306a36Sopenharmony_ci#define FCMPEQ	0x057		/* v8 */
10062306a36Sopenharmony_ci/* single/double instructions (subnormal): should all work */
10162306a36Sopenharmony_ci#define FSQRTS	0x029		/* v7 */
10262306a36Sopenharmony_ci#define FSQRTD	0x02a		/* v7 */
10362306a36Sopenharmony_ci#define FADDS	0x041		/* v6 */
10462306a36Sopenharmony_ci#define FADDD	0x042		/* v6 */
10562306a36Sopenharmony_ci#define FSUBS	0x045		/* v6 */
10662306a36Sopenharmony_ci#define FSUBD	0x046		/* v6 */
10762306a36Sopenharmony_ci#define FMULS	0x049		/* v6 */
10862306a36Sopenharmony_ci#define FMULD	0x04a		/* v6 */
10962306a36Sopenharmony_ci#define FDIVS	0x04d		/* v6 */
11062306a36Sopenharmony_ci#define FDIVD	0x04e		/* v6 */
11162306a36Sopenharmony_ci#define FSMULD	0x069		/* v6 */
11262306a36Sopenharmony_ci#define FDTOS	0x0c6		/* v6 */
11362306a36Sopenharmony_ci#define FSTOD	0x0c9		/* v6 */
11462306a36Sopenharmony_ci#define FSTOI	0x0d1		/* v6 */
11562306a36Sopenharmony_ci#define FDTOI	0x0d2		/* v6 */
11662306a36Sopenharmony_ci#define FABSS	0x009		/* v6 */
11762306a36Sopenharmony_ci#define FCMPS	0x051		/* v6 */
11862306a36Sopenharmony_ci#define FCMPES	0x055		/* v6 */
11962306a36Sopenharmony_ci#define FCMPD	0x052		/* v6 */
12062306a36Sopenharmony_ci#define FCMPED	0x056		/* v6 */
12162306a36Sopenharmony_ci#define FMOVS	0x001		/* v6 */
12262306a36Sopenharmony_ci#define FNEGS	0x005		/* v6 */
12362306a36Sopenharmony_ci#define FITOS	0x0c4		/* v6 */
12462306a36Sopenharmony_ci#define FITOD	0x0c8		/* v6 */
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci#define FSR_TEM_SHIFT	23UL
12762306a36Sopenharmony_ci#define FSR_TEM_MASK	(0x1fUL << FSR_TEM_SHIFT)
12862306a36Sopenharmony_ci#define FSR_AEXC_SHIFT	5UL
12962306a36Sopenharmony_ci#define FSR_AEXC_MASK	(0x1fUL << FSR_AEXC_SHIFT)
13062306a36Sopenharmony_ci#define FSR_CEXC_SHIFT	0UL
13162306a36Sopenharmony_ci#define FSR_CEXC_MASK	(0x1fUL << FSR_CEXC_SHIFT)
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic int do_one_mathemu(u32 insn, unsigned long *fsr, unsigned long *fregs);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci/* Unlike the Sparc64 version (which has a struct fpustate), we
13662306a36Sopenharmony_ci * pass the taskstruct corresponding to the task which currently owns the
13762306a36Sopenharmony_ci * FPU. This is partly because we don't have the fpustate struct and
13862306a36Sopenharmony_ci * partly because the task owning the FPU isn't always current (as is
13962306a36Sopenharmony_ci * the case for the Sparc64 port). This is probably SMP-related...
14062306a36Sopenharmony_ci * This function returns 1 if all queued insns were emulated successfully.
14162306a36Sopenharmony_ci * The test for unimplemented FPop in kernel mode has been moved into
14262306a36Sopenharmony_ci * kernel/traps.c for simplicity.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_ciint do_mathemu(struct pt_regs *regs, struct task_struct *fpt)
14562306a36Sopenharmony_ci{
14662306a36Sopenharmony_ci	/* regs->pc isn't necessarily the PC at which the offending insn is sitting.
14762306a36Sopenharmony_ci	 * The FPU maintains a queue of FPops which cause traps.
14862306a36Sopenharmony_ci	 * When it hits an instruction that requires that the trapped op succeeded
14962306a36Sopenharmony_ci	 * (usually because it reads a reg. that the trapped op wrote) then it
15062306a36Sopenharmony_ci	 * causes this exception. We need to emulate all the insns on the queue
15162306a36Sopenharmony_ci	 * and then allow the op to proceed.
15262306a36Sopenharmony_ci	 * This code should also handle the case where the trap was precise,
15362306a36Sopenharmony_ci	 * in which case the queue length is zero and regs->pc points at the
15462306a36Sopenharmony_ci	 * single FPop to be emulated. (this case is untested, though :->)
15562306a36Sopenharmony_ci	 * You'll need this case if you want to be able to emulate all FPops
15662306a36Sopenharmony_ci	 * because the FPU either doesn't exist or has been software-disabled.
15762306a36Sopenharmony_ci	 * [The UltraSPARC makes FP a precise trap; this isn't as stupid as it
15862306a36Sopenharmony_ci	 * might sound because the Ultra does funky things with a superscalar
15962306a36Sopenharmony_ci	 * architecture.]
16062306a36Sopenharmony_ci	 */
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	/* You wouldn't believe how often I typed 'ftp' when I meant 'fpt' :-> */
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	int i;
16562306a36Sopenharmony_ci	int retcode = 0;                               /* assume all succeed */
16662306a36Sopenharmony_ci	unsigned long insn;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci#ifdef DEBUG_MATHEMU
17162306a36Sopenharmony_ci	printk("In do_mathemu()... pc is %08lx\n", regs->pc);
17262306a36Sopenharmony_ci	printk("fpqdepth is %ld\n", fpt->thread.fpqdepth);
17362306a36Sopenharmony_ci	for (i = 0; i < fpt->thread.fpqdepth; i++)
17462306a36Sopenharmony_ci		printk("%d: %08lx at %08lx\n", i, fpt->thread.fpqueue[i].insn,
17562306a36Sopenharmony_ci		       (unsigned long)fpt->thread.fpqueue[i].insn_addr);
17662306a36Sopenharmony_ci#endif
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	if (fpt->thread.fpqdepth == 0) {                   /* no queue, guilty insn is at regs->pc */
17962306a36Sopenharmony_ci#ifdef DEBUG_MATHEMU
18062306a36Sopenharmony_ci		printk("precise trap at %08lx\n", regs->pc);
18162306a36Sopenharmony_ci#endif
18262306a36Sopenharmony_ci		if (!get_user(insn, (u32 __user *) regs->pc)) {
18362306a36Sopenharmony_ci			retcode = do_one_mathemu(insn, &fpt->thread.fsr, fpt->thread.float_regs);
18462306a36Sopenharmony_ci			if (retcode) {
18562306a36Sopenharmony_ci				/* in this case we need to fix up PC & nPC */
18662306a36Sopenharmony_ci				regs->pc = regs->npc;
18762306a36Sopenharmony_ci				regs->npc += 4;
18862306a36Sopenharmony_ci			}
18962306a36Sopenharmony_ci		}
19062306a36Sopenharmony_ci		return retcode;
19162306a36Sopenharmony_ci	}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	/* Normal case: need to empty the queue... */
19462306a36Sopenharmony_ci	for (i = 0; i < fpt->thread.fpqdepth; i++) {
19562306a36Sopenharmony_ci		retcode = do_one_mathemu(fpt->thread.fpqueue[i].insn, &(fpt->thread.fsr), fpt->thread.float_regs);
19662306a36Sopenharmony_ci		if (!retcode)                               /* insn failed, no point doing any more */
19762306a36Sopenharmony_ci			break;
19862306a36Sopenharmony_ci	}
19962306a36Sopenharmony_ci	/* Now empty the queue and clear the queue_not_empty flag */
20062306a36Sopenharmony_ci	if (retcode)
20162306a36Sopenharmony_ci		fpt->thread.fsr &= ~(0x3000 | FSR_CEXC_MASK);
20262306a36Sopenharmony_ci	else
20362306a36Sopenharmony_ci		fpt->thread.fsr &= ~0x3000;
20462306a36Sopenharmony_ci	fpt->thread.fpqdepth = 0;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	return retcode;
20762306a36Sopenharmony_ci}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci/* All routines returning an exception to raise should detect
21062306a36Sopenharmony_ci * such exceptions _before_ rounding to be consistent with
21162306a36Sopenharmony_ci * the behavior of the hardware in the implemented cases
21262306a36Sopenharmony_ci * (and thus with the recommendations in the V9 architecture
21362306a36Sopenharmony_ci * manual).
21462306a36Sopenharmony_ci *
21562306a36Sopenharmony_ci * We return 0 if a SIGFPE should be sent, 1 otherwise.
21662306a36Sopenharmony_ci */
21762306a36Sopenharmony_cistatic inline int record_exception(unsigned long *pfsr, int eflag)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	unsigned long fsr = *pfsr;
22062306a36Sopenharmony_ci	int would_trap;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	/* Determine if this exception would have generated a trap. */
22362306a36Sopenharmony_ci	would_trap = (fsr & ((long)eflag << FSR_TEM_SHIFT)) != 0UL;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* If trapping, we only want to signal one bit. */
22662306a36Sopenharmony_ci	if (would_trap != 0) {
22762306a36Sopenharmony_ci		eflag &= ((fsr & FSR_TEM_MASK) >> FSR_TEM_SHIFT);
22862306a36Sopenharmony_ci		if ((eflag & (eflag - 1)) != 0) {
22962306a36Sopenharmony_ci			if (eflag & FP_EX_INVALID)
23062306a36Sopenharmony_ci				eflag = FP_EX_INVALID;
23162306a36Sopenharmony_ci			else if (eflag & FP_EX_OVERFLOW)
23262306a36Sopenharmony_ci				eflag = FP_EX_OVERFLOW;
23362306a36Sopenharmony_ci			else if (eflag & FP_EX_UNDERFLOW)
23462306a36Sopenharmony_ci				eflag = FP_EX_UNDERFLOW;
23562306a36Sopenharmony_ci			else if (eflag & FP_EX_DIVZERO)
23662306a36Sopenharmony_ci				eflag = FP_EX_DIVZERO;
23762306a36Sopenharmony_ci			else if (eflag & FP_EX_INEXACT)
23862306a36Sopenharmony_ci				eflag = FP_EX_INEXACT;
23962306a36Sopenharmony_ci		}
24062306a36Sopenharmony_ci	}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	/* Set CEXC, here is the rule:
24362306a36Sopenharmony_ci	 *
24462306a36Sopenharmony_ci	 *    In general all FPU ops will set one and only one
24562306a36Sopenharmony_ci	 *    bit in the CEXC field, this is always the case
24662306a36Sopenharmony_ci	 *    when the IEEE exception trap is enabled in TEM.
24762306a36Sopenharmony_ci	 */
24862306a36Sopenharmony_ci	fsr &= ~(FSR_CEXC_MASK);
24962306a36Sopenharmony_ci	fsr |= ((long)eflag << FSR_CEXC_SHIFT);
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/* Set the AEXC field, rule is:
25262306a36Sopenharmony_ci	 *
25362306a36Sopenharmony_ci	 *    If a trap would not be generated, the
25462306a36Sopenharmony_ci	 *    CEXC just generated is OR'd into the
25562306a36Sopenharmony_ci	 *    existing value of AEXC.
25662306a36Sopenharmony_ci	 */
25762306a36Sopenharmony_ci	if (would_trap == 0)
25862306a36Sopenharmony_ci		fsr |= ((long)eflag << FSR_AEXC_SHIFT);
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	/* If trapping, indicate fault trap type IEEE. */
26162306a36Sopenharmony_ci	if (would_trap != 0)
26262306a36Sopenharmony_ci		fsr |= (1UL << 14);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	*pfsr = fsr;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	return (would_trap ? 0 : 1);
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_citypedef union {
27062306a36Sopenharmony_ci	u32 s;
27162306a36Sopenharmony_ci	u64 d;
27262306a36Sopenharmony_ci	u64 q[2];
27362306a36Sopenharmony_ci} *argp;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_cistatic int do_one_mathemu(u32 insn, unsigned long *pfsr, unsigned long *fregs)
27662306a36Sopenharmony_ci{
27762306a36Sopenharmony_ci	/* Emulate the given insn, updating fsr and fregs appropriately. */
27862306a36Sopenharmony_ci	int type = 0;
27962306a36Sopenharmony_ci	/* r is rd, b is rs2 and a is rs1. The *u arg tells
28062306a36Sopenharmony_ci	   whether the argument should be packed/unpacked (0 - do not unpack/pack, 1 - unpack/pack)
28162306a36Sopenharmony_ci	   non-u args tells the size of the argument (0 - no argument, 1 - single, 2 - double, 3 - quad */
28262306a36Sopenharmony_ci#define TYPE(dummy, r, ru, b, bu, a, au) type = (au << 2) | (a << 0) | (bu << 5) | (b << 3) | (ru << 8) | (r << 6)
28362306a36Sopenharmony_ci	int freg;
28462306a36Sopenharmony_ci	argp rs1 = NULL, rs2 = NULL, rd = NULL;
28562306a36Sopenharmony_ci	FP_DECL_EX;
28662306a36Sopenharmony_ci	FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
28762306a36Sopenharmony_ci	FP_DECL_D(DA); FP_DECL_D(DB); FP_DECL_D(DR);
28862306a36Sopenharmony_ci	FP_DECL_Q(QA); FP_DECL_Q(QB); FP_DECL_Q(QR);
28962306a36Sopenharmony_ci	int IR;
29062306a36Sopenharmony_ci	long fsr;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci#ifdef DEBUG_MATHEMU
29362306a36Sopenharmony_ci	printk("In do_mathemu(), emulating %08lx\n", insn);
29462306a36Sopenharmony_ci#endif
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	if ((insn & 0xc1f80000) == 0x81a00000)	/* FPOP1 */ {
29762306a36Sopenharmony_ci		switch ((insn >> 5) & 0x1ff) {
29862306a36Sopenharmony_ci		case FSQRTQ: TYPE(3,3,1,3,1,0,0); break;
29962306a36Sopenharmony_ci		case FADDQ:
30062306a36Sopenharmony_ci		case FSUBQ:
30162306a36Sopenharmony_ci		case FMULQ:
30262306a36Sopenharmony_ci		case FDIVQ: TYPE(3,3,1,3,1,3,1); break;
30362306a36Sopenharmony_ci		case FDMULQ: TYPE(3,3,1,2,1,2,1); break;
30462306a36Sopenharmony_ci		case FQTOS: TYPE(3,1,1,3,1,0,0); break;
30562306a36Sopenharmony_ci		case FQTOD: TYPE(3,2,1,3,1,0,0); break;
30662306a36Sopenharmony_ci		case FITOQ: TYPE(3,3,1,1,0,0,0); break;
30762306a36Sopenharmony_ci		case FSTOQ: TYPE(3,3,1,1,1,0,0); break;
30862306a36Sopenharmony_ci		case FDTOQ: TYPE(3,3,1,2,1,0,0); break;
30962306a36Sopenharmony_ci		case FQTOI: TYPE(3,1,0,3,1,0,0); break;
31062306a36Sopenharmony_ci		case FSQRTS: TYPE(2,1,1,1,1,0,0); break;
31162306a36Sopenharmony_ci		case FSQRTD: TYPE(2,2,1,2,1,0,0); break;
31262306a36Sopenharmony_ci		case FADDD:
31362306a36Sopenharmony_ci		case FSUBD:
31462306a36Sopenharmony_ci		case FMULD:
31562306a36Sopenharmony_ci		case FDIVD: TYPE(2,2,1,2,1,2,1); break;
31662306a36Sopenharmony_ci		case FADDS:
31762306a36Sopenharmony_ci		case FSUBS:
31862306a36Sopenharmony_ci		case FMULS:
31962306a36Sopenharmony_ci		case FDIVS: TYPE(2,1,1,1,1,1,1); break;
32062306a36Sopenharmony_ci		case FSMULD: TYPE(2,2,1,1,1,1,1); break;
32162306a36Sopenharmony_ci		case FDTOS: TYPE(2,1,1,2,1,0,0); break;
32262306a36Sopenharmony_ci		case FSTOD: TYPE(2,2,1,1,1,0,0); break;
32362306a36Sopenharmony_ci		case FSTOI: TYPE(2,1,0,1,1,0,0); break;
32462306a36Sopenharmony_ci		case FDTOI: TYPE(2,1,0,2,1,0,0); break;
32562306a36Sopenharmony_ci		case FITOS: TYPE(2,1,1,1,0,0,0); break;
32662306a36Sopenharmony_ci		case FITOD: TYPE(2,2,1,1,0,0,0); break;
32762306a36Sopenharmony_ci		case FMOVS:
32862306a36Sopenharmony_ci		case FABSS:
32962306a36Sopenharmony_ci		case FNEGS: TYPE(2,1,0,1,0,0,0); break;
33062306a36Sopenharmony_ci		}
33162306a36Sopenharmony_ci	} else if ((insn & 0xc1f80000) == 0x81a80000)	/* FPOP2 */ {
33262306a36Sopenharmony_ci		switch ((insn >> 5) & 0x1ff) {
33362306a36Sopenharmony_ci		case FCMPS: TYPE(3,0,0,1,1,1,1); break;
33462306a36Sopenharmony_ci		case FCMPES: TYPE(3,0,0,1,1,1,1); break;
33562306a36Sopenharmony_ci		case FCMPD: TYPE(3,0,0,2,1,2,1); break;
33662306a36Sopenharmony_ci		case FCMPED: TYPE(3,0,0,2,1,2,1); break;
33762306a36Sopenharmony_ci		case FCMPQ: TYPE(3,0,0,3,1,3,1); break;
33862306a36Sopenharmony_ci		case FCMPEQ: TYPE(3,0,0,3,1,3,1); break;
33962306a36Sopenharmony_ci		}
34062306a36Sopenharmony_ci	}
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci	if (!type) {	/* oops, didn't recognise that FPop */
34362306a36Sopenharmony_ci#ifdef DEBUG_MATHEMU
34462306a36Sopenharmony_ci		printk("attempt to emulate unrecognised FPop!\n");
34562306a36Sopenharmony_ci#endif
34662306a36Sopenharmony_ci		return 0;
34762306a36Sopenharmony_ci	}
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	/* Decode the registers to be used */
35062306a36Sopenharmony_ci	freg = (*pfsr >> 14) & 0xf;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	*pfsr &= ~0x1c000;				/* clear the traptype bits */
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	freg = ((insn >> 14) & 0x1f);
35562306a36Sopenharmony_ci	switch (type & 0x3) {				/* is rs1 single, double or quad? */
35662306a36Sopenharmony_ci	case 3:
35762306a36Sopenharmony_ci		if (freg & 3) {				/* quadwords must have bits 4&5 of the */
35862306a36Sopenharmony_ci							/* encoded reg. number set to zero. */
35962306a36Sopenharmony_ci			*pfsr |= (6 << 14);
36062306a36Sopenharmony_ci			return 0;			/* simulate invalid_fp_register exception */
36162306a36Sopenharmony_ci		}
36262306a36Sopenharmony_ci		fallthrough;
36362306a36Sopenharmony_ci	case 2:
36462306a36Sopenharmony_ci		if (freg & 1) {				/* doublewords must have bit 5 zeroed */
36562306a36Sopenharmony_ci			*pfsr |= (6 << 14);
36662306a36Sopenharmony_ci			return 0;
36762306a36Sopenharmony_ci		}
36862306a36Sopenharmony_ci	}
36962306a36Sopenharmony_ci	rs1 = (argp)&fregs[freg];
37062306a36Sopenharmony_ci	switch (type & 0x7) {
37162306a36Sopenharmony_ci	case 7: FP_UNPACK_QP (QA, rs1); break;
37262306a36Sopenharmony_ci	case 6: FP_UNPACK_DP (DA, rs1); break;
37362306a36Sopenharmony_ci	case 5: FP_UNPACK_SP (SA, rs1); break;
37462306a36Sopenharmony_ci	}
37562306a36Sopenharmony_ci	freg = (insn & 0x1f);
37662306a36Sopenharmony_ci	switch ((type >> 3) & 0x3) {			/* same again for rs2 */
37762306a36Sopenharmony_ci	case 3:
37862306a36Sopenharmony_ci		if (freg & 3) {				/* quadwords must have bits 4&5 of the */
37962306a36Sopenharmony_ci							/* encoded reg. number set to zero. */
38062306a36Sopenharmony_ci			*pfsr |= (6 << 14);
38162306a36Sopenharmony_ci			return 0;			/* simulate invalid_fp_register exception */
38262306a36Sopenharmony_ci		}
38362306a36Sopenharmony_ci		fallthrough;
38462306a36Sopenharmony_ci	case 2:
38562306a36Sopenharmony_ci		if (freg & 1) {				/* doublewords must have bit 5 zeroed */
38662306a36Sopenharmony_ci			*pfsr |= (6 << 14);
38762306a36Sopenharmony_ci			return 0;
38862306a36Sopenharmony_ci		}
38962306a36Sopenharmony_ci	}
39062306a36Sopenharmony_ci	rs2 = (argp)&fregs[freg];
39162306a36Sopenharmony_ci	switch ((type >> 3) & 0x7) {
39262306a36Sopenharmony_ci	case 7: FP_UNPACK_QP (QB, rs2); break;
39362306a36Sopenharmony_ci	case 6: FP_UNPACK_DP (DB, rs2); break;
39462306a36Sopenharmony_ci	case 5: FP_UNPACK_SP (SB, rs2); break;
39562306a36Sopenharmony_ci	}
39662306a36Sopenharmony_ci	freg = ((insn >> 25) & 0x1f);
39762306a36Sopenharmony_ci	switch ((type >> 6) & 0x3) {			/* and finally rd. This one's a bit different */
39862306a36Sopenharmony_ci	case 0:						/* dest is fcc. (this must be FCMPQ or FCMPEQ) */
39962306a36Sopenharmony_ci		if (freg) {				/* V8 has only one set of condition codes, so */
40062306a36Sopenharmony_ci							/* anything but 0 in the rd field is an error */
40162306a36Sopenharmony_ci			*pfsr |= (6 << 14);		/* (should probably flag as invalid opcode */
40262306a36Sopenharmony_ci			return 0;			/* but SIGFPE will do :-> ) */
40362306a36Sopenharmony_ci		}
40462306a36Sopenharmony_ci		break;
40562306a36Sopenharmony_ci	case 3:
40662306a36Sopenharmony_ci		if (freg & 3) {				/* quadwords must have bits 4&5 of the */
40762306a36Sopenharmony_ci							/* encoded reg. number set to zero. */
40862306a36Sopenharmony_ci			*pfsr |= (6 << 14);
40962306a36Sopenharmony_ci			return 0;			/* simulate invalid_fp_register exception */
41062306a36Sopenharmony_ci		}
41162306a36Sopenharmony_ci		fallthrough;
41262306a36Sopenharmony_ci	case 2:
41362306a36Sopenharmony_ci		if (freg & 1) {				/* doublewords must have bit 5 zeroed */
41462306a36Sopenharmony_ci			*pfsr |= (6 << 14);
41562306a36Sopenharmony_ci			return 0;
41662306a36Sopenharmony_ci		}
41762306a36Sopenharmony_ci		fallthrough;
41862306a36Sopenharmony_ci	case 1:
41962306a36Sopenharmony_ci		rd = (void *)&fregs[freg];
42062306a36Sopenharmony_ci		break;
42162306a36Sopenharmony_ci	}
42262306a36Sopenharmony_ci#ifdef DEBUG_MATHEMU
42362306a36Sopenharmony_ci	printk("executing insn...\n");
42462306a36Sopenharmony_ci#endif
42562306a36Sopenharmony_ci	/* do the Right Thing */
42662306a36Sopenharmony_ci	switch ((insn >> 5) & 0x1ff) {
42762306a36Sopenharmony_ci	/* + */
42862306a36Sopenharmony_ci	case FADDS: FP_ADD_S (SR, SA, SB); break;
42962306a36Sopenharmony_ci	case FADDD: FP_ADD_D (DR, DA, DB); break;
43062306a36Sopenharmony_ci	case FADDQ: FP_ADD_Q (QR, QA, QB); break;
43162306a36Sopenharmony_ci	/* - */
43262306a36Sopenharmony_ci	case FSUBS: FP_SUB_S (SR, SA, SB); break;
43362306a36Sopenharmony_ci	case FSUBD: FP_SUB_D (DR, DA, DB); break;
43462306a36Sopenharmony_ci	case FSUBQ: FP_SUB_Q (QR, QA, QB); break;
43562306a36Sopenharmony_ci	/* * */
43662306a36Sopenharmony_ci	case FMULS: FP_MUL_S (SR, SA, SB); break;
43762306a36Sopenharmony_ci	case FSMULD: FP_CONV (D, S, 2, 1, DA, SA);
43862306a36Sopenharmony_ci		     FP_CONV (D, S, 2, 1, DB, SB);
43962306a36Sopenharmony_ci	case FMULD: FP_MUL_D (DR, DA, DB); break;
44062306a36Sopenharmony_ci	case FDMULQ: FP_CONV (Q, D, 4, 2, QA, DA);
44162306a36Sopenharmony_ci		     FP_CONV (Q, D, 4, 2, QB, DB);
44262306a36Sopenharmony_ci	case FMULQ: FP_MUL_Q (QR, QA, QB); break;
44362306a36Sopenharmony_ci	/* / */
44462306a36Sopenharmony_ci	case FDIVS: FP_DIV_S (SR, SA, SB); break;
44562306a36Sopenharmony_ci	case FDIVD: FP_DIV_D (DR, DA, DB); break;
44662306a36Sopenharmony_ci	case FDIVQ: FP_DIV_Q (QR, QA, QB); break;
44762306a36Sopenharmony_ci	/* sqrt */
44862306a36Sopenharmony_ci	case FSQRTS: FP_SQRT_S (SR, SB); break;
44962306a36Sopenharmony_ci	case FSQRTD: FP_SQRT_D (DR, DB); break;
45062306a36Sopenharmony_ci	case FSQRTQ: FP_SQRT_Q (QR, QB); break;
45162306a36Sopenharmony_ci	/* mov */
45262306a36Sopenharmony_ci	case FMOVS: rd->s = rs2->s; break;
45362306a36Sopenharmony_ci	case FABSS: rd->s = rs2->s & 0x7fffffff; break;
45462306a36Sopenharmony_ci	case FNEGS: rd->s = rs2->s ^ 0x80000000; break;
45562306a36Sopenharmony_ci	/* float to int */
45662306a36Sopenharmony_ci	case FSTOI: FP_TO_INT_S (IR, SB, 32, 1); break;
45762306a36Sopenharmony_ci	case FDTOI: FP_TO_INT_D (IR, DB, 32, 1); break;
45862306a36Sopenharmony_ci	case FQTOI: FP_TO_INT_Q (IR, QB, 32, 1); break;
45962306a36Sopenharmony_ci	/* int to float */
46062306a36Sopenharmony_ci	case FITOS: IR = rs2->s; FP_FROM_INT_S (SR, IR, 32, int); break;
46162306a36Sopenharmony_ci	case FITOD: IR = rs2->s; FP_FROM_INT_D (DR, IR, 32, int); break;
46262306a36Sopenharmony_ci	case FITOQ: IR = rs2->s; FP_FROM_INT_Q (QR, IR, 32, int); break;
46362306a36Sopenharmony_ci	/* float to float */
46462306a36Sopenharmony_ci	case FSTOD: FP_CONV (D, S, 2, 1, DR, SB); break;
46562306a36Sopenharmony_ci	case FSTOQ: FP_CONV (Q, S, 4, 1, QR, SB); break;
46662306a36Sopenharmony_ci	case FDTOQ: FP_CONV (Q, D, 4, 2, QR, DB); break;
46762306a36Sopenharmony_ci	case FDTOS: FP_CONV (S, D, 1, 2, SR, DB); break;
46862306a36Sopenharmony_ci	case FQTOS: FP_CONV (S, Q, 1, 4, SR, QB); break;
46962306a36Sopenharmony_ci	case FQTOD: FP_CONV (D, Q, 2, 4, DR, QB); break;
47062306a36Sopenharmony_ci	/* comparison */
47162306a36Sopenharmony_ci	case FCMPS:
47262306a36Sopenharmony_ci	case FCMPES:
47362306a36Sopenharmony_ci		FP_CMP_S(IR, SB, SA, 3);
47462306a36Sopenharmony_ci		if (IR == 3 &&
47562306a36Sopenharmony_ci		    (((insn >> 5) & 0x1ff) == FCMPES ||
47662306a36Sopenharmony_ci		     FP_ISSIGNAN_S(SA) ||
47762306a36Sopenharmony_ci		     FP_ISSIGNAN_S(SB)))
47862306a36Sopenharmony_ci			FP_SET_EXCEPTION (FP_EX_INVALID);
47962306a36Sopenharmony_ci		break;
48062306a36Sopenharmony_ci	case FCMPD:
48162306a36Sopenharmony_ci	case FCMPED:
48262306a36Sopenharmony_ci		FP_CMP_D(IR, DB, DA, 3);
48362306a36Sopenharmony_ci		if (IR == 3 &&
48462306a36Sopenharmony_ci		    (((insn >> 5) & 0x1ff) == FCMPED ||
48562306a36Sopenharmony_ci		     FP_ISSIGNAN_D(DA) ||
48662306a36Sopenharmony_ci		     FP_ISSIGNAN_D(DB)))
48762306a36Sopenharmony_ci			FP_SET_EXCEPTION (FP_EX_INVALID);
48862306a36Sopenharmony_ci		break;
48962306a36Sopenharmony_ci	case FCMPQ:
49062306a36Sopenharmony_ci	case FCMPEQ:
49162306a36Sopenharmony_ci		FP_CMP_Q(IR, QB, QA, 3);
49262306a36Sopenharmony_ci		if (IR == 3 &&
49362306a36Sopenharmony_ci		    (((insn >> 5) & 0x1ff) == FCMPEQ ||
49462306a36Sopenharmony_ci		     FP_ISSIGNAN_Q(QA) ||
49562306a36Sopenharmony_ci		     FP_ISSIGNAN_Q(QB)))
49662306a36Sopenharmony_ci			FP_SET_EXCEPTION (FP_EX_INVALID);
49762306a36Sopenharmony_ci	}
49862306a36Sopenharmony_ci	if (!FP_INHIBIT_RESULTS) {
49962306a36Sopenharmony_ci		switch ((type >> 6) & 0x7) {
50062306a36Sopenharmony_ci		case 0: fsr = *pfsr;
50162306a36Sopenharmony_ci			if (IR == -1) IR = 2;
50262306a36Sopenharmony_ci			/* fcc is always fcc0 */
50362306a36Sopenharmony_ci			fsr &= ~0xc00; fsr |= (IR << 10);
50462306a36Sopenharmony_ci			*pfsr = fsr;
50562306a36Sopenharmony_ci			break;
50662306a36Sopenharmony_ci		case 1: rd->s = IR; break;
50762306a36Sopenharmony_ci		case 5: FP_PACK_SP (rd, SR); break;
50862306a36Sopenharmony_ci		case 6: FP_PACK_DP (rd, DR); break;
50962306a36Sopenharmony_ci		case 7: FP_PACK_QP (rd, QR); break;
51062306a36Sopenharmony_ci		}
51162306a36Sopenharmony_ci	}
51262306a36Sopenharmony_ci	if (_fex == 0)
51362306a36Sopenharmony_ci		return 1;				/* success! */
51462306a36Sopenharmony_ci	return record_exception(pfsr, _fex);
51562306a36Sopenharmony_ci}
516