diff --git a/lib/util.h b/lib/util.h index 46ac1f0b4..5280793c6 100644 --- a/lib/util.h +++ b/lib/util.h @@ -38,6 +38,22 @@ #define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4 #define MC_PIPE_ERROR_READ -5 +/* gnulib efa15594e17fc20827dba66414fb391e99905394 + + *_GL_CMP (n1, n2) performs a three-valued comparison on n1 vs. n2. + * It returns + * 1 if n1 > n2 + * 0 if n1 == n2 + * -1 if n1 < n2 + * The native code (n1 > n2 ? 1 : n1 < n2 ? -1 : 0) produces a conditional + * jump with nearly all GCC versions up to GCC 10. + * This variant (n1 < n2 ? -1 : n1 > n2) produces a conditional with many + * GCC versions up to GCC 9. + * The better code (n1 > n2) - (n1 < n2) from Hacker's Delight para 2-9 + * avoids conditional jumps in all GCC versions >= 3.4. + */ +#define _GL_CMP(n1, n2) (((n1) > (n2)) - ((n1) < (n2))) + /*** enums ***************************************************************************************/ /* Pathname canonicalization */ diff --git a/src/filemanager/dir.c b/src/filemanager/dir.c index 9eec072d9..92b616668 100644 --- a/src/filemanager/dir.c +++ b/src/filemanager/dir.c @@ -393,7 +393,7 @@ sort_time (file_entry_t * a, file_entry_t * b) if (ad == bd || panels_options.mix_all_files) { - int result = a->st.st_mtime < b->st.st_mtime ? -1 : a->st.st_mtime > b->st.st_mtime; + int result = _GL_CMP (a->st.st_mtime, b->st.st_mtime); if (result != 0) return result * reverse; @@ -414,7 +414,7 @@ sort_ctime (file_entry_t * a, file_entry_t * b) if (ad == bd || panels_options.mix_all_files) { - int result = a->st.st_ctime < b->st.st_ctime ? -1 : a->st.st_ctime > b->st.st_ctime; + int result = _GL_CMP (a->st.st_ctime, b->st.st_ctime); if (result != 0) return result * reverse; @@ -435,7 +435,7 @@ sort_atime (file_entry_t * a, file_entry_t * b) if (ad == bd || panels_options.mix_all_files) { - int result = a->st.st_atime < b->st.st_atime ? -1 : a->st.st_atime > b->st.st_atime; + int result = _GL_CMP (a->st.st_atime, b->st.st_atime); if (result != 0) return result * reverse; @@ -470,7 +470,7 @@ sort_size (file_entry_t * a, file_entry_t * b) if (ad == bd || panels_options.mix_all_files) { - int result = a->st.st_size < b->st.st_size ? -1 : a->st.st_size > b->st.st_size; + int result = _GL_CMP (a->st.st_size, b->st.st_size); if (result != 0) return result * reverse;