minor optimization of strncmp for literal comparison

This commit is contained in:
veclavtalica 2025-01-15 04:36:00 +03:00
parent 9d0a2cab81
commit 760515c551
2 changed files with 17 additions and 14 deletions

View File

@ -22,6 +22,12 @@ static const char *audio_exts[AUDIO_FILE_TYPE_COUNT] = {
".xm", /* AUDIO_FILE_TYPE_XM */ ".xm", /* AUDIO_FILE_TYPE_XM */
}; };
static const uint8_t audio_exts_len[AUDIO_FILE_TYPE_COUNT] = {
sizeof ".ogg" - 1,
sizeof ".wav" - 1,
sizeof ".xm" - 1,
};
/* TODO: allow for vectorization and packed vectors (alignment care and alike) */ /* TODO: allow for vectorization and packed vectors (alignment care and alike) */
/* TODO: count frames without use, free the memory when threshold is met */ /* TODO: count frames without use, free the memory when threshold is met */
@ -59,14 +65,10 @@ static int64_t get_audio_data(const char *path, unsigned char **data) {
static AudioFileType infer_audio_file_type(const char *path) { static AudioFileType infer_audio_file_type(const char *path) {
size_t path_len = SDL_strlen(path); size_t const path_len = SDL_strlen(path);
for (int i = 0; i < AUDIO_FILE_TYPE_COUNT; ++i) { for (int i = 0; i < AUDIO_FILE_TYPE_COUNT; ++i) {
size_t ext_length = SDL_strlen(audio_exts[i]); if (SDL_strncmp(&path[path_len - audio_exts_len[i]], audio_exts[i], audio_exts_len[i]) == 0)
if (path_len <= ext_length)
continue;
if (SDL_strcmp(&path[path_len - ext_length], audio_exts[i]) == 0)
return (AudioFileType)i; return (AudioFileType)i;
} }
@ -297,17 +299,17 @@ TWN_API void audio_parameter(const char *channel, const char *param, float value
return; return;
} }
if (SDL_strcmp(param, "repeat") == 0) { if (SDL_strncmp(param, "repeat", sizeof "repeat" - 1) == 0) {
pair->value.repeat = (bool)value; pair->value.repeat = (bool)value;
} else if (SDL_strcmp(param, "volume") == 0) { } else if (SDL_strncmp(param, "volume", sizeof "volume" - 1) == 0) {
if (value > 1.0f || value < 0.0f) { if (value > 1.0f || value < 0.0f) {
log_warn("Out of range volume for channel %s set", channel); log_warn("Out of range volume for channel %s set", channel);
value = clampf(value, 0.0f, 1.0f); value = clampf(value, 0.0f, 1.0f);
} }
pair->value.volume = value; pair->value.volume = value;
} else if (SDL_strcmp(param, "panning") == 0) { } else if (SDL_strncmp(param, "panning", sizeof "panning" - 1) == 0) {
if (value > 1.0f || value < -1.0f) { if (value > 1.0f || value < -1.0f) {
log_warn("Out of range panning for channel %s set", channel); log_warn("Out of range panning for channel %s set", channel);
value = clampf(value, -1.0f, +1.0f); value = clampf(value, -1.0f, +1.0f);

View File

@ -215,6 +215,7 @@ static void main_loop(void) {
game_object_tick(); game_object_tick();
input_state_update_postframe(&ctx.input); input_state_update_postframe(&ctx.input);
/* TODO: make it works when ctx.ticks_per_second != 60 */
#ifdef TWN_FEATURE_PUSH_AUDIO #ifdef TWN_FEATURE_PUSH_AUDIO
static uint8_t audio_buffer[(AUDIO_FREQUENCY / 60) * sizeof (float) * 2]; static uint8_t audio_buffer[(AUDIO_FREQUENCY / 60) * sizeof (float) * 2];
audio_callback(NULL, audio_buffer, sizeof audio_buffer); audio_callback(NULL, audio_buffer, sizeof audio_buffer);
@ -627,9 +628,9 @@ static bool initialize(void) {
if (!datum_font_filtering.ok) { if (!datum_font_filtering.ok) {
ctx.font_filtering = TEXT_FONT_FILTERING_DEFAULT; ctx.font_filtering = TEXT_FONT_FILTERING_DEFAULT;
} else { } else {
if (SDL_strcmp(datum_font_filtering.u.s, "nearest") == 0) { if (SDL_strncmp(datum_font_filtering.u.s, "nearest", sizeof "nearest") == 0) {
ctx.font_filtering = TEXTURE_FILTER_NEAREAST; ctx.font_filtering = TEXTURE_FILTER_NEAREAST;
} else if (SDL_strcmp(datum_font_filtering.u.s, "linear") == 0) { } else if (SDL_strncmp(datum_font_filtering.u.s, "linear", sizeof "nearest") == 0) {
ctx.font_filtering = TEXTURE_FILTER_LINEAR; ctx.font_filtering = TEXTURE_FILTER_LINEAR;
} else { } else {
ctx.font_filtering = TEXT_FONT_FILTERING_DEFAULT; ctx.font_filtering = TEXT_FONT_FILTERING_DEFAULT;
@ -727,7 +728,7 @@ int enter_loop(int argc, char **argv) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
/* override data directory */ /* override data directory */
if (SDL_strcmp(argv[i], "--data-dir") == 0) { if (SDL_strncmp(argv[i], "--data-dir", sizeof "--data-dir" - 1) == 0) {
if (argv[i+1] == NULL || SDL_strncmp(argv[i+1], "--", 2) == 0) { if (argv[i+1] == NULL || SDL_strncmp(argv[i+1], "--", 2) == 0) {
CRY("Data dir mount override failed.", "No arguments passed (expected 1)."); CRY("Data dir mount override failed.", "No arguments passed (expected 1).");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -744,13 +745,13 @@ int enter_loop(int argc, char **argv) {
} }
/* force debug mode */ /* force debug mode */
if (SDL_strcmp(argv[i], "--debug") == 0) { if (SDL_strncmp(argv[i], "--debug", sizeof "--debug" - 1) == 0) {
force_debug = true; force_debug = true;
continue; continue;
} }
/* force release mode */ /* force release mode */
if (SDL_strcmp(argv[i], "--release") == 0) { if (SDL_strncmp(argv[i], "--release", sizeof "--release" - 1) == 0) {
force_release = false; force_release = false;
continue; continue;
} }