Skip to content

Commit abd072a

Browse files
authored
Merge pull request libretro#92 from jSTE0/microopt
2 parents 01564ac + a8f2691 commit abd072a

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

source/dsp1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void DSP1SetByte(uint8_t byte, uint16_t address)
352352
Op11m = (int16_t)(DSP1.parameters [0] | (DSP1.parameters[1] << 8));
353353
Op11Zr = (int16_t)(DSP1.parameters [2] | (DSP1.parameters[3] << 8));
354354
Op11Yr = (int16_t)(DSP1.parameters [4] | (DSP1.parameters[5] << 8));
355-
Op11Xr = (int16_t)(DSP1.parameters [7] | (DSP1.parameters[7] << 8));
355+
Op11Xr = (int16_t)(DSP1.parameters [6] | (DSP1.parameters[7] << 8));
356356
DSPOp11();
357357
break;
358358
case 0x25:

source/dsp1emu.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,19 @@ void DSP1_Inverse(int16_t Coefficient, int16_t Exponent, int16_t* iCoefficient,
187187
}
188188

189189
/* Step Three: Normalize */
190+
#ifdef __GNUC__
191+
{
192+
const int shift = __builtin_clz(Coefficient) - (8 * sizeof(int) - 15);
193+
Coefficient <<= shift;
194+
Exponent -= shift;
195+
}
196+
#else
190197
while (Coefficient < 0x4000)
191198
{
192199
Coefficient <<= 1;
193200
Exponent--;
194201
}
202+
#endif
195203

196204
/* Step Four: Special Case */
197205
if (Coefficient == 0x4000)
@@ -336,9 +344,18 @@ int16_t DSP1_Cos(int16_t Angle)
336344

337345
void DSP1_Normalize(int16_t m, int16_t* Coefficient, int16_t* Exponent)
338346
{
339-
int16_t i = 0x4000;
340347
int16_t e = 0;
341348

349+
#ifdef __GNUC__
350+
int16_t n = m < 0 ? ~m : m;
351+
352+
if (n == 0)
353+
e = 15;
354+
else
355+
e = __builtin_clz(n) - (8 * sizeof(int) - 15);
356+
#else
357+
int16_t i = 0x4000;
358+
342359
if (m < 0)
343360
{
344361
while ((m & i) && i)
@@ -355,6 +372,7 @@ void DSP1_Normalize(int16_t m, int16_t* Coefficient, int16_t* Exponent)
355372
e++;
356373
}
357374
}
375+
#endif
358376

