Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization attempt of findPeriod using binary search #163

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/ace/managers/ptplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,20 @@ static void printVoices(const tModVoice *pVoices) {
}

static inline UBYTE findPeriod(const UWORD *pPeriods, UWORD uwNote) {
// Find nearest period for a note value
for(UBYTE ubPeriodPos = 0; ubPeriodPos < MOD_PERIOD_TABLE_LENGTH; ++ubPeriodPos) {
if (uwNote >= pPeriods[ubPeriodPos]) {
return ubPeriodPos;
// Find nearest period (equal or lower) for a note value using a binary search lookup
UBYTE ubPeriodPos = 0;
UBYTE maxBound = MOD_PERIOD_TABLE_LENGTH;
while (ubPeriodPos != maxBound) {
UBYTE currentIndex = (ubPeriodPos + maxBound) >> 1;
// if this period is greater, skip 'bottom half' of the array and continue searching
if (pPeriods[currentIndex] > uwNote) {
ubPeriodPos = currentIndex + 1;
}
else { // else, the period we seek is between ubPeriodPos and currentIndex
maxBound = currentIndex;
}
}
return 0;
return ubPeriodPos;
}

static void ptSongStep(void) {
Expand Down