1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Add functions mp_max and mp_max_into.

These are easy, and just like the existing mp_min family; I just
hadn't needed them before now.
This commit is contained in:
Simon Tatham 2019-01-29 20:02:39 +00:00
parent 715356e6d2
commit d4ad7272fd
4 changed files with 27 additions and 5 deletions

12
mpint.c
View File

@ -2109,6 +2109,11 @@ void mp_min_into(mp_int *r, mp_int *x, mp_int *y)
mp_select_into(r, x, y, mp_cmp_hs(x, y));
}
void mp_max_into(mp_int *r, mp_int *x, mp_int *y)
{
mp_select_into(r, y, x, mp_cmp_hs(x, y));
}
mp_int *mp_min(mp_int *x, mp_int *y)
{
mp_int *r = mp_make_sized(size_t_min(x->nw, y->nw));
@ -2116,6 +2121,13 @@ mp_int *mp_min(mp_int *x, mp_int *y)
return r;
}
mp_int *mp_max(mp_int *x, mp_int *y)
{
mp_int *r = mp_make_sized(size_t_max(x->nw, y->nw));
mp_max_into(r, x, y);
return r;
}
mp_int *mp_power_2(size_t power)
{
mp_int *x = mp_new(power + 1);

View File

@ -152,10 +152,13 @@ unsigned mp_hs_integer(mp_int *x, uintmax_t n);
unsigned mp_eq_integer(mp_int *x, uintmax_t n);
/*
* Take the minimum of two mp_ints, without using a conditional branch.
* Take the minimum or maximum of two mp_ints, without using a
* conditional branch.
*/
void mp_min_into(mp_int *r, mp_int *x, mp_int *y);
void mp_max_into(mp_int *r, mp_int *x, mp_int *y);
mp_int *mp_min(mp_int *x, mp_int *y);
mp_int *mp_max(mp_int *x, mp_int *y);
/*
* Diagnostic function. Writes out x in hex to the supplied stdio

View File

@ -252,11 +252,16 @@ class mpint(MyTestBase):
self.assertEqual(mp_eq_integer(am, bi) == 1, ai == bi)
self.assertEqual(mp_hs_integer(am, bi) == 1, ai >= bi)
# mp_min{,_into} is a reasonable thing to test here as well
# mp_{min,max}{,_into} is a reasonable thing to test
# here as well
self.assertEqual(int(mp_min(am, bm)), min(ai, bi))
am2 = mp_copy(am)
mp_min_into(am2, am, bm)
self.assertEqual(int(am2), min(ai, bi))
self.assertEqual(int(mp_max(am, bm)), max(ai, bi))
am_small = mp_copy(am if ai<bi else bm)
mp_min_into(am_small, am, bm)
self.assertEqual(int(am_small), min(ai, bi))
am_big = mp_copy(am if ai>bi else bm)
mp_max_into(am_big, am, bm)
self.assertEqual(int(am_big), max(ai, bi))
def testConditionals(self):
testnumbers = [(mp_copy(n),n) for n in fibonacci_scattered()]

View File

@ -26,7 +26,9 @@ FUNC2(uint, mp_cmp_eq, val_mpint, val_mpint)
FUNC2(uint, mp_hs_integer, val_mpint, uint)
FUNC2(uint, mp_eq_integer, val_mpint, uint)
FUNC3(void, mp_min_into, val_mpint, val_mpint, val_mpint)
FUNC3(void, mp_max_into, val_mpint, val_mpint, val_mpint)
FUNC2(val_mpint, mp_min, val_mpint, val_mpint)
FUNC2(val_mpint, mp_max, val_mpint, val_mpint)
FUNC2(void, mp_copy_into, val_mpint, val_mpint)
FUNC4(void, mp_select_into, val_mpint, val_mpint, val_mpint, uint)
FUNC3(void, mp_add_into, val_mpint, val_mpint, val_mpint)