/src/open62541/deps/pcg_basic.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * PCG Random Number Generation for C. |
3 | | * |
4 | | * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org> |
5 | | * |
6 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | * you may not use this file except in compliance with the License. |
8 | | * You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, software |
13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | * See the License for the specific language governing permissions and |
16 | | * limitations under the License. |
17 | | * |
18 | | * For additional information about the PCG random number generation scheme, |
19 | | * including its license and other licensing options, visit |
20 | | * |
21 | | * http://www.pcg-random.org |
22 | | */ |
23 | | |
24 | | #include "pcg_basic.h" |
25 | | |
26 | 0 | void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state, uint64_t initseq) { |
27 | 0 | rng->state = 0U; |
28 | 0 | rng->inc = (initseq << 1u) | 1u; |
29 | 0 | pcg32_random_r(rng); |
30 | 0 | rng->state += initial_state; |
31 | 0 | pcg32_random_r(rng); |
32 | 0 | } |
33 | | |
34 | 0 | uint32_t pcg32_random_r(pcg32_random_t* rng) { |
35 | 0 | uint64_t oldstate = rng->state; |
36 | 0 | rng->state = oldstate * 6364136223846793005ULL + rng->inc; |
37 | 0 | uint32_t xorshifted = (uint32_t)(((oldstate >> 18u) ^ oldstate) >> 27u); |
38 | 0 | uint32_t rot = (uint32_t)(oldstate >> 59u); |
39 | 0 | return (xorshifted >> rot) | (xorshifted << ((~rot + 1u) & 31)); /* was (xorshifted >> rot) | (xorshifted << ((-rot) & 31)) */ |
40 | 0 | } |