1 /* Alternate implementations of binvert_limb to compare speeds. */
4 Copyright 2000, 2002 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 /* Like the standard version in gmp-impl.h, but with the expressions using a
29 "1-" form. This has the same number of steps, but "1-" is on the
30 dependent chain, whereas the "2*" in the standard version isn't.
31 Depending on the CPU this should be the same or a touch slower. */
33 #if GMP_LIMB_BITS <= 32
34 #define binvert_limb_mul1(inv,n) \
36 mp_limb_t __n = (n); \
38 ASSERT ((__n & 1) == 1); \
39 __inv = binvert_limb_table[(__n&0xFF)/2]; /* 8 */ \
40 __inv = (1 - __n * __inv) * __inv + __inv; /* 16 */ \
41 __inv = (1 - __n * __inv) * __inv + __inv; /* 32 */ \
42 ASSERT (__inv * __n == 1); \
47 #if GMP_LIMB_BITS > 32 && GMP_LIMB_BITS <= 64
48 #define binvert_limb_mul1(inv,n) \
50 mp_limb_t __n = (n); \
52 ASSERT ((__n & 1) == 1); \
53 __inv = binvert_limb_table[(__n&0xFF)/2]; /* 8 */ \
54 __inv = (1 - __n * __inv) * __inv + __inv; /* 16 */ \
55 __inv = (1 - __n * __inv) * __inv + __inv; /* 32 */ \
56 __inv = (1 - __n * __inv) * __inv + __inv; /* 64 */ \
57 ASSERT (__inv * __n == 1); \
63 /* The loop based version used in GMP 3.0 and earlier. Usually slower than
64 multiplying, due to the number of steps that must be performed. Much
65 slower when the processor has a good multiply. */
67 #define binvert_limb_loop(inv,n) \
69 mp_limb_t __v = (n); \
70 mp_limb_t __v_orig = __v; \
71 mp_limb_t __make_zero = 1; \
72 mp_limb_t __two_i = 1; \
73 mp_limb_t __v_inv = 0; \
75 ASSERT ((__v & 1) == 1); \
79 while ((__two_i & __make_zero) == 0) \
80 __two_i <<= 1, __v <<= 1; \
84 while (__make_zero); \
86 ASSERT (__v_orig * __v_inv == 1); \
91 /* Another loop based version with conditionals, but doing a fixed number of
94 #define binvert_limb_cond(inv,n) \
96 mp_limb_t __n = (n); \
97 mp_limb_t __rem = (1 - __n) >> 1; \
98 mp_limb_t __inv = GMP_LIMB_HIGHBIT; \
101 ASSERT ((__n & 1) == 1); \
103 __count = GMP_LIMB_BITS-1; \
109 __inv |= GMP_LIMB_HIGHBIT; \
114 while (-- __count); \
116 ASSERT (__inv * __n == 1); \
121 /* Another loop based bitwise version, but purely arithmetic, no
124 #define binvert_limb_arith(inv,n) \
126 mp_limb_t __n = (n); \
127 mp_limb_t __rem = (1 - __n) >> 1; \
128 mp_limb_t __inv = GMP_LIMB_HIGHBIT; \
129 mp_limb_t __lowbit; \
132 ASSERT ((__n & 1) == 1); \
134 __count = GMP_LIMB_BITS-1; \
137 __lowbit = __rem & 1; \
138 __inv = (__inv >> 1) | (__lowbit << (GMP_LIMB_BITS-1)); \
139 __rem = (__rem - (__n & -__lowbit)) >> 1; \
141 while (-- __count); \
143 ASSERT (__inv * __n == 1); \
149 speed_binvert_limb_mul1 (struct speed_params *s)
151 SPEED_ROUTINE_MODLIMB_INVERT (binvert_limb_mul1);
154 speed_binvert_limb_loop (struct speed_params *s)
156 SPEED_ROUTINE_MODLIMB_INVERT (binvert_limb_loop);
159 speed_binvert_limb_cond (struct speed_params *s)
161 SPEED_ROUTINE_MODLIMB_INVERT (binvert_limb_cond);
164 speed_binvert_limb_arith (struct speed_params *s)
166 SPEED_ROUTINE_MODLIMB_INVERT (binvert_limb_arith);