xref: /linux/arch/arm/nwfpe/double_cpdo.c (revision 63307d015b91e626c97bb82e88054af3d0b74643)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     NetWinder Floating Point Emulator
4     (c) Rebel.COM, 1998,1999
5 
6     Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
7 
8 */
9 
10 #include "fpa11.h"
11 #include "softfloat.h"
12 #include "fpopcode.h"
13 
14 union float64_components {
15 	float64 f64;
16 	unsigned int i[2];
17 };
18 
19 float64 float64_exp(float64 Fm);
20 float64 float64_ln(float64 Fm);
21 float64 float64_sin(float64 rFm);
22 float64 float64_cos(float64 rFm);
23 float64 float64_arcsin(float64 rFm);
24 float64 float64_arctan(float64 rFm);
25 float64 float64_log(float64 rFm);
26 float64 float64_tan(float64 rFm);
27 float64 float64_arccos(float64 rFm);
28 float64 float64_pow(float64 rFn, float64 rFm);
29 float64 float64_pol(float64 rFn, float64 rFm);
30 
31 static float64 float64_rsf(struct roundingData *roundData, float64 rFn, float64 rFm)
32 {
33 	return float64_sub(roundData, rFm, rFn);
34 }
35 
36 static float64 float64_rdv(struct roundingData *roundData, float64 rFn, float64 rFm)
37 {
38 	return float64_div(roundData, rFm, rFn);
39 }
40 
41 static float64 (*const dyadic_double[16])(struct roundingData*, float64 rFn, float64 rFm) = {
42 	[ADF_CODE >> 20] = float64_add,
43 	[MUF_CODE >> 20] = float64_mul,
44 	[SUF_CODE >> 20] = float64_sub,
45 	[RSF_CODE >> 20] = float64_rsf,
46 	[DVF_CODE >> 20] = float64_div,
47 	[RDF_CODE >> 20] = float64_rdv,
48 	[RMF_CODE >> 20] = float64_rem,
49 
50 	/* strictly, these opcodes should not be implemented */
51 	[FML_CODE >> 20] = float64_mul,
52 	[FDV_CODE >> 20] = float64_div,
53 	[FRD_CODE >> 20] = float64_rdv,
54 };
55 
56 static float64 float64_mvf(struct roundingData *roundData,float64 rFm)
57 {
58 	return rFm;
59 }
60 
61 static float64 float64_mnf(struct roundingData *roundData,float64 rFm)
62 {
63 	union float64_components u;
64 
65 	u.f64 = rFm;
66 #ifdef __ARMEB__
67 	u.i[0] ^= 0x80000000;
68 #else
69 	u.i[1] ^= 0x80000000;
70 #endif
71 
72 	return u.f64;
73 }
74 
75 static float64 float64_abs(struct roundingData *roundData,float64 rFm)
76 {
77 	union float64_components u;
78 
79 	u.f64 = rFm;
80 #ifdef __ARMEB__
81 	u.i[0] &= 0x7fffffff;
82 #else
83 	u.i[1] &= 0x7fffffff;
84 #endif
85 
86 	return u.f64;
87 }
88 
89 static float64 (*const monadic_double[16])(struct roundingData *, float64 rFm) = {
90 	[MVF_CODE >> 20] = float64_mvf,
91 	[MNF_CODE >> 20] = float64_mnf,
92 	[ABS_CODE >> 20] = float64_abs,
93 	[RND_CODE >> 20] = float64_round_to_int,
94 	[URD_CODE >> 20] = float64_round_to_int,
95 	[SQT_CODE >> 20] = float64_sqrt,
96 	[NRM_CODE >> 20] = float64_mvf,
97 };
98 
99 unsigned int DoubleCPDO(struct roundingData *roundData, const unsigned int opcode, FPREG * rFd)
100 {
101 	FPA11 *fpa11 = GET_FPA11();
102 	float64 rFm;
103 	unsigned int Fm, opc_mask_shift;
104 
105 	Fm = getFm(opcode);
106 	if (CONSTANT_FM(opcode)) {
107 		rFm = getDoubleConstant(Fm);
108 	} else {
109 		switch (fpa11->fType[Fm]) {
110 		case typeSingle:
111 			rFm = float32_to_float64(fpa11->fpreg[Fm].fSingle);
112 			break;
113 
114 		case typeDouble:
115 			rFm = fpa11->fpreg[Fm].fDouble;
116 			break;
117 
118 		default:
119 			return 0;
120 		}
121 	}
122 
123 	opc_mask_shift = (opcode & MASK_ARITHMETIC_OPCODE) >> 20;
124 	if (!MONADIC_INSTRUCTION(opcode)) {
125 		unsigned int Fn = getFn(opcode);
126 		float64 rFn;
127 
128 		switch (fpa11->fType[Fn]) {
129 		case typeSingle:
130 			rFn = float32_to_float64(fpa11->fpreg[Fn].fSingle);
131 			break;
132 
133 		case typeDouble:
134 			rFn = fpa11->fpreg[Fn].fDouble;
135 			break;
136 
137 		default:
138 			return 0;
139 		}
140 
141 		if (dyadic_double[opc_mask_shift]) {
142 			rFd->fDouble = dyadic_double[opc_mask_shift](roundData, rFn, rFm);
143 		} else {
144 			return 0;
145 		}
146 	} else {
147 		if (monadic_double[opc_mask_shift]) {
148 			rFd->fDouble = monadic_double[opc_mask_shift](roundData, rFm);
149 		} else {
150 			return 0;
151 		}
152 	}
153 
154 	return 1;
155 }
156