Optimize comparisions in file sort functions.

Use three-valued comparison macro from gnulib
efa15594e17fc20827dba66414fb391e99905394.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2020-07-26 19:39:16 +03:00
parent 377476765a
commit 375839d657
2 changed files with 20 additions and 4 deletions

View File

@ -38,6 +38,22 @@
#define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4 #define MC_PIPE_ERROR_CREATE_PIPE_STREAM -4
#define MC_PIPE_ERROR_READ -5 #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 ***************************************************************************************/ /*** enums ***************************************************************************************/
/* Pathname canonicalization */ /* Pathname canonicalization */

View File

@ -393,7 +393,7 @@ sort_time (file_entry_t * a, file_entry_t * b)
if (ad == bd || panels_options.mix_all_files) 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) if (result != 0)
return result * reverse; 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) 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) if (result != 0)
return result * reverse; 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) 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) if (result != 0)
return result * reverse; 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) 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) if (result != 0)
return result * reverse; return result * reverse;