Skip to content

Commit bb19c3b

Browse files
committed
utils: extend_sign bit size now include sign bit
1 parent 6747f45 commit bb19c3b

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

Diff for: src/cpu/gte/opcodes.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ int32_t GTE::clip(int32_t value, int32_t max, int32_t min, uint32_t flags) {
1818

1919
template <int bit_size>
2020
void GTE::checkOverflow(int64_t value, uint32_t overflowBits, uint32_t underflowFlags) {
21-
if (value >= (1LL << bit_size)) flag.reg |= overflowBits;
22-
if (value < -(1LL << bit_size)) flag.reg |= underflowFlags;
21+
if (value >= (1LL << (bit_size - 1))) flag.reg |= overflowBits;
22+
if (value < -(1LL << (bit_size - 1))) flag.reg |= underflowFlags;
2323
}
2424

2525
template <int i>
2626
int64_t GTE::checkMacOverflowAndExtend(int64_t value) {
2727
static_assert(i >= 1 && i <= 3, "Invalid mac for GTE::checkMacOverflowAndExtend");
2828

2929
if (i == 1) {
30-
checkOverflow<43>(value, Flag::MAC1_OVERFLOW_POSITIVE, Flag::MAC1_OVERFLOW_NEGATIVE);
30+
checkOverflow<44>(value, Flag::MAC1_OVERFLOW_POSITIVE, Flag::MAC1_OVERFLOW_NEGATIVE);
3131
} else if (i == 2) {
32-
checkOverflow<43>(value, Flag::MAC2_OVERFLOW_POSITIVE, Flag::MAC2_OVERFLOW_NEGATIVE);
32+
checkOverflow<44>(value, Flag::MAC2_OVERFLOW_POSITIVE, Flag::MAC2_OVERFLOW_NEGATIVE);
3333
} else if (i == 3) {
34-
checkOverflow<43>(value, Flag::MAC3_OVERFLOW_POSITIVE, Flag::MAC3_OVERFLOW_NEGATIVE);
34+
checkOverflow<44>(value, Flag::MAC3_OVERFLOW_POSITIVE, Flag::MAC3_OVERFLOW_NEGATIVE);
3535
}
3636

37-
return extend_sign<43, int64_t>(value);
37+
return extend_sign<44, int64_t>(value);
3838
}
3939

4040
#define O(i, value) checkMacOverflowAndExtend<i>(value)
4141

4242
template <>
4343
int64_t GTE::setMac<0>(int64_t value) {
44-
checkOverflow<31>(value, Flag::MAC0_OVERFLOW_POSITIVE, Flag::MAC0_OVERFLOW_NEGATIVE);
44+
checkOverflow<32>(value, Flag::MAC0_OVERFLOW_POSITIVE, Flag::MAC0_OVERFLOW_NEGATIVE);
4545
mac[0] = value;
4646
return value;
4747
}
@@ -51,11 +51,11 @@ int64_t GTE::setMac(int64_t value) {
5151
static_assert(i >= 1 && i <= 3, "Invalid mac for GTE::setMac");
5252

5353
if (i == 1) {
54-
checkOverflow<43>(value, Flag::MAC1_OVERFLOW_POSITIVE, Flag::MAC1_OVERFLOW_NEGATIVE);
54+
checkOverflow<44>(value, Flag::MAC1_OVERFLOW_POSITIVE, Flag::MAC1_OVERFLOW_NEGATIVE);
5555
} else if (i == 2) {
56-
checkOverflow<43>(value, Flag::MAC2_OVERFLOW_POSITIVE, Flag::MAC2_OVERFLOW_NEGATIVE);
56+
checkOverflow<44>(value, Flag::MAC2_OVERFLOW_POSITIVE, Flag::MAC2_OVERFLOW_NEGATIVE);
5757
} else if (i == 3) {
58-
checkOverflow<43>(value, Flag::MAC3_OVERFLOW_POSITIVE, Flag::MAC3_OVERFLOW_NEGATIVE);
58+
checkOverflow<44>(value, Flag::MAC3_OVERFLOW_POSITIVE, Flag::MAC3_OVERFLOW_NEGATIVE);
5959
}
6060

6161
if (sf) value >>= 12;

Diff for: src/device/gpu/gpu.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ void GPU::cmdPolygon(PolygonArgs arg) {
257257
TextureInfo tex;
258258

259259
for (int i = 0; i < arg.getVertexCount(); i++) {
260-
v[i].pos.x = extend_sign<10>(arguments[ptr] & 0xffff);
261-
v[i].pos.y = extend_sign<10>((arguments[ptr++] & 0xffff0000) >> 16);
260+
v[i].pos.x = extend_sign<11>(arguments[ptr] & 0xffff);
261+
v[i].pos.y = extend_sign<11>((arguments[ptr++] & 0xffff0000) >> 16);
262262

263263
if (!arg.isRawTexture && (!arg.gouroudShading || i == 0)) v[i].color.raw = arguments[0] & 0xffffff;
264264
if (arg.isTextureMapped) {
@@ -310,8 +310,8 @@ void GPU::cmdLine(LineArgs arg) {
310310
line.isSemiTransparent = arg.semiTransparency;
311311
line.gouroudShading = arg.gouroudShading;
312312

313-
line.pos[0].x = extend_sign<10>(arguments[ptr] & 0xffff);
314-
line.pos[0].y = extend_sign<10>((arguments[ptr++] & 0xffff0000) >> 16);
313+
line.pos[0].x = extend_sign<11>(arguments[ptr] & 0xffff);
314+
line.pos[0].y = extend_sign<11>((arguments[ptr++] & 0xffff0000) >> 16);
315315
line.color[0].raw = (arguments[0] & 0xffffff);
316316

317317
if (arg.gouroudShading) {
@@ -320,8 +320,8 @@ void GPU::cmdLine(LineArgs arg) {
320320
line.color[1] = line.color[0];
321321
}
322322

323-
line.pos[1].x = extend_sign<10>((arguments[ptr] & 0xffff));
324-
line.pos[1].y = extend_sign<10>((arguments[ptr++] & 0xffff0000) >> 16);
323+
line.pos[1].x = extend_sign<11>((arguments[ptr] & 0xffff));
324+
line.pos[1].y = extend_sign<11>((arguments[ptr++] & 0xffff0000) >> 16);
325325

326326
drawLine(line);
327327

@@ -353,12 +353,12 @@ void GPU::cmdRectangle(RectangleArgs arg) {
353353
int16_t h = arg.getSize();
354354

355355
if (arg.size == 0) {
356-
w = extend_sign<10>(arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff);
357-
h = extend_sign<10>((arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff0000) >> 16);
356+
w = extend_sign<11>(arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff);
357+
h = extend_sign<11>((arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff0000) >> 16);
358358
}
359359

360-
int16_t x = extend_sign<10>(arguments[1] & 0xffff);
361-
int16_t y = extend_sign<10>((arguments[1] & 0xffff0000) >> 16);
360+
int16_t x = extend_sign<11>(arguments[1] & 0xffff);
361+
int16_t y = extend_sign<11>((arguments[1] & 0xffff0000) >> 16);
362362

363363
primitive::Rect rect;
364364
rect.pos = vec2(x, y);
@@ -630,8 +630,8 @@ void GPU::writeGP0(uint32_t data) {
630630
drawingArea.bottom = (arguments[0] & 0xffc00) >> 10;
631631
} else if (command == 0xe5) {
632632
// Drawing offset
633-
drawingOffsetX = extend_sign<10>(arguments[0] & 0x7ff);
634-
drawingOffsetY = extend_sign<10>((arguments[0] >> 11) & 0x7ff);
633+
drawingOffsetX = extend_sign<11>(arguments[0] & 0x7ff);
634+
drawingOffsetY = extend_sign<11>((arguments[0] >> 11) & 0x7ff);
635635
} else if (command == 0xe6) {
636636
// Mask bit setting
637637
gp0_e6._reg = arguments[0];

Diff for: src/device/mdec/algorithm.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iter
134134

135135
if (src == input.end()) return;
136136
DCT dct = *src++;
137-
int32_t current = extend_sign<9>(dct.dc);
137+
int32_t current = extend_sign<10>(dct.dc);
138138

139139
int32_t value = current * table[0];
140140

@@ -153,7 +153,7 @@ void MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iter
153153

154154
if (src == input.end()) break;
155155
RLE rle = *src++;
156-
current = extend_sign<9>(rle.ac);
156+
current = extend_sign<10>(rle.ac);
157157
n += rle.zeroes + 1;
158158

159159
value = (current * table[n] * dct.qFactor + 4) / 8;

Diff for: src/platform/windows/gui/debug/gpu.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ void GPU::logWindow(System *sys) {
126126
gpu::TextureInfo tex;
127127

128128
for (int i = 0; i < arg.getVertexCount(); i++) {
129-
v[i].pos.x = extend_sign<10>(arguments[ptr] & 0xffff);
130-
v[i].pos.y = extend_sign<10>((arguments[ptr++] & 0xffff0000) >> 16);
129+
v[i].pos.x = extend_sign<11>(arguments[ptr] & 0xffff);
130+
v[i].pos.y = extend_sign<11>((arguments[ptr++] & 0xffff0000) >> 16);
131131

132132
if (!arg.isRawTexture && (!arg.gouroudShading || i == 0)) v[i].color.raw = arguments[0] & 0xffffff;
133133
if (arg.isTextureMapped) {
@@ -208,12 +208,12 @@ void GPU::logWindow(System *sys) {
208208
int16_t h = arg.getSize();
209209

210210
if (arg.size == 0) {
211-
w = extend_sign<10>(arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff);
212-
h = extend_sign<10>((arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff0000) >> 16);
211+
w = extend_sign<11>(arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff);
212+
h = extend_sign<11>((arguments[(arg.isTextureMapped ? 3 : 2)] & 0xffff0000) >> 16);
213213
}
214214

215-
int16_t x = extend_sign<10>(arguments[1] & 0xffff);
216-
int16_t y = extend_sign<10>((arguments[1] & 0xffff0000) >> 16);
215+
int16_t x = extend_sign<11>(arguments[1] & 0xffff);
216+
int16_t y = extend_sign<11>((arguments[1] & 0xffff0000) >> 16);
217217

218218
x += last_offset_x;
219219
y += last_offset_y;

Diff for: src/utils/logic.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ bool or_range(uint32_t v) {
1919

2020
/**
2121
* Sign extend integer to bigger size.
22-
* eg. 10 bit int to int16_t
22+
* eg. 11 bit int to int16_t
2323
* THPS games does not use upper bits resulting in invalid coords.
24+
*
25+
* bit_size - number of bits INCLUDING sign bit
2426
*/
25-
template <size_t bits, typename T = int16_t>
27+
template <size_t bit_size, typename T = int16_t>
2628
T extend_sign(uint64_t n) {
27-
static_assert(bits > 0 && bits < 63, "bits out of range");
29+
static_assert(bit_size > 0 && bit_size < 63, "bit_size out of range");
2830

29-
T mask = ((1LL << bits) - 1);
30-
bool sign = (n & (1LL << bits)) != 0;
31+
T mask = ((1LL << (bit_size - 1)) - 1);
32+
bool sign = (n & (1LL << (bit_size - 1))) != 0;
3133

3234
T val = n & mask;
3335
if (sign) val |= ~mask;

0 commit comments

Comments
 (0)