Krita Source Code Documentation
Loading...
Searching...
No Matches
KisRandomGenerator2D.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project
3 *
4 * SPDX-FileCopyrightText: 2008, 2009 Cyrille Berger <cberger@cberger.net>
5 * SPDX-FileCopyrightText: 2009 Matthew Woehlke <mw_triad@users.sourceforge.net>
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
11
12/* Mac OS X doesn't define a number of UINT* macros without this before stdlib.h */
13#define __STDC_LIMIT_MACROS
14
15#include <stdlib.h>
16#include <stdint.h>
17#include <math.h>
18
19#include "rand_salt.h"
20
21inline quint64 permuteWhole(quint64 n, quint64 a, quint64 b)
22{
23 return ((n * a) + b);
24}
25
26inline quint64 part(quint64 n1, quint64 n2, int p)
27{
28 int b = p * 8;
29 int i = (n1 >> b) & 0xFF;
30 int j = (n2 >> b) & 0xFF;
31 return quint64(salt[i][j]) << b;
32}
33
34struct Q_DECL_HIDDEN KisRandomGenerator2D::Private {
35 quint64 seed;
36};
37
39{
40 d->seed = seed;
41}
42
47
48quint64 KisRandomGenerator2D::randomAt(qint64 x, qint64 y)
49{
50 const quint64 kxa = 427140578808118991LL;
51 const quint64 kya = 166552399647317237LL;
52 const quint64 kxb = 48058817213113801LL;
53 const quint64 kyb = 9206429469018994469LL;
54
55 // Generate salts
56 quint64 n1 = (quint64(x + 5) * kxa) * d->seed;
57 quint64 n2 = (quint64(y + 7) * kya) + (d->seed * 1040097393733LL);
58 n1 = permuteWhole(n1, 8759824322359LL, 13);
59 n2 = permuteWhole(n2, 200560490131LL, 2707);
60 n1 = (n1 >> 32) ^ (n1 << 32);
61 n2 = (n2 >> 32) ^ (n2 << 32);
62 n1 ^= x ^ (quint64(y ^ d->seed) * kyb);
63 n2 ^= y ^ (quint64(x + 13) * kxb);
64
65 // Combine salts
66 quint64 v = 0;
67 for (int p = 0; p < 8; ++p)
68 v |= part(n1, n2, p);
69 return v;
70}
71
72double KisRandomGenerator2D::doubleRandomAt(qint64 x, qint64 y)
73{
74 return randomAt(x, y) / (double)UINT64_MAX;
75}
76
const Params2D p
qreal v
quint64 part(quint64 n1, quint64 n2, int p)
quint64 permuteWhole(quint64 n, quint64 a, quint64 b)
const unsigned char salt[256][256]
Definition rand_salt.h:30
quint64 randomAt(qint64 x, qint64 y)
double doubleRandomAt(qint64 x, qint64 y)