From 760515c551723fe14f650194f6f4f5ac049b1b8c Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Wed, 15 Jan 2025 04:36:00 +0300 Subject: [PATCH] minor optimization of strncmp for literal comparison --- src/twn_audio.c | 20 +++++++++++--------- src/twn_loop.c | 11 ++++++----- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/twn_audio.c b/src/twn_audio.c index 1dc93a1..53d236a 100644 --- a/src/twn_audio.c +++ b/src/twn_audio.c @@ -22,6 +22,12 @@ static const char *audio_exts[AUDIO_FILE_TYPE_COUNT] = { ".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: 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) { - 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) { - size_t ext_length = SDL_strlen(audio_exts[i]); - if (path_len <= ext_length) - continue; - - if (SDL_strcmp(&path[path_len - ext_length], audio_exts[i]) == 0) + if (SDL_strncmp(&path[path_len - audio_exts_len[i]], audio_exts[i], audio_exts_len[i]) == 0) return (AudioFileType)i; } @@ -297,17 +299,17 @@ TWN_API void audio_parameter(const char *channel, const char *param, float value return; } - if (SDL_strcmp(param, "repeat") == 0) { + if (SDL_strncmp(param, "repeat", sizeof "repeat" - 1) == 0) { 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) { log_warn("Out of range volume for channel %s set", channel); value = clampf(value, 0.0f, 1.0f); } 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) { log_warn("Out of range panning for channel %s set", channel); value = clampf(value, -1.0f, +1.0f); diff --git a/src/twn_loop.c b/src/twn_loop.c index 62d7da0..c16fdcd 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -215,6 +215,7 @@ static void main_loop(void) { game_object_tick(); input_state_update_postframe(&ctx.input); + /* TODO: make it works when ctx.ticks_per_second != 60 */ #ifdef TWN_FEATURE_PUSH_AUDIO static uint8_t audio_buffer[(AUDIO_FREQUENCY / 60) * sizeof (float) * 2]; audio_callback(NULL, audio_buffer, sizeof audio_buffer); @@ -627,9 +628,9 @@ static bool initialize(void) { if (!datum_font_filtering.ok) { ctx.font_filtering = TEXT_FONT_FILTERING_DEFAULT; } 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; - } 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; } else { 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) { /* 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) { CRY("Data dir mount override failed.", "No arguments passed (expected 1)."); return EXIT_FAILURE; @@ -744,13 +745,13 @@ int enter_loop(int argc, char **argv) { } /* force debug mode */ - if (SDL_strcmp(argv[i], "--debug") == 0) { + if (SDL_strncmp(argv[i], "--debug", sizeof "--debug" - 1) == 0) { force_debug = true; continue; } /* force release mode */ - if (SDL_strcmp(argv[i], "--release") == 0) { + if (SDL_strncmp(argv[i], "--release", sizeof "--release" - 1) == 0) { force_release = false; continue; }