/src/libgmp/mpz/primorial_ui.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_primorial_ui(RES, N) -- Set RES to N# the product of primes <= N. |
2 | | |
3 | | Contributed to the GNU project by Marco Bodrato. |
4 | | |
5 | | Copyright 2012, 2015, 2016, 2021 Free Software Foundation, Inc. |
6 | | |
7 | | This file is part of the GNU MP Library. |
8 | | |
9 | | The GNU MP Library is free software; you can redistribute it and/or modify |
10 | | it under the terms of either: |
11 | | |
12 | | * the GNU Lesser General Public License as published by the Free |
13 | | Software Foundation; either version 3 of the License, or (at your |
14 | | option) any later version. |
15 | | |
16 | | or |
17 | | |
18 | | * the GNU General Public License as published by the Free Software |
19 | | Foundation; either version 2 of the License, or (at your option) any |
20 | | later version. |
21 | | |
22 | | or both in parallel, as here. |
23 | | |
24 | | The GNU MP Library is distributed in the hope that it will be useful, but |
25 | | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
26 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
27 | | for more details. |
28 | | |
29 | | You should have received copies of the GNU General Public License and the |
30 | | GNU Lesser General Public License along with the GNU MP Library. If not, |
31 | | see https://www.gnu.org/licenses/. */ |
32 | | |
33 | | #include "gmp-impl.h" |
34 | | |
35 | | /* TODO: Remove duplicated constants / macros / static functions... |
36 | | */ |
37 | | |
38 | | /*************************************************************/ |
39 | | /* Section macros: common macros, for swing/fac/bin (&sieve) */ |
40 | | /*************************************************************/ |
41 | | |
42 | | #define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I) \ |
43 | 642 | do { \ |
44 | 642 | if ((PR) > (MAX_PR)) { \ |
45 | 104 | (VEC)[(I)++] = (PR); \ |
46 | 104 | (PR) = (P); \ |
47 | 104 | } else \ |
48 | 642 | (PR) *= (P); \ |
49 | 642 | } while (0) |
50 | | |
51 | | /*********************************************************/ |
52 | | /* Section sieve: sieving functions and tools for primes */ |
53 | | /*********************************************************/ |
54 | | |
55 | | #if WANT_ASSERT |
56 | | /* n_to_bit (n) = ((n-1)&(-CNST_LIMB(2)))/3U-1 */ |
57 | | static mp_limb_t |
58 | 3 | n_to_bit (mp_limb_t n) { return ((n-5)|1)/3U; } |
59 | | |
60 | | static mp_size_t |
61 | 3 | primesieve_size (mp_limb_t n) { return n_to_bit(n) / GMP_LIMB_BITS + 1; } |
62 | | #endif |
63 | | |
64 | | /*********************************************************/ |
65 | | /* Section primorial: implementation */ |
66 | | /*********************************************************/ |
67 | | |
68 | | void |
69 | | mpz_primorial_ui (mpz_ptr res, unsigned long n) |
70 | 3 | { |
71 | 3 | ASSERT (n <= GMP_NUMB_MAX); |
72 | | |
73 | 3 | if (n < 5) |
74 | 0 | { |
75 | | /* The smallest 5 results for primorial are stored */ |
76 | | /* in a 15-bits constant (five octal digits) */ |
77 | 0 | MPZ_NEWALLOC (res, 1)[0] = (066211 >> (n * 3)) & 7; |
78 | 0 | SIZ (res) = 1; |
79 | 0 | } |
80 | 3 | else |
81 | 3 | { |
82 | 3 | mp_limb_t *sieve, *factors; |
83 | 3 | mp_size_t size, j; |
84 | 3 | mp_limb_t prod; |
85 | 3 | TMP_DECL; |
86 | | |
87 | | /* Try to estimate the result size, to avoid */ |
88 | | /* resizing, and to initially store the sieve. */ |
89 | 3 | size = n / GMP_NUMB_BITS; |
90 | 3 | size = size + (size >> 1) + 1; |
91 | 3 | ASSERT (size >= primesieve_size (n)); |
92 | 3 | sieve = MPZ_NEWALLOC (res, size); |
93 | 3 | size = (gmp_primesieve (sieve, n) + 1) / log_n_max (n) + 1; |
94 | | |
95 | 3 | TMP_MARK; |
96 | 3 | factors = TMP_ALLOC_LIMBS (size); |
97 | | |
98 | 3 | j = 0; |
99 | | |
100 | 3 | prod = 6; |
101 | | |
102 | | /* Store primes from 5 to n */ |
103 | 3 | { |
104 | 3 | mp_limb_t max_prod; |
105 | | |
106 | 3 | max_prod = GMP_NUMB_MAX / n; |
107 | | |
108 | | /* Loop on sieved primes. */ |
109 | 28 | for (mp_limb_t i = 4, *sp = sieve; i < n; i += GMP_LIMB_BITS * 3) |
110 | 1.42k | for (mp_limb_t b = i, x = ~ *(sp++); x != 0; b += 3, x >>= 1) |
111 | 1.40k | if (x & 1) |
112 | 642 | { |
113 | 642 | mp_limb_t prime = b | 1; |
114 | 642 | FACTOR_LIST_STORE (prime, prod, max_prod, factors, j); |
115 | 642 | } |
116 | 3 | } |
117 | | |
118 | 3 | if (j != 0) |
119 | 3 | { |
120 | 3 | factors[j++] = prod; |
121 | 3 | mpz_prodlimbs (res, factors, j); |
122 | 3 | } |
123 | 0 | else |
124 | 0 | { |
125 | 0 | PTR (res)[0] = prod; |
126 | 0 | SIZ (res) = 1; |
127 | 0 | } |
128 | | |
129 | 3 | TMP_FREE; |
130 | 3 | } |
131 | 3 | } |