Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/make-prime-list.c |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||||||||
2 | - | |||||||||||||||||||
3 | typedef unsigned __int128 wide_uint; | - | ||||||||||||||||||
4 | - | |||||||||||||||||||
5 | - | |||||||||||||||||||
6 | - | |||||||||||||||||||
7 | - | |||||||||||||||||||
8 | - | |||||||||||||||||||
9 | struct prime | - | ||||||||||||||||||
10 | { | - | ||||||||||||||||||
11 | unsigned p; | - | ||||||||||||||||||
12 | wide_uint pinv; | - | ||||||||||||||||||
13 | wide_uint lim; | - | ||||||||||||||||||
14 | }; | - | ||||||||||||||||||
15 | - | |||||||||||||||||||
16 | static wide_uint __attribute__ ((__const__)) | - | ||||||||||||||||||
17 | binvert (wide_uint a) | - | ||||||||||||||||||
18 | { | - | ||||||||||||||||||
19 | wide_uint x = 0xf5397db1 >> (4*((a/2) & 0x7)); | - | ||||||||||||||||||
20 | for (;;) | - | ||||||||||||||||||
21 | { | - | ||||||||||||||||||
22 | wide_uint y = 2*x - x*x*a; | - | ||||||||||||||||||
23 | if (y == x
| 668-3293 | ||||||||||||||||||
24 | return executed 668 times by 1 test: x;return x; Executed by:
executed 668 times by 1 test: return x; Executed by:
| 668 | ||||||||||||||||||
25 | x = y; | - | ||||||||||||||||||
26 | } executed 3293 times by 1 test: end of block Executed by:
| 3293 | ||||||||||||||||||
27 | } never executed: end of block | 0 | ||||||||||||||||||
28 | - | |||||||||||||||||||
29 | static void | - | ||||||||||||||||||
30 | process_prime (struct prime *info, unsigned p) | - | ||||||||||||||||||
31 | { | - | ||||||||||||||||||
32 | wide_uint max = -1; | - | ||||||||||||||||||
33 | info->p = p; | - | ||||||||||||||||||
34 | info->pinv = binvert (p); | - | ||||||||||||||||||
35 | info->lim = max / p; | - | ||||||||||||||||||
36 | } executed 668 times by 1 test: end of block Executed by:
| 668 | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | static void | - | ||||||||||||||||||
39 | print_wide_uint (wide_uint n, int nesting, unsigned wide_uint_bits) | - | ||||||||||||||||||
40 | { | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | - | |||||||||||||||||||
43 | - | |||||||||||||||||||
44 | int hex_digits_per_literal = 7; | - | ||||||||||||||||||
45 | int bits_per_literal = hex_digits_per_literal * 4; | - | ||||||||||||||||||
46 | - | |||||||||||||||||||
47 | unsigned remainder = n & ((1 << bits_per_literal) - 1); | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | if (n != remainder
| 668-2672 | ||||||||||||||||||
50 | { | - | ||||||||||||||||||
51 | int needs_parentheses = n >> bits_per_literal >> bits_per_literal != 0; | - | ||||||||||||||||||
52 | if (needs_parentheses
| 668-2004 | ||||||||||||||||||
53 | printf ("("); executed 2004 times by 1 test: printf ("("); Executed by:
| 2004 | ||||||||||||||||||
54 | print_wide_uint (n >> bits_per_literal, nesting + 1, wide_uint_bits); | - | ||||||||||||||||||
55 | if (needs_parentheses
| 668-2004 | ||||||||||||||||||
56 | printf (")\n%*s", nesting + 3, ""); executed 2004 times by 1 test: printf (")\n%*s", nesting + 3, ""); Executed by:
| 2004 | ||||||||||||||||||
57 | printf (" << %d | ", bits_per_literal); | - | ||||||||||||||||||
58 | } executed 2672 times by 1 test: end of block Executed by:
| 2672 | ||||||||||||||||||
59 | else if (nesting
| 0-668 | ||||||||||||||||||
60 | { | - | ||||||||||||||||||
61 | printf ("(uintmax_t) "); | - | ||||||||||||||||||
62 | hex_digits_per_literal | - | ||||||||||||||||||
63 | = ((wide_uint_bits - 1) % bits_per_literal) % 4 + 1; | - | ||||||||||||||||||
64 | } executed 668 times by 1 test: end of block Executed by:
| 668 | ||||||||||||||||||
65 | - | |||||||||||||||||||
66 | printf ("0x%0*xU", hex_digits_per_literal, remainder); | - | ||||||||||||||||||
67 | } executed 3340 times by 1 test: end of block Executed by:
| 3340 | ||||||||||||||||||
68 | - | |||||||||||||||||||
69 | static void | - | ||||||||||||||||||
70 | output_primes (const struct prime *primes, unsigned nprimes) | - | ||||||||||||||||||
71 | { | - | ||||||||||||||||||
72 | unsigned i; | - | ||||||||||||||||||
73 | unsigned p; | - | ||||||||||||||||||
74 | int is_prime; | - | ||||||||||||||||||
75 | - | |||||||||||||||||||
76 | - | |||||||||||||||||||
77 | - | |||||||||||||||||||
78 | - | |||||||||||||||||||
79 | unsigned wide_uint_bits = 0; | - | ||||||||||||||||||
80 | wide_uint mask = -1; | - | ||||||||||||||||||
81 | for (wide_uint_bits = 0; mask
| 1-128 | ||||||||||||||||||
82 | mask >>= 1; executed 128 times by 1 test: mask >>= 1; Executed by:
| 128 | ||||||||||||||||||
83 | - | |||||||||||||||||||
84 | puts ("/* Generated file -- DO NOT EDIT */\n"); | - | ||||||||||||||||||
85 | printf ("#define WIDE_UINT_BITS %u\n", wide_uint_bits); | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | for (i = 0, p = 2; i < nprimes
| 1-668 | ||||||||||||||||||
88 | { | - | ||||||||||||||||||
89 | unsigned int d8 = i + 8 < nprimes
| 8-660 | ||||||||||||||||||
90 | if (255 < d8
| 0-668 | ||||||||||||||||||
91 | abort (); never executed: abort (); | 0 | ||||||||||||||||||
92 | printf ("P (%u, %u,\n (", primes[i].p - p, d8); | - | ||||||||||||||||||
93 | print_wide_uint (primes[i].pinv, 0, wide_uint_bits); | - | ||||||||||||||||||
94 | printf ("),\n UINTMAX_MAX / %u)\n", primes[i].p); | - | ||||||||||||||||||
95 | p = primes[i].p; | - | ||||||||||||||||||
96 | } executed 668 times by 1 test: end of block Executed by:
| 668 | ||||||||||||||||||
97 | - | |||||||||||||||||||
98 | printf ("\n#undef FIRST_OMITTED_PRIME\n"); | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | - | |||||||||||||||||||
101 | do | - | ||||||||||||||||||
102 | { | - | ||||||||||||||||||
103 | p += 2; | - | ||||||||||||||||||
104 | for (i = 0, is_prime = 1; is_prime
| 0-20 | ||||||||||||||||||
105 | { | - | ||||||||||||||||||
106 | if (primes[i].p * primes[i].p > p
| 1-19 | ||||||||||||||||||
107 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||||||||
108 | if (p * primes[i].pinv <= primes[i].lim
| 1-18 | ||||||||||||||||||
109 | { | - | ||||||||||||||||||
110 | is_prime = 0; | - | ||||||||||||||||||
111 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||||||||
112 | } | - | ||||||||||||||||||
113 | } executed 18 times by 1 test: end of block Executed by:
| 18 | ||||||||||||||||||
114 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
115 | while (!is_prime
| 1 | ||||||||||||||||||
116 | - | |||||||||||||||||||
117 | printf ("#define FIRST_OMITTED_PRIME %u\n", p); | - | ||||||||||||||||||
118 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
119 | - | |||||||||||||||||||
120 | static void * | - | ||||||||||||||||||
121 | xalloc (size_t s) | - | ||||||||||||||||||
122 | { | - | ||||||||||||||||||
123 | void *p = malloc (s); | - | ||||||||||||||||||
124 | if (p
| 0-2 | ||||||||||||||||||
125 | return executed 2 times by 1 test: p;return p; Executed by:
executed 2 times by 1 test: return p; Executed by:
| 2 | ||||||||||||||||||
126 | - | |||||||||||||||||||
127 | fprintf ( | - | ||||||||||||||||||
128 | stderr | - | ||||||||||||||||||
129 | , "Virtual memory exhausted.\n"); | - | ||||||||||||||||||
130 | exit ( never executed: exit ( 1 ); | 0 | ||||||||||||||||||
131 | 1 never executed: exit ( 1 ); | 0 | ||||||||||||||||||
132 | ); never executed: exit ( 1 ); | 0 | ||||||||||||||||||
133 | } | - | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | int | - | ||||||||||||||||||
136 | main (int argc, char **argv) | - | ||||||||||||||||||
137 | { | - | ||||||||||||||||||
138 | int limit; | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | char *sieve; | - | ||||||||||||||||||
141 | size_t size, i; | - | ||||||||||||||||||
142 | - | |||||||||||||||||||
143 | struct prime *prime_list; | - | ||||||||||||||||||
144 | unsigned nprimes; | - | ||||||||||||||||||
145 | - | |||||||||||||||||||
146 | if (argc != 2
| 0-1 | ||||||||||||||||||
147 | { | - | ||||||||||||||||||
148 | fprintf ( | - | ||||||||||||||||||
149 | stderr | - | ||||||||||||||||||
150 | , "Usage: %s LIMIT\n" | - | ||||||||||||||||||
151 | "Produces a list of odd primes <= LIMIT\n", argv[0]); | - | ||||||||||||||||||
152 | return never executed: return 1 ; never executed: return 1 ; | 0 | ||||||||||||||||||
153 | 1 never executed: return 1 ; | 0 | ||||||||||||||||||
154 | ; never executed: return 1 ; | 0 | ||||||||||||||||||
155 | } | - | ||||||||||||||||||
156 | limit = atoi (argv[1]); | - | ||||||||||||||||||
157 | if (limit < 3
| 0-1 | ||||||||||||||||||
158 | return never executed: return 0 ; never executed: return 0 ; | 0 | ||||||||||||||||||
159 | 0 never executed: return 0 ; | 0 | ||||||||||||||||||
160 | ; never executed: return 0 ; | 0 | ||||||||||||||||||
161 | - | |||||||||||||||||||
162 | - | |||||||||||||||||||
163 | if ( !(limit & 1)
| 0-1 | ||||||||||||||||||
164 | limit--; executed 1 time by 1 test: limit--; Executed by:
| 1 | ||||||||||||||||||
165 | - | |||||||||||||||||||
166 | size = (limit-1)/2; | - | ||||||||||||||||||
167 | - | |||||||||||||||||||
168 | sieve = xalloc (size); | - | ||||||||||||||||||
169 | memset (sieve, 1, size); | - | ||||||||||||||||||
170 | - | |||||||||||||||||||
171 | prime_list = xalloc (size * sizeof (*prime_list)); | - | ||||||||||||||||||
172 | nprimes = 0; | - | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | for (i = 0; i < size
| 1-668 | ||||||||||||||||||
175 | { | - | ||||||||||||||||||
176 | unsigned p = 3+2*i; | - | ||||||||||||||||||
177 | unsigned j; | - | ||||||||||||||||||
178 | - | |||||||||||||||||||
179 | process_prime (&prime_list[nprimes++], p); | - | ||||||||||||||||||
180 | - | |||||||||||||||||||
181 | for (j = (p*p - 3)/2; j < size
| 668-2797 | ||||||||||||||||||
182 | sieve[j] = 0; executed 2797 times by 1 test: sieve[j] = 0; Executed by:
| 2797 | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | while (++
| 1-2498 | ||||||||||||||||||
185 | ; executed 1831 times by 1 test: ; Executed by:
| 1831 | ||||||||||||||||||
186 | } executed 668 times by 1 test: end of block Executed by:
| 668 | ||||||||||||||||||
187 | - | |||||||||||||||||||
188 | output_primes (prime_list, nprimes); | - | ||||||||||||||||||
189 | - | |||||||||||||||||||
190 | free (sieve); | - | ||||||||||||||||||
191 | free (prime_list); | - | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | if (ferror (
| 0-1 | ||||||||||||||||||
194 | stdout
| 0-1 | ||||||||||||||||||
195 | ) + fclose (
| 0-1 | ||||||||||||||||||
196 | stdout
| 0-1 | ||||||||||||||||||
197 | )
| 0-1 | ||||||||||||||||||
198 | { | - | ||||||||||||||||||
199 | fprintf ( | - | ||||||||||||||||||
200 | stderr | - | ||||||||||||||||||
201 | , "write error: %s\n", strerror ( | - | ||||||||||||||||||
202 | (*__errno_location ()) | - | ||||||||||||||||||
203 | )); | - | ||||||||||||||||||
204 | return never executed: return 1 ; never executed: return 1 ; | 0 | ||||||||||||||||||
205 | 1 never executed: return 1 ; | 0 | ||||||||||||||||||
206 | ; never executed: return 1 ; | 0 | ||||||||||||||||||
207 | } | - | ||||||||||||||||||
208 | - | |||||||||||||||||||
209 | return executed 1 time by 1 test: return 0 ; Executed by:
executed 1 time by 1 test: return 0 ; Executed by:
| 1 | ||||||||||||||||||
210 | 0 executed 1 time by 1 test: return 0 ; Executed by:
| 1 | ||||||||||||||||||
211 | ; executed 1 time by 1 test: return 0 ; Executed by:
| 1 | ||||||||||||||||||
212 | } | - | ||||||||||||||||||
Switch to Source code | Preprocessed file |