359377
if (e > 0)
360378
*Coefficient = m * DSP1ROM[0x21 + e] << 1;
@@ -368,9 +386,18 @@ void DSP1_NormalizeDouble(int32_t Product, int16_t* Coefficient, int16_t* Expone
368386
{
369387
int16_t n = Product & 0x7fff;
370388
int16_t m = Product >> 15;
371-
int16_t i = 0x4000;
372389
int16_t e = 0;
373390

391+
#ifdef __GNUC__
392+
int16_t t = m < 0 ? ~m : m;
393+
394+
if (t == 0)
395+
e = 15;
396+
else
397+
e = __builtin_clz(t) - (8 * sizeof(int) - 15);
398+
#else
399+
int16_t i = 0x4000;
400+
374401
if (m < 0)
375402
{
376403
while ((m & i) && i)
@@ -387,6 +414,7 @@ void DSP1_NormalizeDouble(int32_t Product, int16_t* Coefficient, int16_t* Expone
387414
e++;
388415
}
389416
}
417+
#endif
390418

391419
if (e > 0)
392420
{
@@ -396,6 +424,14 @@ void DSP1_NormalizeDouble(int32_t Product, int16_t* Coefficient, int16_t* Expone
396424
*Coefficient += n * DSP1ROM[0x0040 - e] >> 15;
397425
else
398426
{
427+
#ifdef __GNUC__
428+
t = m < 0 ? ~(n | 0x8000) : n;
429+
430+
if (t == 0)
431+
e += 15;
432+
else
433+
e += __builtin_clz(t) - (8 * sizeof(int) - 15);
434+
#else
399435
i = 0x4000;
400436

401437
if (m < 0)
@@ -414,6 +450,7 @@ void DSP1_NormalizeDouble(int32_t Product, int16_t* Coefficient, int16_t* Expone
414450
e++;
415451
}
416452
}
453+
#endif
417454

418455
if (e > 15)
419456
*Coefficient = n * DSP1ROM[0x0012 + e] << 1;

source/fxemu.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "fxemu.h"
44
#include "fxinst.h"
5+
#include "memmap.h"
56
#include <stdlib.h>
67
#include <string.h>
78

@@ -30,33 +31,27 @@ void fx_updateRamBank(uint8_t Byte)
3031

3132
static INLINE void fx_readRegisterSpaceForCheck(void)
3233
{
33-
R15 = GSU.pvRegisters[30];
34-
R15 |= ((uint32_t) GSU.pvRegisters[31]) << 8;
34+
R15 = (uint32_t) READ_WORD(&GSU.pvRegisters[30]);
3535
}
3636

3737
static void fx_readRegisterSpaceForUse(void)
3838
{
39-
static uint32_t avHeight[] = { 128, 160, 192, 256 };
40-
static uint32_t avMult[] = { 16, 32, 32, 64 };
39+
static const uint32_t avHeight[] = { 128, 160, 192, 256 };
40+
static const uint32_t avMult[] = { 16, 32, 32, 64 };
4141
int32_t i;
4242
uint8_t* p = GSU.pvRegisters;
4343

4444
/* Update R0 - R14 */
45-
for (i = 0; i < 15; i++)
46-
{
47-
GSU.avReg[i] = *p++;
48-
GSU.avReg[i] += ((uint32_t)(*p++)) << 8;
49-
}
45+
for (i = 0; i < 15; i++, p += 2)
46+
GSU.avReg[i] = (uint32_t) READ_WORD(p);
5047

5148
/* Update other registers */
5249
p = GSU.pvRegisters;
53-
GSU.vStatusReg = (uint32_t) GSU.pvRegisters[GSU_SFR];
54-
GSU.vStatusReg |= ((uint32_t) GSU.pvRegisters[GSU_SFR + 1]) << 8;
50+
GSU.vStatusReg = (uint32_t) READ_WORD(&GSU.pvRegisters[GSU_SFR]);
5551
GSU.vPrgBankReg = (uint32_t) GSU.pvRegisters[GSU_PBR];
5652
GSU.vRomBankReg = (uint32_t)p[GSU_ROMBR];
5753
GSU.vRamBankReg = ((uint32_t)p[GSU_RAMBR]) & (FX_RAM_BANKS - 1);
58-
GSU.vCacheBaseReg = (uint32_t)p[GSU_CBR];
59-
GSU.vCacheBaseReg |= ((uint32_t)p[GSU_CBR + 1]) << 8;
54+
GSU.vCacheBaseReg = (uint32_t) READ_WORD(&p[GSU_CBR]);
6055

6156
/* Update status register variables */
6257
GSU.vZero = !(GSU.vStatusReg & FLG_Z);
@@ -146,19 +141,15 @@ void fx_computeScreenPointers(void)
146141

147142
static INLINE void fx_writeRegisterSpaceAfterCheck(void)
148143
{
149-
GSU.pvRegisters[30] = (uint8_t) R15;
150-
GSU.pvRegisters[31] = (uint8_t) (R15 >> 8);
144+
WRITE_WORD(&GSU.pvRegisters[30], R15);
151145
}
152146

153147
static void fx_writeRegisterSpaceAfterUse(void)
154148
{
155149
int32_t i;
156150
uint8_t* p = GSU.pvRegisters;
157-
for (i = 0; i < 15; i++)
158-
{
159-
*p++ = (uint8_t)GSU.avReg[i];
160-
*p++ = (uint8_t)(GSU.avReg[i] >> 8);
161-
}
151+
for (i = 0; i < 15; i++, p += 2)
152+
WRITE_WORD(p, GSU.avReg[i]);
162153

163154
/* Update status register */
164155
if (USEX16(GSU.vZero) == 0)
@@ -179,13 +170,11 @@ static void fx_writeRegisterSpaceAfterUse(void)
179170
CF(CY);
180171

181172
p = GSU.pvRegisters;
182-
p[GSU_SFR] = (uint8_t) GSU.vStatusReg;
183-
p[GSU_SFR + 1] = (uint8_t) (GSU.vStatusReg >> 8);
173+
WRITE_WORD(&p[GSU_SFR], GSU.vStatusReg);
184174
p[GSU_PBR] = (uint8_t) GSU.vPrgBankReg;
185175
p[GSU_ROMBR] = (uint8_t)GSU.vRomBankReg;
186176
p[GSU_RAMBR] = (uint8_t)GSU.vRamBankReg;
187-
p[GSU_CBR] = (uint8_t)GSU.vCacheBaseReg;
188-
p[GSU_CBR + 1] = (uint8_t)(GSU.vCacheBaseReg >> 8);
177+
WRITE_WORD(&p[GSU_CBR], GSU.vCacheBaseReg);
189178
}
190179

191180
/* Reset the FxChip */

source/gfx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void S9xStartScreenRefresh(void)
423423
GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
424424
}
425425

426-
if (++IPPU.FrameCount % Memory.ROMFramesPerSecond == 0)
426+
if (++IPPU.FrameCount == (uint32_t)Memory.ROMFramesPerSecond)
427427
IPPU.FrameCount = 0;
428428
}
429429

0 commit comments

Comments
 (0)