From 15b7c542dc44899e8db7addfcc2f1c1c4a188b49 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 19 Feb 2019 06:39:02 -0800 Subject: mpi/arith: optimize "highest bit" with GCC builtins. * arith.c (highest_bit): On GCC, use __builtin_clz. * mpi/mpi.c (s_highest_bit): Likewise. --- mpi/mpi.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'mpi') diff --git a/mpi/mpi.c b/mpi/mpi.c index 6aa905bb..579a43a6 100644 --- a/mpi/mpi.c +++ b/mpi/mpi.c @@ -2949,7 +2949,13 @@ void s_mp_clamp(mp_int *mp) static mp_size s_highest_bit(mp_digit n) { -#if MP_DIGIT_SIZE == 8 +#if defined __GNUC__ && MP_DIGIT_SIZE == SIZEOF_INT + return (n == 0) ? 0 : (MP_DIGIT_BIT - __builtin_clz(n)); +#elif defined __GNUC__ && MP_DIGIT_SIZE == SIZEOF_LONG + return (n == 0) ? 0 : (MP_DIGIT_BIT - __builtin_clzl(n)); +#elif defined __GNUC__ && MP_DIGIT_SIZE == SIZEOF_LONGLONG_T + return (n == 0) ? 0 : (MP_DIGIT_BIT - __builtin_clzll(n)); +#elif MP_DIGIT_SIZE == 8 if (n & 0xFFFFFFFF00000000) { if (n & 0xFFFF000000000000) { if (n & 0xFF00000000000000) { -- cgit v1.2.3