1
/* Alternate implementations of modlimb_invert to compare speeds. */
4
Copyright 2000 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 2.1 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; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31
/* Like the standard version in gmp-impl.h, but with the expressions using a
32
"1-" form. This has the same number of steps, but "1-" is on the
33
dependent chain, whereas the "2*" in the standard version isn't.
34
Depending on the CPU this should be the same or a touch slower. */
36
#if BITS_PER_MP_LIMB <= 32
37
#define modlimb_invert_mul1(inv,n) \
39
mp_limb_t __n = (n); \
41
ASSERT ((__n & 1) == 1); \
42
__inv = modlimb_invert_table[(__n&0xFF)/2]; /* 8 */ \
43
__inv = (1 - __n * __inv) * __inv + __inv; /* 16 */ \
44
__inv = (1 - __n * __inv) * __inv + __inv; /* 32 */ \
45
ASSERT (__inv * __n == 1); \
50
#if BITS_PER_MP_LIMB > 32 && BITS_PER_MP_LIMB <= 64
51
#define modlimb_invert_mul1(inv,n) \
53
mp_limb_t __n = (n); \
55
ASSERT ((__n & 1) == 1); \
56
__inv = modlimb_invert_table[(__n&0xFF)/2]; /* 8 */ \
57
__inv = (1 - __n * __inv) * __inv + __inv; /* 16 */ \
58
__inv = (1 - __n * __inv) * __inv + __inv; /* 32 */ \
59
__inv = (1 - __n * __inv) * __inv + __inv; /* 64 */ \
60
ASSERT (__inv * __n == 1); \
66
/* The loop based version used in GMP 3.0 and earlier. Usually slower than
67
multiplying, due to the number of steps that must be performed. Much
68
slower when the processor has a good multiply. */
70
#define modlimb_invert_loop(inv,n) \
72
mp_limb_t __v = (n); \
73
mp_limb_t __v_orig = __v; \
74
mp_limb_t __make_zero = 1; \
75
mp_limb_t __two_i = 1; \
76
mp_limb_t __v_inv = 0; \
78
ASSERT ((__v & 1) == 1); \
82
while ((__two_i & __make_zero) == 0) \
83
__two_i <<= 1, __v <<= 1; \
87
while (__make_zero); \
89
ASSERT (__v_orig * __v_inv == 1); \
94
/* Another loop based version with conditionals, but doing a fixed number of
97
#define modlimb_invert_cond(inv,n) \
99
mp_limb_t __n = (n); \
100
mp_limb_t __rem = (1 - __n) >> 1; \
101
mp_limb_t __inv = MP_LIMB_T_HIGHBIT; \
104
ASSERT ((__n & 1) == 1); \
106
__count = BITS_PER_MP_LIMB-1; \
112
__inv |= MP_LIMB_T_HIGHBIT; \
117
while (-- __count); \
119
ASSERT (__inv * __n == 1); \
124
/* Another loop based bitwise version, but purely arithmetic, no
127
#define modlimb_invert_arith(inv,n) \
129
mp_limb_t __n = (n); \
130
mp_limb_t __rem = (1 - __n) >> 1; \
131
mp_limb_t __inv = MP_LIMB_T_HIGHBIT; \
132
mp_limb_t __lowbit; \
135
ASSERT ((__n & 1) == 1); \
137
__count = BITS_PER_MP_LIMB-1; \
140
__lowbit = __rem & 1; \
141
__inv = (__inv >> 1) | (__lowbit << (BITS_PER_MP_LIMB-1)); \
142
__rem = (__rem - (__n & -__lowbit)) >> 1; \
144
while (-- __count); \
146
ASSERT (__inv * __n == 1); \
152
speed_modlimb_invert_mul1 (struct speed_params *s)
154
SPEED_ROUTINE_MODLIMB_INVERT (modlimb_invert_mul1);
157
speed_modlimb_invert_loop (struct speed_params *s)
159
SPEED_ROUTINE_MODLIMB_INVERT (modlimb_invert_loop);
162
speed_modlimb_invert_cond (struct speed_params *s)
164
SPEED_ROUTINE_MODLIMB_INVERT (modlimb_invert_cond);
167
speed_modlimb_invert_arith (struct speed_params *s)
169
SPEED_ROUTINE_MODLIMB_INVERT (modlimb_invert_arith);