Loading...
Searching...
No Matches
sieves.h
Go to the documentation of this file.
1#pragma once
2
15
16#include <inttypes.h>
17#include <stdlib.h>
18
19#include <nut/modular_math.h>
20#include <nut/factorization.h>
21
28uint64_t nut_max_prime_divs(uint64_t max);
29
36uint64_t nut_max_primes_le(uint64_t max);
37
55NUT_ATTR_ACCESS(none, 1)
56static inline void *nut_Pitcharr_get(void *buf, size_t pitch, uint64_t i){
57 return buf + i*pitch;
58}
59
67NUT_ATTR_ACCESS(read_only, 1)
68static inline bool nut_Bitarray_get(const uint8_t *buf, uint64_t i){
69 return buf[i/8] & (UINT8_C(1) << (i%8));
70}
71
77NUT_ATTR_ACCESS(read_write, 1)
78static inline void nut_Bitarray_set(uint8_t *buf, uint64_t i, bool v){
79 if(v){
80 buf[i/8] |= 1ull << (i%8);
81 }else{
82 buf[i/8] &= ~(UINT8_C(1) << (i%8));
83 }
84}
85
94NUT_ATTR_ACCESS(read_only, 1)
95static inline uint8_t nut_Bitfield2_arr_get(const uint8_t *buf, uint64_t i){
96 return (buf[i/4] >> (i%4*2)) & 3;
97}
98
107bool nut_u64_make_factorial_tbl(uint64_t k, uint64_t modulus, uint64_t bits, uint64_t max_denom, uint64_t factorials[static bits + k], uint64_t inv_factorials[static max_denom + 1]);
108
121[[deprecated("Replace with nut_sieve_smallest_factors_wheel6")]]
124NUT_ATTR_ACCESS(write_only, 2)
125void *nut_sieve_factorizations(uint64_t max, uint64_t *_w);
126
135uint64_t nut_get_factorizations_pitch(uint64_t w);
136
147[[deprecated("Replace with nut_sieve_smallest_factors_wheel6")]]
150NUT_ATTR_ACCESS(write_only, 2)
151void *nut_sieve_factors(uint64_t max, uint64_t *_w);
152
159uint8_t *nut_sieve_omega(uint64_t max);
160
170[[deprecated("Replace with nut_sieve_smallest_factors_wheel6")]]
172uint64_t *nut_sieve_largest_factors(uint64_t max);
173
179[[deprecated("Replace with nut_fill_factors_from_smallest_wheel6")]]
181NUT_ATTR_ACCESS(read_write, 1)
182NUT_ATTR_ACCESS(read_only, 3)
183void nut_fill_factors_from_largest(nut_Factors *restrict out, uint64_t n, const uint64_t largest_factors[restrict static n + 1]);
184
196[[deprecated("Replace with nut_sieve_smallest_factors_wheel6")]]
198uint32_t *nut_sieve_smallest_factors(uint64_t max);
199
205[[deprecated("Replace with nut_fill_factors_from_smallest_wheel6")]]
207NUT_ATTR_ACCESS(read_write, 1)
208NUT_ATTR_ACCESS(read_only, 3)
209void nut_fill_factors_from_smallest(nut_Factors *restrict out, uint64_t n, const uint32_t smallest_factors[restrict static n + 1]);
210
223uint32_t *nut_sieve_smallest_factors_wheel6(uint64_t max);
224
231NUT_ATTR_ACCESS(read_write, 1)
232NUT_ATTR_ACCESS(read_only, 3)
233void nut_fill_factors_from_smallest_wheel6(nut_Factors *restrict out, uint64_t n, const uint32_t smallest_factors[restrict static n/3 + 1]);
234
242uint64_t nut_get_factors_pitch(uint64_t w);
243
250uint64_t *nut_sieve_sigma_0(uint64_t max);
251
258uint64_t *nut_sieve_sigma_1(uint64_t max);
259
267uint64_t *nut_sieve_sigma_e(uint64_t max, uint64_t e);
268
279uint64_t *nut_sieve_dk(uint64_t max, uint64_t k, uint64_t modulus);
280
287uint64_t *nut_sieve_phi(uint64_t max);
288
295uint64_t *nut_sieve_carmichael(uint64_t max);
296
305uint8_t *nut_sieve_mobius(uint64_t max);
306
314NUT_ATTR_ACCESS(read_only, 2)
315int64_t *nut_compute_mertens_range(uint64_t max, const uint8_t mobius[static max/4 + 1]);
316
324uint8_t *nut_sieve_is_composite(uint64_t max);
325
332NUT_ATTR_ACCESS(read_only, 2)
333bool nut_is_composite(uint64_t n, const uint8_t buf[static n/30 + 1]);
334
343NUT_ATTR_ACCESS(read_only, 2)
344uint64_t *nut_compute_pi_range(uint64_t max, const uint8_t buf[static max/30 + 1]);
345
353NUT_ATTR_ACCESS(read_only, 2)
354NUT_ATTR_ACCESS(read_only, 3)
355NUT_ATTR_NO_SAN("vla-bound")
356uint64_t nut_compute_pi_from_tables(uint64_t n, const uint64_t pi_table[restrict static n/30], const uint8_t buf[restrict static n/30 + 1]);
357
364NUT_ATTR_ACCESS(write_only, 2)
365uint64_t *nut_sieve_primes(uint64_t max, uint64_t *_num_primes);
366
#define NUT_ATTR_RETURNS_NONNULL
Wrapper for gnu::returns_nonnull attr.
#define NUT_ATTR_NO_SAN(name)
Prevent clang from reporting spurious errors when a length zero array is passed to a length annotated...
#define NUT_ATTR_ACCESS(...)
Wrapper for gcc's access function annotation - no clang equivalent, and currently disabled because it...
#define NUT_ATTR_NONNULL(...)
Wrapper for gnu::nonnull attr.
#define NUT_ATTR_PURE
Wrapper for gnu::pure attr - similar to const but the function can read non-volatile memory (includin...
#define NUT_ATTR_CONST
Wrapper for gnu::const attr - such a function has no side effects and cannot read memory.
#define NUT_ATTR_MALLOC
Wrapper for gnu::malloc attr - indicates the function returns a malloc'd pointer that must be free'd.
NUT_ATTR_MALLOC uint8_t * nut_sieve_omega(uint64_t max)
Compute the number of distinct prime divisors of every number in the range from 0 to max.
NUT_ATTR_MALLOC uint8_t * nut_sieve_mobius(uint64_t max)
Compute the Mobius function for every number from 0 to max.
NUT_ATTR_MALLOC uint64_t * nut_sieve_carmichael(uint64_t max)
Compute the Carmichael function for every number from 0 to max.
NUT_ATTR_MALLOC uint64_t * nut_sieve_sigma_e(uint64_t max, uint64_t e)
Compute the sum of some power of divisors (including 1 and n) for every number n from 0 to max.
NUT_ATTR_MALLOC uint8_t * nut_sieve_is_composite(uint64_t max)
Compute a bitarray of whether or not each number from 0 to max is composite.
NUT_ATTR_PURE bool nut_is_composite(uint64_t n, const uint8_t buf[static n/30+1])
Check if a number is composite using a packed bitarray from nut_sieve_is_composite.
NUT_ATTR_MALLOC void * nut_sieve_factorizations(uint64_t max, uint64_t *_w)
Compute the factorization for every number in the range from 0 to max.
NUT_ATTR_MALLOC uint64_t * nut_compute_pi_range(uint64_t max, const uint8_t buf[static max/30+1])
Compute the pi (prime counting) function for every number from 0 to max.
NUT_ATTR_MALLOC int64_t * nut_compute_mertens_range(uint64_t max, const uint8_t mobius[static max/4+1])
Compute the Mertens function (sum of Mobius function) for every number from 0 to max.
NUT_ATTR_CONST uint64_t nut_max_primes_le(uint64_t max)
Compute an upper bound on the number of primes up to max.
NUT_ATTR_CONST uint64_t nut_get_factors_pitch(uint64_t w)
Get the pitch for a pitched array of nut_u64_Pitcharr factor lists.
bool nut_u64_make_factorial_tbl(uint64_t k, uint64_t modulus, uint64_t bits, uint64_t max_denom, uint64_t factorials[static bits+k], uint64_t inv_factorials[static max_denom+1])
Calculate factorials and inverse factorials for a given upper bound and modulus.
NUT_ATTR_MALLOC uint64_t * nut_sieve_sigma_1(uint64_t max)
Compute the sum of divisors (including 1 and n) for every number n from 0 to max.
NUT_ATTR_MALLOC void * nut_sieve_factors(uint64_t max, uint64_t *_w)
Compute the unique prime factors of every number in the range from 0 to max.
NUT_ATTR_CONST uint64_t nut_max_prime_divs(uint64_t max)
Compute the maximum number of unique prime divisors a number can have.
NUT_ATTR_MALLOC uint64_t * nut_sieve_phi(uint64_t max)
Compute Euler's totient function for every number from 0 to max.
void nut_fill_factors_from_smallest_wheel6(nut_Factors *restrict out, uint64_t n, const uint32_t smallest_factors[restrict static n/3+1])
Use a table of smallest prime factors for a wheel of 6 to get the factorization of a number.
NUT_ATTR_MALLOC uint64_t * nut_sieve_sigma_0(uint64_t max)
Compute the number of divisors (including 1 and n) for every number n from 0 to max.
NUT_ATTR_MALLOC uint64_t * nut_sieve_largest_factors(uint64_t max)
Compute the largest prime factor of every number in the range from 0 to max.
NUT_ATTR_CONST uint64_t nut_get_factorizations_pitch(uint64_t w)
Get the pitch for a pitched array of factorization structs with w unique prime divisors.
void nut_fill_factors_from_largest(nut_Factors *restrict out, uint64_t n, const uint64_t largest_factors[restrict static n+1])
Use a table of largest prime factors to get the factorization of a number.
NUT_ATTR_MALLOC uint64_t * nut_sieve_dk(uint64_t max, uint64_t k, uint64_t modulus)
Compute the generalized divisor function dk(n) (number of k-tuples with product n) for every number n...
NUT_ATTR_PURE uint64_t nut_compute_pi_from_tables(uint64_t n, const uint64_t pi_table[restrict static n/30], const uint8_t buf[restrict static n/30+1])
Get the value for the pi (prime counting) function for a particular number using precomputed tables.
NUT_ATTR_MALLOC uint32_t * nut_sieve_smallest_factors_wheel6(uint64_t max)
Compute the smallest prime factor of every number coprime to 6 in the range from 0 to max,...
NUT_ATTR_MALLOC uint32_t * nut_sieve_smallest_factors(uint64_t max)
Compute the smallest prime factor of every number in the range from 0 to max, or 1 for primes Compare...
void nut_fill_factors_from_smallest(nut_Factors *restrict out, uint64_t n, const uint32_t smallest_factors[restrict static n+1])
Use a table of smallest prime factors to get the factorization of a number.
NUT_ATTR_MALLOC uint64_t * nut_sieve_primes(uint64_t max, uint64_t *_num_primes)
Compute an array of all primes from 0 to max.
A prime factorization.