aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-12-20 21:37:29 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-12-20 21:37:29 +0100
commitf079f913711091c87557a4c7854385c408e4ab7c (patch)
treeeac04b00860b08469ee38d2a7252a9eb33b4c00b
parentbb4e32befaf86f80a108d2b7a4b7c47ffcc64e9c (diff)
downloadbusybox-w32-f079f913711091c87557a4c7854385c408e4ab7c.tar.gz
busybox-w32-f079f913711091c87557a4c7854385c408e4ab7c.tar.bz2
busybox-w32-f079f913711091c87557a4c7854385c408e4ab7c.zip
factor: 30% faster trial division (better sieve)
function old new delta packed_wheel - 192 +192 factor_main 108 176 +68 factorize 284 218 -66 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/1 up/down: 260/-66) Total: 194 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/factor.c170
1 files changed, 95 insertions, 75 deletions
diff --git a/coreutils/factor.c b/coreutils/factor.c
index 1f24784fd..30498c73b 100644
--- a/coreutils/factor.c
+++ b/coreutils/factor.c
@@ -19,6 +19,7 @@
19//usage: "Print prime factors" 19//usage: "Print prime factors"
20 20
21#include "libbb.h" 21#include "libbb.h"
22#include "common_bufsiz.h"
22 23
23#if 0 24#if 0
24# define dbg(...) bb_error_msg(__VA_ARGS__) 25# define dbg(...) bb_error_msg(__VA_ARGS__)
@@ -42,6 +43,83 @@ typedef unsigned long half_t;
42#error Cant find an integer type which is half as wide as ullong 43#error Cant find an integer type which is half as wide as ullong
43#endif 44#endif
44 45
46/* The trial divisor increment wheel. Use it to skip over divisors that
47 * are composites of 2, 3, 5, 7, or 11.
48 * Larger wheels improve sieving only slightly, but quickly grow in size
49 * (adding just one prime, 13, results in 5766 element sieve).
50 */
51#define R(a,b,c,d,e,f,g,h,i,j,A,B,C,D,E,F,G,H,I,J) \
52 (((uint64_t)(a<<0) | (b<<3) | (c<<6) | (d<<9) | (e<<12) | (f<<15) | (g<<18) | (h<<21) | (i<<24) | (j<<27)) ) | \
53 (((uint64_t)(A<<0) | (B<<3) | (C<<6) | (D<<9) | (E<<12) | (F<<15) | (G<<18) | (H<<21) | (I<<24) | (J<<27)) << 30) | \
54 (((uint64_t)7 << 60))
55#define P(a,b,c,d,e,f,g,h,i,j,A,B,C,D,E,F,G,H,I,J) \
56 R( (a/2-1),(b/2-1),(c/2-1),(d/2-1),(e/2-1),(f/2-1),(g/2-1),(h/2-1),(i/2-1),(j/2-1), \
57 (A/2-1),(B/2-1),(C/2-1),(D/2-1),(E/2-1),(F/2-1),(G/2-1),(H/2-1),(I/2-1),(J/2-1) )
58static const uint64_t packed_wheel[] = {
59 /*1, 2, 2, 4, 2,*/
60 P( 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4), //01
61 P( 2, 4, 2, 4,14, 4, 6, 2,10, 2, 6, 6, 4, 2, 4, 6, 2,10, 2, 4), //02
62 P( 2,12,10, 2, 4, 2, 4, 6, 2, 6, 4, 6, 6, 6, 2, 6, 4, 2, 6, 4), //03
63 P( 6, 8, 4, 2, 4, 6, 8, 6,10, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2), //04
64 P( 6, 4, 2, 6,10, 2,10, 2, 4, 2, 4, 6, 8, 4, 2, 4,12, 2, 6, 4), //05
65 P( 2, 6, 4, 6,12, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6,10, 2), //06
66 P( 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 6, 6, 2, 6, 6, 4, 6), //07
67 P( 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 6, 4, 8, 6, 4, 6, 2, 4, 6), //08
68 P( 8, 6, 4, 2,10, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 2, 4, 8, 6), //09
69 P( 4, 2, 4, 6, 6, 2, 6, 4, 8, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4), //10
70 P( 6, 6, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2), //11
71 P( 6, 4, 6, 2, 6, 4, 2, 4, 6, 6, 8, 4, 2, 6,10, 8, 4, 2, 4, 2), //12
72 P( 4, 8,10, 6, 2, 4, 8, 6, 6, 4, 2, 4, 6, 2, 6, 4, 6, 2,10, 2), //13
73 P(10, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 6, 6, 4, 6, 8), //14
74 P( 4, 2, 4, 2, 4, 8, 6, 4, 8, 4, 6, 2, 6, 6, 4, 2, 4, 6, 8, 4), //15
75 P( 2, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2,10, 2, 4, 6, 8, 6, 4, 2), //16
76 P( 6, 4, 6, 8, 4, 6, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6, 6, 4, 6), //17
77 P( 6, 2, 6, 6, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2, 6, 4, 2,10, 6), //18
78 P( 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2,12, 6, 4, 6, 2, 4, 6, 2), //19
79 P(12, 4, 2, 4, 8, 6, 4, 2, 4, 2,10, 2,10, 6, 2, 4, 6, 2, 6, 4), //20
80 P( 2, 4, 6, 6, 2, 6, 4, 2,10, 6, 8, 6, 4, 2, 4, 8, 6, 4, 6, 2), //21
81 P( 4, 6, 2, 6, 6, 6, 4, 6, 2, 6, 4, 2, 4, 2,10,12, 2, 4, 2,10), //22
82 P( 2, 6, 4, 2, 4, 6, 6, 2,10, 2, 6, 4,14, 4, 2, 4, 2, 4, 8, 6), //23
83 P( 4, 6, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4,12, 2,12), //24
84};
85#undef P
86#undef r
87#define WHEEL_START 5
88#define WHEEL_SIZE (5 + 24 * 20)
89#define wheel_tab ((uint8_t*)&bb_common_bufsiz1)
90/*
91 * Why, you ask?
92 * plain byte array:
93 * function old new delta
94 * wheel_tab - 485 +485
95 * 3-bit-packed insanity:
96 * packed_wheel - 192 +192
97 * factor_main 108 176 +68
98 */
99static void unpack_wheel(void)
100{
101 int i;
102 uint8_t *p;
103
104 setup_common_bufsiz();
105 wheel_tab[0] = 1;
106 wheel_tab[1] = 2;
107 wheel_tab[2] = 2;
108 wheel_tab[3] = 4;
109 wheel_tab[4] = 2;
110 p = &wheel_tab[5];
111 for (i = 0; i < ARRAY_SIZE(packed_wheel); i++) {
112 uint64_t v = packed_wheel[i];
113 do {
114 *p = ((unsigned)(v & 7) + 1) * 2;
115 //printf("%2u,", *p);
116 p++;
117 v >>= 3;
118 } while ((unsigned)v != 7);
119 //printf("\n");
120 }
121}
122
45static half_t isqrt_odd(wide_t N) 123static half_t isqrt_odd(wide_t N)
46{ 124{
47 half_t s = isqrt(N); 125 half_t s = isqrt(N);
@@ -53,43 +131,20 @@ static half_t isqrt_odd(wide_t N)
53 131
54static NOINLINE void factorize(wide_t N) 132static NOINLINE void factorize(wide_t N)
55{ 133{
134 unsigned w;
56 half_t factor; 135 half_t factor;
57 half_t max_factor; 136 half_t max_factor;
58 // unsigned count3;
59 // unsigned count5;
60 // unsigned count7;
61 // ^^^^^^^^^^^^^^^ commented-out simple sieving code (easier to grasp).
62 // Faster sieving, using one word for potentially up to 6 counters:
63 // count upwards in each mask, counter "triggers" when it sets its mask to "100[0]..."
64 // 10987654321098765432109876543210 - bits 31-0 in 32-bit word
65 // 17777713333311111777775555333 - bit masks for counters for primes 3,5,7,11,13,17
66 // 100000100001000010001001 - value for adding 1 to each mask
67 // 10000010000010000100001000100 - value for checking that any mask reached msb
68 enum {
69 SHIFT_3 = 1 << 0,
70 SHIFT_5 = 1 << 3,
71 SHIFT_7 = 1 << 7,
72 INCREMENT_EACH = SHIFT_3 | SHIFT_5 | SHIFT_7,
73 MULTIPLE_OF_3 = 1 << 2,
74 MULTIPLE_OF_5 = 1 << 6,
75 MULTIPLE_OF_7 = 1 << 11,
76 MULTIPLE_DETECTED = MULTIPLE_OF_3 | MULTIPLE_OF_5 | MULTIPLE_OF_7,
77 };
78 unsigned sieve_word;
79 137
80 if (N < 4) 138 if (N < 4)
81 goto end; 139 goto end;
82 140
83 while (!(N & 1)) {
84 printf(" 2");
85 N >>= 1;
86 }
87
88 /* The code needs to be optimized for the case where 141 /* The code needs to be optimized for the case where
89 * there are large prime factors. For example, 142 * there are large prime factors. For example,
90 * this is not hard: 143 * this is not hard:
91 * 8262075252869367027 = 3 7 17 23 47 101 113 127 131 137 823 144 * 8262075252869367027 = 3 7 17 23 47 101 113 127 131 137 823
92 * (the largest factor to test is only ~sqrt(823) = 28) 145 * (the largest divisor to test for largest factor 823
146 * is only ~sqrt(823) = 28, the entire factorization needs
147 * only ~33 trial divisions)
93 * but this is: 148 * but this is:
94 * 18446744073709551601 = 53 348051774975651917 149 * 18446744073709551601 = 53 348051774975651917
95 * the last factor requires testing up to 150 * the last factor requires testing up to
@@ -98,20 +153,11 @@ static NOINLINE void factorize(wide_t N)
98 * factor 18446744073709551557 (0xffffffffffffffc5). 153 * factor 18446744073709551557 (0xffffffffffffffc5).
99 */ 154 */
100 max_factor = isqrt_odd(N); 155 max_factor = isqrt_odd(N);
101 // count3 = 3; 156 factor = 2;
102 // count5 = 6; 157 w = 0;
103 // count7 = 9;
104 sieve_word = 0
105 /* initial count for SHIFT_n is (n-1)/2*3: */
106 + (MULTIPLE_OF_3 - 3 * SHIFT_3)
107 + (MULTIPLE_OF_5 - 6 * SHIFT_5)
108 + (MULTIPLE_OF_7 - 9 * SHIFT_7)
109 //+ (MULTIPLE_OF_11 - 15 * SHIFT_11)
110 //+ (MULTIPLE_OF_13 - 18 * SHIFT_13)
111 //+ (MULTIPLE_OF_17 - 24 * SHIFT_17)
112 ;
113 factor = 3;
114 for (;;) { 158 for (;;) {
159 half_t fw;
160
115 /* The division is the most costly part of the loop. 161 /* The division is the most costly part of the loop.
116 * On 64bit CPUs, takes at best 12 cycles, often ~20. 162 * On 64bit CPUs, takes at best 12 cycles, often ~20.
117 */ 163 */
@@ -120,44 +166,16 @@ static NOINLINE void factorize(wide_t N)
120 printf(" %"HALF_FMT"u", factor); 166 printf(" %"HALF_FMT"u", factor);
121 max_factor = isqrt_odd(N); 167 max_factor = isqrt_odd(N);
122 } 168 }
123 next_factor:
124 if (factor >= max_factor) 169 if (factor >= max_factor)
125 break; 170 break;
126 factor += 2; 171 fw = factor + wheel_tab[w];
127 /* Rudimentary wheel sieving: skip multiples of 3, 5 and 7: 172 if (fw < factor)
128 * Every third odd number is divisible by three and thus isn't a prime: 173 break; /* overflow */
129 * 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47... 174 factor = fw;
130 * ^ ^ ^ ^ ^ ^ ^ _ ^ ^ _ ^ ^ ^ ^ 175 w++;
131 * (^ = primes, _ = would-be-primes-if-not-divisible-by-5) 176 if (w < WHEEL_SIZE)
132 * The numbers with space under them are excluded by sieve 3.
133 */
134 // count7--;
135 // count5--;
136 // count3--;
137 // if (count3 && count5 && count7)
138 // continue;
139 sieve_word += INCREMENT_EACH;
140 if (!(sieve_word & MULTIPLE_DETECTED))
141 continue; 177 continue;
142 /* 178 w = WHEEL_START;
143 * "factor" is multiple of 3 33% of the time (count3 reached 0),
144 * else, multiple of 5 13% of the time,
145 * else, multiple of 7 7.6% of the time.
146 * Cumulatively, with 3,5,7 sieving we are here 54.3% of the time.
147 */
148 // if (count3 == 0)
149 // count3 = 3;
150 if (sieve_word & MULTIPLE_OF_3)
151 sieve_word -= SHIFT_3 * 3;
152 // if (count5 == 0)
153 // count5 = 5;
154 if (sieve_word & MULTIPLE_OF_5)
155 sieve_word -= SHIFT_5 * 5;
156 // if (count7 == 0)
157 // count7 = 7;
158 if (sieve_word & MULTIPLE_OF_7)
159 sieve_word -= SHIFT_7 * 7;
160 goto next_factor;
161 } 179 }
162 end: 180 end:
163 if (N > 1) 181 if (N > 1)
@@ -182,6 +200,8 @@ static void factorize_numstr(const char *numstr)
182int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 200int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
183int factor_main(int argc UNUSED_PARAM, char **argv) 201int factor_main(int argc UNUSED_PARAM, char **argv)
184{ 202{
203 unpack_wheel();
204
185 //// coreutils has undocumented option ---debug (three dashes) 205 //// coreutils has undocumented option ---debug (three dashes)
186 //getopt32(argv, ""); 206 //getopt32(argv, "");
187 //argv += optind; 207 //argv += optind;