From 36d4b664adba3a927b1708d68f4754e90997b42f Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 12 Nov 2018 13:59:06 -0500 Subject: Add mpn_get_base256 Converts limbs to uint8_t buffer without conditional jumps. Signed-off-by: Simo Sorce --- gmp-glue.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'gmp-glue.c') diff --git a/gmp-glue.c b/gmp-glue.c index c44332df..805b50c4 100644 --- a/gmp-glue.c +++ b/gmp-glue.c @@ -246,6 +246,37 @@ mpn_set_base256_le (mp_limb_t *rp, mp_size_t rn, } } +void +mpn_get_base256 (uint8_t *rp, size_t rn, + const mp_limb_t *xp, mp_size_t xn) +{ + unsigned bits; + mp_limb_t in; + for (bits = in = 0; xn > 0 && rn > 0; ) + { + if (bits >= 8) + { + rp[--rn] = in; + in >>= 8; + bits -= 8; + } + else + { + uint8_t old = in; + in = *xp++; + xn--; + rp[--rn] = old | (in << bits); + in >>= (8 - bits); + bits += GMP_NUMB_BITS - 8; + } + } + while (rn > 0) + { + rp[--rn] = in; + in >>= 8; + } +} + void mpn_get_base256_le (uint8_t *rp, size_t rn, const mp_limb_t *xp, mp_size_t xn) -- cgit v1.2.1