aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-04-10 12:10:46 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-04-10 12:10:46 +0200
commitdd3a3950531396296f062d4764f26c32a803458a (patch)
treedbf2a5fbe44124a93e64cf414d01b345f9875400 /coreutils
parent4908c79a018cdea41d2899ebcef59831117a07cd (diff)
downloadbusybox-w32-dd3a3950531396296f062d4764f26c32a803458a.tar.gz
busybox-w32-dd3a3950531396296f062d4764f26c32a803458a.tar.bz2
busybox-w32-dd3a3950531396296f062d4764f26c32a803458a.zip
factor: 25% faster sieving
function old new delta factorize 287 260 -27 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/factor.c67
1 files changed, 48 insertions, 19 deletions
diff --git a/coreutils/factor.c b/coreutils/factor.c
index aa2f63089..4a131813b 100644
--- a/coreutils/factor.c
+++ b/coreutils/factor.c
@@ -96,9 +96,27 @@ static NOINLINE void factorize(wide_t N)
96{ 96{
97 half_t factor; 97 half_t factor;
98 half_t max_factor; 98 half_t max_factor;
99 unsigned count3; 99 // unsigned count3;
100 unsigned count5; 100 // unsigned count5;
101 unsigned count7; 101 // unsigned count7;
102 // ^^^^^^^^^^^^^^^ commented-out simple siving code (easier to grasp).
103 // Faster sieving, using one word for potentially up to 6 counters:
104 // count upwards in each mask, counter "triggers" when it sets its mask to "100[0]..."
105 // 10987654321098765432109876543210 - bits 31-0 in 32-bit word
106 // 17777713333311111777775555333 - bit masks for counters for primes 3,5,7,11,13,17
107 // 100000100001000010001001 - value for adding 1 to each mask
108 // 10000010000010000100001000100 - value for checking that any mask reached msb
109 enum {
110 SHIFT_3 = 1 << 0,
111 SHIFT_5 = 1 << 3,
112 SHIFT_7 = 1 << 7,
113 INCREMENT_EACH = SHIFT_3 | SHIFT_5 | SHIFT_7,
114 MULTIPLE_OF_3 = 1 << 2,
115 MULTIPLE_OF_5 = 1 << 6,
116 MULTIPLE_OF_7 = 1 << 11,
117 MULTIPLE_3_5_7 = MULTIPLE_OF_3 | MULTIPLE_OF_5 | MULTIPLE_OF_7,
118 };
119 unsigned sieve_word;
102 120
103 if (N < 4) 121 if (N < 4)
104 goto end; 122 goto end;
@@ -119,9 +137,14 @@ static NOINLINE void factorize(wide_t N)
119 * 589959129 - about 100 million iterations. 137 * 589959129 - about 100 million iterations.
120 */ 138 */
121 max_factor = isqrt_odd(N); 139 max_factor = isqrt_odd(N);
122 count3 = 3; 140 // count3 = 3;
123 count5 = 6; 141 // count5 = 6;
124 count7 = 9; 142 // count7 = 9;
143 sieve_word = 0
144 + (MULTIPLE_OF_3 - 3 * SHIFT_3)
145 + (MULTIPLE_OF_5 - 6 * SHIFT_5)
146 + (MULTIPLE_OF_7 - 9 * SHIFT_7)
147 ;
125 factor = 3; 148 factor = 3;
126 for (;;) { 149 for (;;) {
127 /* The division is the most costly part of the loop. 150 /* The division is the most costly part of the loop.
@@ -143,10 +166,13 @@ static NOINLINE void factorize(wide_t N)
143 * (^ = primes, _ = would-be-primes-if-not-divisible-by-5) 166 * (^ = primes, _ = would-be-primes-if-not-divisible-by-5)
144 * The numbers with space under them are excluded by sieve 3. 167 * The numbers with space under them are excluded by sieve 3.
145 */ 168 */
146 count7--; 169 // count7--;
147 count5--; 170 // count5--;
148 count3--; 171 // count3--;
149 if (count3 && count5 && count7) 172 // if (count3 && count5 && count7)
173 // continue;
174 sieve_word += INCREMENT_EACH;
175 if (!(sieve_word & MULTIPLE_3_5_7))
150 continue; 176 continue;
151 /* 177 /*
152 * "factor" is multiple of 3 33% of the time (count3 reached 0), 178 * "factor" is multiple of 3 33% of the time (count3 reached 0),
@@ -154,15 +180,18 @@ static NOINLINE void factorize(wide_t N)
154 * else, multiple of 7 7.6% of the time. 180 * else, multiple of 7 7.6% of the time.
155 * Cumulatively, with 3,5,7 sieving we are here 54.3% of the time. 181 * Cumulatively, with 3,5,7 sieving we are here 54.3% of the time.
156 */ 182 */
157 if (count3 == 0) { 183 // if (count3 == 0)
158 count3 = 3; 184 // count3 = 3;
159 } 185 if (sieve_word & MULTIPLE_OF_3)
160 if (count5 == 0) { 186 sieve_word -= SHIFT_3 * 3;
161 count5 = 5; 187 // if (count5 == 0)
162 } 188 // count5 = 5;
163 if (count7 == 0) { 189 if (sieve_word & MULTIPLE_OF_5)
164 count7 = 7; 190 sieve_word -= SHIFT_5 * 5;
165 } 191 // if (count7 == 0)
192 // count7 = 7;
193 if (sieve_word & MULTIPLE_OF_7)
194 sieve_word -= SHIFT_7 * 7;
166 goto next_factor; 195 goto next_factor;
167 } 196 }
168 end: 197 end: