From 5f934376d5ea8362f19de0bccbfa945fc60d72a0 Mon Sep 17 00:00:00 2001 From: ayuanx Date: Thu, 8 Aug 2024 01:04:02 +1000 Subject: [PATCH] Small compiler optimizations --- README.md | 3 +++ ogg-winmm.c | 2 +- player.c | 22 +++++++++++++--------- stubs.c | 16 ++++++++-------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 487c2b8..e14dc1d 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ NOTE: It actually can also work on Win95/98 if you follow the extra procedure be # Revisions: +v.2024.08.08 +- Small compiler optimizations. + v.2024.03.21 - Fix the truncation at the end of each song ranging from 1ms to 500ms. diff --git a/ogg-winmm.c b/ogg-winmm.c index 04516dc..f9dcc04 100644 --- a/ogg-winmm.c +++ b/ogg-winmm.c @@ -51,7 +51,7 @@ struct play_info struct track_info tracks[MAX_TRACKS+1]; // Track 0 is reserved. struct play_info info = {0}; -DWORD thread = 0; // Needed for Win85/98 compatibility +DWORD thread = 0; // Needed for Win95/98 compatibility HANDLE player = NULL; HANDLE event = NULL; HWND window = NULL; diff --git a/player.c b/player.c index dad4986..4f717b5 100644 --- a/player.c +++ b/player.c @@ -11,7 +11,7 @@ bool plr_run = false; bool plr_bsy = false; unsigned int plr_len = 0; -float plr_vol[2] = {100.0, 100.0}; // Left, Right +float plr_vol[2] = {1.0, 1.0}; // Left, Right HWAVEOUT plr_hw = NULL; HANDLE plr_ev = NULL; @@ -24,10 +24,10 @@ char plr_buf[WAV_BUF_CNT][WAV_BUF_LEN] __attribute__ ((aligned(4))); void plr_volume(int vol_l, int vol_r) { - if (vol_l < 0 || vol_l > 99) plr_vol[0] = 100.0; + if (vol_l < 0 || vol_l > 99) plr_vol[0] = 1.0; else plr_vol[0] = vol_l / 100.0; - if (vol_r < 0 || vol_r > 99) plr_vol[1] = 100.0; + if (vol_r < 0 || vol_r > 99) plr_vol[1] = 1.0; else plr_vol[1] = vol_r / 100.0; } @@ -102,8 +102,8 @@ int plr_play(const char *path, unsigned int from, unsigned int to) if (from == -1) plr_len = 0; else { - if (from) ov_time_seek(&plr_vf, (double)from / 1000); - plr_len = to != -1 ? (unsigned int)ceil((to - from) / 1000.0 * vi->rate) * 2 * vi->channels : -1; // heed alignment + if (from) ov_time_seek(&plr_vf, from * 0.001); + plr_len = to != -1 ? (unsigned int)ceil((to - from) * 0.001 * vi->rate) * 2 * vi->channels : -1; // heed alignment } plr_run = true; @@ -180,11 +180,15 @@ int plr_pump() plr_len -= pos; /* volume control, kinda nasty */ - if (plr_vol[0] != 100.0 || plr_vol[1] != 100.0) { + if (plr_vol[0] != 1.0 || plr_vol[1] != 1.0) { short *sbuf = (short *)buf; for (int j = 0, end = pos / 2; j < end; j+=2) { - if (plr_vol[0] != 100.0) sbuf[j] *= plr_vol[0]; - if (plr_vol[1] != 100.0) sbuf[j+1] *= plr_vol[1]; + // Surprisingly speedwise (fast > slow): float multiplication > float divison >> int multiplication > int division. + // Also remove branching for better compiler SIMD/loop unrolling optimization. + //if (plr_vol[0] != 1.0) sbuf[j] *= plr_vol[0]; + //if (plr_vol[1] != 1.0) sbuf[j+1] *= plr_vol[1]; + sbuf[j] *= plr_vol[0]; + sbuf[j+1] *= plr_vol[1]; } } @@ -215,7 +219,7 @@ int plr_pump() return 1; } -/* TODO: */ +/* Not needed: */ /* int plr_seek(int sec) { diff --git a/stubs.c b/stubs.c index 767b2d5..e17505c 100644 --- a/stubs.c +++ b/stubs.c @@ -3,14 +3,14 @@ #include #include "player.h" -static float midiVol = 100.0; -static float waveVol = 100.0; +static float midiVol = 1.0; +static float waveVol = 1.0; static int waveBits = -1; static HINSTANCE realWinmmDLL = NULL; -void stub_midivol(int vol) { midiVol = vol < 0 || vol > 99 ? 100.0 : vol / 100.0; } -void stub_wavevol(int vol) { waveVol = vol < 0 || vol > 99 ? 100.0 : vol / 100.0; } +void stub_midivol(int vol) { midiVol = vol < 0 || vol > 99 ? 1.0 : vol / 100.0; } +void stub_wavevol(int vol) { waveVol = vol < 0 || vol > 99 ? 1.0 : vol / 100.0; } void unloadRealDLL() { @@ -43,7 +43,7 @@ MMRESULT WINAPI fake_midiStreamOut(HMIDISTRM a0, LPMIDIHDR a1, UINT a2) funcp = (void*)GetProcAddress(loadRealDLL(), "midiStreamOut"); #ifdef MIDI_VELOCITY_SCALING - if (midiVol != 100.0 && a1 && a1->lpData) { + if (midiVol != 1.0 && a1 && a1->lpData) { for (int i = 0, j = a1->dwBytesRecorded; i < j; i += sizeof(DWORD)*3) { MIDIEVENT *pe = (MIDIEVENT *)(a1->lpData + i); if (pe->dwEvent & MEVT_F_LONG) { @@ -86,7 +86,7 @@ MMRESULT WINAPI fake_waveOutWrite(HWAVEOUT a0, LPWAVEHDR a1, UINT a2) funcp = (void*)GetProcAddress(loadRealDLL(), "waveOutWrite"); /* let owr own OGG wave pass through */ - if ((waveVol != 100.0 || midiVol != 100.0 ) && a1 && a1->lpData && a1->dwUser != 0xCDDA7777) { + if ((waveVol != 1.0 || midiVol != 1.0 ) && a1 && a1->lpData && a1->dwUser != 0xCDDA7777) { /* Windows is f**ked up. MIDI synth driver converts MIDI to WAVE and then calls winmm.waveOutWrite!!! */ void *addr = __builtin_return_address(0); char caller[MAX_PATH]; @@ -94,13 +94,13 @@ MMRESULT WINAPI fake_waveOutWrite(HWAVEOUT a0, LPWAVEHDR a1, UINT a2) VirtualQuery(addr, &mbi, sizeof(MEMORY_BASIC_INFORMATION)); GetModuleFileName(mbi.AllocationBase, caller, MAX_PATH); - float vol = 100.0; + float vol = 1.0; char *pos = strrchr(caller, '\\'); /* Mixer: msacm32.drv */ if (strstr(pos, "wdmaud.drv")) vol = midiVol; else if (!strstr(pos, ".drv")) vol = waveVol; - if (vol != 100.0) { + if (vol != 1.0) { short *wave16; char *wave8; switch (waveBits) {