CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/MIPSVFPUUtils.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <cmath>
21
#include <string>
22
#include "Common/CommonTypes.h"
23
#include "Core/MIPS/MIPS.h"
24
25
#define _VD (op & 0x7F)
26
#define _VS ((op>>8) & 0x7F)
27
#define _VT ((op>>16) & 0x7F)
28
29
inline int Xpose(int v) {
30
return v^0x20;
31
}
32
33
// Half of PI, or 90 degrees.
34
#ifndef M_PI_2
35
#define M_PI_2 1.57079632679489661923
36
#endif
37
38
// The VFPU uses weird angles where 4.0 represents a full circle. This makes it possible to return
39
// exact 1.0/-1.0 values at certain angles.
40
//
41
// The current code attempts to match VFPU sin/cos exactly.
42
// Possibly affected games:
43
// Final Fantasy III (#2921 )
44
// Hitman Reborn 2 (#12900)
45
// Cho Aniki Zero (#13705)
46
// Hajime no Ippo (#13671)
47
// Dissidia Duodecim Final Fantasy (#6710 )
48
//
49
// Messing around with the modulo functions? try https://www.desmos.com/calculator.
50
51
extern float vfpu_sin(float);
52
extern float vfpu_cos(float);
53
extern void vfpu_sincos(float, float&, float&);
54
55
extern float vfpu_asin(float);
56
57
inline float vfpu_clamp(float v, float min, float max) {
58
// Note: NAN is preserved, and -0.0 becomes +0.0 if min=+0.0.
59
return v >= max ? max : (v <= min ? min : v);
60
}
61
62
float vfpu_dot(const float a[4], const float b[4]);
63
float vfpu_sqrt(float a);
64
float vfpu_rsqrt(float a);
65
66
extern float vfpu_exp2(float);
67
extern float vfpu_rexp2(float);
68
extern float vfpu_log2(float);
69
extern float vfpu_rcp(float);
70
71
extern void vrnd_init_default(uint32_t *rcx);
72
extern void vrnd_init(uint32_t seed, uint32_t *rcx);
73
extern uint32_t vrnd_generate(uint32_t *rcx);
74
75
inline uint32_t get_uexp(uint32_t x) {
76
return (x >> 23) & 0xFF;
77
}
78
79
inline int32_t get_exp(uint32_t x) {
80
return get_uexp(x) - 127;
81
}
82
83
inline int32_t get_mant(uint32_t x) {
84
// Note: this returns the hidden 1.
85
return (x & 0x007FFFFF) | 0x00800000;
86
}
87
88
inline int32_t get_sign(uint32_t x) {
89
return x & 0x80000000;
90
}
91
92
#define VFPU_FLOAT16_EXP_MAX 0x1f
93
#define VFPU_SH_FLOAT16_SIGN 15
94
#define VFPU_MASK_FLOAT16_SIGN 0x1
95
#define VFPU_SH_FLOAT16_EXP 10
96
#define VFPU_MASK_FLOAT16_EXP 0x1f
97
#define VFPU_SH_FLOAT16_FRAC 0
98
#define VFPU_MASK_FLOAT16_FRAC 0x3ff
99
100
enum VectorSize {
101
V_Single = 1,
102
V_Pair = 2,
103
V_Triple = 3,
104
V_Quad = 4,
105
V_Invalid = -1,
106
};
107
108
enum MatrixSize {
109
M_1x1 = 1,
110
M_2x2 = 2,
111
M_3x3 = 3,
112
M_4x4 = 4,
113
M_Invalid = -1
114
};
115
116
inline u32 VFPU_SWIZZLE(int x, int y, int z, int w) {
117
return (x << 0) | (y << 2) | (z << 4) | (w << 6);
118
}
119
120
inline u32 VFPU_MASK(int x, int y, int z, int w) {
121
return (x << 0) | (y << 1) | (z << 2) | (w << 3);
122
}
123
124
inline u32 VFPU_ANY_SWIZZLE() {
125
return 0x000000FF;
126
}
127
128
inline u32 VFPU_ABS(int x, int y, int z, int w) {
129
return VFPU_MASK(x, y, z, w) << 8;
130
}
131
132
inline u32 VFPU_CONST(int x, int y, int z, int w) {
133
return VFPU_MASK(x, y, z, w) << 12;
134
}
135
136
inline u32 VFPU_NEGATE(int x, int y, int z, int w) {
137
return VFPU_MASK(x, y, z, w) << 16;
138
}
139
140
enum class VFPUConst {
141
NONE = -1,
142
ZERO,
143
ONE,
144
TWO,
145
HALF,
146
THREE,
147
THIRD,
148
FOURTH,
149
SIXTH,
150
};
151
152
inline u32 VFPU_MAKE_CONSTANTS(VFPUConst x, VFPUConst y, VFPUConst z, VFPUConst w) {
153
u32 result = 0;
154
if (x != VFPUConst::NONE) {
155
// This sets the constant flag and the swizzle/abs flags for the right constant.
156
result |= (((int)x & 3) << 0) | (((int)x & 4) << 6) | (1 << 12);
157
}
158
if (y != VFPUConst::NONE) {
159
result |= (((int)y & 3) << 2) | (((int)y & 4) << 7) | (1 << 13);
160
}
161
if (z != VFPUConst::NONE) {
162
result |= (((int)z & 3) << 4) | (((int)z & 4) << 8) | (1 << 14);
163
}
164
if (w != VFPUConst::NONE) {
165
result |= (((int)w & 3) << 6) | (((int)w & 4) << 9) | (1 << 15);
166
}
167
return result;
168
}
169
170
u32 VFPURewritePrefix(int ctrl, u32 remove, u32 add);
171
172
void ReadMatrix(float *rd, MatrixSize size, int reg);
173
void WriteMatrix(const float *rs, MatrixSize size, int reg);
174
175
void WriteVector(const float *rs, VectorSize N, int reg);
176
void ReadVector(float *rd, VectorSize N, int reg);
177
178
void GetVectorRegs(u8 regs[4], VectorSize N, int vectorReg);
179
void GetMatrixRegs(u8 regs[16], MatrixSize N, int matrixReg);
180
181
// Translate between vector and matrix size. Possibly we should simply
182
// join the two enums, but the type safety is kind of nice.
183
VectorSize GetVectorSize(MatrixSize sz);
184
MatrixSize GetMatrixSize(VectorSize sz);
185
186
// Note that if matrix is a transposed matrix (E format), GetColumn will actually return rows,
187
// and vice versa.
188
int GetColumnName(int matrix, MatrixSize msize, int column, int offset);
189
int GetRowName(int matrix, MatrixSize msize, int row, int offset);
190
191
int GetMatrixName(int matrix, MatrixSize msize, int column, int row, bool transposed);
192
193
void GetMatrixColumns(int matrixReg, MatrixSize msize, u8 vecs[4]);
194
void GetMatrixRows(int matrixReg, MatrixSize msize, u8 vecs[4]);
195
196
enum MatrixOverlapType {
197
OVERLAP_NONE = 0,
198
OVERLAP_PARTIAL = 1,
199
OVERLAP_EQUAL = 2,
200
// Transposed too? (same space but transposed)
201
};
202
203
MatrixOverlapType GetMatrixOverlap(int m1, int m2, MatrixSize msize);
204
205
// Returns a number from 0-7, good for checking overlap for 4x4 matrices.
206
static inline int GetMtx(int matrixReg) {
207
return (matrixReg >> 2) & 7;
208
}
209
210
static inline VectorSize GetVecSize(MIPSOpcode op) {
211
int a = (op >> 7) & 1;
212
int b = (op >> 14) & 2;
213
return (VectorSize)(a + b + 1); // Safe, there are no other possibilities
214
}
215
216
static inline MatrixSize GetMtxSize(MIPSOpcode op) {
217
int a = (op >> 7) & 1;
218
int b = (op >> 14) & 2;
219
return (MatrixSize)(a + b + 1); // Safe, there are no other possibilities
220
}
221
222
VectorSize GetHalfVectorSizeSafe(VectorSize sz);
223
VectorSize GetHalfVectorSize(VectorSize sz);
224
VectorSize GetDoubleVectorSizeSafe(VectorSize sz);
225
VectorSize GetDoubleVectorSize(VectorSize sz);
226
VectorSize MatrixVectorSizeSafe(MatrixSize sz);
227
VectorSize MatrixVectorSize(MatrixSize sz);
228
229
static inline int GetNumVectorElements(VectorSize sz) {
230
switch (sz) {
231
case V_Single: return 1;
232
case V_Pair: return 2;
233
case V_Triple: return 3;
234
case V_Quad: return 4;
235
default: return 0;
236
}
237
}
238
239
int GetMatrixSideSafe(MatrixSize sz);
240
int GetMatrixSide(MatrixSize sz);
241
std::string GetVectorNotation(int reg, VectorSize size);
242
std::string GetMatrixNotation(int reg, MatrixSize size);
243
static inline bool IsMatrixTransposed(int matrixReg) {
244
return (matrixReg >> 5) & 1;
245
}
246
static inline bool IsVectorColumn(int vectorReg) {
247
return !((vectorReg >> 5) & 1);
248
}
249
static inline int TransposeMatrixReg(int matrixReg) {
250
return matrixReg ^ 0x20;
251
}
252
int GetVectorOverlap(int reg1, VectorSize size1, int reg2, VectorSize size2);
253
254
bool GetVFPUCtrlMask(int reg, u32 *mask);
255
256
float Float16ToFloat32(unsigned short l);
257
void InitVFPU();
258
259