[PATCH 2/3] mpih-const-time: avoid branches in _gcry_mpih_cmp_ui

Jussi Kivilinna <[email protected]>
Newsgroups gmane.comp.encryption.gpg.libgcrypt.devel
Message-ID <[email protected]>
* mpi/mpih-const-time.c (_gcry_mpih_cmp_ui): Avoid conditional
branches for return value selection.
--

Signed-off-by: Jussi Kivilinna <[email protected]>
---
 mpi/mpih-const-time.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/mpi/mpih-const-time.c b/mpi/mpih-const-time.c
index e684b956..d8b66c46 100644
--- a/mpi/mpih-const-time.c
+++ b/mpi/mpih-const-time.c
@@ -222,20 +222,15 @@ _gcry_mpih_mod_lli (mpi_ptr_t vp, mpi_size_t vsize,
 int
 _gcry_mpih_cmp_ui (mpi_ptr_t up, mpi_size_t usize, unsigned long v)
 {
-  int is_all_zero = 1;
+  unsigned long is_all_zero = ct_ulong_gen_mask(1);
+  int cmp0;
   mpi_size_t i;
 
+  cmp0 = -mpih_ct_limb_less_than (up[0], v);
+  cmp0 |= mpih_ct_limb_greater_than (up[0], v);
+
   for (i = 1; i < usize; i++)
-    is_all_zero &= mpih_limb_is_zero (up[i]);
+    is_all_zero &= ct_ulong_gen_mask(mpih_limb_is_zero (up[i]));
 
-  if (is_all_zero)
-    {
-      if (up[0] < v)
-        return -1;
-      else if (up[0] > v)
-        return 1;
-      else
-        return 0;
-    }
-  return 1;
+  return cmp0 & (int)is_all_zero;
 }
-- 
2.45.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.