From 8eca08c6ac49bb3f1169b780e07e7d26b1a255dd Mon Sep 17 00:00:00 2001 From: twinaphex Date: Tue, 16 May 2017 03:20:39 +0200 Subject: [PATCH] Start using string_is_equal_fast/string_is_not_equal_fast macros instead of straight memcmp --- driver.c | 2 +- dynamic.c | 4 ++-- input/input_config.c | 8 ++++---- libretro-common/file/config_file.c | 8 ++++---- libretro-common/formats/png/rpng.c | 5 +++-- libretro-common/include/string/stdstring.h | 3 +++ libretro-common/lists/dir_list.c | 2 +- libretro-db/libretrodb.c | 12 ++++++------ menu/drivers/xmb.c | 10 +++++----- menu/menu_display.c | 16 ++++++++-------- menu/menu_displaylist.c | 10 +++++----- menu/widgets/menu_osk.c | 10 +++++----- setting_list.c | 4 ++-- tasks/task_database.c | 4 ++-- tasks/task_database_cue.c | 16 ++++++++-------- tasks/task_netplay_find_content.c | 10 +++++----- 16 files changed, 64 insertions(+), 60 deletions(-) diff --git a/driver.c b/driver.c index 4fd383c952..b7024289d2 100644 --- a/driver.c +++ b/driver.c @@ -202,7 +202,7 @@ bool driver_find_next(const char *label, char *s, size_t len) { int i = driver_find_index(label, s); - if (i >= 0 && (memcmp(s, "null", 4) != 0)) + if (i >= 0 && string_is_not_equal_fast(s, "null", 4)) { find_driver_nonempty(label, i + 1, s, len); return true; diff --git a/dynamic.c b/dynamic.c index 8a3deb74b0..b8a58121dc 100644 --- a/dynamic.c +++ b/dynamic.c @@ -906,7 +906,7 @@ static bool dynamic_verify_hw_context(enum retro_hw_context_type type, switch (type) { case RETRO_HW_CONTEXT_VULKAN: - if (memcmp(video_ident, "vulkan", 6) != 0) + if (string_is_not_equal_fast(video_ident, "vulkan", 6)) return false; break; case RETRO_HW_CONTEXT_OPENGLES2: @@ -914,7 +914,7 @@ static bool dynamic_verify_hw_context(enum retro_hw_context_type type, case RETRO_HW_CONTEXT_OPENGLES_VERSION: case RETRO_HW_CONTEXT_OPENGL: case RETRO_HW_CONTEXT_OPENGL_CORE: - if (memcmp(video_ident, "gl", 2) != 0) + if (string_is_not_equal_fast(video_ident, "gl", 2)) return false; break; default: diff --git a/input/input_config.c b/input/input_config.c index 6e2978745f..303084c958 100644 --- a/input/input_config.c +++ b/input/input_config.c @@ -293,13 +293,13 @@ static void parse_hat(struct retro_keybind *bind, const char *str) return; } - if (memcmp(dir, "up", 2) == 0) + if (string_is_equal_fast(dir, "up", 2)) hat_dir = HAT_UP_MASK; - else if (memcmp(dir, "down", 4) == 0) + else if (string_is_equal_fast(dir, "down", 4)) hat_dir = HAT_DOWN_MASK; - else if (memcmp(dir, "left", 4) == 0) + else if (string_is_equal_fast(dir, "left", 4)) hat_dir = HAT_LEFT_MASK; - else if (memcmp(dir, "right", 5) == 0) + else if (string_is_equal_fast(dir, "right", 5)) hat_dir = HAT_RIGHT_MASK; if (hat_dir) diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index 983ef103aa..3c55fce147 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -747,13 +747,13 @@ bool config_get_bool(config_file_t *conf, const char *key, bool *in) if (entry) { - if (memcmp(entry->value, "true", 4) == 0) + if (string_is_equal_fast(entry->value, "true", 4)) *in = true; - else if (memcmp(entry->value, "1", 1) == 0) + else if (string_is_equal_fast(entry->value, "1", 1)) *in = true; - else if (memcmp(entry->value, "false", 5) == 0) + else if (string_is_equal_fast(entry->value, "false", 5)) *in = false; - else if (memcmp(entry->value, "0", 1) == 0) + else if (string_is_equal_fast(entry->value, "0", 1)) *in = false; else return false; diff --git a/libretro-common/formats/png/rpng.c b/libretro-common/formats/png/rpng.c index 3d9877eb6a..56c506d429 100644 --- a/libretro-common/formats/png/rpng.c +++ b/libretro-common/formats/png/rpng.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "rpng_internal.h" @@ -152,7 +153,7 @@ static enum png_chunk_type png_chunk_type(const struct png_chunk *chunk) for (i = 0; i < ARRAY_SIZE(chunk_map); i++) { - if (memcmp(chunk->type, chunk_map[i].id, 4) == 0) + if (string_is_equal_fast(chunk->type, chunk_map[i].id, 4)) return chunk_map[i].type; } @@ -1153,7 +1154,7 @@ bool rpng_start(rpng_t *rpng) for (i = 0; i < 8; i++) header[i] = rpng->buff_data[i]; - if (memcmp(header, png_magic, sizeof(png_magic)) != 0) + if (string_is_not_equal_fast(header, png_magic, sizeof(png_magic))) return false; rpng->buff_data += 8; diff --git a/libretro-common/include/string/stdstring.h b/libretro-common/include/string/stdstring.h index 07bedb931b..845d636948 100644 --- a/libretro-common/include/string/stdstring.h +++ b/libretro-common/include/string/stdstring.h @@ -48,6 +48,9 @@ static INLINE bool string_is_equal(const char *a, const char *b) return (*(const unsigned char*)a - *(const unsigned char*)b) == 0; } +#define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0) +#define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0) + static INLINE bool string_is_equal_noncase(const char *a, const char *b) { int result; diff --git a/libretro-common/lists/dir_list.c b/libretro-common/lists/dir_list.c index 8aa3955064..9e9fcd9310 100644 --- a/libretro-common/lists/dir_list.c +++ b/libretro-common/lists/dir_list.c @@ -124,7 +124,7 @@ static int parse_dir_entry(const char *name, char *file_path, if (!include_dirs && is_dir) return 1; - if ((memcmp(name, ".", 1) == 0) || (memcmp(name, "..", 2) == 0)) + if (string_is_equal_fast(name, ".", 1) || string_is_equal_fast(name, "..", 2)) return 1; if (!is_dir && ext_list && diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 051a307c58..3c8dd8756f 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -273,18 +273,13 @@ static int libretrodb_find_index(libretrodb_t *db, const char *index_name, return -1; } -static int node_compare(const void *a, const void *b, void *ctx) -{ - return memcmp(a, b, *(uint8_t *)ctx); -} - static int binsearch(const void *buff, const void *item, uint64_t count, uint8_t field_size, uint64_t *offset) { int mid = (int)(count / 2); int item_size = field_size + sizeof(uint64_t); uint64_t *current = (uint64_t *)buff + (mid * item_size); - int rv = node_compare(current, item, &field_size); + int rv = memcmp(current, item, field_size); if (rv == 0) { @@ -458,6 +453,11 @@ static uint64_t libretrodb_tell(libretrodb_t *db) return filestream_seek(db->fd, 0, SEEK_CUR); } +static int node_compare(const void *a, const void *b, void *ctx) +{ + return memcmp(a, b, *(uint8_t *)ctx); +} + int libretrodb_create_index(libretrodb_t *db, const char *name, const char *field_name) { diff --git a/menu/drivers/xmb.c b/menu/drivers/xmb.c index 19a95b4a99..3ba4face42 100644 --- a/menu/drivers/xmb.c +++ b/menu/drivers/xmb.c @@ -943,7 +943,7 @@ static void xmb_update_thumbnail_path(void *data, unsigned i) playlist_get_index(playlist, i, NULL, NULL, NULL, &core_name, NULL, NULL); - if (core_name && (memcmp(core_name, "imageviewer", 11) == 0)) + if (core_name && string_is_equal_fast(core_name, "imageviewer", 11)) { strlcpy(xmb->thumbnail_file_path, entry.label, sizeof(xmb->thumbnail_file_path)); @@ -1013,9 +1013,9 @@ static void xmb_update_savestate_thumbnail_path(void *data, unsigned i) xmb->savestate_thumbnail_file_path[0] = '\0'; if ( (settings->bools.savestate_thumbnail_enable) - && ((memcmp(entry.label, "state_slot", 10) == 0) - || (memcmp(entry.label, "loadstate", 9) == 0) - || (memcmp(entry.label, "savestate", 9) == 0))) + && ((string_is_equal_fast(entry.label, "state_slot", 10)) + || (string_is_equal_fast(entry.label, "loadstate", 9)) + || (string_is_equal_fast(entry.label, "savestate", 9)))) { char path[PATH_MAX_LENGTH]; global_t *global = global_get_ptr(); @@ -2488,7 +2488,7 @@ static void xmb_render(void *data, bool is_idle) static bool xmb_shader_pipeline_active(video_frame_info_t *video_info) { - if (memcmp(menu_driver_ident(), "xmb", 3) != 0) + if (string_is_not_equal_fast(menu_driver_ident(), "xmb", 3)) return false; if (video_info->menu_shader_pipeline == XMB_SHADER_PIPELINE_WALLPAPER) return false; diff --git a/menu/menu_display.c b/menu/menu_display.c index 445947d294..d36450fd4c 100644 --- a/menu/menu_display.c +++ b/menu/menu_display.c @@ -126,35 +126,35 @@ static bool menu_display_check_compatibility( case MENU_VIDEO_DRIVER_GENERIC: return true; case MENU_VIDEO_DRIVER_OPENGL: - if (memcmp(video_driver, "gl", 2) == 0) + if (string_is_equal_fast(video_driver, "gl", 2)) return true; break; case MENU_VIDEO_DRIVER_VULKAN: - if (memcmp(video_driver, "vulkan", 6) == 0) + if (string_is_equal_fast(video_driver, "vulkan", 6)) return true; break; case MENU_VIDEO_DRIVER_DIRECT3D: - if (memcmp(video_driver, "d3d", 3) == 0) + if (string_is_equal_fast(video_driver, "d3d", 3)) return true; break; case MENU_VIDEO_DRIVER_VITA2D: - if (memcmp(video_driver, "vita2d", 6) == 0) + if (string_is_equal_fast(video_driver, "vita2d", 6)) return true; break; case MENU_VIDEO_DRIVER_CTR: - if (memcmp(video_driver, "ctr", 3) == 0) + if (string_is_equal_fast(video_driver, "ctr", 3)) return true; break; case MENU_VIDEO_DRIVER_CACA: - if (memcmp(video_driver, "caca", 4) == 0) + if (string_is_equal_fast(video_driver, "caca", 4)) return true; break; case MENU_VIDEO_DRIVER_GDI: - if (memcmp(video_driver, "gdi", 3) == 0) + if (string_is_equal_fast(video_driver, "gdi", 3)) return true; break; case MENU_VIDEO_DRIVER_VGA: - if (memcmp(video_driver, "vga", 3) == 0) + if (string_is_equal_fast(video_driver, "vga", 3)) return true; break; } diff --git a/menu/menu_displaylist.c b/menu/menu_displaylist.c index f83829836d..6cb6b12a4e 100644 --- a/menu/menu_displaylist.c +++ b/menu/menu_displaylist.c @@ -289,7 +289,7 @@ static void print_buf_lines(file_list_t *list, char *buf, if (!string_is_empty(last)) { - if (memcmp(last, "_libretro", 9) != 0) + if (string_is_not_equal_fast(last, "_libretro", 9)) *last = '\0'; } @@ -352,7 +352,7 @@ static int menu_displaylist_parse_netplay( msg_hash_to_str(MENU_ENUM_LABEL_NETPLAY_LAN_SCAN_SETTINGS), MENU_ENUM_LABEL_NETPLAY_LAN_SCAN_SETTINGS, MENU_SETTING_GROUP, 0, 0); - if (memcmp(settings->arrays.menu_driver, "xmb", 3) != 0) + if (string_is_not_equal_fast(settings->arrays.menu_driver, "xmb", 3)) menu_entries_append_enum(info->list, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NETPLAY_TAB), msg_hash_to_str(MENU_ENUM_LABEL_NETPLAY_TAB), @@ -2760,7 +2760,7 @@ static int menu_displaylist_parse_horizontal_list( playlist_qsort(playlist); - if (memcmp(lpl_basename, "content_history", 15) == 0) + if (string_is_equal_fast(lpl_basename, "content_history", 15)) is_historylist = true; menu_displaylist_parse_playlist(info, @@ -5012,7 +5012,7 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type, void *data) info->need_push = true; break; case DISPLAYLIST_WIFI_SETTINGS_LIST: - if (memcmp(settings->arrays.wifi_driver, "null", 4) == 0) + if (string_is_equal_fast(settings->arrays.wifi_driver, "null", 4)) menu_entries_append_enum(info->list, msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_NETWORKS_FOUND), msg_hash_to_str(MENU_ENUM_LABEL_NO_NETWORKS_FOUND), @@ -5632,7 +5632,7 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type, void *data) MENU_ENUM_LABEL_LOGGING_SETTINGS, PARSE_ACTION, false); ret = menu_displaylist_parse_settings_enum(menu, info, MENU_ENUM_LABEL_FRAME_THROTTLE_SETTINGS, PARSE_ACTION, false); - if (memcmp(settings->arrays.record_driver, "null", 4) != 0) + if (string_is_not_equal_fast(settings->arrays.record_driver, "null", 4)) ret = menu_displaylist_parse_settings_enum(menu, info, MENU_ENUM_LABEL_RECORDING_SETTINGS, PARSE_ACTION, false); ret = menu_displaylist_parse_settings_enum(menu, info, diff --git a/menu/widgets/menu_osk.c b/menu/widgets/menu_osk.c index 94b2e0aa18..d853d9d52d 100644 --- a/menu/widgets/menu_osk.c +++ b/menu/widgets/menu_osk.c @@ -98,16 +98,16 @@ void menu_event_osk_append(int ptr) menu_event_set_osk_idx(OSK_LOWERCASE_LATIN); else if (string_is_equal(osk_grid[ptr],"\xe2\x8a\x95")) /* plus sign (next button) */ #else - if (memcmp(osk_grid[ptr], "Bksp", 4) == 0) + if (string_is_equal_fast(osk_grid[ptr], "Bksp", 4)) input_keyboard_event(true, '\x7f', '\x7f', 0, RETRO_DEVICE_KEYBOARD); - else if (memcmp(osk_grid[ptr], "Enter", 5) == 0) + else if (string_is_equal_fast(osk_grid[ptr], "Enter", 5)) input_keyboard_event(true, '\n', '\n', 0, RETRO_DEVICE_KEYBOARD); else - if (memcmp(osk_grid[ptr], "Upper", 5) == 0) + if (string_is_equal_fast(osk_grid[ptr], "Upper", 5)) menu_event_set_osk_idx(OSK_UPPERCASE_LATIN); - else if (memcmp(osk_grid[ptr], "Lower", 5) == 0) + else if (string_is_equal_fast(osk_grid[ptr], "Lower", 5)) menu_event_set_osk_idx(OSK_LOWERCASE_LATIN); - else if (memcmp(osk_grid[ptr], "Next", 4) == 0) + else if (string_is_equal_fast(osk_grid[ptr], "Next", 4)) #endif if (menu_event_get_osk_idx() < OSK_TYPE_LAST - 1) menu_event_set_osk_idx((enum osk_type)(menu_event_get_osk_idx() + 1)); diff --git a/setting_list.c b/setting_list.c index dca4568aae..909ce638e0 100644 --- a/setting_list.c +++ b/setting_list.c @@ -386,9 +386,9 @@ int setting_set_with_string_representation(rarch_setting_t* setting, strlcpy(setting->value.target.string, value, setting->size); break; case ST_BOOL: - if (memcmp(value, "true", 4) == 0) + if (string_is_equal_fast(value, "true", 4)) *setting->value.target.boolean = true; - else if (memcmp(value, "false", 5) == 0) + else if (string_is_equal_fast(value, "false", 5)) *setting->value.target.boolean = false; break; default: diff --git a/tasks/task_database.c b/tasks/task_database.c index 862f8e9956..42f119a65d 100644 --- a/tasks/task_database.c +++ b/tasks/task_database.c @@ -140,13 +140,13 @@ static int iso_get_serial(database_state_handle_t *db_state, if (rv < 0) return rv; - if (memcmp(system_name, "psp", 3) == 0) + if (string_is_equal_fast(system_name, "psp", 3)) { if (detect_psp_game(name, serial) == 0) return 0; RARCH_LOG("%s '%s'\n", msg_hash_to_str(MSG_FOUND_DISK_LABEL), serial); } - else if (memcmp(system_name, "ps1", 3) == 0) + else if (string_is_equal_fast(system_name, "ps1", 3)) { if (detect_ps1_game(name, serial) == 0) return 0; diff --git a/tasks/task_database_cue.c b/tasks/task_database_cue.c index 9e43c965b6..2e61686abf 100644 --- a/tasks/task_database_cue.c +++ b/tasks/task_database_cue.c @@ -278,9 +278,9 @@ int detect_psp_game(const char *track_path, char *game_id) { game_id[5] = '\0'; if ( - (memcmp(game_id, "ULES-", 5) == 0) - || (memcmp(game_id, "ULUS-", 5) == 0) - || (memcmp(game_id, "ULJS-", 5) == 0) + (string_is_equal_fast(game_id, "ULES-", 5)) + || (string_is_equal_fast(game_id, "ULUS-", 5)) + || (string_is_equal_fast(game_id, "ULJS-", 5)) || (memcmp(game_id, "ULEM-", 5) == 0) || (memcmp(game_id, "ULUM-", 5) == 0) @@ -354,7 +354,7 @@ int detect_system(const char *track_path, const char **system_name) goto clean; } - if (memcmp(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN) == 0) + if (string_is_equal_fast(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN)) { *system_name = MAGIC_NUMBERS[i].system_name; rv = 0; @@ -366,7 +366,7 @@ int detect_system(const char *track_path, const char **system_name) if (filestream_read(fd, magic, 8) > 0) { magic[8] = '\0'; - if (memcmp(magic, "PSP GAME", 8) == 0) + if (string_is_equal_fast(magic, "PSP GAME", 8)) { *system_name = "psp\0"; rv = 0; @@ -403,7 +403,7 @@ int find_first_data_track(const char *cue_path, while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) { - if (memcmp(tmp_token, "FILE", 4) == 0) + if (string_is_equal_fast(tmp_token, "FILE", 4)) { char cue_dir[PATH_MAX_LENGTH]; @@ -415,13 +415,13 @@ int find_first_data_track(const char *cue_path, fill_pathname_join(track_path, cue_dir, tmp_token, max_len); } - else if (memcmp(tmp_token, "TRACK", 5) == 0) + else if (string_is_equal_fast(tmp_token, "TRACK", 5)) { int m, s, f; get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); - if (memcmp(tmp_token, "AUDIO", 5) == 0) + if (string_is_equal_fast(tmp_token, "AUDIO", 5)) continue; find_token(fd, "INDEX"); diff --git a/tasks/task_netplay_find_content.c b/tasks/task_netplay_find_content.c index d8eeff7a94..7729767127 100644 --- a/tasks/task_netplay_find_content.c +++ b/tasks/task_netplay_find_content.c @@ -61,7 +61,7 @@ static void netplay_crc_scan_callback(void *task_data, #ifdef HAVE_MENU if (!string_is_empty(state->core_path) && !string_is_empty(state->content_path) && - memcmp(state->content_path, "N/A", 3) != 0) + string_is_not_equal_fast(state->content_path, "N/A", 3)) { command_event(CMD_EVENT_NETPLAY_INIT_DIRECT_DEFERRED, state->hostname); task_push_load_content_with_new_core_from_menu( @@ -73,7 +73,7 @@ static void netplay_crc_scan_callback(void *task_data, else #endif if (!string_is_empty(state->core_path) && !string_is_empty(state->content_path) && - memcmp(state->content_path, "N/A", 3) == 0) + string_is_equal_fast(state->content_path, "N/A", 3)) { content_ctx_info_t content_info = {0}; @@ -115,11 +115,11 @@ static void task_netplay_crc_scan_handler(retro_task_t *task) } if (state->lpl_list->size == 0 && - memcmp(state->content_path, "N/A", 3) != 0) + string_is_not_equal_fast(state->content_path, "N/A", 3)) goto no_playlists; /* Core requires content */ - if (memcmp(state->content_path, "N/A", 3) != 0) + if (string_is_not_equal_fast(state->content_path, "N/A", 3)) { /* CRC matching */ if (!string_is_equal(state->content_crc, "00000000|crc")) @@ -289,7 +289,7 @@ bool task_push_netplay_crc_scan(uint32_t crc, char* name, { strlcpy(state->core_path, info->list[i].path, sizeof(state->core_path)); - if ((memcmp(state->content_path, "N/A", 3) != 0) && + if (string_is_not_equal_fast(state->content_path, "N/A", 3) && !string_is_empty(info->list[i].supported_extensions)) { strlcpy(state->core_extensions,