LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
mprand.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK(Jericho Knox)
3 *
4 * This file is part of LAMMP.
5 *
6 * LAMMP is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License (LGPL) as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed WITHOUT ANY WARRANTY.
12 *
13 * See <https://www.gnu.org/licenses/>.
14 */
15
16#include "../../../include/lammp/mprand.h"
17#include "../../../include/lammp/impl/rand_state.h"
18#include "../../../include/lammp/impl/tmp_alloc.h"
19
24
25#define GLOBAL_RNG_INIT_STATE 10451216379200822465ULL
26#define GLOBAL_RNG_INIT_SEED_TYPE 1
28
29void lmmp_global_rng_init_(int seed, int seed_type) {
31 lmmp_global_rng.seed_type = seed_type % 2;
32}
33
35 seed_type %= 2;
36 if (seed_type == 0) {
39 mp_size_t i = 0;
40 for (; i + 3 < n; i += 4) {
45 }
46 for (; i < n; ++i) {
48 }
49 } else {
52 mp_size_t i = 0;
53 for (; i + 3 < n; i += 4) {
58 }
59 for (; i < n; ++i) {
61 }
62 }
63 while (n > 0 && dst[n - 1] == 0) {
64 --n;
65 }
66 return n;
67}
68
78
82
95
99 if (k <= rng->stream.k) return;
100 rng->stream.state = (mp_limb_t*)lmmp_realloc(rng->stream.state, k * sizeof(mp_limb_t));
101
104 pcg64_le_seq_init(&rng->stream, rng->stream.k, new_seed);
105 rng->stream.k = k;
106}
107
109 if (rng != NULL) {
110 if (rng->stream.state != NULL) {
111 lmmp_free(rng->stream.state);
112 }
113 lmmp_free(rng);
114 }
115}
116
120 lmmp_param_assert(rng->stream.state != NULL);
121 lmmp_param_assert(n > 0);
123 pcg64_le_seq_next(dst, n, &rng->stream);
124 while (n > 0 && dst[n - 1] == 0) {
125 --n;
126 }
127 return n;
128}
#define k
void * lmmp_realloc(void *ptr, size_t size)
内存重分配函数(调用lmmp_realloc_fn)
Definition memory.c:184
mp_limb_t * mp_ptr
Definition lmmp.h:80
uint64_t mp_size_t
Definition lmmp.h:77
void * lmmp_alloc(size_t size)
内存分配函数(调用lmmp_heap_alloc_fn)
Definition memory.c:164
#define LAMMP_THREAD_LOCAL
Definition lmmp.h:102
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
Definition memory.c:202
uint64_t mp_limb_t
Definition lmmp.h:76
#define lmmp_param_assert(x)
Definition lmmp.h:401
#define GLOBAL_RNG_INIT_SEED_TYPE
Definition mprand.c:26
void lmmp_global_rng_init_(int seed, int seed_type)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition mprand.c:29
void lmmp_strong_rng_free_(lmmp_strong_rng_t *rng)
销毁强随机数生成器
Definition mprand.c:108
mp_size_t lmmp_strong_random_(mp_ptr restrict dst, mp_size_t n, lmmp_strong_rng_t *rng)
Definition mprand.c:117
lmmp_strong_rng_t * lmmp_strong_rng_init_(mp_size_t k, int seed)
创建k维度强随机数生成器
Definition mprand.c:83
#define GLOBAL_RNG_INIT_STATE
Definition mprand.c:25
pcg64_le_seq_t stream
Definition mprand.c:80
void lmmp_strong_rng_extern_(lmmp_strong_rng_t *rng, mp_size_t k)
将rng内部状态拓展至k维度
Definition mprand.c:96
mp_size_t lmmp_seed_random_(mp_ptr restrict dst, mp_size_t n, mp_limb_t seed, int seed_type)
Definition mprand.c:34
static _Thread_local lmmp_global_rng_t lmmp_global_rng
Definition mprand.c:27
mp_size_t lmmp_random_(mp_ptr restrict dst, mp_size_t n)
Definition mprand.c:69
mp_limb_t state
Definition mprand.c:21
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition mprand.c:20
#define n
static void pcg64_le_seq_init(pcg64_le_seq_t *rng, mp_size_t i, mp_limb_t seed)
Definition rand_state.h:128
static mp_limb_t lmmp_xoshiro256pp_random(xoshiro256pp_state *rng)
Definition rand_state.h:91
static void lmmp_xoshiro256pp_srandom(xoshiro256pp_state *rng, mp_limb_t seed)
Definition rand_state.h:106
mp_size_t k
Definition rand_state.h:124
static mp_limb_t lmmp_seed_generator(mp_limb_t seed)
种子生成器
Definition rand_state.h:48
static void lmmp_pcg64_128_srandom(pcg64_128_state *rng, mp_limb_t seed)
Definition rand_state.h:64
static mp_limb_t rotl(const mp_limb_t x, int k)
Definition rand_state.h:38
static mp_limb_t lmmp_pcg64_128_random(pcg64_128_state *rng)
Definition rand_state.h:79
static void pcg64_le_seq_next(mp_ptr restrict dst, mp_size_t n, pcg64_le_seq_t *rng)
Definition rand_state.h:182
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition rand_state.h:22
#define ALLOC_TYPE(n, type)
Definition tmp_alloc.h:173