LAMMP 4.1.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
prime_table.h
浏览该文件的文档.
1/*
2 * [LAMMP]
3 * Copyright (C) [2025-2026] [HJimmyK(Jericho Knox)]
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#ifndef __LAMMP_PRIME_TABLE_H__
20#define __LAMMP_PRIME_TABLE_H__
21#include "../numth.h"
22
23typedef uint64_t lmmp_bitset_t;
24typedef uint64_t* lmmp_bitset_p;
25
26#define LMMP_BITSET_BITS (64)
27#define LMMP_BITSET_MASK (0xffffffffffffffffull)
28#define LMMP_BITSET_BYTES (8)
29
30#define PRIME_SHORT_TABLE_SIZE 6542
31
32#define PRIME_SHORT_TABLE_N 0x10000
33
35
36/**
37 * @brief 根据全局素数表判断一个数是否为素数
38 * @param p 待判断的数
39 * @warning p>1, p%2==1
40 * @note 若 p 超过了当前全局素数表的范围,则会触发 debug_assert
41 * @return true 素数,false 合数
42 */
44
45/**
46 * @brief 计算小于等于 n 的素数数量
47 * @param n 范围
48 * @return 素数数量
49 */
51
52/**
53 * @brief 估计 n 范围内的素数数量
54 * @param n 范围
55 * @note 不会低估素数数量,可能恰好超过 pi(n),用以估计素数数组需要的空间
56 * @return 素数数量
57 */
59
60/**
61 * @brief 初始化全局素数表
62 * @param n 素数表大小(含n)
63 */
65
66/**
67 * @brief 释放全局素数表
68 */
70
71typedef struct {
72 uintp pp; // 仅存储奇素数
73 uint size; // pp 数组大小
74 uint start_idx; // 位图下一次解析的起始索引
75 uint end_num; // 达到此数时,位图解析结束
76 int is_end; // 是否已经遍历到全局质数表末尾
78
79/*
80 * 示例代码(遍历全局奇素数表):
81 *
82 lmmp_prime_int_table_init_(n);
83 prime_cache_t cache;
84 lmmp_prime_cache_init_(&cache, n);
85 while (cache.is_end == 0) {
86 lmmp_prime_cache_next_(&cache);
87 for (uint i = 0; i < cache.size; i++) {
88 ....
89 }
90 }
91 lmmp_prime_cache_free_(&cache);
92*/
93
94/**
95 * @brief 初始化素数表缓存
96 * @param cache 缓存结构体
97 * @param n 遍历素数表的范围(超过n时或者遍历到全局质数表末尾,is_end 置为 1)
98 */
100
101/**
102 * @brief 素数表缓存更新(从小到大遍历全局质数表)
103 * @param cache 缓存结构体
104 * @note 缓存只存储奇素数,当遍历到全局质数表末尾或者达到设置的范围时,is_end 置为 1
105 */
107
108/**
109 * @brief 释放素数表缓存
110 * @param cache 缓存结构体
111 */
113
114// 3,5,7,11的余数掩码表
115extern const lmmp_bitset_t r35711_mask_map[19];
116
117/**
118 * @brief 校验是否能被3,5,7,11整除,能够整除则返回1,否则返回0
119 */
120static inline int trial_div35711(ulong n) {
121#define MOD 1155
122 uint rem = n % MOD;
123 uint idx = rem / LMMP_BITSET_BITS;
124 uint bit = rem % LMMP_BITSET_BITS;
125 return (r35711_mask_map[idx] >> bit) & 1ULL;
126#undef MOD
127}
128
129#endif // __LAMMP_PRIME_TABLE_H__
uint32_t uint
Definition numth.h:35
uint32_t * uintp
Definition numth.h:43
uint16_t ushort
Definition numth.h:33
uint64_t ulong
Definition numth.h:36
uint64_t lmmp_bitset_t
Definition prime_table.h:23
static int trial_div35711(ulong n)
校验是否能被3,5,7,11整除,能够整除则返回1,否则返回0
void lmmp_prime_cache_free_(prime_cache_t *cache)
释放素数表缓存
#define PRIME_SHORT_TABLE_SIZE
Definition prime_table.h:30
ulong lmmp_prime_size_(ulong n)
估计 n 范围内的素数数量
Definition prime_table.c:11
const lmmp_bitset_t r35711_mask_map[19]
bool lmmp_is_prime_table_(uint p)
根据全局素数表判断一个数是否为素数
void lmmp_prime_cache_next_(prime_cache_t *cache)
素数表缓存更新(从小到大遍历全局质数表)
uint64_t * lmmp_bitset_p
Definition prime_table.h:24
void lmmp_prime_int_table_free_(void)
释放全局素数表
#define LMMP_BITSET_BITS
Definition prime_table.h:26
#define MOD
const ushort prime_short_table[6542]
void lmmp_prime_int_table_init_(uint n)
初始化全局素数表
Definition prime_table.c:70
ushort lmmp_prime_cnt16_(ushort n)
计算小于等于 n 的素数数量
void lmmp_prime_cache_init_(prime_cache_t *cache, uint n)
初始化素数表缓存