diff options
author | Ron Yorston <rmy@pobox.com> | 2021-01-14 13:28:49 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-01-14 13:28:49 +0000 |
commit | 89963b524d211e1aec12b72b3725be05ee95c8cf (patch) | |
tree | 48590aef62b7ee7686b7898256f29def8d9c50b9 /coreutils/factor.c | |
parent | 9aa5a829070392c2ac6494d0c4e674c0c2bc7dab (diff) | |
parent | 2b7c1aa92c68524559a2067609d09309d5c09adc (diff) | |
download | busybox-w32-89963b524d211e1aec12b72b3725be05ee95c8cf.tar.gz busybox-w32-89963b524d211e1aec12b72b3725be05ee95c8cf.tar.bz2 busybox-w32-89963b524d211e1aec12b72b3725be05ee95c8cf.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'coreutils/factor.c')
-rw-r--r-- | coreutils/factor.c | 209 |
1 files changed, 132 insertions, 77 deletions
diff --git a/coreutils/factor.c b/coreutils/factor.c index 47fe179dc..a7a5a5030 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,9 +43,116 @@ 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)) << 1) | \ | ||
53 | (((uint64_t)(A<<0) | (B<<3) | (C<<6) | (D<<9) | (E<<12) | (F<<15) | (G<<18) | (H<<21) | (I<<24) | (J<<27)) << 31) | ||
54 | #define P(a,b,c,d,e,f,g,h,i,j,A,B,C,D,E,F,G,H,I,J) \ | ||
55 | R( (a/2),(b/2),(c/2),(d/2),(e/2),(f/2),(g/2),(h/2),(i/2),(j/2), \ | ||
56 | (A/2),(B/2),(C/2),(D/2),(E/2),(F/2),(G/2),(H/2),(I/2),(J/2) ) | ||
57 | static const uint64_t packed_wheel[] = { | ||
58 | /*1, 2, 2, 4, 2,*/ | ||
59 | P( 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4), //01 | ||
60 | P( 2, 4, 2, 4,14, 4, 6, 2,10, 2, 6, 6, 4, 2, 4, 6, 2,10, 2, 4), //02 | ||
61 | P( 2,12,10, 2, 4, 2, 4, 6, 2, 6, 4, 6, 6, 6, 2, 6, 4, 2, 6, 4), //03 | ||
62 | P( 6, 8, 4, 2, 4, 6, 8, 6,10, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2), //04 | ||
63 | P( 6, 4, 2, 6,10, 2,10, 2, 4, 2, 4, 6, 8, 4, 2, 4,12, 2, 6, 4), //05 | ||
64 | P( 2, 6, 4, 6,12, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6,10, 2), //06 | ||
65 | P( 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 6, 6, 2, 6, 6, 4, 6), //07 | ||
66 | P( 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 6, 4, 8, 6, 4, 6, 2, 4, 6), //08 | ||
67 | P( 8, 6, 4, 2,10, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 2, 4, 8, 6), //09 | ||
68 | P( 4, 2, 4, 6, 6, 2, 6, 4, 8, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4), //10 | ||
69 | P( 6, 6, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2), //11 | ||
70 | P( 6, 4, 6, 2, 6, 4, 2, 4, 6, 6, 8, 4, 2, 6,10, 8, 4, 2, 4, 2), //12 | ||
71 | P( 4, 8,10, 6, 2, 4, 8, 6, 6, 4, 2, 4, 6, 2, 6, 4, 6, 2,10, 2), //13 | ||
72 | P(10, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 6, 6, 4, 6, 8), //14 | ||
73 | P( 4, 2, 4, 2, 4, 8, 6, 4, 8, 4, 6, 2, 6, 6, 4, 2, 4, 6, 8, 4), //15 | ||
74 | P( 2, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2,10, 2, 4, 6, 8, 6, 4, 2), //16 | ||
75 | P( 6, 4, 6, 8, 4, 6, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6, 6, 4, 6), //17 | ||
76 | P( 6, 2, 6, 6, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2, 6, 4, 2,10, 6), //18 | ||
77 | P( 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2,12, 6, 4, 6, 2, 4, 6, 2), //19 | ||
78 | P(12, 4, 2, 4, 8, 6, 4, 2, 4, 2,10, 2,10, 6, 2, 4, 6, 2, 6, 4), //20 | ||
79 | P( 2, 4, 6, 6, 2, 6, 4, 2,10, 6, 8, 6, 4, 2, 4, 8, 6, 4, 6, 2), //21 | ||
80 | P( 4, 6, 2, 6, 6, 6, 4, 6, 2, 6, 4, 2, 4, 2,10,12, 2, 4, 2,10), //22 | ||
81 | P( 2, 6, 4, 2, 4, 6, 6, 2,10, 2, 6, 4,14, 4, 2, 4, 2, 4, 8, 6), //23 | ||
82 | P( 4, 6, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4,12, 2,12), //24 | ||
83 | }; | ||
84 | #undef P | ||
85 | #undef R | ||
86 | #define WHEEL_START 5 | ||
87 | #define WHEEL_SIZE (5 + 24 * 20) | ||
88 | #define square_count (((uint8_t*)&bb_common_bufsiz1)[0]) | ||
89 | #define wheel_tab (((uint8_t*)&bb_common_bufsiz1) + 1) | ||
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 171 +63 | ||
98 | */ | ||
99 | static 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 | while ((v & 0xe) != 0) { | ||
114 | *p = v & 0xe; | ||
115 | //printf("%2u,", *p); | ||
116 | p++; | ||
117 | v >>= 3; | ||
118 | } | ||
119 | //printf("\n"); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | /* Prevent inlining, factorize() needs all help it can get with reducing register pressure */ | ||
124 | static NOINLINE void print_w(wide_t n) | ||
125 | { | ||
126 | unsigned rep = square_count; | ||
127 | do | ||
128 | printf(" %"LL_FMT"u", n); | ||
129 | while (--rep != 0); | ||
130 | } | ||
131 | static NOINLINE void print_h(half_t n) | ||
132 | { | ||
133 | print_w(n); | ||
134 | } | ||
135 | |||
136 | static void factorize(wide_t N); | ||
137 | |||
45 | static half_t isqrt_odd(wide_t N) | 138 | static half_t isqrt_odd(wide_t N) |
46 | { | 139 | { |
47 | half_t s = isqrt(N); | 140 | half_t s = isqrt(N); |
141 | /* s^2 is <= N, (s+1)^2 > N */ | ||
142 | |||
143 | /* If s^2 in fact is EQUAL to N, it's very lucky. | ||
144 | * Examples: | ||
145 | * factor 18446743988964486098 = 2 * 3037000493 * 3037000493 | ||
146 | * factor 18446743902517389507 = 3 * 2479700513 * 2479700513 | ||
147 | */ | ||
148 | if ((wide_t)s * s == N) { | ||
149 | /* factorize sqrt(N), printing each factor twice */ | ||
150 | square_count *= 2; | ||
151 | factorize(s); | ||
152 | /* Let caller know we recursed */ | ||
153 | return 0; | ||
154 | } | ||
155 | |||
48 | /* Subtract 1 from even s, odd s won't change: */ | 156 | /* Subtract 1 from even s, odd s won't change: */ |
49 | /* (doesnt work for zero, but we know that s != 0 here) */ | 157 | /* (doesnt work for zero, but we know that s != 0 here) */ |
50 | s = (s - 1) | 1; | 158 | s = (s - 1) | 1; |
@@ -53,43 +161,20 @@ static half_t isqrt_odd(wide_t N) | |||
53 | 161 | ||
54 | static NOINLINE void factorize(wide_t N) | 162 | static NOINLINE void factorize(wide_t N) |
55 | { | 163 | { |
164 | unsigned w; | ||
56 | half_t factor; | 165 | half_t factor; |
57 | half_t max_factor; | 166 | 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 | 167 | ||
80 | if (N < 4) | 168 | if (N < 4) |
81 | goto end; | 169 | goto end; |
82 | 170 | ||
83 | while (!(N & 1)) { | ||
84 | printf(" 2"); | ||
85 | N >>= 1; | ||
86 | } | ||
87 | |||
88 | /* The code needs to be optimized for the case where | 171 | /* The code needs to be optimized for the case where |
89 | * there are large prime factors. For example, | 172 | * there are large prime factors. For example, |
90 | * this is not hard: | 173 | * this is not hard: |
91 | * 8262075252869367027 = 3 7 17 23 47 101 113 127 131 137 823 | 174 | * 8262075252869367027 = 3 7 17 23 47 101 113 127 131 137 823 |
92 | * (the largest factor to test is only ~sqrt(823) = 28) | 175 | * (the largest divisor to test for largest factor 823 |
176 | * is only ~sqrt(823) = 28, the entire factorization needs | ||
177 | * only ~33 trial divisions) | ||
93 | * but this is: | 178 | * but this is: |
94 | * 18446744073709551601 = 53 348051774975651917 | 179 | * 18446744073709551601 = 53 348051774975651917 |
95 | * the last factor requires testing up to | 180 | * the last factor requires testing up to |
@@ -98,70 +183,37 @@ static NOINLINE void factorize(wide_t N) | |||
98 | * factor 18446744073709551557 (0xffffffffffffffc5). | 183 | * factor 18446744073709551557 (0xffffffffffffffc5). |
99 | */ | 184 | */ |
100 | max_factor = isqrt_odd(N); | 185 | max_factor = isqrt_odd(N); |
101 | // count3 = 3; | 186 | if (!max_factor) |
102 | // count5 = 6; | 187 | return; /* square was detected and recursively factored */ |
103 | // count7 = 9; | 188 | factor = 2; |
104 | sieve_word = 0 | 189 | w = 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 (;;) { | 190 | for (;;) { |
191 | half_t fw; | ||
192 | |||
115 | /* The division is the most costly part of the loop. | 193 | /* The division is the most costly part of the loop. |
116 | * On 64bit CPUs, takes at best 12 cycles, often ~20. | 194 | * On 64bit CPUs, takes at best 12 cycles, often ~20. |
117 | */ | 195 | */ |
118 | while ((N % factor) == 0) { /* not likely */ | 196 | while ((N % factor) == 0) { /* not likely */ |
119 | N = N / factor; | 197 | N = N / factor; |
120 | printf(" %"HALF_FMT"u", factor); | 198 | print_h(factor); |
121 | max_factor = isqrt_odd(N); | 199 | max_factor = isqrt_odd(N); |
200 | if (!max_factor) | ||
201 | return; /* square was detected */ | ||
122 | } | 202 | } |
123 | next_factor: | ||
124 | if (factor >= max_factor) | 203 | if (factor >= max_factor) |
125 | break; | 204 | break; |
126 | factor += 2; | 205 | fw = factor + wheel_tab[w]; |
127 | /* Rudimentary wheel sieving: skip multiples of 3, 5 and 7: | 206 | if (fw < factor) |
128 | * Every third odd number is divisible by three and thus isn't a prime: | 207 | 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... | 208 | factor = fw; |
130 | * ^ ^ ^ ^ ^ ^ ^ _ ^ ^ _ ^ ^ ^ ^ | 209 | w++; |
131 | * (^ = primes, _ = would-be-primes-if-not-divisible-by-5) | 210 | 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; | 211 | continue; |
142 | /* | 212 | 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 | } | 213 | } |
162 | end: | 214 | end: |
163 | if (N > 1) | 215 | if (N > 1) |
164 | printf(" %"LL_FMT"u", N); | 216 | print_w(N); |
165 | bb_putchar('\n'); | 217 | bb_putchar('\n'); |
166 | } | 218 | } |
167 | 219 | ||
@@ -176,12 +228,15 @@ static void factorize_numstr(const char *numstr) | |||
176 | if (errno) | 228 | if (errno) |
177 | bb_show_usage(); | 229 | bb_show_usage(); |
178 | printf("%"LL_FMT"u:", N); | 230 | printf("%"LL_FMT"u:", N); |
231 | square_count = 1; | ||
179 | factorize(N); | 232 | factorize(N); |
180 | } | 233 | } |
181 | 234 | ||
182 | int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 235 | int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
183 | int factor_main(int argc UNUSED_PARAM, char **argv) | 236 | int factor_main(int argc UNUSED_PARAM, char **argv) |
184 | { | 237 | { |
238 | unpack_wheel(); | ||
239 | |||
185 | //// coreutils has undocumented option ---debug (three dashes) | 240 | //// coreutils has undocumented option ---debug (three dashes) |
186 | //getopt32(argv, ""); | 241 | //getopt32(argv, ""); |
187 | //argv += optind; | 242 | //argv += optind; |