TrinityCore
SFMTRand.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "SFMTRand.h"
19#include <algorithm>
20#include <array>
21#include <functional>
22#include <random>
23#include <ctime>
24
25#if __has_include(<mm_malloc.h>)
26#include <mm_malloc.h>
27#elif __has_include(<malloc.h>)
28#include <malloc.h>
29#else
30static __inline__ void *__attribute__((__always_inline__, __nodebug__, __malloc__))
31_mm_malloc(size_t __size, size_t __align)
32{
33 if (__align == 1)
34 {
35 return malloc(__size);
36 }
37
38 if (!(__align & (__align - 1)) && __align < sizeof(void *))
39 __align = sizeof(void *);
40
42
43 if (posix_memalign(&__mallocedMemory, __align, __size))
44 return NULL;
45
46 return __mallocedMemory;
47}
48
49static __inline__ void __attribute__((__always_inline__, __nodebug__))
50_mm_free(void *__p)
51{
52 free(__p);
53}
54#endif
55
57{
58 std::random_device dev;
59 if (dev.entropy() > 0)
60 {
61 std::array<uint32, SFMT_N32> seed;
62 std::generate(seed.begin(), seed.end(), std::ref(dev));
63
64 sfmt_init_by_array(&_state, seed.data(), seed.size());
65 }
66 else
67 sfmt_init_gen_rand(&_state, uint32(time(nullptr)));
68}
69
70uint32 SFMTRand::RandomUInt32() // Output random bits
71{
72 return sfmt_genrand_uint32(&_state);
73}
74
75void* SFMTRand::operator new(size_t size, std::nothrow_t const&)
76{
77 return _mm_malloc(size, 16);
78}
79
80void SFMTRand::operator delete(void* ptr, std::nothrow_t const&)
81{
82 _mm_free(ptr);
83}
84
85void* SFMTRand::operator new(size_t size)
86{
87 return _mm_malloc(size, 16);
88}
89
90void SFMTRand::operator delete(void* ptr)
91{
92 _mm_free(ptr);
93}
94
95void* SFMTRand::operator new[](size_t size, std::nothrow_t const&)
96{
97 return _mm_malloc(size, 16);
98}
99
100void SFMTRand::operator delete[](void* ptr, std::nothrow_t const&)
101{
102 _mm_free(ptr);
103}
104
105void* SFMTRand::operator new[](size_t size)
106{
107 return _mm_malloc(size, 16);
108}
109
110void SFMTRand::operator delete[](void* ptr)
111{
112 _mm_free(ptr);
113}
uint32_t uint32
Definition: Define.h:142
void * __mallocedMemory
Definition: SFMTRand.cpp:41
static __inline__ void size_t __align
Definition: SFMTRand.cpp:32
static __inline__ void * __attribute__((__always_inline__, __nodebug__, __malloc__)) _mm_malloc(size_t __size
uint32 RandomUInt32()
Definition: SFMTRand.cpp:70
sfmt_t _state
Definition: SFMTRand.h:41
SFMTRand()
Definition: SFMTRand.cpp:56
constexpr std::size_t size()
Definition: UpdateField.h:796