Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
118 строк · 2.9 Кб
1
/*
2
 * This file is part of the Pandaria 5.4.8 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 "SFMT.h"
19
#include "SFMTRand.h"
20
#include <algorithm>
21
#include <array>
22
#include <functional>
23
#include <random>
24
#include <ctime>
25

26
#if defined(__aarch64__)
27
    #if defined(__clang__)
28
        #include <mm_malloc.h>
29
    #elif defined(__GNUC__)
30
        static __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

41
            void *__mallocedMemory;
42

43
            if (posix_memalign(&__mallocedMemory, __align, __size))
44
                return NULL;
45

46
            return __mallocedMemory;
47
        }
48

49
        static __inline__ void __attribute__((__always_inline__, __nodebug__))
50
        _mm_free(void *__p)
51
        {
52
            free(__p);
53
        }
54
    #else
55
        #error aarch64 only on clang and gcc
56
    #endif
57
#else
58
    #include <emmintrin.h>
59
#endif
60

61
SFMTRand::SFMTRand()
62
{
63
    std::random_device dev;
64
    if (dev.entropy() > 0)
65
    {
66
        std::array<uint32, SFMT_N32> seed;
67
        std::generate(seed.begin(), seed.end(), std::ref(dev));
68

69
        sfmt_init_by_array(&_state, seed.data(), seed.size());
70
    }
71
    else
72
        sfmt_init_gen_rand(&_state, uint32(time(nullptr)));
73
}
74

75
uint32 SFMTRand::RandomUInt32()                            // Output random bits
76
{
77
    return sfmt_genrand_uint32(&_state);
78
}
79

80
void* SFMTRand::operator new(size_t size, std::nothrow_t const&)
81
{
82
    return _mm_malloc(size, 16);
83
}
84

85
void SFMTRand::operator delete(void* ptr, std::nothrow_t const&)
86
{
87
    _mm_free(ptr);
88
}
89

90
void* SFMTRand::operator new(size_t size)
91
{
92
    return _mm_malloc(size, 16);
93
}
94

95
void SFMTRand::operator delete(void* ptr)
96
{
97
    _mm_free(ptr);
98
}
99

100
void* SFMTRand::operator new[](size_t size, std::nothrow_t const&)
101
{
102
    return _mm_malloc(size, 16);
103
}
104

105
void SFMTRand::operator delete[](void* ptr, std::nothrow_t const&)
106
{
107
    _mm_free(ptr);
108
}
109

110
void* SFMTRand::operator new[](size_t size)
111
{
112
    return _mm_malloc(size, 16);
113
}
114

115
void SFMTRand::operator delete[](void* ptr)
116
{
117
    _mm_free(ptr);
118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.