[PATCH 4/4] mpi/generic: use longlong.h for carry handling
Jussi Kivilinna
jussi.kivilinna at iki.fi
Sat Jun 22 16:40:50 CEST 2024
* mpi/generic/mpih-add1.c (_gcry_mpih_add_n): Use add_ssaaaa for
carry handling.
* mpi/generic/mpih-mul1.c (_gcry_mpih_mul_1): Likewise.
* mpi/generic/mpih-mul2.c (_gcry_mpih_addmul_1): Likewise.
* mpi/generic/mpih-mul3.c (_gcry_mpih_submul_1): Use add_ssaaaa
and sub_ddmmss for carry handling.
* mpi/generic/mpih-sub1.c (_gcry_mpih_sub_n): Likewise.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
mpi/generic/mpih-add1.c | 15 ++++++++-------
mpi/generic/mpih-mul1.c | 7 +++----
mpi/generic/mpih-mul2.c | 13 +++++--------
mpi/generic/mpih-mul3.c | 14 ++++++--------
mpi/generic/mpih-sub1.c | 16 +++++++++-------
5 files changed, 31 insertions(+), 34 deletions(-)
diff --git a/mpi/generic/mpih-add1.c b/mpi/generic/mpih-add1.c
index 0a51f06c..4c95609b 100644
--- a/mpi/generic/mpih-add1.c
+++ b/mpi/generic/mpih-add1.c
@@ -1,5 +1,5 @@
/* mpihelp-add_1.c - MPI helper functions
- * Copyright (C) 1994, 1996, 1997, 1998,
+ * Copyright (C) 1994, 1996, 1997, 1998,
* 2000, 2002 Free Software Foundation, Inc.
*
* This file is part of Libgcrypt.
@@ -48,16 +48,17 @@ _gcry_mpih_add_n (mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
res_ptr -= j;
cy = 0;
- do
+ do
{
y = s2_ptr[j];
x = s1_ptr[j];
- y += cy; /* add previous carry to one addend */
- cy = y < cy; /* get out carry from that addition */
- y += x; /* add other addend */
- cy += y < x; /* get out carry from that add, combine */
+ /* Add previous carry to one addend and get out carry from
+ * that addition. */
+ add_ssaaaa (cy, y, 0, y, 0, cy);
+ /* Add other addend and get out carry from that add, combine. */
+ add_ssaaaa (cy, y, cy, y, 0, x);
res_ptr[j] = y;
- }
+ }
while ( ++j );
return cy;
diff --git a/mpi/generic/mpih-mul1.c b/mpi/generic/mpih-mul1.c
index e88be914..accae1dc 100644
--- a/mpi/generic/mpih-mul1.c
+++ b/mpi/generic/mpih-mul1.c
@@ -48,13 +48,12 @@ _gcry_mpih_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
res_ptr -= j;
cy_limb = 0;
- do
+ do
{
umul_ppmm( prod_high, prod_low, s1_ptr[j], s2_limb );
- prod_low += cy_limb;
- cy_limb = (prod_low < cy_limb?1:0) + prod_high;
+ add_ssaaaa( cy_limb, prod_low, prod_high, prod_low, 0, cy_limb );
res_ptr[j] = prod_low;
- }
+ }
while( ++j );
return cy_limb;
diff --git a/mpi/generic/mpih-mul2.c b/mpi/generic/mpih-mul2.c
index bc925e01..931438dd 100644
--- a/mpi/generic/mpih-mul2.c
+++ b/mpi/generic/mpih-mul2.c
@@ -48,20 +48,17 @@ _gcry_mpih_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
s1_ptr -= j;
cy_limb = 0;
- do
+ do
{
umul_ppmm( prod_high, prod_low, s1_ptr[j], s2_limb );
-
- prod_low += cy_limb;
- cy_limb = (prod_low < cy_limb?1:0) + prod_high;
+ add_ssaaaa( cy_limb, prod_low, prod_high, prod_low, 0, cy_limb );
x = res_ptr[j];
- prod_low = x + prod_low;
- cy_limb += prod_low < x?1:0;
+ add_ssaaaa( cy_limb, prod_low, cy_limb, prod_low, 0, x );
res_ptr[j] = prod_low;
- }
+ }
while ( ++j );
-
+
return cy_limb;
}
diff --git a/mpi/generic/mpih-mul3.c b/mpi/generic/mpih-mul3.c
index 4ecd2c6b..7372c845 100644
--- a/mpi/generic/mpih-mul3.c
+++ b/mpi/generic/mpih-mul3.c
@@ -36,7 +36,7 @@ mpi_limb_t
_gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi_size_t s1_size, mpi_limb_t s2_limb)
{
- mpi_limb_t cy_limb;
+ mpi_limb_t cy_limb, borrow;
mpi_size_t j;
mpi_limb_t prod_high, prod_low;
mpi_limb_t x;
@@ -48,18 +48,16 @@ _gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
s1_ptr -= j;
cy_limb = 0;
- do
+ do
{
umul_ppmm( prod_high, prod_low, s1_ptr[j], s2_limb);
-
- prod_low += cy_limb;
- cy_limb = (prod_low < cy_limb?1:0) + prod_high;
+ add_ssaaaa( cy_limb, prod_low, prod_high, prod_low, 0, cy_limb );
x = res_ptr[j];
- prod_low = x - prod_low;
- cy_limb += prod_low > x?1:0;
+ sub_ddmmss( borrow, prod_low, 0, x, 0, prod_low );
+ cy_limb -= borrow;
res_ptr[j] = prod_low;
- }
+ }
while( ++j );
return cy_limb;
diff --git a/mpi/generic/mpih-sub1.c b/mpi/generic/mpih-sub1.c
index b8eff0db..bb51487c 100644
--- a/mpi/generic/mpih-sub1.c
+++ b/mpi/generic/mpih-sub1.c
@@ -35,7 +35,7 @@ mpi_limb_t
_gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
mpi_ptr_t s2_ptr, mpi_size_t size)
{
- mpi_limb_t x, y, cy;
+ mpi_limb_t x, y, cy, borrow;
mpi_size_t j;
/* The loop counter and index J goes from -SIZE to -1. This way
@@ -48,16 +48,18 @@ _gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
res_ptr -= j;
cy = 0;
- do
+ do
{
y = s2_ptr[j];
x = s1_ptr[j];
- y += cy; /* add previous carry to subtrahend */
- cy = y < cy; /* get out carry from that addition */
- y = x - y; /* main subtract */
- cy += y > x; /* get out carry from the subtract, combine */
+ /* Add previous carry to subtrahend and get out carry from
+ * that addition. */
+ add_ssaaaa (cy, y, 0, y, 0, cy);
+ /* Main subtract and get out carry from the subtract, combine. */
+ sub_ddmmss( borrow, y, 0, x, 0, y );
+ cy -= borrow;
res_ptr[j] = y;
- }
+ }
while( ++j );
return cy;
--
2.43.0
More information about the Gcrypt-devel
mailing list